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.

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