2020-01-14 15:32:07 +03:00
|
|
|
package block
|
2018-01-30 11:56:36 +01:00
|
|
|
|
|
|
|
import (
|
2019-10-11 11:40:54 +03:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2019-09-25 19:54:31 +03:00
|
|
|
"github.com/Workiva/go-datastructures/queue"
|
2020-03-03 17:21:42 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2018-01-30 11:56:36 +01:00
|
|
|
)
|
|
|
|
|
2018-02-01 21:28:45 +01:00
|
|
|
// Block represents one block in the chain.
|
|
|
|
type Block struct {
|
2018-03-17 12:53:21 +01:00
|
|
|
// The base of the block.
|
2020-01-15 11:29:50 +03:00
|
|
|
Base
|
2018-03-17 12:53:21 +01:00
|
|
|
|
|
|
|
// Transaction list.
|
2018-03-23 20:36:59 +00:00
|
|
|
Transactions []*transaction.Transaction `json:"tx"`
|
2018-03-17 12:53:21 +01:00
|
|
|
|
|
|
|
// True if this block is created from trimmed data.
|
2018-03-23 20:36:59 +00:00
|
|
|
Trimmed bool `json:"-"`
|
2018-01-30 11:56:36 +01:00
|
|
|
}
|
2018-01-31 09:27:08 +01:00
|
|
|
|
2018-03-17 12:53:21 +01:00
|
|
|
// Header returns the Header of the Block.
|
2018-02-04 20:54:51 +01:00
|
|
|
func (b *Block) Header() *Header {
|
|
|
|
return &Header{
|
2020-01-15 11:29:50 +03:00
|
|
|
Base: b.Base,
|
2018-02-04 20:54:51 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-01 21:28:45 +01:00
|
|
|
|
2019-12-25 11:28:59 +03:00
|
|
|
func merkleTreeFromTransactions(txes []*transaction.Transaction) (*hash.MerkleTree, error) {
|
2019-10-15 12:52:10 +03:00
|
|
|
hashes := make([]util.Uint256, len(txes))
|
|
|
|
for i, tx := range txes {
|
2018-03-25 12:45:54 +02:00
|
|
|
hashes[i] = tx.Hash()
|
|
|
|
}
|
|
|
|
|
2019-12-25 11:28:59 +03:00
|
|
|
return hash.NewMerkleTree(hashes)
|
2019-10-15 12:52:10 +03:00
|
|
|
}
|
|
|
|
|
2020-01-14 15:32:07 +03:00
|
|
|
// RebuildMerkleRoot rebuilds the merkleroot of the block.
|
|
|
|
func (b *Block) RebuildMerkleRoot() error {
|
2019-10-15 12:52:10 +03:00
|
|
|
merkle, err := merkleTreeFromTransactions(b.Transactions)
|
2018-03-25 12:45:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b.MerkleRoot = merkle.Root()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-22 17:56:03 +03:00
|
|
|
// Verify verifies the integrity of the block.
|
2019-10-15 12:52:10 +03:00
|
|
|
func (b *Block) Verify() error {
|
2019-09-30 16:55:02 +03:00
|
|
|
// There has to be some transaction inside.
|
|
|
|
if len(b.Transactions) == 0 {
|
2019-10-11 11:40:54 +03:00
|
|
|
return errors.New("no transactions")
|
2019-09-30 16:55:02 +03:00
|
|
|
}
|
2018-02-04 20:54:51 +01:00
|
|
|
// The first TX has to be a miner transaction.
|
2018-03-04 14:56:49 +01:00
|
|
|
if b.Transactions[0].Type != transaction.MinerType {
|
2019-10-11 11:40:54 +03:00
|
|
|
return fmt.Errorf("the first transaction is %s", b.Transactions[0].Type)
|
2018-02-04 20:54:51 +01:00
|
|
|
}
|
|
|
|
// If the first TX is a minerTX then all others cant.
|
|
|
|
for _, tx := range b.Transactions[1:] {
|
2018-03-04 14:56:49 +01:00
|
|
|
if tx.Type == transaction.MinerType {
|
2019-11-27 12:23:18 +03:00
|
|
|
return fmt.Errorf("miner transaction %s is not the first one", tx.Hash().StringLE())
|
2018-02-04 20:54:51 +01:00
|
|
|
}
|
|
|
|
}
|
2019-10-15 12:52:10 +03: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 20:54:51 +01:00
|
|
|
}
|
2019-10-11 11:40:54 +03:00
|
|
|
return nil
|
2018-02-01 21:28:45 +01:00
|
|
|
}
|
|
|
|
|
2018-03-17 12:53:21 +01: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 12:18:13 +03:00
|
|
|
br := io.NewBinReaderFromBuf(b)
|
2019-09-16 19:31:49 +03:00
|
|
|
block.decodeHashableFields(br)
|
2018-03-17 12:53:21 +01:00
|
|
|
|
2019-12-12 20:17:50 +03:00
|
|
|
_ = br.ReadB()
|
2018-03-17 12:53:21 +01:00
|
|
|
|
2019-09-16 19:31:49 +03:00
|
|
|
block.Script.DecodeBinary(br)
|
2018-03-17 12:53:21 +01:00
|
|
|
|
2019-08-28 19:27:06 +03:00
|
|
|
lenTX := br.ReadVarUint()
|
2018-03-17 12:53:21 +01:00
|
|
|
block.Transactions = make([]*transaction.Transaction, lenTX)
|
|
|
|
for i := 0; i < int(lenTX); i++ {
|
|
|
|
var hash util.Uint256
|
2019-12-12 18:52:23 +03:00
|
|
|
hash.DecodeBinary(br)
|
2018-03-17 12:53:21 +01:00
|
|
|
block.Transactions[i] = transaction.NewTrimmedTX(hash)
|
|
|
|
}
|
|
|
|
|
2019-08-28 19:27:06 +03:00
|
|
|
return block, br.Err
|
2018-03-17 12:53:21 +01: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 12:18:13 +03:00
|
|
|
buf := io.NewBufBinWriter()
|
2019-09-16 19:31:49 +03:00
|
|
|
b.encodeHashableFields(buf.BinWriter)
|
2019-12-12 20:17:50 +03:00
|
|
|
buf.WriteB(1)
|
2019-09-16 19:31:49 +03:00
|
|
|
b.Script.EncodeBinary(buf.BinWriter)
|
2018-03-17 12:53:21 +01:00
|
|
|
|
2019-09-16 12:18:13 +03:00
|
|
|
buf.WriteVarUint(uint64(len(b.Transactions)))
|
2018-03-17 12:53:21 +01:00
|
|
|
for _, tx := range b.Transactions {
|
2019-12-09 18:25:15 +03:00
|
|
|
h := tx.Hash()
|
|
|
|
h.EncodeBinary(buf.BinWriter)
|
2019-08-28 19:27:06 +03:00
|
|
|
}
|
2019-09-16 12:18:13 +03:00
|
|
|
if buf.Err != nil {
|
|
|
|
return nil, buf.Err
|
2018-03-17 12:53:21 +01:00
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
2018-01-31 09:27:08 +01:00
|
|
|
}
|
|
|
|
|
2019-09-16 19:31:49 +03:00
|
|
|
// DecodeBinary decodes the block from the given BinReader, implementing
|
|
|
|
// Serializable interface.
|
|
|
|
func (b *Block) DecodeBinary(br *io.BinReader) {
|
2020-01-15 11:29:50 +03:00
|
|
|
b.Base.DecodeBinary(br)
|
2019-11-14 10:50:03 +03:00
|
|
|
br.ReadArray(&b.Transactions)
|
2018-02-01 21:28:45 +01:00
|
|
|
}
|
2018-03-17 12:53:21 +01:00
|
|
|
|
2019-09-16 19:31:49 +03:00
|
|
|
// EncodeBinary encodes the block to the given BinWriter, implementing
|
|
|
|
// Serializable interface.
|
|
|
|
func (b *Block) EncodeBinary(bw *io.BinWriter) {
|
2020-01-15 11:29:50 +03:00
|
|
|
b.Base.EncodeBinary(bw)
|
2019-11-14 11:07:23 +03:00
|
|
|
bw.WriteArray(b.Transactions)
|
2018-03-17 12:53:21 +01:00
|
|
|
}
|
2019-09-25 19:54:31 +03: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
|
|
|
|
}
|
|
|
|
}
|