2018-01-30 10:56:36 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
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"
|
2018-03-17 11:53:21 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
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
|
|
|
|
2018-03-25 10:45:54 +00:00
|
|
|
// rebuildMerkleRoot rebuild the merkleroot of the block.
|
|
|
|
func (b *Block) rebuildMerkleRoot() error {
|
|
|
|
hashes := make([]util.Uint256, len(b.Transactions))
|
|
|
|
for i, tx := range b.Transactions {
|
|
|
|
hashes[i] = tx.Hash()
|
|
|
|
}
|
|
|
|
|
|
|
|
merkle, err := crypto.NewMerkleTree(hashes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b.MerkleRoot = merkle.Root()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-04 19:54:51 +00:00
|
|
|
// Verify the integrity of the block.
|
|
|
|
func (b *Block) Verify(full bool) bool {
|
|
|
|
// The first TX has to be a miner transaction.
|
2018-03-04 13:56:49 +00:00
|
|
|
if b.Transactions[0].Type != transaction.MinerType {
|
2018-02-04 19:54:51 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
// 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 {
|
2018-02-04 19:54:51 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: When full is true, do a full verification.
|
|
|
|
if full {
|
2018-03-17 11:53:21 +00:00
|
|
|
log.Warn("full verification of blocks is not yet implemented")
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
|
|
|
return true
|
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-09-16 09:18:13 +00:00
|
|
|
buf.WriteLE(uint8(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-09-16 09:18:13 +00:00
|
|
|
buf.WriteLE(tx.Hash())
|
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)
|
2018-01-31 10:47:54 +00:00
|
|
|
|
2019-08-28 16:27:06 +00:00
|
|
|
lentx := br.ReadVarUint()
|
2018-03-04 13:56:49 +00:00
|
|
|
b.Transactions = make([]*transaction.Transaction, lentx)
|
2018-01-31 10:47:54 +00:00
|
|
|
for i := 0; i < int(lentx); i++ {
|
2018-03-09 15:55:25 +00:00
|
|
|
b.Transactions[i] = &transaction.Transaction{}
|
2019-09-16 16:31:49 +00:00
|
|
|
b.Transactions[i].DecodeBinary(br)
|
2018-01-31 10:47:54 +00:00
|
|
|
}
|
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-08-30 07:42:07 +00:00
|
|
|
bw.WriteVarUint(uint64(len(b.Transactions)))
|
|
|
|
for _, tx := range b.Transactions {
|
2019-09-16 16:31:49 +00:00
|
|
|
tx.EncodeBinary(bw)
|
2019-08-30 07:42:07 +00:00
|
|
|
}
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|