You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
4.9 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package types
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "sync"
  8. "github.com/tendermint/tendermint/Godeps/_workspace/src/code.google.com/p/go.crypto/ripemd160"
  9. "github.com/tendermint/tendermint/wire"
  10. . "github.com/tendermint/tendermint/common"
  11. "github.com/tendermint/tendermint/merkle"
  12. )
  13. const (
  14. partSize = 4096 // 4KB
  15. )
  16. var (
  17. ErrPartSetUnexpectedIndex = errors.New("Error part set unexpected index")
  18. ErrPartSetInvalidProof = errors.New("Error part set invalid proof")
  19. )
  20. type Part struct {
  21. Proof merkle.SimpleProof `json:"proof"`
  22. Bytes []byte `json:"bytes"`
  23. // Cache
  24. hash []byte
  25. }
  26. func (part *Part) Hash() []byte {
  27. if part.hash != nil {
  28. return part.hash
  29. } else {
  30. hasher := ripemd160.New()
  31. hasher.Write(part.Bytes) // doesn't err
  32. part.hash = hasher.Sum(nil)
  33. return part.hash
  34. }
  35. }
  36. func (part *Part) String() string {
  37. return part.StringIndented("")
  38. }
  39. func (part *Part) StringIndented(indent string) string {
  40. return fmt.Sprintf(`Part{
  41. %s Proof: %v
  42. %s Bytes: %X
  43. %s}`,
  44. indent, part.Proof.StringIndented(indent+" "),
  45. indent, part.Bytes,
  46. indent)
  47. }
  48. //-------------------------------------
  49. type PartSetHeader struct {
  50. Total int `json:"total"`
  51. Hash []byte `json:"hash"`
  52. }
  53. func (psh PartSetHeader) String() string {
  54. return fmt.Sprintf("PartSet{T:%v %X}", psh.Total, Fingerprint(psh.Hash))
  55. }
  56. func (psh PartSetHeader) IsZero() bool {
  57. return psh.Total == 0
  58. }
  59. func (psh PartSetHeader) Equals(other PartSetHeader) bool {
  60. return psh.Total == other.Total && bytes.Equal(psh.Hash, other.Hash)
  61. }
  62. func (psh PartSetHeader) WriteSignBytes(w io.Writer, n *int64, err *error) {
  63. wire.WriteTo([]byte(Fmt(`{"hash":"%X","total":%v}`, psh.Hash, psh.Total)), w, n, err)
  64. }
  65. //-------------------------------------
  66. type PartSet struct {
  67. total int
  68. hash []byte
  69. mtx sync.Mutex
  70. parts []*Part
  71. partsBitArray *BitArray
  72. count int
  73. }
  74. // Returns an immutable, full PartSet from the data bytes.
  75. // The data bytes are split into "partSize" chunks, and merkle tree computed.
  76. func NewPartSetFromData(data []byte) *PartSet {
  77. // divide data into 4kb parts.
  78. total := (len(data) + partSize - 1) / partSize
  79. parts := make([]*Part, total)
  80. parts_ := make([]merkle.Hashable, total)
  81. partsBitArray := NewBitArray(total)
  82. for i := 0; i < total; i++ {
  83. part := &Part{
  84. Bytes: data[i*partSize : MinInt(len(data), (i+1)*partSize)],
  85. }
  86. parts[i] = part
  87. parts_[i] = part
  88. partsBitArray.SetIndex(i, true)
  89. }
  90. // Compute merkle proofs
  91. proofs := merkle.SimpleProofsFromHashables(parts_)
  92. for i := 0; i < total; i++ {
  93. parts[i].Proof = *proofs[i]
  94. }
  95. return &PartSet{
  96. total: total,
  97. hash: proofs[0].RootHash,
  98. parts: parts,
  99. partsBitArray: partsBitArray,
  100. count: total,
  101. }
  102. }
  103. // Returns an empty PartSet ready to be populated.
  104. func NewPartSetFromHeader(header PartSetHeader) *PartSet {
  105. return &PartSet{
  106. total: header.Total,
  107. hash: header.Hash,
  108. parts: make([]*Part, header.Total),
  109. partsBitArray: NewBitArray(header.Total),
  110. count: 0,
  111. }
  112. }
  113. func (ps *PartSet) Header() PartSetHeader {
  114. if ps == nil {
  115. return PartSetHeader{}
  116. } else {
  117. return PartSetHeader{
  118. Total: ps.total,
  119. Hash: ps.hash,
  120. }
  121. }
  122. }
  123. func (ps *PartSet) HasHeader(header PartSetHeader) bool {
  124. if ps == nil {
  125. return false
  126. } else {
  127. return ps.Header().Equals(header)
  128. }
  129. }
  130. func (ps *PartSet) BitArray() *BitArray {
  131. ps.mtx.Lock()
  132. defer ps.mtx.Unlock()
  133. return ps.partsBitArray.Copy()
  134. }
  135. func (ps *PartSet) Hash() []byte {
  136. if ps == nil {
  137. return nil
  138. }
  139. return ps.hash
  140. }
  141. func (ps *PartSet) HashesTo(hash []byte) bool {
  142. if ps == nil {
  143. return false
  144. }
  145. return bytes.Equal(ps.hash, hash)
  146. }
  147. func (ps *PartSet) Count() int {
  148. if ps == nil {
  149. return 0
  150. }
  151. return ps.count
  152. }
  153. func (ps *PartSet) Total() int {
  154. if ps == nil {
  155. return 0
  156. }
  157. return ps.total
  158. }
  159. func (ps *PartSet) AddPart(part *Part) (bool, error) {
  160. ps.mtx.Lock()
  161. defer ps.mtx.Unlock()
  162. // Invalid part index
  163. if part.Proof.Index >= ps.total {
  164. return false, ErrPartSetUnexpectedIndex
  165. }
  166. // If part already exists, return false.
  167. if ps.parts[part.Proof.Index] != nil {
  168. return false, nil
  169. }
  170. // Check hash proof
  171. if !part.Proof.Verify(part.Hash(), ps.Hash()) {
  172. return false, ErrPartSetInvalidProof
  173. }
  174. // Add part
  175. ps.parts[part.Proof.Index] = part
  176. ps.partsBitArray.SetIndex(part.Proof.Index, true)
  177. ps.count++
  178. return true, nil
  179. }
  180. func (ps *PartSet) GetPart(index int) *Part {
  181. ps.mtx.Lock()
  182. defer ps.mtx.Unlock()
  183. return ps.parts[index]
  184. }
  185. func (ps *PartSet) IsComplete() bool {
  186. return ps.count == ps.total
  187. }
  188. func (ps *PartSet) GetReader() io.Reader {
  189. if !ps.IsComplete() {
  190. PanicSanity("Cannot GetReader() on incomplete PartSet")
  191. }
  192. buf := []byte{}
  193. for _, part := range ps.parts {
  194. buf = append(buf, part.Bytes...)
  195. }
  196. return bytes.NewReader(buf)
  197. }
  198. func (ps *PartSet) StringShort() string {
  199. if ps == nil {
  200. return "nil-PartSet"
  201. } else {
  202. return fmt.Sprintf("(%v of %v)", ps.Count(), ps.Total())
  203. }
  204. }