From c789431402359c83a786980e551628a5287928d3 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 25 Mar 2021 22:18:47 +0300 Subject: [PATCH] payload: drop Network from P2PNotaryRequest It's not needed now. --- pkg/core/notary_test.go | 3 -- pkg/network/message.go | 2 +- pkg/network/payload/notary_request.go | 34 ++-------------------- pkg/network/payload/notary_request_test.go | 4 +-- pkg/network/server_test.go | 2 -- pkg/rpc/client/rpc.go | 3 +- pkg/rpc/server/server.go | 2 +- pkg/rpc/server/server_test.go | 3 -- 8 files changed, 7 insertions(+), 46 deletions(-) diff --git a/pkg/core/notary_test.go b/pkg/core/notary_test.go index 001bf9ebc..493cdd492 100644 --- a/pkg/core/notary_test.go +++ b/pkg/core/notary_test.go @@ -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 diff --git a/pkg/network/message.go b/pkg/network/message.go index 6bb05e380..d5cf628ca 100644 --- a/pkg/network/message.go +++ b/pkg/network/message.go @@ -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: diff --git a/pkg/network/payload/notary_request.go b/pkg/network/payload/notary_request.go index 8eb8e6444..e78178901 100644 --- a/pkg/network/payload/notary_request.go +++ b/pkg/network/payload/notary_request.go @@ -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 } diff --git a/pkg/network/payload/notary_request_test.go b/pkg/network/payload/notary_request_test.go index 7261eecb5..e148ab9ba 100644 --- a/pkg/network/payload/notary_request_test.go +++ b/pkg/network/payload/notary_request_test.go @@ -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) } diff --git a/pkg/network/server_test.go b/pkg/network/server_test.go index ff0d4ab70..395e65ed0 100644 --- a/pkg/network/server_test.go +++ b/pkg/network/server_test.go @@ -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()} diff --git a/pkg/rpc/client/rpc.go b/pkg/rpc/client/rpc.go index b2a251b3a..9465badb3 100644 --- a/pkg/rpc/client/rpc.go +++ b/pkg/rpc/client/rpc.go @@ -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) diff --git a/pkg/rpc/server/server.go b/pkg/rpc/server/server.go index 57827e056..f097df5c1 100644 --- a/pkg/rpc/server/server.go +++ b/pkg/rpc/server/server.go @@ -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 } diff --git a/pkg/rpc/server/server_test.go b/pkg/rpc/server/server_test.go index d4d6d052c..c29c4cdd3 100644 --- a/pkg/rpc/server/server_test.go +++ b/pkg/rpc/server/server_test.go @@ -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, }