tendermint/types/part_set.go

242 lines
4.8 KiB
Go
Raw Normal View History

package types
2014-09-14 15:37:32 -07:00
import (
"bytes"
"errors"
2014-10-17 01:01:59 -07:00
"fmt"
"io"
2014-09-14 15:37:32 -07:00
"sync"
"code.google.com/p/go.crypto/ripemd160"
2015-07-10 12:15:46 -07:00
"github.com/tendermint/go-wire"
. "github.com/tendermint/go-common"
"github.com/tendermint/go-merkle"
2014-09-14 15:37:32 -07:00
)
const (
partSize = 4096 // 4KB
)
var (
ErrPartSetUnexpectedIndex = errors.New("Error part set unexpected index")
2015-06-18 20:19:39 -07:00
ErrPartSetInvalidProof = errors.New("Error part set invalid proof")
2014-09-14 15:37:32 -07:00
)
type Part struct {
2015-06-18 20:19:39 -07:00
Proof merkle.SimpleProof `json:"proof"`
Bytes []byte `json:"bytes"`
2014-09-14 15:37:32 -07:00
// Cache
hash []byte
}
2014-10-30 03:32:09 -07:00
func (part *Part) Hash() []byte {
if part.hash != nil {
return part.hash
2014-09-14 15:37:32 -07:00
} else {
2015-07-10 12:15:46 -07:00
hasher := ripemd160.New()
2015-07-19 16:42:52 -07:00
hasher.Write(part.Bytes) // doesn't err
2014-10-30 03:32:09 -07:00
part.hash = hasher.Sum(nil)
return part.hash
2014-09-14 15:37:32 -07:00
}
}
2014-10-30 03:32:09 -07:00
func (part *Part) String() string {
2014-12-23 01:35:54 -08:00
return part.StringIndented("")
2014-10-17 01:01:59 -07:00
}
2014-12-23 01:35:54 -08:00
func (part *Part) StringIndented(indent string) string {
2014-10-17 01:01:59 -07:00
return fmt.Sprintf(`Part{
2015-06-18 20:19:39 -07:00
%s Proof: %v
%s Bytes: %X
2014-10-17 01:01:59 -07:00
%s}`,
2015-06-18 20:19:39 -07:00
indent, part.Proof.StringIndented(indent+" "),
indent, part.Bytes,
2014-10-17 01:01:59 -07:00
indent)
}
2014-09-14 15:37:32 -07:00
//-------------------------------------
2014-10-30 03:32:09 -07:00
type PartSetHeader struct {
Total int `json:"total"`
2015-05-01 17:26:49 -07:00
Hash []byte `json:"hash"`
2014-10-30 03:32:09 -07:00
}
func (psh PartSetHeader) String() string {
return fmt.Sprintf("PartSet{T:%v %X}", psh.Total, Fingerprint(psh.Hash))
2014-10-30 03:32:09 -07:00
}
2014-11-03 15:50:23 -08:00
func (psh PartSetHeader) IsZero() bool {
return psh.Total == 0
}
2014-10-30 03:32:09 -07:00
func (psh PartSetHeader) Equals(other PartSetHeader) bool {
2014-10-31 18:35:38 -07:00
return psh.Total == other.Total && bytes.Equal(psh.Hash, other.Hash)
2014-10-30 03:32:09 -07:00
}
2015-04-27 20:55:28 -07:00
func (psh PartSetHeader) WriteSignBytes(w io.Writer, n *int64, err *error) {
2015-07-25 15:45:45 -07:00
wire.WriteTo([]byte(Fmt(`{"hash":"%X","total":%v}`, psh.Hash, psh.Total)), w, n, err)
2015-04-27 20:55:28 -07:00
}
2014-10-30 03:32:09 -07:00
//-------------------------------------
2014-09-14 15:37:32 -07:00
type PartSet struct {
total int
2014-10-31 18:35:38 -07:00
hash []byte
2014-09-14 15:37:32 -07:00
mtx sync.Mutex
parts []*Part
partsBitArray *BitArray
count int
2014-09-14 15:37:32 -07:00
}
2014-12-23 01:35:54 -08:00
// Returns an immutable, full PartSet from the data bytes.
// The data bytes are split into "partSize" chunks, and merkle tree computed.
2014-09-14 15:37:32 -07:00
func NewPartSetFromData(data []byte) *PartSet {
// divide data into 4kb parts.
total := (len(data) + partSize - 1) / partSize
parts := make([]*Part, total)
parts_ := make([]merkle.Hashable, total)
partsBitArray := NewBitArray(total)
2014-09-14 15:37:32 -07:00
for i := 0; i < total; i++ {
part := &Part{
Bytes: data[i*partSize : MinInt(len(data), (i+1)*partSize)],
}
parts[i] = part
parts_[i] = part
partsBitArray.SetIndex(i, true)
2014-09-14 15:37:32 -07:00
}
2015-06-18 20:19:39 -07:00
// Compute merkle proofs
proofs := merkle.SimpleProofsFromHashables(parts_)
2014-09-14 15:37:32 -07:00
for i := 0; i < total; i++ {
2015-06-18 20:19:39 -07:00
parts[i].Proof = *proofs[i]
2014-09-14 15:37:32 -07:00
}
return &PartSet{
total: total,
2015-06-18 20:19:39 -07:00
hash: proofs[0].RootHash,
2014-09-14 15:37:32 -07:00
parts: parts,
partsBitArray: partsBitArray,
count: total,
2014-09-14 15:37:32 -07:00
}
}
// Returns an empty PartSet ready to be populated.
2014-10-30 03:32:09 -07:00
func NewPartSetFromHeader(header PartSetHeader) *PartSet {
2014-09-14 15:37:32 -07:00
return &PartSet{
2014-10-30 03:32:09 -07:00
total: header.Total,
2014-10-31 18:35:38 -07:00
hash: header.Hash,
2014-10-30 03:32:09 -07:00
parts: make([]*Part, header.Total),
partsBitArray: NewBitArray(header.Total),
2014-09-14 15:37:32 -07:00
count: 0,
}
}
2014-10-30 03:32:09 -07:00
func (ps *PartSet) Header() PartSetHeader {
if ps == nil {
return PartSetHeader{}
} else {
return PartSetHeader{
Total: ps.total,
2014-10-31 18:35:38 -07:00
Hash: ps.hash,
2014-10-30 03:32:09 -07:00
}
}
}
2014-11-03 15:50:23 -08:00
func (ps *PartSet) HasHeader(header PartSetHeader) bool {
if ps == nil {
return false
} else {
return ps.Header().Equals(header)
}
}
func (ps *PartSet) BitArray() *BitArray {
2014-09-14 15:37:32 -07:00
ps.mtx.Lock()
defer ps.mtx.Unlock()
return ps.partsBitArray.Copy()
}
2014-10-30 03:32:09 -07:00
func (ps *PartSet) Hash() []byte {
2014-10-21 01:18:46 -07:00
if ps == nil {
return nil
}
2014-10-30 03:32:09 -07:00
return ps.hash
2014-09-14 15:37:32 -07:00
}
2014-10-30 03:32:09 -07:00
func (ps *PartSet) HashesTo(hash []byte) bool {
2014-10-26 13:26:27 -07:00
if ps == nil {
return false
}
2014-10-30 03:32:09 -07:00
return bytes.Equal(ps.hash, hash)
2014-10-26 13:26:27 -07:00
}
func (ps *PartSet) Count() int {
2014-10-18 01:42:33 -07:00
if ps == nil {
return 0
}
return ps.count
}
func (ps *PartSet) Total() int {
2014-09-14 15:37:32 -07:00
if ps == nil {
return 0
}
return ps.total
}
func (ps *PartSet) AddPart(part *Part) (bool, error) {
ps.mtx.Lock()
defer ps.mtx.Unlock()
// Invalid part index
2015-06-18 20:19:39 -07:00
if part.Proof.Index >= ps.total {
2014-09-14 15:37:32 -07:00
return false, ErrPartSetUnexpectedIndex
}
// If part already exists, return false.
2015-06-18 20:19:39 -07:00
if ps.parts[part.Proof.Index] != nil {
2014-09-14 15:37:32 -07:00
return false, nil
}
2015-06-18 20:19:39 -07:00
// Check hash proof
if !part.Proof.Verify(part.Hash(), ps.Hash()) {
return false, ErrPartSetInvalidProof
2014-09-14 15:37:32 -07:00
}
// Add part
2015-06-18 20:19:39 -07:00
ps.parts[part.Proof.Index] = part
ps.partsBitArray.SetIndex(part.Proof.Index, true)
2014-09-14 15:37:32 -07:00
ps.count++
return true, nil
}
func (ps *PartSet) GetPart(index int) *Part {
2014-09-14 15:37:32 -07:00
ps.mtx.Lock()
defer ps.mtx.Unlock()
return ps.parts[index]
}
func (ps *PartSet) IsComplete() bool {
return ps.count == ps.total
}
func (ps *PartSet) GetReader() io.Reader {
2014-09-14 15:37:32 -07:00
if !ps.IsComplete() {
2015-07-19 16:42:52 -07:00
PanicSanity("Cannot GetReader() on incomplete PartSet")
2014-09-14 15:37:32 -07:00
}
buf := []byte{}
for _, part := range ps.parts {
buf = append(buf, part.Bytes...)
}
return bytes.NewReader(buf)
}
2014-10-18 01:42:33 -07:00
2014-12-23 01:35:54 -08:00
func (ps *PartSet) StringShort() string {
2014-10-18 01:42:33 -07:00
if ps == nil {
return "nil-PartSet"
} else {
return fmt.Sprintf("(%v of %v)", ps.Count(), ps.Total())
}
}