From 4333ad49490ce3cb1f5adcf0b5738911e141558c Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 8 Jul 2022 14:50:00 +0300 Subject: [PATCH] result: drop NewBlock/NewHeader/LedgerAux Client's don't care about any of these. --- pkg/rpc/response/result/block.go | 24 ------------------------ pkg/rpc/response/result/block_header.go | 19 ------------------- pkg/rpc/server/server.go | 25 +++++++++++++++++++++++-- 3 files changed, 23 insertions(+), 45 deletions(-) diff --git a/pkg/rpc/response/result/block.go b/pkg/rpc/response/result/block.go index 96394776f..f69b5f554 100644 --- a/pkg/rpc/response/result/block.go +++ b/pkg/rpc/response/result/block.go @@ -5,16 +5,10 @@ import ( "errors" "github.com/nspcc-dev/neo-go/pkg/core/block" - "github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/util" ) type ( - // LedgerAux is a set of methods needed to construct some outputs. - LedgerAux interface { - BlockHeight() uint32 - GetHeaderHash(int) util.Uint256 - } // Block wrapper used for the representation of // block.Block / block.Base on the RPC Server. Block struct { @@ -31,24 +25,6 @@ type ( } ) -// NewBlock creates a new Block wrapper. -func NewBlock(b *block.Block, chain LedgerAux) Block { - res := Block{ - Block: *b, - BlockMetadata: BlockMetadata{ - Size: io.GetVarSize(b), - Confirmations: chain.BlockHeight() - b.Index + 1, - }, - } - - hash := chain.GetHeaderHash(int(b.Index) + 1) - if !hash.Equals(util.Uint256{}) { - res.NextBlockHash = &hash - } - - return res -} - // MarshalJSON implements the json.Marshaler interface. func (b Block) MarshalJSON() ([]byte, error) { output, err := json.Marshal(b.BlockMetadata) diff --git a/pkg/rpc/response/result/block_header.go b/pkg/rpc/response/result/block_header.go index 1c23e51c7..421abc536 100644 --- a/pkg/rpc/response/result/block_header.go +++ b/pkg/rpc/response/result/block_header.go @@ -5,8 +5,6 @@ import ( "errors" "github.com/nspcc-dev/neo-go/pkg/core/block" - "github.com/nspcc-dev/neo-go/pkg/io" - "github.com/nspcc-dev/neo-go/pkg/util" ) type ( @@ -18,23 +16,6 @@ type ( } ) -// NewHeader creates a new Header wrapper. -func NewHeader(h *block.Header, chain LedgerAux) Header { - res := Header{ - Header: *h, - BlockMetadata: BlockMetadata{ - Size: io.GetVarSize(h), - Confirmations: chain.BlockHeight() - h.Index + 1, - }, - } - - hash := chain.GetHeaderHash(int(h.Index) + 1) - if !hash.Equals(util.Uint256{}) { - res.NextBlockHash = &hash - } - return res -} - // MarshalJSON implements the json.Marshaler interface. func (h Header) MarshalJSON() ([]byte, error) { output, err := json.Marshal(h.BlockMetadata) diff --git a/pkg/rpc/server/server.go b/pkg/rpc/server/server.go index 0c2d7e9be..8ecd24be4 100644 --- a/pkg/rpc/server/server.go +++ b/pkg/rpc/server/server.go @@ -582,6 +582,19 @@ func (s *Server) blockHashFromParam(param *params.Param) (util.Uint256, *respons return hash, nil } +func (s *Server) fillBlockMetadata(obj io.Serializable, h *block.Header) result.BlockMetadata { + res := result.BlockMetadata{ + Size: io.GetVarSize(obj), // obj can be a Block or a Header. + Confirmations: s.chain.BlockHeight() - h.Index + 1, + } + + hash := s.chain.GetHeaderHash(int(h.Index) + 1) + if !hash.Equals(util.Uint256{}) { + res.NextBlockHash = &hash + } + return res +} + func (s *Server) getBlock(reqParams params.Params) (interface{}, *response.Error) { param := reqParams.Value(0) hash, respErr := s.blockHashFromParam(param) @@ -595,7 +608,11 @@ func (s *Server) getBlock(reqParams params.Params) (interface{}, *response.Error } if v, _ := reqParams.Value(1).GetBoolean(); v { - return result.NewBlock(block, s.chain), nil + res := result.Block{ + Block: *block, + BlockMetadata: s.fillBlockMetadata(block, &block.Header), + } + return res, nil } writer := io.NewBufBinWriter() block.EncodeBinary(writer.BinWriter) @@ -1639,7 +1656,11 @@ func (s *Server) getBlockHeader(reqParams params.Params) (interface{}, *response } if verbose { - return result.NewHeader(h, s.chain), nil + res := result.Header{ + Header: *h, + BlockMetadata: s.fillBlockMetadata(h, h), + } + return res, nil } buf := io.NewBufBinWriter()