mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-27 05:42:56 +00:00
ddd1d92ff1
The idea here is to preserve the history of `dev` branch development and its code when merging with the `master`. Later this code could be moved into the masters code where appropriate.
132 lines
2.9 KiB
Go
132 lines
2.9 KiB
Go
package payload
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"io"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/wire/payload/transaction"
|
|
"github.com/CityOfZion/neo-go/pkg/wire/util"
|
|
)
|
|
|
|
var (
|
|
errPadding = errors.New("There is a padding mismatch")
|
|
)
|
|
|
|
//BlockBase represents the base of the block
|
|
// This is different than the block header. See HeadersMessage
|
|
type BlockBase struct {
|
|
// Version of the block.
|
|
Version uint32 `json:"version"`
|
|
|
|
// hash of the previous block.
|
|
PrevHash util.Uint256 `json:"previousblockhash"`
|
|
|
|
// Root hash of a transaction list.
|
|
MerkleRoot util.Uint256 `json:"merkleroot"`
|
|
|
|
// 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.
|
|
Timestamp uint32 `json:"time"`
|
|
|
|
// index/height of the block
|
|
Index uint32 `json:"height"`
|
|
|
|
// Random number also called nonce
|
|
ConsensusData uint64 `json:"nonce"`
|
|
|
|
// Contract addresss of the next miner
|
|
NextConsensus util.Uint160 `json:"next_consensus"`
|
|
|
|
// Padding that is fixed to 1
|
|
_ uint8
|
|
|
|
// Script used to validate the block
|
|
Witness transaction.Witness `json:"script"`
|
|
|
|
// hash of this block, created when binary encoded.
|
|
Hash util.Uint256
|
|
}
|
|
|
|
// EncodePayload implements the Messager interface
|
|
func (b *BlockBase) EncodePayload(bw *util.BinWriter) error {
|
|
|
|
b.encodeHashableFields(bw)
|
|
|
|
bw.Write(uint8(1))
|
|
b.Witness.Encode(bw)
|
|
|
|
return bw.Err
|
|
}
|
|
|
|
// Decode decodes an io.Reader into a Blockbase
|
|
func (b *BlockBase) Decode(r io.Reader) error {
|
|
br := &util.BinReader{R: r}
|
|
b.DecodePayload(br)
|
|
return br.Err
|
|
}
|
|
|
|
// Encode encodes a blockbase into an io.Writer
|
|
func (b *BlockBase) Encode(w io.Writer) error {
|
|
bw := &util.BinWriter{W: w}
|
|
b.EncodePayload(bw)
|
|
return bw.Err
|
|
}
|
|
|
|
func (b *BlockBase) encodeHashableFields(bw *util.BinWriter) {
|
|
bw.Write(b.Version)
|
|
bw.Write(b.PrevHash)
|
|
bw.Write(b.MerkleRoot)
|
|
bw.Write(b.Timestamp)
|
|
bw.Write(b.Index)
|
|
bw.Write(b.ConsensusData)
|
|
bw.Write(b.NextConsensus)
|
|
}
|
|
|
|
// DecodePayload implements the messager interface
|
|
func (b *BlockBase) DecodePayload(br *util.BinReader) error {
|
|
|
|
b.decodeHashableFields(br)
|
|
|
|
var padding uint8
|
|
br.Read(&padding)
|
|
if padding != 1 {
|
|
return errPadding
|
|
}
|
|
|
|
b.Witness = transaction.Witness{}
|
|
b.Witness.Decode(br)
|
|
|
|
err := b.createHash()
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return br.Err
|
|
}
|
|
|
|
func (b *BlockBase) decodeHashableFields(br *util.BinReader) {
|
|
br.Read(&b.Version)
|
|
br.Read(&b.PrevHash)
|
|
br.Read(&b.MerkleRoot)
|
|
br.Read(&b.Timestamp)
|
|
br.Read(&b.Index)
|
|
br.Read(&b.ConsensusData)
|
|
br.Read(&b.NextConsensus)
|
|
}
|
|
|
|
func (b *BlockBase) createHash() error {
|
|
|
|
hash, err := util.CalculateHash(b.encodeHashableFields)
|
|
b.Hash = hash
|
|
return err
|
|
}
|
|
|
|
// Bytes returns the byte representation of Blockbase
|
|
func (b *BlockBase) Bytes() ([]byte, error) {
|
|
buf := new(bytes.Buffer)
|
|
err := b.Encode(buf)
|
|
return buf.Bytes(), err
|
|
}
|