2018-01-30 10:56:36 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2019-10-11 08:40:54 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2018-03-04 13:56:49 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
2018-03-25 10:45:54 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto"
|
2019-09-16 09:18:13 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2018-02-04 19:54:51 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
2019-09-25 16:54:31 +00:00
|
|
|
"github.com/Workiva/go-datastructures/queue"
|
2018-01-30 10:56:36 +00:00
|
|
|
)
|
|
|
|
|
2018-02-01 20:28:45 +00:00
|
|
|
// Block represents one block in the chain.
|
|
|
|
type Block struct {
|
2018-03-17 11:53:21 +00:00
|
|
|
// The base of the block.
|
2018-02-01 20:28:45 +00:00
|
|
|
BlockBase
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
// Transaction list.
|
2018-03-23 20:36:59 +00:00
|
|
|
Transactions []*transaction.Transaction `json:"tx"`
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
// True if this block is created from trimmed data.
|
2018-03-23 20:36:59 +00:00
|
|
|
Trimmed bool `json:"-"`
|
2018-01-30 10:56:36 +00:00
|
|
|
}
|
2018-01-31 08:27:08 +00:00
|
|
|
|
2018-03-17 11:53:21 +00:00
|
|
|
// Header returns the Header of the Block.
|
2018-02-04 19:54:51 +00:00
|
|
|
func (b *Block) Header() *Header {
|
|
|
|
return &Header{
|
|
|
|
BlockBase: b.BlockBase,
|
|
|
|
}
|
|
|
|
}
|
2018-02-01 20:28:45 +00:00
|
|
|
|
2019-10-15 09:52:10 +00:00
|
|
|
func merkleTreeFromTransactions(txes []*transaction.Transaction) (*crypto.MerkleTree, error) {
|
|
|
|
hashes := make([]util.Uint256, len(txes))
|
|
|
|
for i, tx := range txes {
|
2018-03-25 10:45:54 +00:00
|
|
|
hashes[i] = tx.Hash()
|
|
|
|
}
|
|
|
|
|
2019-10-15 09:52:10 +00:00
|
|
|
return crypto.NewMerkleTree(hashes)
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// rebuildMerkleRoot rebuilds the merkleroot of the block.
|
2019-10-15 09:52:10 +00:00
|
|
|
func (b *Block) rebuildMerkleRoot() error {
|
|
|
|
merkle, err := merkleTreeFromTransactions(b.Transactions)
|
2018-03-25 10:45:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b.MerkleRoot = merkle.Root()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Verify verifies the integrity of the block.
|
2019-10-15 09:52:10 +00:00
|
|
|
func (b *Block) Verify() error {
|
2019-09-30 13:55:02 +00:00
|
|
|
// There has to be some transaction inside.
|
|
|
|
if len(b.Transactions) == 0 {
|
2019-10-11 08:40:54 +00:00
|
|
|
return errors.New("no transactions")
|
2019-09-30 13:55:02 +00:00
|
|
|
}
|
2018-02-04 19:54:51 +00:00
|
|
|
// The first TX has to be a miner transaction.
|
2018-03-04 13:56:49 +00:00
|
|
|
if b.Transactions[0].Type != transaction.MinerType {
|
2019-10-11 08:40:54 +00:00
|
|
|
return fmt.Errorf("the first transaction is %s", b.Transactions[0].Type)
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
|
|
|
// If the first TX is a minerTX then all others cant.
|
|
|
|
for _, tx := range b.Transactions[1:] {
|
2018-03-04 13:56:49 +00:00
|
|
|
if tx.Type == transaction.MinerType {
|
2019-11-27 09:23:18 +00:00
|
|
|
return fmt.Errorf("miner transaction %s is not the first one", tx.Hash().StringLE())
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-15 09:52:10 +00:00
|
|
|
merkle, err := merkleTreeFromTransactions(b.Transactions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !b.MerkleRoot.Equals(merkle.Root()) {
|
|
|
|
return errors.New("MerkleRoot mismatch")
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
2019-10-11 08:40:54 +00:00
|
|
|
return nil
|
2018-02-01 20:28:45 +00:00
|
|
|
}
|
|
|
|
|
2018-03-17 11:53:21 +00:00
|
|
|
// NewBlockFromTrimmedBytes returns a new block from trimmed data.
|
|
|
|
// This is commonly used to create a block from stored data.
|
|
|
|
// Blocks created from trimmed data will have their Trimmed field
|
|
|
|
// set to true.
|
|
|
|
func NewBlockFromTrimmedBytes(b []byte) (*Block, error) {
|
|
|
|
block := &Block{
|
|
|
|
Trimmed: true,
|
|
|
|
}
|
|
|
|
|
2019-09-16 09:18:13 +00:00
|
|
|
br := io.NewBinReaderFromBuf(b)
|
2019-09-16 16:31:49 +00:00
|
|
|
block.decodeHashableFields(br)
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
var padding uint8
|
2019-08-28 16:27:06 +00:00
|
|
|
br.ReadLE(&padding)
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
block.Script = &transaction.Witness{}
|
2019-09-16 16:31:49 +00:00
|
|
|
block.Script.DecodeBinary(br)
|
2018-03-17 11:53:21 +00:00
|
|
|
|
2019-08-28 16:27:06 +00:00
|
|
|
lenTX := br.ReadVarUint()
|
2018-03-17 11:53:21 +00:00
|
|
|
block.Transactions = make([]*transaction.Transaction, lenTX)
|
|
|
|
for i := 0; i < int(lenTX); i++ {
|
|
|
|
var hash util.Uint256
|
2019-08-28 16:27:06 +00:00
|
|
|
br.ReadLE(&hash)
|
2018-03-17 11:53:21 +00:00
|
|
|
block.Transactions[i] = transaction.NewTrimmedTX(hash)
|
|
|
|
}
|
|
|
|
|
2019-08-28 16:27:06 +00:00
|
|
|
return block, br.Err
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Trim returns a subset of the block data to save up space
|
|
|
|
// in storage.
|
|
|
|
// Notice that only the hashes of the transactions are stored.
|
|
|
|
func (b *Block) Trim() ([]byte, error) {
|
2019-09-16 09:18:13 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
2019-09-16 16:31:49 +00:00
|
|
|
b.encodeHashableFields(buf.BinWriter)
|
2019-12-06 15:22:21 +00:00
|
|
|
buf.WriteBytes([]byte{1})
|
2019-09-16 16:31:49 +00:00
|
|
|
b.Script.EncodeBinary(buf.BinWriter)
|
2018-03-17 11:53:21 +00:00
|
|
|
|
2019-09-16 09:18:13 +00:00
|
|
|
buf.WriteVarUint(uint64(len(b.Transactions)))
|
2018-03-17 11:53:21 +00:00
|
|
|
for _, tx := range b.Transactions {
|
2019-12-06 15:22:21 +00:00
|
|
|
tx.Hash().EncodeBinary(buf.BinWriter)
|
2019-08-28 16:27:06 +00:00
|
|
|
}
|
2019-09-16 09:18:13 +00:00
|
|
|
if buf.Err != nil {
|
|
|
|
return nil, buf.Err
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
2018-01-31 08:27:08 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 16:31:49 +00:00
|
|
|
// DecodeBinary decodes the block from the given BinReader, implementing
|
|
|
|
// Serializable interface.
|
|
|
|
func (b *Block) DecodeBinary(br *io.BinReader) {
|
|
|
|
b.BlockBase.DecodeBinary(br)
|
2019-11-14 07:50:03 +00:00
|
|
|
br.ReadArray(&b.Transactions)
|
2018-02-01 20:28:45 +00:00
|
|
|
}
|
2018-03-17 11:53:21 +00:00
|
|
|
|
2019-09-16 16:31:49 +00:00
|
|
|
// EncodeBinary encodes the block to the given BinWriter, implementing
|
|
|
|
// Serializable interface.
|
|
|
|
func (b *Block) EncodeBinary(bw *io.BinWriter) {
|
|
|
|
b.BlockBase.EncodeBinary(bw)
|
2019-11-14 08:07:23 +00:00
|
|
|
bw.WriteArray(b.Transactions)
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
2019-09-25 16:54:31 +00:00
|
|
|
|
|
|
|
// Compare implements the queue Item interface.
|
|
|
|
func (b *Block) Compare(item queue.Item) int {
|
|
|
|
other := item.(*Block)
|
|
|
|
switch {
|
|
|
|
case b.Index > other.Index:
|
|
|
|
return 1
|
|
|
|
case b.Index == other.Index:
|
|
|
|
return 0
|
|
|
|
default:
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
}
|