tendermint/types/part_set.go

274 lines
5.5 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"
"golang.org/x/crypto/ripemd160"
2015-07-10 12:15:46 -07:00
. "github.com/tendermint/go-common"
"github.com/tendermint/go-merkle"
2015-11-10 13:10:43 -08:00
"github.com/tendermint/go-wire"
2014-09-14 15:37:32 -07:00
)
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-12-10 11:09:25 -08:00
Index int `json:"index"`
2015-06-18 20:19:39 -07:00
Bytes []byte `json:"bytes"`
2015-12-10 11:09:25 -08:00
Proof merkle.SimpleProof `json:"proof"`
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 {
2015-12-10 11:09:25 -08:00
return fmt.Sprintf(`Part{#%v
2015-12-09 17:09:06 -08:00
%s Bytes: %X...
2015-12-10 11:09:25 -08:00
%s Proof: %v
2014-10-17 01:01:59 -07:00
%s}`,
2015-12-10 11:09:25 -08:00
part.Index,
2015-12-09 17:09:06 -08:00
indent, Fingerprint(part.Bytes),
2015-12-10 11:09:25 -08:00
indent, part.Proof.StringIndented(indent+" "),
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 {
2016-08-16 14:59:19 -07:00
return fmt.Sprintf("%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-11-10 13:10:43 -08:00
func (psh PartSetHeader) WriteSignBytes(w io.Writer, n *int, 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.
func NewPartSetFromData(data []byte, partSize int) *PartSet {
2014-09-14 15:37:32 -07:00
// 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{
2015-12-10 11:09:25 -08:00
Index: i,
2014-09-14 15:37:32 -07:00
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
2015-12-10 11:09:25 -08:00
root, 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-12-10 11:09:25 -08:00
hash: root,
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
}
2016-03-11 18:38:15 -08:00
func (ps *PartSet) AddPart(part *Part, verify bool) (bool, error) {
2014-09-14 15:37:32 -07:00
ps.mtx.Lock()
defer ps.mtx.Unlock()
// Invalid part index
2015-12-10 11:09:25 -08:00
if part.Index >= ps.total {
2014-09-14 15:37:32 -07:00
return false, ErrPartSetUnexpectedIndex
}
// If part already exists, return false.
2015-12-10 11:09:25 -08:00
if ps.parts[part.Index] != nil {
2014-09-14 15:37:32 -07:00
return false, nil
}
2015-06-18 20:19:39 -07:00
// Check hash proof
2016-03-11 18:38:15 -08:00
if verify {
if !part.Proof.Verify(part.Index, ps.total, part.Hash(), ps.Hash()) {
return false, ErrPartSetInvalidProof
}
2014-09-14 15:37:32 -07:00
}
// Add part
2015-12-10 11:09:25 -08:00
ps.parts[part.Index] = part
ps.partsBitArray.SetIndex(part.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
}
2016-03-11 19:49:43 -08:00
return NewPartSetReader(ps.parts)
2014-09-14 15:37:32 -07:00
}
2014-10-18 01:42:33 -07:00
2016-03-11 19:49:43 -08:00
type PartSetReader struct {
i int
parts []*Part
reader *bytes.Reader
}
func NewPartSetReader(parts []*Part) *PartSetReader {
return &PartSetReader{
i: 0,
parts: parts,
reader: bytes.NewReader(parts[0].Bytes),
}
}
func (psr *PartSetReader) Read(p []byte) (n int, err error) {
readerLen := psr.reader.Len()
if readerLen >= len(p) {
return psr.reader.Read(p)
} else if readerLen > 0 {
n1, err := psr.Read(p[:readerLen])
if err != nil {
return n1, err
}
n2, err := psr.Read(p[readerLen:])
return n1 + n2, err
}
psr.i += 1
if psr.i >= len(psr.parts) {
2016-03-20 22:24:26 -07:00
return 0, io.EOF
2016-03-11 19:49:43 -08:00
}
psr.reader = bytes.NewReader(psr.parts[psr.i].Bytes)
return psr.Read(p)
}
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())
}
}