payload: drop Network from P2PNotaryRequest

It's not needed now.
This commit is contained in:
Roman Khimov 2021-03-25 22:18:47 +03:00
parent 8c110a6147
commit c789431402
8 changed files with 7 additions and 46 deletions

View file

@ -12,7 +12,6 @@ import (
"github.com/nspcc-dev/neo-go/internal/testchain"
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/blockchainer"
"github.com/nspcc-dev/neo-go/pkg/core/mempool"
@ -199,7 +198,6 @@ func TestNotary(t *testing.T) {
payloads[i] = &payload.P2PNotaryRequest{
MainTransaction: main,
FallbackTransaction: fallback,
Network: netmode.UnitTestNet,
}
}
return payloads
@ -249,7 +247,6 @@ func TestNotary(t *testing.T) {
payloads[i] = &payload.P2PNotaryRequest{
MainTransaction: main,
FallbackTransaction: fallback,
Network: netmode.UnitTestNet,
}
}
return payloads

View file

@ -149,7 +149,7 @@ func (m *Message) decodePayload() error {
case CMDExtensible:
p = payload.NewExtensible()
case CMDP2PNotaryRequest:
p = &payload.P2PNotaryRequest{Network: m.Network}
p = &payload.P2PNotaryRequest{}
case CMDGetBlocks:
p = &payload.GetBlocks{}
case CMDGetHeaders:

View file

@ -4,7 +4,6 @@ import (
"bytes"
"errors"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/io"
@ -16,17 +15,15 @@ import (
type P2PNotaryRequest struct {
MainTransaction *transaction.Transaction
FallbackTransaction *transaction.Transaction
Network netmode.Magic
Witness transaction.Witness
hash util.Uint256
signedHash util.Uint256
hash util.Uint256
}
// NewP2PNotaryRequestFromBytes decodes P2PNotaryRequest from the given bytes.
func NewP2PNotaryRequestFromBytes(network netmode.Magic, b []byte) (*P2PNotaryRequest, error) {
req := &P2PNotaryRequest{Network: network}
func NewP2PNotaryRequestFromBytes(b []byte) (*P2PNotaryRequest, error) {
req := &P2PNotaryRequest{}
br := io.NewBinReaderFromBuf(b)
req.DecodeBinary(br)
if br.Err != nil {
@ -59,36 +56,11 @@ func (r *P2PNotaryRequest) Hash() util.Uint256 {
return r.hash
}
// GetSignedHash returns a hash of the payload used to verify it.
func (r *P2PNotaryRequest) GetSignedHash() util.Uint256 {
if r.signedHash.Equals(util.Uint256{}) {
if r.createHash() != nil {
panic("failed to compute hash!")
}
}
return r.signedHash
}
// GetSignedPart returns a part of the payload which must be signed.
func (r *P2PNotaryRequest) GetSignedPart() []byte {
if r.hash.Equals(util.Uint256{}) {
if r.createHash() != nil {
panic("failed to compute hash!")
}
}
buf := io.NewBufBinWriter()
buf.WriteU32LE(uint32(r.Network))
buf.WriteBytes(r.hash[:])
return buf.Bytes()
}
// createHash creates hash of the payload.
func (r *P2PNotaryRequest) createHash() error {
buf := io.NewBufBinWriter()
r.encodeHashableFields(buf.BinWriter)
r.hash = hash.Sha256(buf.Bytes())
signed := r.GetSignedPart()
r.signedHash = hash.Sha256(signed)
return nil
}

View file

@ -4,7 +4,6 @@ import (
"testing"
"github.com/nspcc-dev/neo-go/internal/random"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
@ -171,7 +170,6 @@ func TestNotaryRequestBytesFromBytes(t *testing.T) {
_ = fallbackTx.Hash()
_ = fallbackTx.Size()
p := &P2PNotaryRequest{
Network: netmode.UnitTestNet,
MainTransaction: mainTx,
FallbackTransaction: fallbackTx,
Witness: transaction.Witness{
@ -183,7 +181,7 @@ func TestNotaryRequestBytesFromBytes(t *testing.T) {
_ = p.Hash() // initialize hash caches
bytes, err := p.Bytes()
require.NoError(t, err)
actual, err := NewP2PNotaryRequestFromBytes(netmode.UnitTestNet, bytes)
actual, err := NewP2PNotaryRequestFromBytes(bytes)
require.NoError(t, err)
require.Equal(t, p, actual)
}

View file

@ -542,7 +542,6 @@ func TestGetData(t *testing.T) {
r := &payload.P2PNotaryRequest{
MainTransaction: mainTx,
FallbackTransaction: fallbackTx,
Network: netmode.UnitTestNet,
Witness: transaction.Witness{
InvocationScript: []byte{1, 2, 3},
VerificationScript: []byte{1, 2, 3},
@ -725,7 +724,6 @@ func TestInv(t *testing.T) {
r := &payload.P2PNotaryRequest{
MainTransaction: newDummyTx(),
FallbackTransaction: fallbackTx,
Network: netmode.UnitTestNet,
}
require.NoError(t, s.notaryRequestPool.Add(r.FallbackTransaction, s.chain, r))
hs := []util.Uint256{random.Uint256(), r.FallbackTransaction.Hash(), random.Uint256()}

View file

@ -659,10 +659,9 @@ func (c *Client) SignAndPushP2PNotaryRequest(mainTx *transaction.Transaction, fa
req := &payload.P2PNotaryRequest{
MainTransaction: mainTx,
FallbackTransaction: fallbackTx,
Network: c.GetNetwork(),
}
req.Witness = transaction.Witness{
InvocationScript: append([]byte{byte(opcode.PUSHDATA1), 64}, acc.PrivateKey().Sign(req.GetSignedPart())...),
InvocationScript: append([]byte{byte(opcode.PUSHDATA1), 64}, acc.PrivateKey().SignHashable(uint32(c.GetNetwork()), req)...),
VerificationScript: acc.GetVerificationScript(),
}
actualHash, err := c.SubmitP2PNotaryRequest(req)

View file

@ -1279,7 +1279,7 @@ func (s *Server) submitNotaryRequest(ps request.Params) (interface{}, *response.
if err != nil {
return nil, response.ErrInvalidParams
}
r, err := payload.NewP2PNotaryRequestFromBytes(s.network, bytePayload)
r, err := payload.NewP2PNotaryRequestFromBytes(bytePayload)
if err != nil {
return nil, response.ErrInvalidParams
}

View file

@ -19,7 +19,6 @@ import (
"github.com/gorilla/websocket"
"github.com/nspcc-dev/neo-go/internal/testchain"
"github.com/nspcc-dev/neo-go/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/core"
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/fee"
@ -1099,7 +1098,6 @@ func TestSubmitNotaryRequest(t *testing.T) {
{InvocationScript: []byte{1, 2, 3}, VerificationScript: []byte{1, 2, 3}}},
}
p := &payload.P2PNotaryRequest{
Network: netmode.UnitTestNet,
MainTransaction: mainTx,
FallbackTransaction: fallbackTx,
Witness: transaction.Witness{
@ -1143,7 +1141,6 @@ func TestSubmitNotaryRequest(t *testing.T) {
VerificationScript: sender.PublicKey().GetVerificationScript(),
})
p := &payload.P2PNotaryRequest{
Network: netmode.UnitTestNet,
MainTransaction: mainTx,
FallbackTransaction: fallbackTx,
}