mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-23 13:41:37 +00:00
block: rename BlockBase to Base
This commit is contained in:
parent
63c56cca5c
commit
489b88afbb
14 changed files with 41 additions and 41 deletions
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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[:])
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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})
|
||||
}
|
||||
|
|
|
@ -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")),
|
||||
|
|
|
@ -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)},
|
||||
|
|
|
@ -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")),
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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++ {
|
||||
|
|
|
@ -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},
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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"`
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue