payload: drop Network from Extensible

It's only used to sign/verify it and is not a part of the structure. It's
still neded in consensus.Payload though because that's the way dbft library
is.
This commit is contained in:
Roman Khimov 2021-03-25 21:59:54 +03:00
parent 95c279325a
commit f91ff78918
10 changed files with 18 additions and 76 deletions

View file

@ -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,
}
}

View file

@ -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 {

View file

@ -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 {

View file

@ -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,
}
}

View file

@ -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:

View file

@ -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()
}

View file

@ -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())

View file

@ -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)

View file

@ -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()

View file

@ -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()