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.

243 lines
4.8 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
10 years ago
  1. package types
  2. import (
  3. "bytes"
  4. "crypto/sha256"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "sync"
  9. "github.com/tendermint/tendermint/binary"
  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 := sha256.New()
  31. _, err := hasher.Write(part.Bytes)
  32. if err != nil {
  33. panic(err)
  34. }
  35. part.hash = hasher.Sum(nil)
  36. return part.hash
  37. }
  38. }
  39. func (part *Part) String() string {
  40. return part.StringIndented("")
  41. }
  42. func (part *Part) StringIndented(indent string) string {
  43. return fmt.Sprintf(`Part{
  44. %s Proof: %v
  45. %s Bytes: %X
  46. %s}`,
  47. indent, part.Proof.StringIndented(indent+" "),
  48. indent, part.Bytes,
  49. indent)
  50. }
  51. //-------------------------------------
  52. type PartSetHeader struct {
  53. Total int `json:"total"`
  54. Hash []byte `json:"hash"`
  55. }
  56. func (psh PartSetHeader) String() string {
  57. return fmt.Sprintf("PartSet{T:%v %X}", psh.Total, Fingerprint(psh.Hash))
  58. }
  59. func (psh PartSetHeader) IsZero() bool {
  60. return psh.Total == 0
  61. }
  62. func (psh PartSetHeader) Equals(other PartSetHeader) bool {
  63. return psh.Total == other.Total && bytes.Equal(psh.Hash, other.Hash)
  64. }
  65. func (psh PartSetHeader) WriteSignBytes(w io.Writer, n *int64, err *error) {
  66. binary.WriteTo([]byte(Fmt(`{"hash":"%X","total":%v}`, psh.Hash, psh.Total)), w, n, err)
  67. }
  68. //-------------------------------------
  69. type PartSet struct {
  70. total int
  71. hash []byte
  72. mtx sync.Mutex
  73. parts []*Part
  74. partsBitArray *BitArray
  75. count int
  76. }
  77. // Returns an immutable, full PartSet from the data bytes.
  78. // The data bytes are split into "partSize" chunks, and merkle tree computed.
  79. func NewPartSetFromData(data []byte) *PartSet {
  80. // divide data into 4kb parts.
  81. total := (len(data) + partSize - 1) / partSize
  82. parts := make([]*Part, total)
  83. parts_ := make([]merkle.Hashable, total)
  84. partsBitArray := NewBitArray(total)
  85. for i := 0; i < total; i++ {
  86. part := &Part{
  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. 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: proofs[0].RootHash,
  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.Proof.Index >= ps.total {
  167. return false, ErrPartSetUnexpectedIndex
  168. }
  169. // If part already exists, return false.
  170. if ps.parts[part.Proof.Index] != nil {
  171. return false, nil
  172. }
  173. // Check hash proof
  174. if !part.Proof.Verify(part.Hash(), ps.Hash()) {
  175. return false, ErrPartSetInvalidProof
  176. }
  177. // Add part
  178. ps.parts[part.Proof.Index] = part
  179. ps.partsBitArray.SetIndex(part.Proof.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. panic("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. }