From 489b88afbba54bcd9d4cd5ca13d5b8e2cfaaee1d Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 15 Jan 2020 11:29:50 +0300 Subject: [PATCH] block: rename BlockBase to Base --- pkg/consensus/block.go | 4 ++-- pkg/core/block/block.go | 8 ++++---- pkg/core/block/block_base.go | 24 ++++++++++++------------ pkg/core/block/block_test.go | 8 ++++---- pkg/core/block/header.go | 6 +++--- pkg/core/block/header_test.go | 2 +- pkg/core/dao_test.go | 4 ++-- pkg/core/helper_test.go | 4 ++-- pkg/core/util.go | 4 ++-- pkg/network/blockqueue_test.go | 2 +- pkg/network/payload/headers_test.go | 2 +- pkg/network/payload/merkleblock.go | 10 +++++----- pkg/rpc/wrappers/block.go | 2 +- pkg/rpc/wrappers/tx_raw_output.go | 2 +- 14 files changed, 41 insertions(+), 41 deletions(-) diff --git a/pkg/consensus/block.go b/pkg/consensus/block.go index f71f4bfb5..90824fe9e 100644 --- a/pkg/consensus/block.go +++ b/pkg/consensus/block.go @@ -20,7 +20,7 @@ var _ block.Block = (*neoBlock)(nil) // Sign implements block.Block interface. func (n *neoBlock) Sign(key crypto.PrivateKey) error { - data := n.BlockBase.GetHashableData() + data := n.Base.GetHashableData() sig, err := key.Sign(data[:]) if err != nil { return err @@ -33,7 +33,7 @@ func (n *neoBlock) Sign(key crypto.PrivateKey) error { // Verify implements block.Block interface. func (n *neoBlock) Verify(key crypto.PublicKey, sign []byte) error { - data := n.BlockBase.GetHashableData() + data := n.Base.GetHashableData() return key.Verify(data, sign) } diff --git a/pkg/core/block/block.go b/pkg/core/block/block.go index 3077095c7..4eb533a8d 100644 --- a/pkg/core/block/block.go +++ b/pkg/core/block/block.go @@ -14,7 +14,7 @@ import ( // Block represents one block in the chain. type Block struct { // The base of the block. - BlockBase + Base // Transaction list. Transactions []*transaction.Transaction `json:"tx"` @@ -26,7 +26,7 @@ type Block struct { // Header returns the Header of the Block. func (b *Block) Header() *Header { return &Header{ - BlockBase: b.BlockBase, + Base: b.Base, } } @@ -126,14 +126,14 @@ func (b *Block) Trim() ([]byte, error) { // DecodeBinary decodes the block from the given BinReader, implementing // Serializable interface. func (b *Block) DecodeBinary(br *io.BinReader) { - b.BlockBase.DecodeBinary(br) + b.Base.DecodeBinary(br) br.ReadArray(&b.Transactions) } // EncodeBinary encodes the block to the given BinWriter, implementing // Serializable interface. func (b *Block) EncodeBinary(bw *io.BinWriter) { - b.BlockBase.EncodeBinary(bw) + b.Base.EncodeBinary(bw) bw.WriteArray(b.Transactions) } diff --git a/pkg/core/block/block_base.go b/pkg/core/block/block_base.go index 404701059..8655ea9ac 100644 --- a/pkg/core/block/block_base.go +++ b/pkg/core/block/block_base.go @@ -9,8 +9,8 @@ import ( "github.com/CityOfZion/neo-go/pkg/util" ) -// BlockBase holds the base info of a block -type BlockBase struct { +// Base holds the base info of a block +type Base struct { // Version of the block. Version uint32 `json:"version"` @@ -47,14 +47,14 @@ type BlockBase struct { verificationHash util.Uint256 } -// Verify verifies the integrity of the BlockBase. -func (b *BlockBase) Verify() bool { +// Verify verifies the integrity of the Base. +func (b *Base) Verify() bool { // TODO: Need a persisted blockchain for this. return true } // Hash returns the hash of the block. -func (b *BlockBase) Hash() util.Uint256 { +func (b *Base) Hash() util.Uint256 { if b.hash.Equals(util.Uint256{}) { b.createHash() } @@ -62,7 +62,7 @@ func (b *BlockBase) Hash() util.Uint256 { } // VerificationHash returns the hash of the block used to verify it. -func (b *BlockBase) VerificationHash() util.Uint256 { +func (b *Base) VerificationHash() util.Uint256 { if b.verificationHash.Equals(util.Uint256{}) { b.createHash() } @@ -70,7 +70,7 @@ func (b *BlockBase) VerificationHash() util.Uint256 { } // DecodeBinary implements Serializable interface. -func (b *BlockBase) DecodeBinary(br *io.BinReader) { +func (b *Base) DecodeBinary(br *io.BinReader) { b.decodeHashableFields(br) padding := []byte{0} @@ -84,14 +84,14 @@ func (b *BlockBase) DecodeBinary(br *io.BinReader) { } // EncodeBinary implements Serializable interface -func (b *BlockBase) EncodeBinary(bw *io.BinWriter) { +func (b *Base) EncodeBinary(bw *io.BinWriter) { b.encodeHashableFields(bw) bw.WriteBytes([]byte{1}) b.Script.EncodeBinary(bw) } // GetHashableData returns serialized hashable data of the block. -func (b *BlockBase) GetHashableData() []byte { +func (b *Base) GetHashableData() []byte { buf := io.NewBufBinWriter() // No error can occure while encoding hashable fields. b.encodeHashableFields(buf.BinWriter) @@ -105,7 +105,7 @@ func (b *BlockBase) GetHashableData() []byte { // version, PrevBlock, MerkleRoot, timestamp, and height, the nonce, NextMiner. // Since MerkleRoot already contains the hash value of all transactions, // the modification of transaction will influence the hash value of the block. -func (b *BlockBase) createHash() { +func (b *Base) createHash() { bb := b.GetHashableData() b.verificationHash = hash.Sha256(bb) b.hash = hash.Sha256(b.verificationHash.BytesBE()) @@ -113,7 +113,7 @@ func (b *BlockBase) createHash() { // encodeHashableFields will only encode the fields used for hashing. // see Hash() for more information about the fields. -func (b *BlockBase) encodeHashableFields(bw *io.BinWriter) { +func (b *Base) encodeHashableFields(bw *io.BinWriter) { bw.WriteU32LE(b.Version) bw.WriteBytes(b.PrevHash[:]) bw.WriteBytes(b.MerkleRoot[:]) @@ -125,7 +125,7 @@ func (b *BlockBase) encodeHashableFields(bw *io.BinWriter) { // decodeHashableFields decodes the fields used for hashing. // see Hash() for more information about the fields. -func (b *BlockBase) decodeHashableFields(br *io.BinReader) { +func (b *Base) decodeHashableFields(br *io.BinReader) { b.Version = br.ReadU32LE() br.ReadBytes(b.PrevHash[:]) br.ReadBytes(b.MerkleRoot[:]) diff --git a/pkg/core/block/block_test.go b/pkg/core/block/block_test.go index 42355e9c0..571e404d3 100644 --- a/pkg/core/block/block_test.go +++ b/pkg/core/block/block_test.go @@ -79,7 +79,7 @@ func TestTrimmedBlock(t *testing.T) { func newDumbBlock() *Block { return &Block{ - BlockBase: BlockBase{ + Base: Base{ Version: 0, PrevHash: hash.Sha256([]byte("a")), MerkleRoot: hash.Sha256([]byte("b")), @@ -291,9 +291,9 @@ func TestBlockSizeCalculation(t *testing.T) { } func TestBlockCompare(t *testing.T) { - b1 := Block{BlockBase: BlockBase{Index: 1}} - b2 := Block{BlockBase: BlockBase{Index: 2}} - b3 := Block{BlockBase: BlockBase{Index: 3}} + b1 := Block{Base: Base{Index: 1}} + b2 := Block{Base: Base{Index: 2}} + b3 := Block{Base: Base{Index: 3}} assert.Equal(t, 1, b2.Compare(&b1)) assert.Equal(t, 0, b2.Compare(&b2)) assert.Equal(t, -1, b2.Compare(&b3)) diff --git a/pkg/core/block/header.go b/pkg/core/block/header.go index 5f9060f3d..98804870c 100644 --- a/pkg/core/block/header.go +++ b/pkg/core/block/header.go @@ -9,14 +9,14 @@ import ( // Header holds the head info of a block. type Header struct { // Base of the block. - BlockBase + Base // Padding that is fixed to 0. _ uint8 } // DecodeBinary implements Serializable interface. func (h *Header) DecodeBinary(r *io.BinReader) { - h.BlockBase.DecodeBinary(r) + h.Base.DecodeBinary(r) padding := []byte{0} r.ReadBytes(padding) @@ -28,6 +28,6 @@ func (h *Header) DecodeBinary(r *io.BinReader) { // EncodeBinary implements Serializable interface. func (h *Header) EncodeBinary(w *io.BinWriter) { - h.BlockBase.EncodeBinary(w) + h.Base.EncodeBinary(w) w.WriteBytes([]byte{0}) } diff --git a/pkg/core/block/header_test.go b/pkg/core/block/header_test.go index 020afcbda..d7ea801f5 100644 --- a/pkg/core/block/header_test.go +++ b/pkg/core/block/header_test.go @@ -12,7 +12,7 @@ import ( ) func TestHeaderEncodeDecode(t *testing.T) { - header := Header{BlockBase: BlockBase{ + header := Header{Base: Base{ Version: 0, PrevHash: hash.Sha256([]byte("prevhash")), MerkleRoot: hash.Sha256([]byte("merkleroot")), diff --git a/pkg/core/dao_test.go b/pkg/core/dao_test.go index cf06aa757..0bee6800f 100644 --- a/pkg/core/dao_test.go +++ b/pkg/core/dao_test.go @@ -262,7 +262,7 @@ func TestGetBlock_NotExists(t *testing.T) { func TestPutGetBlock(t *testing.T) { dao := newDao(storage.NewMemoryStore()) b := &block.Block{ - BlockBase: block.BlockBase{ + Base: block.Base{ Script: transaction.Witness{ VerificationScript: []byte{byte(opcode.PUSH1)}, InvocationScript: []byte{byte(opcode.NOP)}, @@ -303,7 +303,7 @@ func TestGetCurrentHeaderHeight_NoHeader(t *testing.T) { func TestGetCurrentHeaderHeight_Store(t *testing.T) { dao := newDao(storage.NewMemoryStore()) b := &block.Block{ - BlockBase: block.BlockBase{ + Base: block.Base{ Script: transaction.Witness{ VerificationScript: []byte{byte(opcode.PUSH1)}, InvocationScript: []byte{byte(opcode.NOP)}, diff --git a/pkg/core/helper_test.go b/pkg/core/helper_test.go index 9a452ffc9..915d33ea6 100644 --- a/pkg/core/helper_test.go +++ b/pkg/core/helper_test.go @@ -62,7 +62,7 @@ func newBlock(index uint32, txs ...*transaction.Transaction) *block.Block { VerificationScript: valScript, } b := &block.Block{ - BlockBase: block.BlockBase{ + Base: block.Base{ Version: 0, PrevHash: newBlockPrevHash, Timestamp: uint32(time.Now().UTC().Unix()) + index, @@ -144,7 +144,7 @@ func getBlockData(i int) (map[string]interface{}, error) { func newDumbBlock() *block.Block { return &block.Block{ - BlockBase: block.BlockBase{ + Base: block.Base{ Version: 0, PrevHash: hash.Sha256([]byte("a")), MerkleRoot: hash.Sha256([]byte("b")), diff --git a/pkg/core/util.go b/pkg/core/util.go index 3269035de..4465a0621 100644 --- a/pkg/core/util.go +++ b/pkg/core/util.go @@ -25,7 +25,7 @@ func createGenesisBlock(cfg config.ProtocolConfiguration) (*block.Block, error) return nil, err } - base := block.BlockBase{ + base := block.Base{ Version: 0, PrevHash: util.Uint256{}, Timestamp: uint32(time.Date(2016, 7, 15, 15, 8, 21, 0, time.UTC).Unix()), @@ -50,7 +50,7 @@ func createGenesisBlock(cfg config.ProtocolConfiguration) (*block.Block, error) scriptOut := hash.Hash160(rawScript) b := &block.Block{ - BlockBase: base, + Base: base, Transactions: []*transaction.Transaction{ { Type: transaction.MinerType, diff --git a/pkg/network/blockqueue_test.go b/pkg/network/blockqueue_test.go index 881759d33..62a43595f 100644 --- a/pkg/network/blockqueue_test.go +++ b/pkg/network/blockqueue_test.go @@ -15,7 +15,7 @@ func TestBlockQueue(t *testing.T) { bq := newBlockQueue(0, chain, zaptest.NewLogger(t)) blocks := make([]*block.Block, 11) for i := 1; i < 11; i++ { - blocks[i] = &block.Block{BlockBase: block.BlockBase{Index: uint32(i)}} + blocks[i] = &block.Block{Base: block.Base{Index: uint32(i)}} } // not the ones expected currently for i := 3; i < 5; i++ { diff --git a/pkg/network/payload/headers_test.go b/pkg/network/payload/headers_test.go index f9fa957d0..008804eb0 100644 --- a/pkg/network/payload/headers_test.go +++ b/pkg/network/payload/headers_test.go @@ -30,7 +30,7 @@ func newTestHeaders(n int) *Headers { for i := range headers.Hdrs { headers.Hdrs[i] = &block.Header{ - BlockBase: block.BlockBase{ + Base: block.Base{ Index: uint32(i + 1), Script: transaction.Witness{ InvocationScript: []byte{0x0}, diff --git a/pkg/network/payload/merkleblock.go b/pkg/network/payload/merkleblock.go index 9759a853c..2aa31033e 100644 --- a/pkg/network/payload/merkleblock.go +++ b/pkg/network/payload/merkleblock.go @@ -8,7 +8,7 @@ import ( // MerkleBlock represents a merkle block packet payload. type MerkleBlock struct { - *block.BlockBase + *block.Base TxCount int Hashes []util.Uint256 Flags []byte @@ -16,8 +16,8 @@ type MerkleBlock struct { // DecodeBinary implements Serializable interface. func (m *MerkleBlock) DecodeBinary(br *io.BinReader) { - m.BlockBase = &block.BlockBase{} - m.BlockBase.DecodeBinary(br) + m.Base = &block.Base{} + m.Base.DecodeBinary(br) m.TxCount = int(br.ReadVarUint()) br.ReadArray(&m.Hashes) @@ -26,8 +26,8 @@ func (m *MerkleBlock) DecodeBinary(br *io.BinReader) { // EncodeBinary implements Serializable interface. func (m *MerkleBlock) EncodeBinary(bw *io.BinWriter) { - m.BlockBase = &block.BlockBase{} - m.BlockBase.EncodeBinary(bw) + m.Base = &block.Base{} + m.Base.EncodeBinary(bw) bw.WriteVarUint(uint64(m.TxCount)) bw.WriteArray(m.Hashes) diff --git a/pkg/rpc/wrappers/block.go b/pkg/rpc/wrappers/block.go index 77cf71bb0..2380f2f27 100644 --- a/pkg/rpc/wrappers/block.go +++ b/pkg/rpc/wrappers/block.go @@ -8,7 +8,7 @@ import ( type ( // Block wrapper used for the representation of - // block.Block / block.BlockBase on the RPC Server. + // block.Block / block.Base on the RPC Server. Block struct { *block.Block Confirmations uint32 `json:"confirmations"` diff --git a/pkg/rpc/wrappers/tx_raw_output.go b/pkg/rpc/wrappers/tx_raw_output.go index 53f3025c9..cdc2723cb 100644 --- a/pkg/rpc/wrappers/tx_raw_output.go +++ b/pkg/rpc/wrappers/tx_raw_output.go @@ -24,7 +24,7 @@ type TransactionOutputRaw struct { // NewTransactionOutputRaw returns a new ransactionOutputRaw object. func NewTransactionOutputRaw(tx *transaction.Transaction, header *block.Header, chain core.Blockchainer) TransactionOutputRaw { // confirmations formula - confirmations := int(chain.BlockHeight() - header.BlockBase.Index + 1) + confirmations := int(chain.BlockHeight() - header.Base.Index + 1) // set index position for i, o := range tx.Outputs { o.Position = i