2020-01-14 12:32:07 +00:00
|
|
|
package block
|
2018-01-30 10:56:36 +00:00
|
|
|
|
|
|
|
import (
|
2020-05-13 18:27:08 +00:00
|
|
|
"encoding/json"
|
2019-10-11 08:40:54 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2019-09-25 16:54:31 +00:00
|
|
|
"github.com/Workiva/go-datastructures/queue"
|
2020-03-03 14:21:42 +00: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 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.
|
2020-01-15 08:29:50 +00:00
|
|
|
Base
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
// Transaction list.
|
2020-05-13 18:27:08 +00:00
|
|
|
Transactions []*transaction.Transaction
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
// True if this block is created from trimmed data.
|
2020-05-13 18:27:08 +00:00
|
|
|
Trimmed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// auxTxes is used for JSON i/o.
|
|
|
|
type auxTxes struct {
|
|
|
|
Transactions []*transaction.Transaction `json:"tx"`
|
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{
|
2020-01-15 08:29:50 +00:00
|
|
|
Base: b.Base,
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-01 20:28:45 +00:00
|
|
|
|
2019-12-25 08:28:59 +00:00
|
|
|
func merkleTreeFromTransactions(txes []*transaction.Transaction) (*hash.MerkleTree, error) {
|
2019-10-15 09:52:10 +00:00
|
|
|
hashes := make([]util.Uint256, len(txes))
|
|
|
|
for i, tx := range txes {
|
2018-03-25 10:45:54 +00:00
|
|
|
hashes[i] = tx.Hash()
|
|
|
|
}
|
|
|
|
|
2019-12-25 08:28:59 +00:00
|
|
|
return hash.NewMerkleTree(hashes)
|
2019-10-15 09:52:10 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 12:32:07 +00:00
|
|
|
// RebuildMerkleRoot rebuilds the merkleroot of the block.
|
|
|
|
func (b *Block) RebuildMerkleRoot() error {
|
2019-10-15 09:52:10 +00:00
|
|
|
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
|
|
|
|
2019-12-12 17:17:50 +00:00
|
|
|
_ = br.ReadB()
|
2018-03-17 11:53:21 +00:00
|
|
|
|
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-12-12 15:52:23 +00:00
|
|
|
hash.DecodeBinary(br)
|
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-12 17:17:50 +00:00
|
|
|
buf.WriteB(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-09 15:25:15 +00:00
|
|
|
h := tx.Hash()
|
|
|
|
h.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) {
|
2020-01-15 08:29:50 +00:00
|
|
|
b.Base.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) {
|
2020-01-15 08:29:50 +00:00
|
|
|
b.Base.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
|
|
|
|
}
|
|
|
|
}
|
2020-05-13 18:27:08 +00:00
|
|
|
|
|
|
|
// MarshalJSON implements json.Marshaler interface.
|
|
|
|
func (b Block) MarshalJSON() ([]byte, error) {
|
|
|
|
txes, err := json.Marshal(auxTxes{b.Transactions})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
baseBytes, err := json.Marshal(b.Base)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stitch them together.
|
|
|
|
if baseBytes[len(baseBytes)-1] != '}' || txes[0] != '{' {
|
|
|
|
return nil, errors.New("can't merge internal jsons")
|
|
|
|
}
|
|
|
|
baseBytes[len(baseBytes)-1] = ','
|
|
|
|
baseBytes = append(baseBytes, txes[1:]...)
|
|
|
|
return baseBytes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler interface.
|
|
|
|
func (b *Block) UnmarshalJSON(data []byte) error {
|
|
|
|
// As Base and txes are at the same level in json,
|
|
|
|
// do unmarshalling separately for both structs.
|
|
|
|
txes := new(auxTxes)
|
|
|
|
err := json.Unmarshal(data, txes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
base := new(Base)
|
|
|
|
err = json.Unmarshal(data, base)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.Base = *base
|
|
|
|
b.Transactions = txes.Transactions
|
|
|
|
return nil
|
|
|
|
}
|