package blocks
|
|
|
|
import (
|
|
. "github.com/tendermint/tendermint/binary"
|
|
"github.com/tendermint/tendermint/merkle"
|
|
"io"
|
|
)
|
|
|
|
/* Block */
|
|
type Block struct {
|
|
Header
|
|
Validation
|
|
Txs
|
|
}
|
|
|
|
func ReadBlock(r io.Reader) *Block {
|
|
return &Block{
|
|
Header: ReadHeader(r),
|
|
Validation: ReadValidation(r),
|
|
Txs: ReadTxs(r),
|
|
}
|
|
}
|
|
|
|
func (self *Block) Validate() bool {
|
|
return false
|
|
}
|
|
|
|
func (self *Block) WriteTo(w io.Writer) (n int64, err error) {
|
|
n, err = WriteTo(&self.Header, w, n, err)
|
|
n, err = WriteTo(&self.Validation, w, n, err)
|
|
n, err = WriteTo(&self.Txs, w, n, err)
|
|
return
|
|
}
|
|
|
|
/* Block > Header */
|
|
type Header struct {
|
|
Name String
|
|
Height UInt64
|
|
Fees UInt64
|
|
Time UInt64
|
|
PrevHash ByteSlice
|
|
ValidationHash ByteSlice
|
|
TxsHash ByteSlice
|
|
}
|
|
|
|
func ReadHeader(r io.Reader) Header {
|
|
return Header{
|
|
Name: ReadString(r),
|
|
Height: ReadUInt64(r),
|
|
Fees: ReadUInt64(r),
|
|
Time: ReadUInt64(r),
|
|
PrevHash: ReadByteSlice(r),
|
|
ValidationHash: ReadByteSlice(r),
|
|
TxsHash: ReadByteSlice(r),
|
|
}
|
|
}
|
|
|
|
func (self *Header) WriteTo(w io.Writer) (n int64, err error) {
|
|
n, err = WriteTo(self.Name, w, n, err)
|
|
n, err = WriteTo(self.Height, w, n, err)
|
|
n, err = WriteTo(self.Fees, w, n, err)
|
|
n, err = WriteTo(self.Time, w, n, err)
|
|
n, err = WriteTo(self.PrevHash, w, n, err)
|
|
n, err = WriteTo(self.ValidationHash, w, n, err)
|
|
n, err = WriteTo(self.TxsHash, w, n, err)
|
|
return
|
|
}
|
|
|
|
/* Block > Validation */
|
|
type Validation struct {
|
|
Signatures []Signature
|
|
Adjustments []Adjustment
|
|
}
|
|
|
|
func ReadValidation(r io.Reader) Validation {
|
|
numSigs := int(ReadUInt64(r))
|
|
numAdjs := int(ReadUInt64(r))
|
|
sigs := make([]Signature, 0, numSigs)
|
|
for i := 0; i < numSigs; i++ {
|
|
sigs = append(sigs, ReadSignature(r))
|
|
}
|
|
adjs := make([]Adjustment, 0, numAdjs)
|
|
for i := 0; i < numAdjs; i++ {
|
|
adjs = append(adjs, ReadAdjustment(r))
|
|
}
|
|
return Validation{
|
|
Signatures: sigs,
|
|
Adjustments: adjs,
|
|
}
|
|
}
|
|
|
|
func (self *Validation) WriteTo(w io.Writer) (n int64, err error) {
|
|
n, err = WriteTo(UInt64(len(self.Signatures)), w, n, err)
|
|
n, err = WriteTo(UInt64(len(self.Adjustments)), w, n, err)
|
|
for _, sig := range self.Signatures {
|
|
n, err = WriteTo(sig, w, n, err)
|
|
}
|
|
for _, adj := range self.Adjustments {
|
|
n, err = WriteTo(adj, w, n, err)
|
|
}
|
|
return
|
|
}
|
|
|
|
/* Block > Txs */
|
|
type Txs struct {
|
|
Txs []Tx
|
|
}
|
|
|
|
func ReadTxs(r io.Reader) Txs {
|
|
numTxs := int(ReadUInt64(r))
|
|
txs := make([]Tx, 0, numTxs)
|
|
for i := 0; i < numTxs; i++ {
|
|
txs = append(txs, ReadTx(r))
|
|
}
|
|
return Txs{txs}
|
|
}
|
|
|
|
func (self *Txs) WriteTo(w io.Writer) (n int64, err error) {
|
|
n, err = WriteTo(UInt64(len(self.Txs)), w, n, err)
|
|
for _, tx := range self.Txs {
|
|
n, err = WriteTo(tx, w, n, err)
|
|
}
|
|
return
|
|
}
|
|
|
|
func (self *Txs) MerkleHash() ByteSlice {
|
|
bs := make([]Binary, 0, len(self.Txs))
|
|
for i, tx := range self.Txs {
|
|
bs[i] = Binary(tx)
|
|
}
|
|
return merkle.HashFromBinarySlice(bs)
|
|
}
|