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"
|
2020-10-02 14:25:55 +00:00
|
|
|
"math"
|
2022-06-08 15:12:41 +00:00
|
|
|
"math/big"
|
2019-10-11 08:40:54 +00:00
|
|
|
|
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"
|
2022-06-08 15:12:41 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2018-01-30 10:56:36 +00:00
|
|
|
)
|
|
|
|
|
2020-10-02 14:25:55 +00:00
|
|
|
const (
|
|
|
|
// MaxTransactionsPerBlock is the maximum number of transactions per block.
|
2021-03-01 13:49:24 +00:00
|
|
|
MaxTransactionsPerBlock = math.MaxUint16
|
2020-10-02 14:25:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ErrMaxContentsPerBlock is returned when the maximum number of contents per block is reached.
|
|
|
|
var ErrMaxContentsPerBlock = errors.New("the number of contents exceeds the maximum number of contents per block")
|
|
|
|
|
2021-03-15 10:00:04 +00:00
|
|
|
var expectedHeaderSizeWithEmptyWitness int
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
expectedHeaderSizeWithEmptyWitness = io.GetVarSize(new(Header))
|
|
|
|
}
|
|
|
|
|
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.
|
2021-03-01 13:44:47 +00:00
|
|
|
Header
|
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
|
|
|
|
}
|
|
|
|
|
2020-06-18 09:00:51 +00:00
|
|
|
// auxBlockOut is used for JSON i/o.
|
|
|
|
type auxBlockOut struct {
|
2021-03-01 12:20:27 +00:00
|
|
|
Transactions []*transaction.Transaction `json:"tx"`
|
2018-01-30 10:56:36 +00:00
|
|
|
}
|
2018-01-31 08:27:08 +00:00
|
|
|
|
2020-06-18 09:00:51 +00:00
|
|
|
// auxBlockIn is used for JSON i/o.
|
|
|
|
type auxBlockIn struct {
|
2021-03-01 12:20:27 +00:00
|
|
|
Transactions []json.RawMessage `json:"tx"`
|
2020-06-18 09:00:51 +00:00
|
|
|
}
|
|
|
|
|
2020-09-16 09:33:39 +00:00
|
|
|
// ComputeMerkleRoot computes Merkle tree root hash based on actual block's data.
|
|
|
|
func (b *Block) ComputeMerkleRoot() util.Uint256 {
|
2021-03-01 12:20:27 +00:00
|
|
|
hashes := make([]util.Uint256, len(b.Transactions))
|
2020-04-22 05:57:55 +00:00
|
|
|
for i, tx := range b.Transactions {
|
2021-03-01 12:20:27 +00:00
|
|
|
hashes[i] = tx.Hash()
|
2018-03-25 10:45:54 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 15:38:15 +00:00
|
|
|
return hash.CalcMerkleRoot(hashes)
|
2019-10-15 09:52:10 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 12:32:07 +00:00
|
|
|
// RebuildMerkleRoot rebuilds the merkleroot of the block.
|
2020-09-15 15:38:15 +00:00
|
|
|
func (b *Block) RebuildMerkleRoot() {
|
2020-09-16 09:33:39 +00:00
|
|
|
b.MerkleRoot = b.ComputeMerkleRoot()
|
2018-02-01 20:28:45 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 20:05:28 +00:00
|
|
|
// NewTrimmedFromReader returns a new block from trimmed data.
|
2018-03-17 11:53:21 +00:00
|
|
|
// This is commonly used to create a block from stored data.
|
|
|
|
// Blocks created from trimmed data will have their Trimmed field
|
|
|
|
// set to true.
|
2021-12-07 20:05:28 +00:00
|
|
|
func NewTrimmedFromReader(stateRootEnabled bool, br *io.BinReader) (*Block, error) {
|
2018-03-17 11:53:21 +00:00
|
|
|
block := &Block{
|
2021-03-01 13:44:47 +00:00
|
|
|
Header: Header{
|
2020-11-17 12:57:50 +00:00
|
|
|
StateRootEnabled: stateRootEnabled,
|
2020-06-18 09:00:51 +00:00
|
|
|
},
|
2018-03-17 11:53:21 +00:00
|
|
|
Trimmed: true,
|
|
|
|
}
|
|
|
|
|
2021-03-01 13:44:47 +00:00
|
|
|
block.Header.DecodeBinary(br)
|
2020-04-22 05:57:55 +00:00
|
|
|
lenHashes := br.ReadVarUint()
|
2021-03-01 13:49:24 +00:00
|
|
|
if lenHashes > MaxTransactionsPerBlock {
|
2020-10-02 14:25:55 +00:00
|
|
|
return nil, ErrMaxContentsPerBlock
|
|
|
|
}
|
2020-04-22 05:57:55 +00:00
|
|
|
if lenHashes > 0 {
|
2021-03-01 12:20:27 +00:00
|
|
|
block.Transactions = make([]*transaction.Transaction, lenHashes)
|
|
|
|
for i := 0; i < int(lenHashes); i++ {
|
2020-04-22 05:57:55 +00:00
|
|
|
var hash util.Uint256
|
|
|
|
hash.DecodeBinary(br)
|
|
|
|
block.Transactions[i] = transaction.NewTrimmedTX(hash)
|
|
|
|
}
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
|
2019-08-28 16:27:06 +00:00
|
|
|
return block, br.Err
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 18:46:52 +00:00
|
|
|
// New creates a new blank block with proper state root setting.
|
|
|
|
func New(stateRootEnabled bool) *Block {
|
2020-06-18 09:00:51 +00:00
|
|
|
return &Block{
|
2021-03-01 13:44:47 +00:00
|
|
|
Header: Header{
|
2020-11-17 12:57:50 +00:00
|
|
|
StateRootEnabled: stateRootEnabled,
|
2020-06-18 09:00:51 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-18 07:49:25 +00:00
|
|
|
// EncodeTrimmed writes trimmed representation of the block data into w. Trimmed blocks
|
|
|
|
// do not store complete transactions, instead they only store their hashes.
|
|
|
|
func (b *Block) EncodeTrimmed(w *io.BinWriter) {
|
|
|
|
b.Header.EncodeBinary(w)
|
|
|
|
|
|
|
|
w.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()
|
2022-03-18 07:49:25 +00:00
|
|
|
h.EncodeBinary(w)
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
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) {
|
2021-03-01 13:44:47 +00:00
|
|
|
b.Header.DecodeBinary(br)
|
2020-04-22 05:57:55 +00:00
|
|
|
contentsCount := br.ReadVarUint()
|
2021-03-01 13:49:24 +00:00
|
|
|
if contentsCount > MaxTransactionsPerBlock {
|
2020-10-02 14:25:55 +00:00
|
|
|
br.Err = ErrMaxContentsPerBlock
|
|
|
|
return
|
|
|
|
}
|
2021-03-01 12:20:27 +00:00
|
|
|
txes := make([]*transaction.Transaction, contentsCount)
|
|
|
|
for i := 0; i < int(contentsCount); i++ {
|
2021-03-25 16:18:01 +00:00
|
|
|
tx := &transaction.Transaction{}
|
2020-04-22 05:57:55 +00:00
|
|
|
tx.DecodeBinary(br)
|
|
|
|
txes[i] = tx
|
|
|
|
}
|
|
|
|
b.Transactions = txes
|
2020-06-05 12:24:23 +00:00
|
|
|
if br.Err != nil {
|
|
|
|
return
|
|
|
|
}
|
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) {
|
2021-03-01 13:44:47 +00:00
|
|
|
b.Header.EncodeBinary(bw)
|
2021-03-01 12:20:27 +00:00
|
|
|
bw.WriteVarUint(uint64(len(b.Transactions)))
|
2020-04-22 05:57:55 +00:00
|
|
|
for i := 0; i < len(b.Transactions); i++ {
|
|
|
|
b.Transactions[i].EncodeBinary(bw)
|
|
|
|
}
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
2019-09-25 16:54:31 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
2020-05-13 18:27:08 +00:00
|
|
|
func (b Block) MarshalJSON() ([]byte, error) {
|
2022-07-21 10:15:31 +00:00
|
|
|
abo := auxBlockOut{
|
2021-03-01 12:20:27 +00:00
|
|
|
Transactions: b.Transactions,
|
2022-07-21 10:15:31 +00:00
|
|
|
}
|
|
|
|
// `"tx": []` (C#) vs `"tx": null` (default Go when missing any transactions)
|
|
|
|
if abo.Transactions == nil {
|
|
|
|
abo.Transactions = []*transaction.Transaction{}
|
|
|
|
}
|
|
|
|
auxb, err := json.Marshal(abo)
|
2020-05-13 18:27:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-01 13:44:47 +00:00
|
|
|
baseBytes, err := json.Marshal(b.Header)
|
2020-05-13 18:27:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stitch them together.
|
|
|
|
if baseBytes[len(baseBytes)-1] != '}' || auxb[0] != '{' {
|
|
|
|
return nil, errors.New("can't merge internal jsons")
|
|
|
|
}
|
|
|
|
baseBytes[len(baseBytes)-1] = ','
|
|
|
|
baseBytes = append(baseBytes, auxb[1:]...)
|
|
|
|
return baseBytes, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
2020-05-13 18:27:08 +00:00
|
|
|
func (b *Block) UnmarshalJSON(data []byte) error {
|
|
|
|
// As Base and auxb are at the same level in json,
|
|
|
|
// do unmarshalling separately for both structs.
|
2020-06-18 09:00:51 +00:00
|
|
|
auxb := new(auxBlockIn)
|
2020-05-13 18:27:08 +00:00
|
|
|
err := json.Unmarshal(data, auxb)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-01 13:44:47 +00:00
|
|
|
err = json.Unmarshal(data, &b.Header)
|
2020-05-13 18:27:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-18 09:00:51 +00:00
|
|
|
if len(auxb.Transactions) != 0 {
|
|
|
|
b.Transactions = make([]*transaction.Transaction, 0, len(auxb.Transactions))
|
|
|
|
for _, txBytes := range auxb.Transactions {
|
2021-03-25 16:18:01 +00:00
|
|
|
tx := &transaction.Transaction{}
|
2020-06-18 09:00:51 +00:00
|
|
|
err = tx.UnmarshalJSON(txBytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.Transactions = append(b.Transactions, tx)
|
|
|
|
}
|
|
|
|
}
|
2020-05-13 18:27:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-03-15 10:00:04 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetExpectedBlockSize returns the expected block size which should be equal to io.GetVarSize(b).
|
2021-03-15 10:00:04 +00:00
|
|
|
func (b *Block) GetExpectedBlockSize() int {
|
|
|
|
var transactionsSize int
|
|
|
|
for _, tx := range b.Transactions {
|
|
|
|
transactionsSize += tx.Size()
|
|
|
|
}
|
|
|
|
return b.GetExpectedBlockSizeWithoutTransactions(len(b.Transactions)) + transactionsSize
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetExpectedBlockSizeWithoutTransactions returns the expected block size without transactions size.
|
2021-03-15 10:00:04 +00:00
|
|
|
func (b *Block) GetExpectedBlockSizeWithoutTransactions(txCount int) int {
|
|
|
|
size := expectedHeaderSizeWithEmptyWitness - 1 - 1 + // 1 is for the zero-length (new(Header)).Script.Invocation/Verification
|
|
|
|
io.GetVarSize(&b.Script) +
|
|
|
|
io.GetVarSize(txCount)
|
|
|
|
if b.StateRootEnabled {
|
|
|
|
size += util.Uint256Size
|
|
|
|
}
|
|
|
|
return size
|
|
|
|
}
|
2022-06-08 15:12:41 +00:00
|
|
|
|
|
|
|
// ToStackItem converts Block to stackitem.Item.
|
|
|
|
func (b *Block) ToStackItem() stackitem.Item {
|
2022-07-06 08:52:06 +00:00
|
|
|
items := []stackitem.Item{
|
2022-06-08 15:12:41 +00:00
|
|
|
stackitem.NewByteArray(b.Hash().BytesBE()),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(int64(b.Version))),
|
|
|
|
stackitem.NewByteArray(b.PrevHash.BytesBE()),
|
|
|
|
stackitem.NewByteArray(b.MerkleRoot.BytesBE()),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(int64(b.Timestamp))),
|
|
|
|
stackitem.NewBigInteger(new(big.Int).SetUint64(b.Nonce)),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(int64(b.Index))),
|
|
|
|
stackitem.NewByteArray(b.NextConsensus.BytesBE()),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(int64(len(b.Transactions)))),
|
2022-07-06 08:52:06 +00:00
|
|
|
}
|
|
|
|
if b.StateRootEnabled {
|
|
|
|
items = append(items, stackitem.NewByteArray(b.PrevStateRoot.BytesBE()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return stackitem.NewArray(items)
|
2022-06-08 15:12:41 +00:00
|
|
|
}
|