2020-01-14 12:32:07 +00:00
|
|
|
package block
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
import (
|
2020-05-13 18:27:08 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2018-03-17 11:53:21 +00:00
|
|
|
|
2020-06-18 09:00:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
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"
|
2020-05-13 18:27:08 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
2020-03-03 14:21:42 +00:00
|
|
|
"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.
|
2020-05-13 18:27:08 +00:00
|
|
|
Version uint32
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
// hash of the previous block.
|
2020-05-13 18:27:08 +00:00
|
|
|
PrevHash util.Uint256
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
// Root hash of a transaction list.
|
2020-05-13 18:27:08 +00:00
|
|
|
MerkleRoot util.Uint256
|
2018-03-17 11:53:21 +00:00
|
|
|
|
2020-04-24 09:49:17 +00:00
|
|
|
// Timestamp is a millisecond-precision timestamp.
|
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.
|
2020-05-13 18:27:08 +00:00
|
|
|
Timestamp uint64
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
// index/height of the block
|
2020-05-13 18:27:08 +00:00
|
|
|
Index uint32
|
2018-03-17 11:53:21 +00:00
|
|
|
|
2019-02-13 18:01:10 +00:00
|
|
|
// Contract address of the next miner
|
2020-05-13 18:27:08 +00:00
|
|
|
NextConsensus util.Uint160
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
// Script used to validate the block
|
2020-05-13 18:27:08 +00:00
|
|
|
Script transaction.Witness
|
2018-03-17 11:53:21 +00:00
|
|
|
|
2020-06-18 09:00:51 +00:00
|
|
|
// Network magic number this block belongs to. This one actually is not
|
|
|
|
// a part of the wire-representation of Block, but it's absolutely
|
|
|
|
// necessary for correct signing/verification.
|
|
|
|
Network netmode.Magic
|
|
|
|
|
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-05-13 18:27:08 +00:00
|
|
|
// baseAux is used to marshal/unmarshal to/from JSON, it's almost the same
|
|
|
|
// as original Base, but with Nonce and NextConsensus fields differing and
|
|
|
|
// Hash added.
|
|
|
|
type baseAux struct {
|
|
|
|
Hash util.Uint256 `json:"hash"`
|
|
|
|
Version uint32 `json:"version"`
|
|
|
|
PrevHash util.Uint256 `json:"previousblockhash"`
|
|
|
|
MerkleRoot util.Uint256 `json:"merkleroot"`
|
|
|
|
Timestamp uint64 `json:"time"`
|
|
|
|
Index uint32 `json:"index"`
|
|
|
|
NextConsensus string `json:"nextconsensus"`
|
|
|
|
Witnesses []transaction.Witness `json:"witnesses"`
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-08-29 15:44:45 +00:00
|
|
|
// GetSignedHash returns a hash of the block used to verify it.
|
|
|
|
func (b *Base) GetSignedHash() 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)
|
2020-06-18 07:40:23 +00:00
|
|
|
witnessCount := br.ReadVarUint()
|
|
|
|
if br.Err == nil && witnessCount != 1 {
|
|
|
|
br.Err = errors.New("wrong witness count")
|
2019-09-16 16:31:49 +00:00
|
|
|
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)
|
2020-06-18 07:40:23 +00:00
|
|
|
bw.WriteVarUint(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()
|
2020-06-18 09:00:51 +00:00
|
|
|
buf.WriteU32LE(uint32(b.Network))
|
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[:])
|
2020-04-21 11:26:57 +00:00
|
|
|
bw.WriteU64LE(b.Timestamp)
|
2019-12-12 15:52:23 +00:00
|
|
|
bw.WriteU32LE(b.Index)
|
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[:])
|
2020-04-21 11:26:57 +00:00
|
|
|
b.Timestamp = br.ReadU64LE()
|
2019-12-12 15:52:23 +00:00
|
|
|
b.Index = br.ReadU32LE()
|
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
|
|
|
}
|
2020-05-13 18:27:08 +00:00
|
|
|
|
|
|
|
// MarshalJSON implements json.Marshaler interface.
|
|
|
|
func (b Base) MarshalJSON() ([]byte, error) {
|
|
|
|
aux := baseAux{
|
|
|
|
Hash: b.Hash(),
|
|
|
|
Version: b.Version,
|
|
|
|
PrevHash: b.PrevHash,
|
|
|
|
MerkleRoot: b.MerkleRoot,
|
|
|
|
Timestamp: b.Timestamp,
|
|
|
|
Index: b.Index,
|
|
|
|
NextConsensus: address.Uint160ToString(b.NextConsensus),
|
|
|
|
Witnesses: []transaction.Witness{b.Script},
|
|
|
|
}
|
|
|
|
return json.Marshal(aux)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler interface.
|
|
|
|
func (b *Base) UnmarshalJSON(data []byte) error {
|
|
|
|
var aux = new(baseAux)
|
|
|
|
var nextC util.Uint160
|
|
|
|
|
|
|
|
err := json.Unmarshal(data, aux)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
nextC, err = address.StringToUint160(aux.NextConsensus)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(aux.Witnesses) != 1 {
|
|
|
|
return errors.New("wrong number of witnesses")
|
|
|
|
}
|
|
|
|
b.Version = aux.Version
|
|
|
|
b.PrevHash = aux.PrevHash
|
|
|
|
b.MerkleRoot = aux.MerkleRoot
|
|
|
|
b.Timestamp = aux.Timestamp
|
|
|
|
b.Index = aux.Index
|
|
|
|
b.NextConsensus = nextC
|
|
|
|
b.Script = aux.Witnesses[0]
|
|
|
|
if !aux.Hash.Equals(b.Hash()) {
|
|
|
|
return errors.New("json 'hash' doesn't match block hash")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|