tendermint/blocks/block.go

314 lines
6.7 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"
"fmt"
"io"
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"
"github.com/tendermint/tendermint/config"
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
)
func CalcBlockURI(height uint32, hash []byte) string {
return fmt.Sprintf("%v://block/%v#%X",
config.Config.Network,
height,
hash,
)
}
2014-06-05 02:34:45 -07:00
type Block struct {
2014-07-01 14:50:24 -07:00
Header
Validation
2014-07-18 21:21:42 -07:00
Txs
2014-08-10 16:35:08 -07:00
// Volatile
hash []byte
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),
2014-07-18 21:21:42 -07:00
Txs: ReadTxs(r),
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) {
n, err = WriteTo(&b.Header, w, n, err)
n, err = WriteTo(&b.Validation, w, n, err)
n, err = WriteTo(&b.Txs, w, n, err)
return
}
func (b *Block) ValidateBasic() error {
// Basic validation that doesn't involve context.
// XXX
return nil
}
func (b *Block) URI() string {
return CalcBlockURI(uint32(b.Height), b.Hash())
}
func (b *Block) Hash() []byte {
if b.hash != nil {
return b.hash
} else {
hashes := []Binary{
ByteSlice(b.Header.Hash()),
ByteSlice(b.Validation.Hash()),
ByteSlice(b.Txs.Hash()),
}
// Merkle hash from sub-hashes.
return merkle.HashFromBinarySlice(hashes)
}
}
// The returns parts must be signed afterwards.
func (b *Block) ToBlockParts() (parts []*BlockPart) {
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,
Index: UInt16(i),
Total: UInt16(total),
Bytes: partBytes,
Signature: Signature{}, // No signature.
}
parts = append(parts, part)
}
return parts
}
//-----------------------------------------------------------------------------
/*
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 {
Height UInt32
Round UInt16 // Add Round? Well I need to know...
Index UInt16
Total UInt16
Bytes ByteSlice
Signature
// Volatile
hash []byte
}
func ReadBlockPart(r io.Reader) *BlockPart {
return &BlockPart{
Height: ReadUInt32(r),
Round: ReadUInt16(r),
Index: ReadUInt16(r),
Total: ReadUInt16(r),
Bytes: ReadByteSlice(r),
Signature: ReadSignature(r),
}
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) {
n, err = WriteTo(&bp.Height, w, n, err)
n, err = WriteTo(&bp.Round, w, n, err)
n, err = WriteTo(&bp.Index, w, n, err)
n, err = WriteTo(&bp.Total, w, n, err)
n, err = WriteTo(&bp.Bytes, w, n, err)
n, err = WriteTo(&bp.Signature, w, n, err)
2014-07-01 14:50:24 -07:00
return
2014-06-05 18:17:09 -07:00
}
2014-08-10 16:35:08 -07:00
func (bp *BlockPart) URI() string {
return fmt.Sprintf("%v://block/%v/%v[%v/%v]#%X\n",
config.Config.Network,
bp.Height,
bp.Round,
bp.Index,
bp.Total,
bp.BlockPartHash(),
)
}
func (bp *BlockPart) BlockPartHash() []byte {
if bp.hash != nil {
return bp.hash
} else {
hasher := sha256.New()
hasher.Write(bp.Bytes)
bp.hash = hasher.Sum(nil)
return bp.hash
}
}
// Signs the URI, which includes all data and metadata.
// XXX implement or change
func (bp *BlockPart) Sign(acc *PrivAccount) {
// TODO: populate Signature
}
// XXX maybe change.
func (bp *BlockPart) ValidateWithSigner(signer *Account) error {
// TODO: Sanity check height, index, total, bytes, etc.
if !signer.Verify([]byte(bp.URI()), bp.Signature.Bytes) {
return ErrInvalidBlockPartSignature
}
return nil
}
//-----------------------------------------------------------------------------
/* Header is part of a Block */
2014-06-05 02:34:45 -07:00
type Header struct {
2014-07-01 14:50:24 -07:00
Name String
2014-08-10 16:35:08 -07:00
Height UInt32
2014-07-01 14:50:24 -07:00
Fees UInt64
2014-08-10 16:35:08 -07:00
Time Time
2014-07-01 14:50:24 -07:00
PrevHash ByteSlice
ValidationHash ByteSlice
2014-07-18 21:21:42 -07:00
TxsHash ByteSlice
2014-08-10 16:35:08 -07:00
// Volatile
hash []byte
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),
2014-08-10 16:35:08 -07:00
Height: ReadUInt32(r),
2014-07-01 14:50:24 -07:00
Fees: ReadUInt64(r),
2014-08-10 16:35:08 -07:00
Time: ReadTime(r),
2014-07-01 14:50:24 -07:00
PrevHash: ReadByteSlice(r),
ValidationHash: ReadByteSlice(r),
2014-07-18 21:21:42 -07:00
TxsHash: ReadByteSlice(r),
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) {
n, err = WriteTo(h.Name, w, n, err)
n, err = WriteTo(h.Height, w, n, err)
n, err = WriteTo(h.Fees, w, n, err)
n, err = WriteTo(h.Time, w, n, err)
n, err = WriteTo(h.PrevHash, w, n, err)
n, err = WriteTo(h.ValidationHash, w, n, err)
n, err = WriteTo(h.TxsHash, w, 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
}
}
/* Validation is part of a block */
2014-06-05 02:34:45 -07:00
type Validation struct {
2014-07-01 14:50:24 -07:00
Signatures []Signature
Adjustments []Adjustment
2014-08-10 16:35:08 -07:00
// Volatile
hash []byte
2014-06-05 02:34:45 -07:00
}
2014-06-05 11:04:56 -07:00
func ReadValidation(r io.Reader) Validation {
2014-08-10 16:35:08 -07:00
numSigs := int(ReadUInt32(r))
numAdjs := int(ReadUInt32(r))
2014-07-01 14:50:24 -07:00
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-08-10 16:35:08 -07:00
func (v *Validation) WriteTo(w io.Writer) (n int64, err error) {
n, err = WriteTo(UInt32(len(v.Signatures)), w, n, err)
n, err = WriteTo(UInt32(len(v.Adjustments)), w, n, err)
for _, sig := range v.Signatures {
2014-07-18 21:21:42 -07:00
n, err = WriteTo(sig, w, n, err)
2014-07-01 14:50:24 -07:00
}
2014-08-10 16:35:08 -07:00
for _, adj := range v.Adjustments {
2014-07-18 21:21:42 -07:00
n, err = WriteTo(adj, w, 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
}
}
/* Txs is part of a block */
2014-07-18 21:21:42 -07:00
type Txs 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
}
2014-07-18 21:21:42 -07:00
func ReadTxs(r io.Reader) Txs {
2014-08-10 16:35:08 -07:00
numTxs := int(ReadUInt32(r))
2014-07-01 14:50:24 -07:00
txs := make([]Tx, 0, numTxs)
for i := 0; i < numTxs; i++ {
txs = append(txs, ReadTx(r))
}
2014-08-10 16:35:08 -07:00
return Txs{Txs: txs}
2014-06-05 02:34:45 -07:00
}
2014-08-10 16:35:08 -07:00
func (txs *Txs) WriteTo(w io.Writer) (n int64, err error) {
n, err = WriteTo(UInt32(len(txs.Txs)), w, n, err)
for _, tx := range txs.Txs {
2014-07-18 21:21:42 -07:00
n, err = WriteTo(tx, w, 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 (txs *Txs) Hash() []byte {
if txs.hash != nil {
return txs.hash
} else {
bs := make([]Binary, 0, len(txs.Txs))
for i, tx := range txs.Txs {
bs[i] = Binary(tx)
}
txs.hash = merkle.HashFromBinarySlice(bs)
return txs.hash
2014-07-01 14:50:24 -07:00
}
2014-06-05 02:34:45 -07:00
}