tendermint/blocks/block.go

284 lines
6.3 KiB
Go
Raw Normal View History

2014-06-04 01:40:17 -07:00
package blocks
import (
2014-08-10 16:35:08 -07:00
"crypto/sha256"
"io"
"time"
2014-08-10 16:35:08 -07:00
2014-07-01 14:50:24 -07:00
. "github.com/tendermint/tendermint/binary"
2014-08-10 16:35:08 -07:00
. "github.com/tendermint/tendermint/common"
2014-07-01 14:50:24 -07:00
"github.com/tendermint/tendermint/merkle"
2014-06-04 01:40:17 -07:00
)
2014-08-10 16:35:08 -07:00
const (
defaultBlockPartSizeBytes = 4096
)
2014-06-05 02:34:45 -07:00
type Block struct {
2014-07-01 14:50:24 -07:00
Header
Validation
Data
2014-08-10 16:35:08 -07:00
// Volatile
hash []byte
2014-06-04 01:40:17 -07:00
}
func ReadBlock(r io.Reader, n *int64, err *error) *Block {
2014-07-01 14:50:24 -07:00
return &Block{
Header: ReadHeader(r, n, err),
Validation: ReadValidation(r, n, err),
Data: ReadData(r, n, err),
2014-07-01 14:50:24 -07:00
}
2014-06-04 01:40:17 -07:00
}
2014-08-10 16:35:08 -07:00
func (b *Block) WriteTo(w io.Writer) (n int64, err error) {
WriteBinary(w, &b.Header, &n, &err)
WriteBinary(w, &b.Validation, &n, &err)
WriteBinary(w, &b.Data, &n, &err)
2014-08-10 16:35:08 -07:00
return
}
func (b *Block) ValidateBasic() error {
// TODO Basic validation that doesn't involve context.
2014-08-10 16:35:08 -07:00
return nil
}
func (b *Block) Hash() []byte {
if b.hash != nil {
return b.hash
} else {
hashes := [][]byte{
b.Header.Hash(),
b.Validation.Hash(),
b.Data.Hash(),
2014-08-10 16:35:08 -07:00
}
// Merkle hash from sub-hashes.
return merkle.HashFromByteSlices(hashes)
2014-08-10 16:35:08 -07:00
}
}
// The returns parts must be signed afterwards.
func (b *Block) ToBlockPartSet() *BlockPartSet {
var parts []*BlockPart
2014-08-10 16:35:08 -07:00
blockBytes := BinaryBytes(b)
total := (len(blockBytes) + defaultBlockPartSizeBytes - 1) / defaultBlockPartSizeBytes
for i := 0; i < total; i++ {
start := defaultBlockPartSizeBytes * i
end := MinInt(start+defaultBlockPartSizeBytes, len(blockBytes))
partBytes := make([]byte, end-start)
copy(partBytes, blockBytes[start:end]) // Do not ref the original byteslice.
part := &BlockPart{
Height: b.Height,
2014-08-30 16:28:51 -07:00
Index: uint16(i),
Total: uint16(total),
2014-08-10 16:35:08 -07:00
Bytes: partBytes,
Signature: Signature{}, // No signature.
}
parts = append(parts, part)
}
return NewBlockPartSet(b.Height, parts)
2014-08-10 16:35:08 -07:00
}
// Makes an empty next block.
func (b *Block) MakeNextBlock() *Block {
return &Block{
Header: Header{
Name: b.Header.Name,
Height: b.Header.Height + 1,
//Fees: uint64(0),
Time: time.Now(),
PrevHash: b.Hash(),
//ValidationStateHash: nil,
//AccountStateHash: nil,
},
}
}
2014-08-10 16:35:08 -07:00
//-----------------------------------------------------------------------------
/*
BlockPart represents a chunk of the bytes of a block.
Each block is divided into fixed length chunks (e.g. 4Kb)
for faster propagation across the gossip network.
*/
type BlockPart struct {
2014-08-30 16:28:51 -07:00
Height uint32
Round uint16 // Add Round? Well I need to know...
Index uint16
Total uint16
Bytes []byte
2014-08-10 16:35:08 -07:00
Signature
// Volatile
hash []byte
}
func ReadBlockPart(r io.Reader, n *int64, err *error) *BlockPart {
2014-08-10 16:35:08 -07:00
return &BlockPart{
Height: ReadUInt32(r, n, err),
Round: ReadUInt16(r, n, err),
Index: ReadUInt16(r, n, err),
Total: ReadUInt16(r, n, err),
Bytes: ReadByteSlice(r, n, err),
Signature: ReadSignature(r, n, err),
2014-08-10 16:35:08 -07:00
}
2014-06-04 01:40:17 -07:00
}
2014-06-05 02:34:45 -07:00
2014-08-10 16:35:08 -07:00
func (bp *BlockPart) WriteTo(w io.Writer) (n int64, err error) {
WriteUInt32(w, bp.Height, &n, &err)
WriteUInt16(w, bp.Round, &n, &err)
WriteUInt16(w, bp.Index, &n, &err)
WriteUInt16(w, bp.Total, &n, &err)
WriteByteSlice(w, bp.Bytes, &n, &err)
WriteBinary(w, bp.Signature, &n, &err)
2014-07-01 14:50:24 -07:00
return
2014-06-05 18:17:09 -07:00
}
// Hash returns the hash of the block part data bytes.
func (bp *BlockPart) Hash() []byte {
2014-08-10 16:35:08 -07:00
if bp.hash != nil {
return bp.hash
} else {
hasher := sha256.New()
hasher.Write(bp.Bytes)
bp.hash = hasher.Sum(nil)
return bp.hash
}
}
//-----------------------------------------------------------------------------
2014-06-05 02:34:45 -07:00
type Header struct {
Name string
Height uint32
Fees uint64
Time time.Time
PrevHash []byte
ValidationStateHash []byte
AccountStateHash []byte
2014-08-10 16:35:08 -07:00
// Volatile
hash []byte
2014-06-05 02:34:45 -07:00
}
func ReadHeader(r io.Reader, n *int64, err *error) (h Header) {
if *err != nil {
return Header{}
}
2014-07-01 14:50:24 -07:00
return Header{
Name: ReadString(r, n, err),
Height: ReadUInt32(r, n, err),
Fees: ReadUInt64(r, n, err),
Time: ReadTime(r, n, err),
PrevHash: ReadByteSlice(r, n, err),
ValidationStateHash: ReadByteSlice(r, n, err),
AccountStateHash: ReadByteSlice(r, n, err),
2014-07-01 14:50:24 -07:00
}
2014-06-05 02:34:45 -07:00
}
2014-08-10 16:35:08 -07:00
func (h *Header) WriteTo(w io.Writer) (n int64, err error) {
WriteString(w, h.Name, &n, &err)
WriteUInt32(w, h.Height, &n, &err)
WriteUInt64(w, h.Fees, &n, &err)
WriteTime(w, h.Time, &n, &err)
WriteByteSlice(w, h.PrevHash, &n, &err)
WriteByteSlice(w, h.ValidationStateHash, &n, &err)
WriteByteSlice(w, h.AccountStateHash, &n, &err)
2014-07-01 14:50:24 -07:00
return
2014-06-05 02:34:45 -07:00
}
2014-08-10 16:35:08 -07:00
func (h *Header) Hash() []byte {
if h.hash != nil {
return h.hash
} else {
hasher := sha256.New()
_, err := h.WriteTo(hasher)
if err != nil {
panic(err)
}
h.hash = hasher.Sum(nil)
return h.hash
}
}
//-----------------------------------------------------------------------------
2014-06-05 02:34:45 -07:00
type Validation struct {
2014-09-10 02:43:16 -07:00
Signatures []Signature
2014-08-10 16:35:08 -07:00
// Volatile
hash []byte
2014-06-05 02:34:45 -07:00
}
func ReadValidation(r io.Reader, n *int64, err *error) Validation {
numSigs := ReadUInt32(r, n, err)
2014-07-01 14:50:24 -07:00
sigs := make([]Signature, 0, numSigs)
2014-08-30 16:28:51 -07:00
for i := uint32(0); i < numSigs; i++ {
sigs = append(sigs, ReadSignature(r, n, err))
2014-07-01 14:50:24 -07:00
}
return Validation{
2014-09-10 02:43:16 -07:00
Signatures: sigs,
2014-07-01 14:50:24 -07:00
}
2014-06-05 02:34:45 -07:00
}
2014-08-10 16:35:08 -07:00
func (v *Validation) WriteTo(w io.Writer) (n int64, err error) {
WriteUInt32(w, uint32(len(v.Signatures)), &n, &err)
2014-08-10 16:35:08 -07:00
for _, sig := range v.Signatures {
WriteBinary(w, sig, &n, &err)
2014-07-01 14:50:24 -07:00
}
return
2014-06-05 11:04:56 -07:00
}
2014-06-05 02:34:45 -07:00
2014-08-10 16:35:08 -07:00
func (v *Validation) Hash() []byte {
if v.hash != nil {
return v.hash
} else {
hasher := sha256.New()
_, err := v.WriteTo(hasher)
if err != nil {
panic(err)
}
v.hash = hasher.Sum(nil)
return v.hash
}
}
//-----------------------------------------------------------------------------
type Data struct {
2014-07-01 14:50:24 -07:00
Txs []Tx
2014-08-10 16:35:08 -07:00
// Volatile
hash []byte
2014-06-05 02:34:45 -07:00
}
func ReadData(r io.Reader, n *int64, err *error) Data {
numTxs := ReadUInt32(r, n, err)
2014-07-01 14:50:24 -07:00
txs := make([]Tx, 0, numTxs)
2014-08-30 16:28:51 -07:00
for i := uint32(0); i < numTxs; i++ {
txs = append(txs, ReadTx(r, n, err))
2014-07-01 14:50:24 -07:00
}
return Data{Txs: txs}
2014-06-05 02:34:45 -07:00
}
func (data *Data) WriteTo(w io.Writer) (n int64, err error) {
WriteUInt32(w, uint32(len(data.Txs)), &n, &err)
for _, tx := range data.Txs {
WriteBinary(w, tx, &n, &err)
2014-07-01 14:50:24 -07:00
}
return
2014-06-05 02:34:45 -07:00
}
func (data *Data) Hash() []byte {
if data.hash != nil {
return data.hash
2014-08-10 16:35:08 -07:00
} else {
bs := make([]Binary, 0, len(data.Txs))
for i, tx := range data.Txs {
2014-08-10 16:35:08 -07:00
bs[i] = Binary(tx)
}
data.hash = merkle.HashFromBinarySlice(bs)
return data.hash
2014-07-01 14:50:24 -07:00
}
2014-06-05 02:34:45 -07:00
}