network: rename GetBlockData command

GetBlockData -> GetBlockByIndex
This commit is contained in:
Anna Shaleva 2020-07-31 13:58:22 +03:00
parent 7c6cdcbcc9
commit 737ba700e9
5 changed files with 35 additions and 35 deletions

View file

@ -61,18 +61,18 @@ const (
CMDPong CommandType = 0x19
// synchronization
CMDGetHeaders CommandType = 0x20
CMDHeaders CommandType = 0x21
CMDGetBlocks CommandType = 0x24
CMDMempool CommandType = 0x25
CMDInv CommandType = 0x27
CMDGetData CommandType = 0x28
CMDGetBlockData CommandType = 0x29
CMDNotFound CommandType = 0x2a
CMDTX = CommandType(payload.TXType)
CMDBlock = CommandType(payload.BlockType)
CMDConsensus = CommandType(payload.ConsensusType)
CMDReject CommandType = 0x2f
CMDGetHeaders CommandType = 0x20
CMDHeaders CommandType = 0x21
CMDGetBlocks CommandType = 0x24
CMDMempool CommandType = 0x25
CMDInv CommandType = 0x27
CMDGetData CommandType = 0x28
CMDGetBlockByIndex CommandType = 0x29
CMDNotFound CommandType = 0x2a
CMDTX = CommandType(payload.TXType)
CMDBlock = CommandType(payload.BlockType)
CMDConsensus = CommandType(payload.ConsensusType)
CMDReject CommandType = 0x2f
// SPV protocol
CMDFilterLoad CommandType = 0x30
@ -149,8 +149,8 @@ func (m *Message) decodePayload() error {
fallthrough
case CMDGetHeaders:
p = &payload.GetBlocks{}
case CMDGetBlockData:
p = &payload.GetBlockData{}
case CMDGetBlockByIndex:
p = &payload.GetBlockByIndex{}
case CMDHeaders:
p = &payload.Headers{Network: m.Network}
case CMDTX:

View file

@ -20,7 +20,7 @@ func _() {
_ = x[CMDMempool-37]
_ = x[CMDInv-39]
_ = x[CMDGetData-40]
_ = x[CMDGetBlockData-41]
_ = x[CMDGetBlockByIndex-41]
_ = x[CMDNotFound-42]
_ = x[CMDTX-43]
_ = x[CMDBlock-44]
@ -39,7 +39,7 @@ const (
_CommandType_name_2 = "CMDPingCMDPong"
_CommandType_name_3 = "CMDGetHeadersCMDHeaders"
_CommandType_name_4 = "CMDGetBlocksCMDMempool"
_CommandType_name_5 = "CMDInvCMDGetDataCMDGetBlockDataCMDNotFoundCMDTXCMDBlockCMDConsensus"
_CommandType_name_5 = "CMDInvCMDGetDataCMDGetBlockByIndexCMDNotFoundCMDTXCMDBlockCMDConsensus"
_CommandType_name_6 = "CMDRejectCMDFilterLoadCMDFilterAddCMDFilterClear"
_CommandType_name_7 = "CMDMerkleBlock"
_CommandType_name_8 = "CMDAlert"
@ -51,7 +51,7 @@ var (
_CommandType_index_2 = [...]uint8{0, 7, 14}
_CommandType_index_3 = [...]uint8{0, 13, 23}
_CommandType_index_4 = [...]uint8{0, 12, 22}
_CommandType_index_5 = [...]uint8{0, 6, 16, 31, 42, 47, 55, 67}
_CommandType_index_5 = [...]uint8{0, 6, 16, 34, 45, 50, 58, 70}
_CommandType_index_6 = [...]uint8{0, 9, 22, 34, 48}
)

View file

@ -9,22 +9,22 @@ import (
// maximum number of blocks to query about
const maxBlockCount = 500
// GetBlockData payload
type GetBlockData struct {
// GetBlockByIndex payload
type GetBlockByIndex struct {
IndexStart uint32
Count uint16
}
// NewGetBlockData returns GetBlockData payload with specified start index and count
func NewGetBlockData(indexStart uint32, count uint16) *GetBlockData {
return &GetBlockData{
// NewGetBlockByIndex returns GetBlockByIndex payload with specified start index and count
func NewGetBlockByIndex(indexStart uint32, count uint16) *GetBlockByIndex {
return &GetBlockByIndex{
IndexStart: indexStart,
Count: count,
}
}
// DecodeBinary implements Serializable interface.
func (d *GetBlockData) DecodeBinary(br *io.BinReader) {
func (d *GetBlockByIndex) DecodeBinary(br *io.BinReader) {
d.IndexStart = br.ReadU32LE()
d.Count = br.ReadU16LE()
if d.Count == 0 || d.Count > maxBlockCount {
@ -33,7 +33,7 @@ func (d *GetBlockData) DecodeBinary(br *io.BinReader) {
}
// EncodeBinary implements Serializable interface.
func (d *GetBlockData) EncodeBinary(bw *io.BinWriter) {
func (d *GetBlockByIndex) EncodeBinary(bw *io.BinWriter) {
bw.WriteU32LE(d.IndexStart)
bw.WriteU16LE(d.Count)
}

View file

@ -8,18 +8,18 @@ import (
)
func TestGetBlockDataEncodeDecode(t *testing.T) {
d := NewGetBlockData(123, 100)
testserdes.EncodeDecodeBinary(t, d, new(GetBlockData))
d := NewGetBlockByIndex(123, 100)
testserdes.EncodeDecodeBinary(t, d, new(GetBlockByIndex))
// invalid block count
d = NewGetBlockData(5, 0)
d = NewGetBlockByIndex(5, 0)
data, err := testserdes.EncodeBinary(d)
require.NoError(t, err)
require.Error(t, testserdes.DecodeBinary(data, new(GetBlockData)))
require.Error(t, testserdes.DecodeBinary(data, new(GetBlockByIndex)))
// invalid block count
d = NewGetBlockData(5, maxBlockCount+1)
d = NewGetBlockByIndex(5, maxBlockCount+1)
data, err = testserdes.EncodeBinary(d)
require.NoError(t, err)
require.Error(t, testserdes.DecodeBinary(data, new(GetBlockData)))
require.Error(t, testserdes.DecodeBinary(data, new(GetBlockByIndex)))
}

View file

@ -609,8 +609,8 @@ func (s *Server) handleGetBlocksCmd(p Peer, gb *payload.GetBlocks) error {
return p.EnqueueP2PMessage(msg)
}
// handleGetBlockDataCmd processes the getblockdata request.
func (s *Server) handleGetBlockDataCmd(p Peer, gbd *payload.GetBlockData) error {
// handleGetBlockByIndexCmd processes the getblockbyindex request.
func (s *Server) handleGetBlockByIndexCmd(p Peer, gbd *payload.GetBlockByIndex) error {
for i := gbd.IndexStart; i < gbd.IndexStart+uint32(gbd.Count); i++ {
b, err := s.chain.GetBlock(s.chain.GetHeaderHash(int(i)))
if err != nil {
@ -750,9 +750,9 @@ func (s *Server) handleMessage(peer Peer, msg *Message) error {
case CMDGetBlocks:
gb := msg.Payload.(*payload.GetBlocks)
return s.handleGetBlocksCmd(peer, gb)
case CMDGetBlockData:
gbd := msg.Payload.(*payload.GetBlockData)
return s.handleGetBlockDataCmd(peer, gbd)
case CMDGetBlockByIndex:
gbd := msg.Payload.(*payload.GetBlockByIndex)
return s.handleGetBlockByIndexCmd(peer, gbd)
case CMDGetData:
inv := msg.Payload.(*payload.Inventory)
return s.handleGetDataCmd(peer, inv)