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.

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