2018-03-17 11:53:21 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
2019-08-23 15:50:45 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
2018-03-17 11:53:21 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// BlockBase holds the base info of a block
|
|
|
|
type BlockBase struct {
|
|
|
|
// 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
|
2018-03-23 20:36:59 +00:00
|
|
|
Script *transaction.Witness `json:"script"`
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
// hash of this block, created when binary encoded.
|
|
|
|
hash util.Uint256
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify verifies the integrity of the BlockBase.
|
|
|
|
func (b *BlockBase) Verify() bool {
|
|
|
|
// TODO: Need a persisted blockchain for this.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hash return the hash of the block.
|
|
|
|
func (b *BlockBase) 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements the payload interface.
|
|
|
|
func (b *BlockBase) DecodeBinary(r io.Reader) error {
|
|
|
|
if err := b.decodeHashableFields(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var padding uint8
|
2019-08-28 16:27:06 +00:00
|
|
|
br := util.BinReader{R: r}
|
|
|
|
br.ReadLE(&padding)
|
|
|
|
if br.Err != nil {
|
|
|
|
return br.Err
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
if padding != 1 {
|
|
|
|
return fmt.Errorf("format error: padding must equal 1 got %d", padding)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.Script = &transaction.Witness{}
|
|
|
|
return b.Script.DecodeBinary(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements the Payload interface
|
|
|
|
func (b *BlockBase) EncodeBinary(w io.Writer) error {
|
|
|
|
if err := b.encodeHashableFields(w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-28 16:27:06 +00:00
|
|
|
bw := util.BinWriter{W: w}
|
|
|
|
bw.WriteLE(uint8(1))
|
|
|
|
if bw.Err != nil {
|
|
|
|
return bw.Err
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
return b.Script.EncodeBinary(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2018-03-25 10:45:54 +00:00
|
|
|
func (b *BlockBase) createHash() error {
|
2018-03-17 11:53:21 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2018-03-25 10:45:54 +00:00
|
|
|
if err := b.encodeHashableFields(buf); err != nil {
|
|
|
|
return err
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
2019-08-23 15:50:45 +00:00
|
|
|
b.hash = hash.DoubleSha256(buf.Bytes())
|
2018-03-25 10:45:54 +00:00
|
|
|
|
|
|
|
return nil
|
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.
|
|
|
|
func (b *BlockBase) encodeHashableFields(w io.Writer) error {
|
2019-08-28 16:27:06 +00:00
|
|
|
bw := util.BinWriter{W: w}
|
|
|
|
bw.WriteLE(b.Version)
|
|
|
|
bw.WriteLE(b.PrevHash)
|
|
|
|
bw.WriteLE(b.MerkleRoot)
|
|
|
|
bw.WriteLE(b.Timestamp)
|
|
|
|
bw.WriteLE(b.Index)
|
|
|
|
bw.WriteLE(b.ConsensusData)
|
|
|
|
bw.WriteLE(b.NextConsensus)
|
|
|
|
return bw.Err
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// decodeHashableFields will only decode the fields used for hashing.
|
|
|
|
// see Hash() for more information about the fields.
|
|
|
|
func (b *BlockBase) decodeHashableFields(r io.Reader) error {
|
2019-08-28 16:27:06 +00:00
|
|
|
br := util.BinReader{R: r}
|
|
|
|
br.ReadLE(&b.Version)
|
|
|
|
br.ReadLE(&b.PrevHash)
|
|
|
|
br.ReadLE(&b.MerkleRoot)
|
|
|
|
br.ReadLE(&b.Timestamp)
|
|
|
|
br.ReadLE(&b.Index)
|
|
|
|
br.ReadLE(&b.ConsensusData)
|
|
|
|
br.ReadLE(&b.NextConsensus)
|
|
|
|
|
|
|
|
if br.Err != nil {
|
|
|
|
return br.Err
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make the hash of the block here so we dont need to do this
|
|
|
|
// again.
|
2018-03-25 10:45:54 +00:00
|
|
|
return b.createHash()
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|