2020-01-14 12:32:07 +00:00
|
|
|
package block
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
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-03-17 11:53:21 +00:00
|
|
|
)
|
|
|
|
|
2020-01-15 08:29:50 +00:00
|
|
|
// Base holds the base info of a block
|
|
|
|
type Base struct {
|
2018-03-17 11:53:21 +00:00
|
|
|
// Version of the block.
|
2018-03-23 20:36:59 +00:00
|
|
|
Version uint32 `json:"version"`
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
// hash of the previous block.
|
2018-03-23 20:36:59 +00:00
|
|
|
PrevHash util.Uint256 `json:"previousblockhash"`
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
// Root hash of a transaction list.
|
2018-03-23 20:36:59 +00:00
|
|
|
MerkleRoot util.Uint256 `json:"merkleroot"`
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
// The time stamp of each block must be later than previous block's time stamp.
|
|
|
|
// Generally the difference of two block's time stamp is about 15 seconds and imprecision is allowed.
|
|
|
|
// The height of the block must be exactly equal to the height of the previous block plus 1.
|
2018-03-23 20:36:59 +00:00
|
|
|
Timestamp uint32 `json:"time"`
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
// index/height of the block
|
2018-03-23 20:36:59 +00:00
|
|
|
Index uint32 `json:"height"`
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
// Random number also called nonce
|
2018-03-23 20:36:59 +00:00
|
|
|
ConsensusData uint64 `json:"nonce"`
|
2018-03-17 11:53:21 +00:00
|
|
|
|
2019-02-13 18:01:10 +00:00
|
|
|
// Contract address of the next miner
|
2018-03-30 06:15:03 +00:00
|
|
|
NextConsensus util.Uint160 `json:"next_consensus"`
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
// Padding that is fixed to 1
|
|
|
|
_ uint8
|
|
|
|
|
|
|
|
// Script used to validate the block
|
2019-12-09 14:14:10 +00:00
|
|
|
Script transaction.Witness `json:"script"`
|
2018-03-17 11:53:21 +00:00
|
|
|
|
2019-10-15 09:52:10 +00:00
|
|
|
// Hash of this block, created when binary encoded (double SHA256).
|
2018-03-17 11:53:21 +00:00
|
|
|
hash util.Uint256
|
2019-10-15 09:52:10 +00:00
|
|
|
|
|
|
|
// Hash of the block used to verify it (single SHA256).
|
|
|
|
verificationHash util.Uint256
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
|
2020-01-15 08:29:50 +00:00
|
|
|
// Verify verifies the integrity of the Base.
|
|
|
|
func (b *Base) Verify() bool {
|
2018-03-17 11:53:21 +00:00
|
|
|
// TODO: Need a persisted blockchain for this.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Hash returns the hash of the block.
|
2020-01-15 08:29:50 +00:00
|
|
|
func (b *Base) Hash() util.Uint256 {
|
2018-03-25 10:45:54 +00:00
|
|
|
if b.hash.Equals(util.Uint256{}) {
|
|
|
|
b.createHash()
|
|
|
|
}
|
2018-03-17 11:53:21 +00:00
|
|
|
return b.hash
|
|
|
|
}
|
|
|
|
|
2019-10-15 09:52:10 +00:00
|
|
|
// VerificationHash returns the hash of the block used to verify it.
|
2020-01-15 08:29:50 +00:00
|
|
|
func (b *Base) VerificationHash() util.Uint256 {
|
2019-10-15 09:52:10 +00:00
|
|
|
if b.verificationHash.Equals(util.Uint256{}) {
|
2019-11-26 12:07:59 +00:00
|
|
|
b.createHash()
|
2019-10-15 09:52:10 +00:00
|
|
|
}
|
|
|
|
return b.verificationHash
|
|
|
|
}
|
|
|
|
|
2019-09-16 16:31:49 +00:00
|
|
|
// DecodeBinary implements Serializable interface.
|
2020-01-15 08:29:50 +00:00
|
|
|
func (b *Base) DecodeBinary(br *io.BinReader) {
|
2019-09-16 16:31:49 +00:00
|
|
|
b.decodeHashableFields(br)
|
2018-03-17 11:53:21 +00:00
|
|
|
|
2019-12-06 15:37:46 +00:00
|
|
|
padding := []byte{0}
|
|
|
|
br.ReadBytes(padding)
|
|
|
|
if padding[0] != 1 {
|
2019-09-16 16:31:49 +00:00
|
|
|
br.Err = fmt.Errorf("format error: padding must equal 1 got %d", padding)
|
|
|
|
return
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 16:31:49 +00:00
|
|
|
b.Script.DecodeBinary(br)
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 16:31:49 +00:00
|
|
|
// EncodeBinary implements Serializable interface
|
2020-01-15 08:29:50 +00:00
|
|
|
func (b *Base) EncodeBinary(bw *io.BinWriter) {
|
2019-09-16 16:31:49 +00:00
|
|
|
b.encodeHashableFields(bw)
|
2019-12-06 15:22:21 +00:00
|
|
|
bw.WriteBytes([]byte{1})
|
2019-09-16 16:31:49 +00:00
|
|
|
b.Script.EncodeBinary(bw)
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 13:31:04 +00:00
|
|
|
// GetSignedPart returns serialized hashable data of the block.
|
|
|
|
func (b *Base) GetSignedPart() []byte {
|
2019-10-15 09:52:10 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
2019-11-26 12:07:59 +00:00
|
|
|
// No error can occure while encoding hashable fields.
|
2019-10-15 09:52:10 +00:00
|
|
|
b.encodeHashableFields(buf.BinWriter)
|
2019-11-26 12:07:59 +00:00
|
|
|
|
|
|
|
return buf.Bytes()
|
2019-10-15 09:52:10 +00:00
|
|
|
}
|
|
|
|
|
2018-03-17 11:53:21 +00:00
|
|
|
// createHash creates the hash of the block.
|
|
|
|
// When calculating the hash value of the block, instead of calculating the entire block,
|
|
|
|
// only first seven fields in the block head will be calculated, which are
|
|
|
|
// version, PrevBlock, MerkleRoot, timestamp, and height, the nonce, NextMiner.
|
|
|
|
// Since MerkleRoot already contains the hash value of all transactions,
|
|
|
|
// the modification of transaction will influence the hash value of the block.
|
2020-01-15 08:29:50 +00:00
|
|
|
func (b *Base) createHash() {
|
2020-04-13 13:31:04 +00:00
|
|
|
bb := b.GetSignedPart()
|
2019-10-15 09:52:10 +00:00
|
|
|
b.verificationHash = hash.Sha256(bb)
|
2019-12-12 15:01:30 +00:00
|
|
|
b.hash = hash.Sha256(b.verificationHash.BytesBE())
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// encodeHashableFields will only encode the fields used for hashing.
|
|
|
|
// see Hash() for more information about the fields.
|
2020-01-15 08:29:50 +00:00
|
|
|
func (b *Base) encodeHashableFields(bw *io.BinWriter) {
|
2019-12-12 15:52:23 +00:00
|
|
|
bw.WriteU32LE(b.Version)
|
2019-12-06 15:22:21 +00:00
|
|
|
bw.WriteBytes(b.PrevHash[:])
|
|
|
|
bw.WriteBytes(b.MerkleRoot[:])
|
2019-12-12 15:52:23 +00:00
|
|
|
bw.WriteU32LE(b.Timestamp)
|
|
|
|
bw.WriteU32LE(b.Index)
|
|
|
|
bw.WriteU64LE(b.ConsensusData)
|
2019-12-06 15:22:21 +00:00
|
|
|
bw.WriteBytes(b.NextConsensus[:])
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// decodeHashableFields decodes the fields used for hashing.
|
2018-03-17 11:53:21 +00:00
|
|
|
// see Hash() for more information about the fields.
|
2020-01-15 08:29:50 +00:00
|
|
|
func (b *Base) decodeHashableFields(br *io.BinReader) {
|
2019-12-12 15:52:23 +00:00
|
|
|
b.Version = br.ReadU32LE()
|
2019-12-06 15:37:46 +00:00
|
|
|
br.ReadBytes(b.PrevHash[:])
|
|
|
|
br.ReadBytes(b.MerkleRoot[:])
|
2019-12-12 15:52:23 +00:00
|
|
|
b.Timestamp = br.ReadU32LE()
|
|
|
|
b.Index = br.ReadU32LE()
|
|
|
|
b.ConsensusData = br.ReadU64LE()
|
2019-12-06 15:37:46 +00:00
|
|
|
br.ReadBytes(b.NextConsensus[:])
|
2019-08-28 16:27:06 +00:00
|
|
|
|
2018-03-17 11:53:21 +00:00
|
|
|
// Make the hash of the block here so we dont need to do this
|
|
|
|
// again.
|
2019-09-16 16:31:49 +00:00
|
|
|
if br.Err == nil {
|
2019-11-26 12:07:59 +00:00
|
|
|
b.createHash()
|
2019-09-16 16:31:49 +00:00
|
|
|
}
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|