2018-01-30 10:56:36 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2018-02-01 20:28:45 +00:00
|
|
|
"bytes"
|
|
|
|
"crypto/sha256"
|
2018-01-31 08:27:08 +00:00
|
|
|
"encoding/binary"
|
2018-02-04 19:54:51 +00:00
|
|
|
"fmt"
|
2018-01-31 08:27:08 +00:00
|
|
|
"io"
|
2018-02-04 19:54:51 +00:00
|
|
|
"log"
|
2018-01-31 08:27:08 +00:00
|
|
|
|
2018-03-04 13:56:49 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
2018-02-04 19:54:51 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
2018-01-30 10:56:36 +00:00
|
|
|
)
|
|
|
|
|
2018-02-01 20:28:45 +00:00
|
|
|
// BlockBase holds the base info of a block
|
|
|
|
type BlockBase struct {
|
2018-01-30 10:56:36 +00:00
|
|
|
Version uint32
|
|
|
|
// hash of the previous block.
|
2018-02-04 19:54:51 +00:00
|
|
|
PrevHash util.Uint256
|
2018-01-30 10:56:36 +00:00
|
|
|
// Root hash of a transaction list.
|
2018-02-04 19:54:51 +00:00
|
|
|
MerkleRoot util.Uint256
|
2018-02-01 20:28:45 +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-01-30 10:56:36 +00:00
|
|
|
Timestamp uint32
|
2018-02-04 19:54:51 +00:00
|
|
|
// index/height of the block
|
|
|
|
Index uint32
|
|
|
|
// Random number also called nonce
|
|
|
|
ConsensusData uint64
|
2018-03-04 13:56:49 +00:00
|
|
|
// Contract addresss of the next miner
|
2018-02-04 19:54:51 +00:00
|
|
|
NextConsensus util.Uint160
|
2018-02-01 20:28:45 +00:00
|
|
|
// fixed to 1
|
2018-02-04 19:54:51 +00:00
|
|
|
_ uint8 // padding
|
2018-01-30 10:56:36 +00:00
|
|
|
// Script used to validate the block
|
2018-03-04 13:56:49 +00:00
|
|
|
Script *transaction.Witness
|
2018-03-09 15:55:25 +00:00
|
|
|
|
|
|
|
// hash of this block, created when binary encoded.
|
|
|
|
hash util.Uint256
|
2018-02-01 20:28:45 +00:00
|
|
|
}
|
|
|
|
|
2018-02-04 19:54:51 +00:00
|
|
|
// DecodeBinary implements the payload interface.
|
|
|
|
func (b *BlockBase) DecodeBinary(r io.Reader) error {
|
2018-02-07 14:16:50 +00:00
|
|
|
if err := binary.Read(r, binary.LittleEndian, &b.Version); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Read(r, binary.LittleEndian, &b.PrevHash); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Read(r, binary.LittleEndian, &b.MerkleRoot); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Read(r, binary.LittleEndian, &b.Timestamp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Read(r, binary.LittleEndian, &b.Index); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Read(r, binary.LittleEndian, &b.ConsensusData); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Read(r, binary.LittleEndian, &b.NextConsensus); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-04 19:54:51 +00:00
|
|
|
|
|
|
|
var padding uint8
|
2018-02-07 14:16:50 +00:00
|
|
|
if err := binary.Read(r, binary.LittleEndian, &padding); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-04 19:54:51 +00:00
|
|
|
if padding != 1 {
|
|
|
|
return fmt.Errorf("format error: padding must equal 1 got %d", padding)
|
|
|
|
}
|
|
|
|
|
2018-03-04 13:56:49 +00:00
|
|
|
b.Script = &transaction.Witness{}
|
2018-03-09 15:55:25 +00:00
|
|
|
if err := b.Script.DecodeBinary(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the hash of the block here so we dont need to do this
|
|
|
|
// again.
|
|
|
|
hash, err := b.createHash()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.hash = hash
|
|
|
|
return nil
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 15:55:25 +00:00
|
|
|
// Hash return the hash of the block.
|
|
|
|
func (b *BlockBase) Hash() util.Uint256 {
|
|
|
|
return b.hash
|
|
|
|
}
|
|
|
|
|
|
|
|
// createHash creates the hash of the block.
|
2018-02-04 19:54:51 +00:00
|
|
|
// 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-09 15:55:25 +00:00
|
|
|
func (b *BlockBase) createHash() (hash util.Uint256, err error) {
|
2018-02-04 19:54:51 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err = b.encodeHashableFields(buf); err != nil {
|
2018-03-09 15:55:25 +00:00
|
|
|
return hash, err
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Double hash the encoded fields.
|
|
|
|
hash = sha256.Sum256(buf.Bytes())
|
2018-03-02 15:24:09 +00:00
|
|
|
hash = sha256.Sum256(hash.Bytes())
|
2018-02-04 19:54:51 +00:00
|
|
|
return hash, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2018-03-09 15:55:25 +00:00
|
|
|
if err := binary.Write(w, binary.LittleEndian, &b.Version); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Write(w, binary.LittleEndian, &b.PrevHash); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Write(w, binary.LittleEndian, &b.MerkleRoot); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Write(w, binary.LittleEndian, &b.Timestamp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Write(w, binary.LittleEndian, &b.Index); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Write(w, binary.LittleEndian, &b.ConsensusData); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return binary.Write(w, binary.LittleEndian, &b.NextConsensus)
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 14:16:50 +00:00
|
|
|
// EncodeBinary implements the Payload interface
|
|
|
|
func (b *BlockBase) EncodeBinary(w io.Writer) error {
|
|
|
|
if err := b.encodeHashableFields(w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Write(w, binary.LittleEndian, uint8(1)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return b.Script.EncodeBinary(w)
|
|
|
|
}
|
|
|
|
|
2018-02-04 19:54:51 +00:00
|
|
|
// Header holds the head info of a block
|
|
|
|
type Header struct {
|
2018-02-01 20:28:45 +00:00
|
|
|
BlockBase
|
2018-03-09 15:55:25 +00:00
|
|
|
_ uint8 // padding fixed to 0
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 06:43:32 +00:00
|
|
|
// Verify the integrity of the header
|
|
|
|
func (h *Header) Verify() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-02-04 19:54:51 +00:00
|
|
|
// DecodeBinary impelements the Payload interface.
|
|
|
|
func (h *Header) DecodeBinary(r io.Reader) error {
|
|
|
|
if err := h.BlockBase.DecodeBinary(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var padding uint8
|
|
|
|
binary.Read(r, binary.LittleEndian, &padding)
|
|
|
|
if padding != 0 {
|
|
|
|
return fmt.Errorf("format error: padding must equal 0 got %d", padding)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary impelements the Payload interface.
|
|
|
|
func (h *Header) EncodeBinary(w io.Writer) error {
|
2018-02-07 14:16:50 +00:00
|
|
|
if err := h.BlockBase.EncodeBinary(w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return binary.Write(w, binary.LittleEndian, uint8(0))
|
2018-02-01 20:28:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Block represents one block in the chain.
|
|
|
|
type Block struct {
|
|
|
|
BlockBase
|
2018-03-04 13:56:49 +00:00
|
|
|
Transactions []*transaction.Transaction
|
2018-01-30 10:56:36 +00:00
|
|
|
}
|
2018-01-31 08:27:08 +00:00
|
|
|
|
2018-02-04 19:54:51 +00:00
|
|
|
// Header returns a pointer to the head of the block (BlockHead).
|
|
|
|
func (b *Block) Header() *Header {
|
|
|
|
return &Header{
|
|
|
|
BlockBase: b.BlockBase,
|
|
|
|
}
|
|
|
|
}
|
2018-02-01 20:28:45 +00:00
|
|
|
|
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 {
|
|
|
|
log.Println("full verification of blocks is not yet implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2018-02-01 20:28:45 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 08:27:08 +00:00
|
|
|
// EncodeBinary encodes the block to the given writer.
|
|
|
|
func (b *Block) EncodeBinary(w io.Writer) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-04 19:54:51 +00:00
|
|
|
// DecodeBinary decodes the block from the given reader.
|
2018-01-31 08:27:08 +00:00
|
|
|
func (b *Block) DecodeBinary(r io.Reader) error {
|
2018-02-04 19:54:51 +00:00
|
|
|
if err := b.BlockBase.DecodeBinary(r); err != nil {
|
2018-01-31 10:47:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-04 19:54:51 +00:00
|
|
|
lentx := util.ReadVarUint(r)
|
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{}
|
|
|
|
if err := b.Transactions[i].DecodeBinary(r); err != nil {
|
2018-01-31 10:47:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-01-31 08:27:08 +00:00
|
|
|
|
2018-02-04 19:54:51 +00:00
|
|
|
return nil
|
2018-02-01 20:28:45 +00:00
|
|
|
}
|