From 737ba700e9d44aed1be4bb2474a0014901813772 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Fri, 31 Jul 2020 13:58:22 +0300 Subject: [PATCH] network: rename GetBlockData command GetBlockData -> GetBlockByIndex --- pkg/network/message.go | 28 +++++++++---------- pkg/network/message_string.go | 6 ++-- .../{getblockdata.go => getblockbyindex.go} | 14 +++++----- ...ckdata_test.go => getblockbyindex_test.go} | 12 ++++---- pkg/network/server.go | 10 +++---- 5 files changed, 35 insertions(+), 35 deletions(-) rename pkg/network/payload/{getblockdata.go => getblockbyindex.go} (60%) rename pkg/network/payload/{getblockdata_test.go => getblockbyindex_test.go} (54%) diff --git a/pkg/network/message.go b/pkg/network/message.go index 1fee8e24f..d336bd5ad 100644 --- a/pkg/network/message.go +++ b/pkg/network/message.go @@ -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: diff --git a/pkg/network/message_string.go b/pkg/network/message_string.go index d505f9696..233c9084b 100644 --- a/pkg/network/message_string.go +++ b/pkg/network/message_string.go @@ -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} ) diff --git a/pkg/network/payload/getblockdata.go b/pkg/network/payload/getblockbyindex.go similarity index 60% rename from pkg/network/payload/getblockdata.go rename to pkg/network/payload/getblockbyindex.go index 2ed9c0d9f..bd7688abe 100644 --- a/pkg/network/payload/getblockdata.go +++ b/pkg/network/payload/getblockbyindex.go @@ -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) } diff --git a/pkg/network/payload/getblockdata_test.go b/pkg/network/payload/getblockbyindex_test.go similarity index 54% rename from pkg/network/payload/getblockdata_test.go rename to pkg/network/payload/getblockbyindex_test.go index 6704af858..e02dfafa2 100644 --- a/pkg/network/payload/getblockdata_test.go +++ b/pkg/network/payload/getblockbyindex_test.go @@ -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))) } diff --git a/pkg/network/server.go b/pkg/network/server.go index 7ea3f2f3b..874642588 100644 --- a/pkg/network/server.go +++ b/pkg/network/server.go @@ -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)