Merge pull request #982 from nspcc-dev/neo3/protocol/getblockdata

protocol: implement getblockdata p2p command
This commit is contained in:
Roman Khimov 2020-05-22 19:28:42 +03:00 committed by GitHub
commit bd98940a54
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 105 additions and 26 deletions

View file

@ -0,0 +1,39 @@
package payload
import (
"errors"
"github.com/nspcc-dev/neo-go/pkg/io"
)
// maximum number of blocks to query about
const maxBlockCount = 500
// GetBlockData payload
type GetBlockData struct {
IndexStart uint32
Count uint16
}
// NewGetBlockData returns GetBlockData payload with specified start index and count
func NewGetBlockData(indexStart uint32, count uint16) *GetBlockData {
return &GetBlockData{
IndexStart: indexStart,
Count: count,
}
}
// DecodeBinary implements Serializable interface.
func (d *GetBlockData) DecodeBinary(br *io.BinReader) {
d.IndexStart = br.ReadU32LE()
d.Count = br.ReadU16LE()
if d.Count == 0 || d.Count > maxBlockCount {
br.Err = errors.New("invalid block count")
}
}
// EncodeBinary implements Serializable interface.
func (d *GetBlockData) EncodeBinary(bw *io.BinWriter) {
bw.WriteU32LE(d.IndexStart)
bw.WriteU16LE(d.Count)
}

View file

@ -0,0 +1,25 @@
package payload
import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/require"
)
func TestGetBlockDataEncodeDecode(t *testing.T) {
d := NewGetBlockData(123, 100)
testserdes.EncodeDecodeBinary(t, d, new(GetBlockData))
// invalid block count
d = NewGetBlockData(5, 0)
data, err := testserdes.EncodeBinary(d)
require.NoError(t, err)
require.Error(t, testserdes.DecodeBinary(data, new(GetBlockData)))
// invalid block count
d = NewGetBlockData(5, maxBlockCount+1)
data, err = testserdes.EncodeBinary(d)
require.NoError(t, err)
require.Error(t, testserdes.DecodeBinary(data, new(GetBlockData)))
}