mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-27 03:58:06 +00:00
network: refactor P2PNotaryRequest decoding
We need to provide magic for both main and fallback transactions during decoding, because transactions hashes depend on it.
This commit is contained in:
parent
c14e34cdb5
commit
5c2ea2d5bb
3 changed files with 40 additions and 5 deletions
|
@ -24,6 +24,31 @@ type P2PNotaryRequest struct {
|
||||||
signedHash util.Uint256
|
signedHash util.Uint256
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewP2PNotaryRequestFromBytes decodes P2PNotaryRequest from the given bytes.
|
||||||
|
func NewP2PNotaryRequestFromBytes(network netmode.Magic, b []byte) (*P2PNotaryRequest, error) {
|
||||||
|
req := &P2PNotaryRequest{Network: network}
|
||||||
|
br := io.NewBinReaderFromBuf(b)
|
||||||
|
req.DecodeBinary(br)
|
||||||
|
if br.Err != nil {
|
||||||
|
return nil, br.Err
|
||||||
|
}
|
||||||
|
_ = br.ReadB()
|
||||||
|
if br.Err == nil {
|
||||||
|
return nil, errors.New("additional data after the payload")
|
||||||
|
}
|
||||||
|
return req, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bytes returns serialized P2PNotaryRequest payload.
|
||||||
|
func (r *P2PNotaryRequest) Bytes() ([]byte, error) {
|
||||||
|
buf := io.NewBufBinWriter()
|
||||||
|
r.EncodeBinary(buf.BinWriter)
|
||||||
|
if buf.Err != nil {
|
||||||
|
return nil, buf.Err
|
||||||
|
}
|
||||||
|
return buf.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
// Hash returns payload's hash.
|
// Hash returns payload's hash.
|
||||||
func (r *P2PNotaryRequest) Hash() util.Uint256 {
|
func (r *P2PNotaryRequest) Hash() util.Uint256 {
|
||||||
if r.hash.Equals(util.Uint256{}) {
|
if r.hash.Equals(util.Uint256{}) {
|
||||||
|
@ -74,8 +99,8 @@ func (r *P2PNotaryRequest) updateHashes(b []byte) {
|
||||||
|
|
||||||
// DecodeBinaryUnsigned reads payload from w excluding signature.
|
// DecodeBinaryUnsigned reads payload from w excluding signature.
|
||||||
func (r *P2PNotaryRequest) decodeHashableFields(br *io.BinReader) {
|
func (r *P2PNotaryRequest) decodeHashableFields(br *io.BinReader) {
|
||||||
r.MainTransaction = new(transaction.Transaction)
|
r.MainTransaction = &transaction.Transaction{Network: r.Network}
|
||||||
r.FallbackTransaction = new(transaction.Transaction)
|
r.FallbackTransaction = &transaction.Transaction{Network: r.Network}
|
||||||
r.MainTransaction.DecodeBinary(br)
|
r.MainTransaction.DecodeBinary(br)
|
||||||
r.FallbackTransaction.DecodeBinary(br)
|
r.FallbackTransaction.DecodeBinary(br)
|
||||||
if br.Err == nil {
|
if br.Err == nil {
|
||||||
|
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/nspcc-dev/neo-go/internal/random"
|
"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/core/transaction"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||||
|
@ -143,8 +143,9 @@ func TestNotaryRequestIsValid(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNotaryRequestEncodeDecodeBinary(t *testing.T) {
|
func TestNotaryRequestBytesFromBytes(t *testing.T) {
|
||||||
mainTx := &transaction.Transaction{
|
mainTx := &transaction.Transaction{
|
||||||
|
Network: netmode.UnitTestNet,
|
||||||
Attributes: []transaction.Attribute{{Type: transaction.NotaryAssistedT, Value: &transaction.NotaryAssisted{NKeys: 1}}},
|
Attributes: []transaction.Attribute{{Type: transaction.NotaryAssistedT, Value: &transaction.NotaryAssisted{NKeys: 1}}},
|
||||||
Script: []byte{0, 1, 2},
|
Script: []byte{0, 1, 2},
|
||||||
ValidUntilBlock: 123,
|
ValidUntilBlock: 123,
|
||||||
|
@ -157,6 +158,7 @@ func TestNotaryRequestEncodeDecodeBinary(t *testing.T) {
|
||||||
_ = mainTx.Hash()
|
_ = mainTx.Hash()
|
||||||
_ = mainTx.Size()
|
_ = mainTx.Size()
|
||||||
fallbackTx := &transaction.Transaction{
|
fallbackTx := &transaction.Transaction{
|
||||||
|
Network: netmode.UnitTestNet,
|
||||||
Script: []byte{3, 2, 1},
|
Script: []byte{3, 2, 1},
|
||||||
ValidUntilBlock: 123,
|
ValidUntilBlock: 123,
|
||||||
Attributes: []transaction.Attribute{
|
Attributes: []transaction.Attribute{
|
||||||
|
@ -172,6 +174,7 @@ func TestNotaryRequestEncodeDecodeBinary(t *testing.T) {
|
||||||
_ = fallbackTx.Hash()
|
_ = fallbackTx.Hash()
|
||||||
_ = fallbackTx.Size()
|
_ = fallbackTx.Size()
|
||||||
p := &P2PNotaryRequest{
|
p := &P2PNotaryRequest{
|
||||||
|
Network: netmode.UnitTestNet,
|
||||||
MainTransaction: mainTx,
|
MainTransaction: mainTx,
|
||||||
FallbackTransaction: fallbackTx,
|
FallbackTransaction: fallbackTx,
|
||||||
Witness: transaction.Witness{
|
Witness: transaction.Witness{
|
||||||
|
@ -180,5 +183,10 @@ func TestNotaryRequestEncodeDecodeBinary(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
require.Equal(t, hash.Sha256(p.GetSignedHash().BytesBE()), p.Hash())
|
require.Equal(t, hash.Sha256(p.GetSignedHash().BytesBE()), p.Hash())
|
||||||
testserdes.EncodeDecodeBinary(t, p, new(P2PNotaryRequest))
|
|
||||||
|
bytes, err := p.Bytes()
|
||||||
|
require.NoError(t, err)
|
||||||
|
actual, err := NewP2PNotaryRequestFromBytes(netmode.UnitTestNet, bytes)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, p, actual)
|
||||||
}
|
}
|
||||||
|
|
|
@ -524,6 +524,7 @@ func TestGetData(t *testing.T) {
|
||||||
})
|
})
|
||||||
t.Run("p2pNotaryRequest", func(t *testing.T) {
|
t.Run("p2pNotaryRequest", func(t *testing.T) {
|
||||||
mainTx := &transaction.Transaction{
|
mainTx := &transaction.Transaction{
|
||||||
|
Network: netmode.UnitTestNet,
|
||||||
Attributes: []transaction.Attribute{{Type: transaction.NotaryAssistedT, Value: &transaction.NotaryAssisted{NKeys: 1}}},
|
Attributes: []transaction.Attribute{{Type: transaction.NotaryAssistedT, Value: &transaction.NotaryAssisted{NKeys: 1}}},
|
||||||
Script: []byte{0, 1, 2},
|
Script: []byte{0, 1, 2},
|
||||||
ValidUntilBlock: 123,
|
ValidUntilBlock: 123,
|
||||||
|
@ -533,6 +534,7 @@ func TestGetData(t *testing.T) {
|
||||||
mainTx.Size()
|
mainTx.Size()
|
||||||
mainTx.Hash()
|
mainTx.Hash()
|
||||||
fallbackTx := &transaction.Transaction{
|
fallbackTx := &transaction.Transaction{
|
||||||
|
Network: netmode.UnitTestNet,
|
||||||
Script: []byte{1, 2, 3},
|
Script: []byte{1, 2, 3},
|
||||||
ValidUntilBlock: 123,
|
ValidUntilBlock: 123,
|
||||||
Attributes: []transaction.Attribute{
|
Attributes: []transaction.Attribute{
|
||||||
|
|
Loading…
Reference in a new issue