diff --git a/pkg/consensus/consensus.go b/pkg/consensus/consensus.go index f01cdf6a0..2ced9fca8 100644 --- a/pkg/consensus/consensus.go +++ b/pkg/consensus/consensus.go @@ -201,12 +201,12 @@ var ( func NewPayload(m netmode.Magic, stateRootEnabled bool) *Payload { return &Payload{ Extensible: npayload.Extensible{ - Network: m, Category: Category, }, message: message{ stateRootEnabled: stateRootEnabled, }, + network: m, } } diff --git a/pkg/consensus/payload.go b/pkg/consensus/payload.go index ffcaf24fc..f06d0a54c 100644 --- a/pkg/consensus/payload.go +++ b/pkg/consensus/payload.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/nspcc-dev/dbft/payload" + "github.com/nspcc-dev/neo-go/pkg/config/netmode" "github.com/nspcc-dev/neo-go/pkg/io" npayload "github.com/nspcc-dev/neo-go/pkg/network/payload" "github.com/nspcc-dev/neo-go/pkg/util" @@ -28,6 +29,7 @@ type ( Payload struct { npayload.Extensible message + network netmode.Magic } ) @@ -128,7 +130,7 @@ func (p *Payload) EncodeBinary(w *io.BinWriter) { // It also sets corresponding verification and invocation scripts. func (p *Payload) Sign(key *privateKey) error { p.encodeData() - sig := key.SignHash(p.GetSignedHash()) + sig := key.PrivateKey.SignHashable(uint32(p.network), &p.Extensible) buf := io.NewBufBinWriter() emit.Bytes(buf.BinWriter, sig) @@ -138,22 +140,6 @@ func (p *Payload) Sign(key *privateKey) error { return nil } -// GetSignedPart implements crypto.Verifiable interface. -func (p *Payload) GetSignedPart() []byte { - if p.Extensible.Data == nil { - p.encodeData() - } - return p.Extensible.GetSignedPart() -} - -// GetSignedHash returns a hash of the payload used to verify it. -func (p *Payload) GetSignedHash() util.Uint256 { - if p.Extensible.Data == nil { - p.encodeData() - } - return p.Extensible.GetSignedHash() -} - // Hash implements payload.ConsensusPayload interface. func (p *Payload) Hash() util.Uint256 { if p.Extensible.Data == nil { diff --git a/pkg/consensus/payload_test.go b/pkg/consensus/payload_test.go index 7a313dd42..f5d6175b9 100644 --- a/pkg/consensus/payload_test.go +++ b/pkg/consensus/payload_test.go @@ -79,7 +79,7 @@ func TestConsensusPayload_Setters(t *testing.T) { func TestConsensusPayload_Serializable(t *testing.T) { for _, mt := range messageTypes { p := randomPayload(t, mt) - actual := &Payload{Extensible: npayload.Extensible{Network: netmode.UnitTestNet}} + actual := &Payload{Extensible: npayload.Extensible{}, network: netmode.UnitTestNet} data, err := testserdes.EncodeBinary(p) require.NoError(t, err) require.NoError(t, testserdes.DecodeBinary(data, &actual.Extensible)) @@ -158,12 +158,12 @@ func randomPayload(t *testing.T, mt messageType) *Payload { payload: randomMessage(t, mt), }, Extensible: npayload.Extensible{ - Network: netmode.UnitTestNet, Witness: transaction.Witness{ InvocationScript: random.Bytes(3), VerificationScript: []byte{byte(opcode.PUSH0)}, }, }, + network: netmode.UnitTestNet, } if mt == changeViewType { diff --git a/pkg/consensus/recovery_message.go b/pkg/consensus/recovery_message.go index f24376cb8..6f8b66f2d 100644 --- a/pkg/consensus/recovery_message.go +++ b/pkg/consensus/recovery_message.go @@ -298,7 +298,6 @@ func fromPayload(t messageType, recovery *Payload, p io.Serializable) *Payload { return &Payload{ Extensible: npayload.Extensible{ Category: Category, - Network: recovery.Network, ValidBlockEnd: recovery.BlockIndex, }, message: message{ @@ -308,5 +307,6 @@ func fromPayload(t messageType, recovery *Payload, p io.Serializable) *Payload { payload: p, stateRootEnabled: recovery.stateRootEnabled, }, + network: recovery.network, } } diff --git a/pkg/network/message.go b/pkg/network/message.go index 80fbb68ba..6bb05e380 100644 --- a/pkg/network/message.go +++ b/pkg/network/message.go @@ -147,7 +147,7 @@ func (m *Message) decodePayload() error { case CMDBlock: p = block.New(m.StateRootInHeader) case CMDExtensible: - p = payload.NewExtensible(m.Network) + p = payload.NewExtensible() case CMDP2PNotaryRequest: p = &payload.P2PNotaryRequest{Network: m.Network} case CMDGetBlocks: diff --git a/pkg/network/payload/extensible.go b/pkg/network/payload/extensible.go index ab09f2cb5..529a8dc37 100644 --- a/pkg/network/payload/extensible.go +++ b/pkg/network/payload/extensible.go @@ -4,7 +4,6 @@ import ( "errors" "math" - "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" @@ -18,8 +17,6 @@ const ( // Extensible represents payload containing arbitrary data. type Extensible struct { - // Network represents network magic. - Network netmode.Magic // Category is payload type. Category string // ValidBlockStart is starting height for payload to be valid. @@ -33,16 +30,14 @@ type Extensible struct { // Witness is payload witness. Witness transaction.Witness - hash util.Uint256 - signedHash util.Uint256 - signedpart []byte + hash util.Uint256 } var errInvalidPadding = errors.New("invalid padding") // NewExtensible creates new extensible payload. -func NewExtensible(network netmode.Magic) *Extensible { - return &Extensible{Network: network} +func NewExtensible() *Extensible { + return &Extensible{} } func (e *Extensible) encodeBinaryUnsigned(w *io.BinWriter) { @@ -81,22 +76,6 @@ func (e *Extensible) DecodeBinary(r *io.BinReader) { e.Witness.DecodeBinary(r) } -// GetSignedPart implements crypto.Verifiable. -func (e *Extensible) GetSignedPart() []byte { - if e.signedpart == nil { - e.createHash() - } - return e.signedpart -} - -// GetSignedHash implements crypto.Verifiable. -func (e *Extensible) GetSignedHash() util.Uint256 { - if e.signedHash.Equals(util.Uint256{}) { - e.createHash() - } - return e.signedHash -} - // Hash returns payload hash. func (e *Extensible) Hash() util.Uint256 { if e.hash.Equals(util.Uint256{}) { @@ -110,14 +89,4 @@ func (e *Extensible) createHash() { buf := io.NewBufBinWriter() e.encodeBinaryUnsigned(buf.BinWriter) e.hash = hash.Sha256(buf.Bytes()) - e.updateSignedPart() - e.signedHash = hash.Sha256(e.signedpart) -} - -// updateSignedPart updates serialized message if needed. -func (e *Extensible) updateSignedPart() { - buf := io.NewBufBinWriter() - buf.WriteU32LE(uint32(e.Network)) - buf.WriteBytes(e.hash[:]) - e.signedpart = buf.Bytes() } diff --git a/pkg/network/payload/extensible_test.go b/pkg/network/payload/extensible_test.go index 88ae45189..f1f6b28c8 100644 --- a/pkg/network/payload/extensible_test.go +++ b/pkg/network/payload/extensible_test.go @@ -7,7 +7,6 @@ import ( "github.com/nspcc-dev/neo-go/internal/random" "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/transaction" "github.com/nspcc-dev/neo-go/pkg/io" "github.com/stretchr/testify/require" @@ -54,23 +53,13 @@ func TestExtensible_Serializable(t *testing.T) { func TestExtensible_Hashes(t *testing.T) { getExtensiblePair := func() (*Extensible, *Extensible) { - p1 := NewExtensible(netmode.UnitTestNet) + p1 := NewExtensible() p1.Data = []byte{1, 2, 3} - p2 := NewExtensible(netmode.UnitTestNet) + p2 := NewExtensible() p2.Data = []byte{3, 2, 1} return p1, p2 } - t.Run("GetSignedPart", func(t *testing.T) { - p1, p2 := getExtensiblePair() - require.NotEqual(t, p1.GetSignedPart(), p2.GetSignedPart()) - require.NotEqual(t, p1.GetSignedPart(), p2.GetSignedPart()) - }) - t.Run("GetSignedHash", func(t *testing.T) { - p1, p2 := getExtensiblePair() - require.NotEqual(t, p1.GetSignedHash(), p2.GetSignedHash()) - require.NotEqual(t, p1.GetSignedHash(), p2.GetSignedHash()) - }) t.Run("Hash", func(t *testing.T) { p1, p2 := getExtensiblePair() require.NotEqual(t, p1.Hash(), p2.Hash()) diff --git a/pkg/network/server_test.go b/pkg/network/server_test.go index 0f30f8771..ff0d4ab70 100644 --- a/pkg/network/server_test.go +++ b/pkg/network/server_test.go @@ -407,7 +407,7 @@ func TestConsensus(t *testing.T) { p.handshaked = true newConsensusMessage := func(start, end uint32) *Message { - pl := payload.NewExtensible(netmode.UnitTestNet) + pl := payload.NewExtensible() pl.Category = consensus.Category pl.ValidBlockStart = start pl.ValidBlockEnd = end @@ -438,7 +438,7 @@ func TestConsensus(t *testing.T) { require.Error(t, s.handleMessage(p, msg)) }) t.Run("invalid category", func(t *testing.T) { - pl := payload.NewExtensible(netmode.UnitTestNet) + pl := payload.NewExtensible() pl.Category = "invalid" pl.ValidBlockEnd = s.chain.BlockHeight() + 1 msg := NewMessage(CMDExtensible, pl) @@ -706,7 +706,7 @@ func TestInv(t *testing.T) { require.Equal(t, []util.Uint256{hs[0], hs[2]}, actual) }) t.Run("extensible", func(t *testing.T) { - ep := payload.NewExtensible(netmode.UnitTestNet) + ep := payload.NewExtensible() s.chain.(*fakechain.FakeChain).VerifyWitnessF = func() error { return nil } ep.ValidBlockEnd = s.chain.(*fakechain.FakeChain).BlockHeight() + 1 ok, err := s.extensiblePool.Add(ep) diff --git a/pkg/services/stateroot/network.go b/pkg/services/stateroot/network.go index 9f72b7758..203478b17 100644 --- a/pkg/services/stateroot/network.go +++ b/pkg/services/stateroot/network.go @@ -81,7 +81,6 @@ func (s *service) sendValidatedRoot(r *state.MPTRoot, priv *keys.PrivateKey) { m := NewMessage(s.Network, RootT, r) m.EncodeBinary(w.BinWriter) ep := &payload.Extensible{ - Network: s.Network, ValidBlockStart: r.Index, ValidBlockEnd: r.Index + transaction.MaxValidUntilBlockIncrement, Sender: priv.GetScriptHash(), @@ -90,7 +89,7 @@ func (s *service) sendValidatedRoot(r *state.MPTRoot, priv *keys.PrivateKey) { VerificationScript: s.getAccount().GetVerificationScript(), }, } - sig := priv.SignHash(ep.GetSignedHash()) + sig := priv.SignHashable(uint32(s.Network), ep) buf := io.NewBufBinWriter() emit.Bytes(buf.BinWriter, sig) ep.Witness.InvocationScript = buf.Bytes() diff --git a/pkg/services/stateroot/validators.go b/pkg/services/stateroot/validators.go index 298afc1ca..2e48bacc5 100644 --- a/pkg/services/stateroot/validators.go +++ b/pkg/services/stateroot/validators.go @@ -68,7 +68,6 @@ func (s *service) signAndSend(r *state.MPTRoot) error { return w.Err } e := &payload.Extensible{ - Network: s.Network, ValidBlockStart: r.Index, ValidBlockEnd: r.Index + transaction.MaxValidUntilBlockIncrement, Sender: s.getAccount().PrivateKey().GetScriptHash(), @@ -77,7 +76,7 @@ func (s *service) signAndSend(r *state.MPTRoot) error { VerificationScript: s.getAccount().GetVerificationScript(), }, } - sig = acc.PrivateKey().SignHash(e.GetSignedHash()) + sig = acc.PrivateKey().SignHashable(uint32(s.Network), e) buf := io.NewBufBinWriter() emit.Bytes(buf.BinWriter, sig) e.Witness.InvocationScript = buf.Bytes()