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.
|
// Sign implements block.Block interface.
|
||||||
func (n *neoBlock) Sign(key crypto.PrivateKey) error {
|
func (n *neoBlock) Sign(key crypto.PrivateKey) error {
|
||||||
data := n.BlockBase.GetHashableData()
|
data := n.Base.GetHashableData()
|
||||||
sig, err := key.Sign(data[:])
|
sig, err := key.Sign(data[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -33,7 +33,7 @@ func (n *neoBlock) Sign(key crypto.PrivateKey) error {
|
||||||
|
|
||||||
// Verify implements block.Block interface.
|
// Verify implements block.Block interface.
|
||||||
func (n *neoBlock) Verify(key crypto.PublicKey, sign []byte) error {
|
func (n *neoBlock) Verify(key crypto.PublicKey, sign []byte) error {
|
||||||
data := n.BlockBase.GetHashableData()
|
data := n.Base.GetHashableData()
|
||||||
return key.Verify(data, sign)
|
return key.Verify(data, sign)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ import (
|
||||||
// Block represents one block in the chain.
|
// Block represents one block in the chain.
|
||||||
type Block struct {
|
type Block struct {
|
||||||
// The base of the block.
|
// The base of the block.
|
||||||
BlockBase
|
Base
|
||||||
|
|
||||||
// Transaction list.
|
// Transaction list.
|
||||||
Transactions []*transaction.Transaction `json:"tx"`
|
Transactions []*transaction.Transaction `json:"tx"`
|
||||||
|
@ -26,7 +26,7 @@ type Block struct {
|
||||||
// Header returns the Header of the Block.
|
// Header returns the Header of the Block.
|
||||||
func (b *Block) Header() *Header {
|
func (b *Block) Header() *Header {
|
||||||
return &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
|
// DecodeBinary decodes the block from the given BinReader, implementing
|
||||||
// Serializable interface.
|
// Serializable interface.
|
||||||
func (b *Block) DecodeBinary(br *io.BinReader) {
|
func (b *Block) DecodeBinary(br *io.BinReader) {
|
||||||
b.BlockBase.DecodeBinary(br)
|
b.Base.DecodeBinary(br)
|
||||||
br.ReadArray(&b.Transactions)
|
br.ReadArray(&b.Transactions)
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodeBinary encodes the block to the given BinWriter, implementing
|
// EncodeBinary encodes the block to the given BinWriter, implementing
|
||||||
// Serializable interface.
|
// Serializable interface.
|
||||||
func (b *Block) EncodeBinary(bw *io.BinWriter) {
|
func (b *Block) EncodeBinary(bw *io.BinWriter) {
|
||||||
b.BlockBase.EncodeBinary(bw)
|
b.Base.EncodeBinary(bw)
|
||||||
bw.WriteArray(b.Transactions)
|
bw.WriteArray(b.Transactions)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,8 +9,8 @@ import (
|
||||||
"github.com/CityOfZion/neo-go/pkg/util"
|
"github.com/CityOfZion/neo-go/pkg/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// BlockBase holds the base info of a block
|
// Base holds the base info of a block
|
||||||
type BlockBase struct {
|
type Base struct {
|
||||||
// Version of the block.
|
// Version of the block.
|
||||||
Version uint32 `json:"version"`
|
Version uint32 `json:"version"`
|
||||||
|
|
||||||
|
@ -47,14 +47,14 @@ type BlockBase struct {
|
||||||
verificationHash util.Uint256
|
verificationHash util.Uint256
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify verifies the integrity of the BlockBase.
|
// Verify verifies the integrity of the Base.
|
||||||
func (b *BlockBase) Verify() bool {
|
func (b *Base) Verify() bool {
|
||||||
// TODO: Need a persisted blockchain for this.
|
// TODO: Need a persisted blockchain for this.
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hash returns the hash of the block.
|
// 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{}) {
|
if b.hash.Equals(util.Uint256{}) {
|
||||||
b.createHash()
|
b.createHash()
|
||||||
}
|
}
|
||||||
|
@ -62,7 +62,7 @@ func (b *BlockBase) Hash() util.Uint256 {
|
||||||
}
|
}
|
||||||
|
|
||||||
// VerificationHash returns the hash of the block used to verify it.
|
// 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{}) {
|
if b.verificationHash.Equals(util.Uint256{}) {
|
||||||
b.createHash()
|
b.createHash()
|
||||||
}
|
}
|
||||||
|
@ -70,7 +70,7 @@ func (b *BlockBase) VerificationHash() util.Uint256 {
|
||||||
}
|
}
|
||||||
|
|
||||||
// DecodeBinary implements Serializable interface.
|
// DecodeBinary implements Serializable interface.
|
||||||
func (b *BlockBase) DecodeBinary(br *io.BinReader) {
|
func (b *Base) DecodeBinary(br *io.BinReader) {
|
||||||
b.decodeHashableFields(br)
|
b.decodeHashableFields(br)
|
||||||
|
|
||||||
padding := []byte{0}
|
padding := []byte{0}
|
||||||
|
@ -84,14 +84,14 @@ func (b *BlockBase) DecodeBinary(br *io.BinReader) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodeBinary implements Serializable interface
|
// EncodeBinary implements Serializable interface
|
||||||
func (b *BlockBase) EncodeBinary(bw *io.BinWriter) {
|
func (b *Base) EncodeBinary(bw *io.BinWriter) {
|
||||||
b.encodeHashableFields(bw)
|
b.encodeHashableFields(bw)
|
||||||
bw.WriteBytes([]byte{1})
|
bw.WriteBytes([]byte{1})
|
||||||
b.Script.EncodeBinary(bw)
|
b.Script.EncodeBinary(bw)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetHashableData returns serialized hashable data of the block.
|
// GetHashableData returns serialized hashable data of the block.
|
||||||
func (b *BlockBase) GetHashableData() []byte {
|
func (b *Base) GetHashableData() []byte {
|
||||||
buf := io.NewBufBinWriter()
|
buf := io.NewBufBinWriter()
|
||||||
// No error can occure while encoding hashable fields.
|
// No error can occure while encoding hashable fields.
|
||||||
b.encodeHashableFields(buf.BinWriter)
|
b.encodeHashableFields(buf.BinWriter)
|
||||||
|
@ -105,7 +105,7 @@ func (b *BlockBase) GetHashableData() []byte {
|
||||||
// version, PrevBlock, MerkleRoot, timestamp, and height, the nonce, NextMiner.
|
// version, PrevBlock, MerkleRoot, timestamp, and height, the nonce, NextMiner.
|
||||||
// Since MerkleRoot already contains the hash value of all transactions,
|
// Since MerkleRoot already contains the hash value of all transactions,
|
||||||
// the modification of transaction will influence the hash value of the block.
|
// the modification of transaction will influence the hash value of the block.
|
||||||
func (b *BlockBase) createHash() {
|
func (b *Base) createHash() {
|
||||||
bb := b.GetHashableData()
|
bb := b.GetHashableData()
|
||||||
b.verificationHash = hash.Sha256(bb)
|
b.verificationHash = hash.Sha256(bb)
|
||||||
b.hash = hash.Sha256(b.verificationHash.BytesBE())
|
b.hash = hash.Sha256(b.verificationHash.BytesBE())
|
||||||
|
@ -113,7 +113,7 @@ func (b *BlockBase) createHash() {
|
||||||
|
|
||||||
// encodeHashableFields will only encode the fields used for hashing.
|
// encodeHashableFields will only encode the fields used for hashing.
|
||||||
// see Hash() for more information about the fields.
|
// 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.WriteU32LE(b.Version)
|
||||||
bw.WriteBytes(b.PrevHash[:])
|
bw.WriteBytes(b.PrevHash[:])
|
||||||
bw.WriteBytes(b.MerkleRoot[:])
|
bw.WriteBytes(b.MerkleRoot[:])
|
||||||
|
@ -125,7 +125,7 @@ func (b *BlockBase) encodeHashableFields(bw *io.BinWriter) {
|
||||||
|
|
||||||
// decodeHashableFields decodes the fields used for hashing.
|
// decodeHashableFields decodes the fields used for hashing.
|
||||||
// see Hash() for more information about the fields.
|
// 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()
|
b.Version = br.ReadU32LE()
|
||||||
br.ReadBytes(b.PrevHash[:])
|
br.ReadBytes(b.PrevHash[:])
|
||||||
br.ReadBytes(b.MerkleRoot[:])
|
br.ReadBytes(b.MerkleRoot[:])
|
||||||
|
|
|
@ -79,7 +79,7 @@ func TestTrimmedBlock(t *testing.T) {
|
||||||
|
|
||||||
func newDumbBlock() *Block {
|
func newDumbBlock() *Block {
|
||||||
return &Block{
|
return &Block{
|
||||||
BlockBase: BlockBase{
|
Base: Base{
|
||||||
Version: 0,
|
Version: 0,
|
||||||
PrevHash: hash.Sha256([]byte("a")),
|
PrevHash: hash.Sha256([]byte("a")),
|
||||||
MerkleRoot: hash.Sha256([]byte("b")),
|
MerkleRoot: hash.Sha256([]byte("b")),
|
||||||
|
@ -291,9 +291,9 @@ func TestBlockSizeCalculation(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBlockCompare(t *testing.T) {
|
func TestBlockCompare(t *testing.T) {
|
||||||
b1 := Block{BlockBase: BlockBase{Index: 1}}
|
b1 := Block{Base: Base{Index: 1}}
|
||||||
b2 := Block{BlockBase: BlockBase{Index: 2}}
|
b2 := Block{Base: Base{Index: 2}}
|
||||||
b3 := Block{BlockBase: BlockBase{Index: 3}}
|
b3 := Block{Base: Base{Index: 3}}
|
||||||
assert.Equal(t, 1, b2.Compare(&b1))
|
assert.Equal(t, 1, b2.Compare(&b1))
|
||||||
assert.Equal(t, 0, b2.Compare(&b2))
|
assert.Equal(t, 0, b2.Compare(&b2))
|
||||||
assert.Equal(t, -1, b2.Compare(&b3))
|
assert.Equal(t, -1, b2.Compare(&b3))
|
||||||
|
|
|
@ -9,14 +9,14 @@ import (
|
||||||
// Header holds the head info of a block.
|
// Header holds the head info of a block.
|
||||||
type Header struct {
|
type Header struct {
|
||||||
// Base of the block.
|
// Base of the block.
|
||||||
BlockBase
|
Base
|
||||||
// Padding that is fixed to 0.
|
// Padding that is fixed to 0.
|
||||||
_ uint8
|
_ uint8
|
||||||
}
|
}
|
||||||
|
|
||||||
// DecodeBinary implements Serializable interface.
|
// DecodeBinary implements Serializable interface.
|
||||||
func (h *Header) DecodeBinary(r *io.BinReader) {
|
func (h *Header) DecodeBinary(r *io.BinReader) {
|
||||||
h.BlockBase.DecodeBinary(r)
|
h.Base.DecodeBinary(r)
|
||||||
|
|
||||||
padding := []byte{0}
|
padding := []byte{0}
|
||||||
r.ReadBytes(padding)
|
r.ReadBytes(padding)
|
||||||
|
@ -28,6 +28,6 @@ func (h *Header) DecodeBinary(r *io.BinReader) {
|
||||||
|
|
||||||
// EncodeBinary implements Serializable interface.
|
// EncodeBinary implements Serializable interface.
|
||||||
func (h *Header) EncodeBinary(w *io.BinWriter) {
|
func (h *Header) EncodeBinary(w *io.BinWriter) {
|
||||||
h.BlockBase.EncodeBinary(w)
|
h.Base.EncodeBinary(w)
|
||||||
w.WriteBytes([]byte{0})
|
w.WriteBytes([]byte{0})
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHeaderEncodeDecode(t *testing.T) {
|
func TestHeaderEncodeDecode(t *testing.T) {
|
||||||
header := Header{BlockBase: BlockBase{
|
header := Header{Base: Base{
|
||||||
Version: 0,
|
Version: 0,
|
||||||
PrevHash: hash.Sha256([]byte("prevhash")),
|
PrevHash: hash.Sha256([]byte("prevhash")),
|
||||||
MerkleRoot: hash.Sha256([]byte("merkleroot")),
|
MerkleRoot: hash.Sha256([]byte("merkleroot")),
|
||||||
|
|
|
@ -262,7 +262,7 @@ func TestGetBlock_NotExists(t *testing.T) {
|
||||||
func TestPutGetBlock(t *testing.T) {
|
func TestPutGetBlock(t *testing.T) {
|
||||||
dao := newDao(storage.NewMemoryStore())
|
dao := newDao(storage.NewMemoryStore())
|
||||||
b := &block.Block{
|
b := &block.Block{
|
||||||
BlockBase: block.BlockBase{
|
Base: block.Base{
|
||||||
Script: transaction.Witness{
|
Script: transaction.Witness{
|
||||||
VerificationScript: []byte{byte(opcode.PUSH1)},
|
VerificationScript: []byte{byte(opcode.PUSH1)},
|
||||||
InvocationScript: []byte{byte(opcode.NOP)},
|
InvocationScript: []byte{byte(opcode.NOP)},
|
||||||
|
@ -303,7 +303,7 @@ func TestGetCurrentHeaderHeight_NoHeader(t *testing.T) {
|
||||||
func TestGetCurrentHeaderHeight_Store(t *testing.T) {
|
func TestGetCurrentHeaderHeight_Store(t *testing.T) {
|
||||||
dao := newDao(storage.NewMemoryStore())
|
dao := newDao(storage.NewMemoryStore())
|
||||||
b := &block.Block{
|
b := &block.Block{
|
||||||
BlockBase: block.BlockBase{
|
Base: block.Base{
|
||||||
Script: transaction.Witness{
|
Script: transaction.Witness{
|
||||||
VerificationScript: []byte{byte(opcode.PUSH1)},
|
VerificationScript: []byte{byte(opcode.PUSH1)},
|
||||||
InvocationScript: []byte{byte(opcode.NOP)},
|
InvocationScript: []byte{byte(opcode.NOP)},
|
||||||
|
|
|
@ -62,7 +62,7 @@ func newBlock(index uint32, txs ...*transaction.Transaction) *block.Block {
|
||||||
VerificationScript: valScript,
|
VerificationScript: valScript,
|
||||||
}
|
}
|
||||||
b := &block.Block{
|
b := &block.Block{
|
||||||
BlockBase: block.BlockBase{
|
Base: block.Base{
|
||||||
Version: 0,
|
Version: 0,
|
||||||
PrevHash: newBlockPrevHash,
|
PrevHash: newBlockPrevHash,
|
||||||
Timestamp: uint32(time.Now().UTC().Unix()) + index,
|
Timestamp: uint32(time.Now().UTC().Unix()) + index,
|
||||||
|
@ -144,7 +144,7 @@ func getBlockData(i int) (map[string]interface{}, error) {
|
||||||
|
|
||||||
func newDumbBlock() *block.Block {
|
func newDumbBlock() *block.Block {
|
||||||
return &block.Block{
|
return &block.Block{
|
||||||
BlockBase: block.BlockBase{
|
Base: block.Base{
|
||||||
Version: 0,
|
Version: 0,
|
||||||
PrevHash: hash.Sha256([]byte("a")),
|
PrevHash: hash.Sha256([]byte("a")),
|
||||||
MerkleRoot: hash.Sha256([]byte("b")),
|
MerkleRoot: hash.Sha256([]byte("b")),
|
||||||
|
|
|
@ -25,7 +25,7 @@ func createGenesisBlock(cfg config.ProtocolConfiguration) (*block.Block, error)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
base := block.BlockBase{
|
base := block.Base{
|
||||||
Version: 0,
|
Version: 0,
|
||||||
PrevHash: util.Uint256{},
|
PrevHash: util.Uint256{},
|
||||||
Timestamp: uint32(time.Date(2016, 7, 15, 15, 8, 21, 0, time.UTC).Unix()),
|
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)
|
scriptOut := hash.Hash160(rawScript)
|
||||||
|
|
||||||
b := &block.Block{
|
b := &block.Block{
|
||||||
BlockBase: base,
|
Base: base,
|
||||||
Transactions: []*transaction.Transaction{
|
Transactions: []*transaction.Transaction{
|
||||||
{
|
{
|
||||||
Type: transaction.MinerType,
|
Type: transaction.MinerType,
|
||||||
|
|
|
@ -15,7 +15,7 @@ func TestBlockQueue(t *testing.T) {
|
||||||
bq := newBlockQueue(0, chain, zaptest.NewLogger(t))
|
bq := newBlockQueue(0, chain, zaptest.NewLogger(t))
|
||||||
blocks := make([]*block.Block, 11)
|
blocks := make([]*block.Block, 11)
|
||||||
for i := 1; i < 11; i++ {
|
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
|
// not the ones expected currently
|
||||||
for i := 3; i < 5; i++ {
|
for i := 3; i < 5; i++ {
|
||||||
|
|
|
@ -30,7 +30,7 @@ func newTestHeaders(n int) *Headers {
|
||||||
|
|
||||||
for i := range headers.Hdrs {
|
for i := range headers.Hdrs {
|
||||||
headers.Hdrs[i] = &block.Header{
|
headers.Hdrs[i] = &block.Header{
|
||||||
BlockBase: block.BlockBase{
|
Base: block.Base{
|
||||||
Index: uint32(i + 1),
|
Index: uint32(i + 1),
|
||||||
Script: transaction.Witness{
|
Script: transaction.Witness{
|
||||||
InvocationScript: []byte{0x0},
|
InvocationScript: []byte{0x0},
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
|
|
||||||
// MerkleBlock represents a merkle block packet payload.
|
// MerkleBlock represents a merkle block packet payload.
|
||||||
type MerkleBlock struct {
|
type MerkleBlock struct {
|
||||||
*block.BlockBase
|
*block.Base
|
||||||
TxCount int
|
TxCount int
|
||||||
Hashes []util.Uint256
|
Hashes []util.Uint256
|
||||||
Flags []byte
|
Flags []byte
|
||||||
|
@ -16,8 +16,8 @@ type MerkleBlock struct {
|
||||||
|
|
||||||
// DecodeBinary implements Serializable interface.
|
// DecodeBinary implements Serializable interface.
|
||||||
func (m *MerkleBlock) DecodeBinary(br *io.BinReader) {
|
func (m *MerkleBlock) DecodeBinary(br *io.BinReader) {
|
||||||
m.BlockBase = &block.BlockBase{}
|
m.Base = &block.Base{}
|
||||||
m.BlockBase.DecodeBinary(br)
|
m.Base.DecodeBinary(br)
|
||||||
|
|
||||||
m.TxCount = int(br.ReadVarUint())
|
m.TxCount = int(br.ReadVarUint())
|
||||||
br.ReadArray(&m.Hashes)
|
br.ReadArray(&m.Hashes)
|
||||||
|
@ -26,8 +26,8 @@ func (m *MerkleBlock) DecodeBinary(br *io.BinReader) {
|
||||||
|
|
||||||
// EncodeBinary implements Serializable interface.
|
// EncodeBinary implements Serializable interface.
|
||||||
func (m *MerkleBlock) EncodeBinary(bw *io.BinWriter) {
|
func (m *MerkleBlock) EncodeBinary(bw *io.BinWriter) {
|
||||||
m.BlockBase = &block.BlockBase{}
|
m.Base = &block.Base{}
|
||||||
m.BlockBase.EncodeBinary(bw)
|
m.Base.EncodeBinary(bw)
|
||||||
|
|
||||||
bw.WriteVarUint(uint64(m.TxCount))
|
bw.WriteVarUint(uint64(m.TxCount))
|
||||||
bw.WriteArray(m.Hashes)
|
bw.WriteArray(m.Hashes)
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
|
|
||||||
type (
|
type (
|
||||||
// Block wrapper used for the representation of
|
// 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 struct {
|
||||||
*block.Block
|
*block.Block
|
||||||
Confirmations uint32 `json:"confirmations"`
|
Confirmations uint32 `json:"confirmations"`
|
||||||
|
|
|
@ -24,7 +24,7 @@ type TransactionOutputRaw struct {
|
||||||
// NewTransactionOutputRaw returns a new ransactionOutputRaw object.
|
// NewTransactionOutputRaw returns a new ransactionOutputRaw object.
|
||||||
func NewTransactionOutputRaw(tx *transaction.Transaction, header *block.Header, chain core.Blockchainer) TransactionOutputRaw {
|
func NewTransactionOutputRaw(tx *transaction.Transaction, header *block.Header, chain core.Blockchainer) TransactionOutputRaw {
|
||||||
// confirmations formula
|
// confirmations formula
|
||||||
confirmations := int(chain.BlockHeight() - header.BlockBase.Index + 1)
|
confirmations := int(chain.BlockHeight() - header.Base.Index + 1)
|
||||||
// set index position
|
// set index position
|
||||||
for i, o := range tx.Outputs {
|
for i, o := range tx.Outputs {
|
||||||
o.Position = i
|
o.Position = i
|
||||||
|
|
Loading…
Reference in a new issue