tendermint/blocks/block.go

138 lines
2.9 KiB
Go
Raw Normal View History

2014-06-04 01:40:17 -07:00
package blocks
import (
2014-07-01 14:50:24 -07:00
. "github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/merkle"
"io"
2014-06-04 01:40:17 -07:00
)
2014-06-05 02:34:45 -07:00
/* Block */
2014-06-04 01:40:17 -07:00
2014-06-05 02:34:45 -07:00
type Block struct {
2014-07-01 14:50:24 -07:00
Header
Validation
Data
// Checkpoint
2014-06-04 01:40:17 -07:00
}
2014-06-05 02:34:45 -07:00
func ReadBlock(r io.Reader) *Block {
2014-07-01 14:50:24 -07:00
return &Block{
Header: ReadHeader(r),
Validation: ReadValidation(r),
Data: ReadData(r),
}
2014-06-04 01:40:17 -07:00
}
func (self *Block) Validate() bool {
2014-07-01 14:50:24 -07:00
return false
2014-06-04 01:40:17 -07:00
}
2014-06-05 02:34:45 -07:00
2014-06-05 18:17:09 -07:00
func (self *Block) WriteTo(w io.Writer) (n int64, err error) {
2014-07-01 14:50:24 -07:00
n, err = WriteOnto(&self.Header, w, n, err)
n, err = WriteOnto(&self.Validation, w, n, err)
n, err = WriteOnto(&self.Data, w, n, err)
return
2014-06-05 18:17:09 -07:00
}
2014-06-05 02:34:45 -07:00
/* Block > Header */
type Header struct {
2014-07-01 14:50:24 -07:00
Name String
Height UInt64
Fees UInt64
Time UInt64
PrevHash ByteSlice
ValidationHash ByteSlice
DataHash ByteSlice
2014-06-05 02:34:45 -07:00
}
2014-06-05 11:04:56 -07:00
func ReadHeader(r io.Reader) Header {
2014-07-01 14:50:24 -07:00
return Header{
Name: ReadString(r),
Height: ReadUInt64(r),
Fees: ReadUInt64(r),
Time: ReadUInt64(r),
PrevHash: ReadByteSlice(r),
ValidationHash: ReadByteSlice(r),
DataHash: ReadByteSlice(r),
}
2014-06-05 02:34:45 -07:00
}
func (self *Header) WriteTo(w io.Writer) (n int64, err error) {
2014-07-01 14:50:24 -07:00
n, err = WriteOnto(self.Name, w, n, err)
n, err = WriteOnto(self.Height, w, n, err)
n, err = WriteOnto(self.Fees, w, n, err)
n, err = WriteOnto(self.Time, w, n, err)
n, err = WriteOnto(self.PrevHash, w, n, err)
n, err = WriteOnto(self.ValidationHash, w, n, err)
n, err = WriteOnto(self.DataHash, w, n, err)
return
2014-06-05 02:34:45 -07:00
}
/* Block > Validation */
type Validation struct {
2014-07-01 14:50:24 -07:00
Signatures []Signature
Adjustments []Adjustment
2014-06-05 02:34:45 -07:00
}
2014-06-05 11:04:56 -07:00
func ReadValidation(r io.Reader) Validation {
2014-07-01 14:50:24 -07:00
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,
}
2014-06-05 02:34:45 -07:00
}
2014-06-05 11:04:56 -07:00
func (self *Validation) WriteTo(w io.Writer) (n int64, err error) {
2014-07-01 14:50:24 -07:00
n, err = WriteOnto(UInt64(len(self.Signatures)), w, n, err)
n, err = WriteOnto(UInt64(len(self.Adjustments)), w, n, err)
for _, sig := range self.Signatures {
n, err = WriteOnto(sig, w, n, err)
}
for _, adj := range self.Adjustments {
n, err = WriteOnto(adj, w, n, err)
}
return
2014-06-05 11:04:56 -07:00
}
2014-06-05 02:34:45 -07:00
/* Block > Data */
type Data struct {
2014-07-01 14:50:24 -07:00
Txs []Tx
2014-06-05 02:34:45 -07:00
}
2014-06-05 11:04:56 -07:00
func ReadData(r io.Reader) Data {
2014-07-01 14:50:24 -07:00
numTxs := int(ReadUInt64(r))
txs := make([]Tx, 0, numTxs)
for i := 0; i < numTxs; i++ {
txs = append(txs, ReadTx(r))
}
return Data{txs}
2014-06-05 02:34:45 -07:00
}
func (self *Data) WriteTo(w io.Writer) (n int64, err error) {
2014-07-01 14:50:24 -07:00
n, err = WriteOnto(UInt64(len(self.Txs)), w, n, err)
for _, tx := range self.Txs {
n, err = WriteOnto(tx, w, n, err)
}
return
2014-06-05 02:34:45 -07:00
}
func (self *Data) MerkleHash() ByteSlice {
2014-07-01 14:50:24 -07:00
bs := make([]Binary, 0, len(self.Txs))
for i, tx := range self.Txs {
bs[i] = Binary(tx)
}
return merkle.HashFromBinarySlice(bs)
2014-06-05 02:34:45 -07:00
}