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.

281 lines
5.6 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) (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 !part.Proof.Verify(part.Index, ps.total, part.Hash(), ps.Hash()) {
  165. return false, ErrPartSetInvalidProof
  166. }
  167. // Add part
  168. ps.parts[part.Index] = part
  169. ps.partsBitArray.SetIndex(part.Index, true)
  170. ps.count++
  171. return true, nil
  172. }
  173. func (ps *PartSet) GetPart(index int) *Part {
  174. ps.mtx.Lock()
  175. defer ps.mtx.Unlock()
  176. return ps.parts[index]
  177. }
  178. func (ps *PartSet) IsComplete() bool {
  179. return ps.count == ps.total
  180. }
  181. func (ps *PartSet) GetReader() io.Reader {
  182. if !ps.IsComplete() {
  183. cmn.PanicSanity("Cannot GetReader() on incomplete PartSet")
  184. }
  185. return NewPartSetReader(ps.parts)
  186. }
  187. type PartSetReader struct {
  188. i int
  189. parts []*Part
  190. reader *bytes.Reader
  191. }
  192. func NewPartSetReader(parts []*Part) *PartSetReader {
  193. return &PartSetReader{
  194. i: 0,
  195. parts: parts,
  196. reader: bytes.NewReader(parts[0].Bytes),
  197. }
  198. }
  199. func (psr *PartSetReader) Read(p []byte) (n int, err error) {
  200. readerLen := psr.reader.Len()
  201. if readerLen >= len(p) {
  202. return psr.reader.Read(p)
  203. } else if readerLen > 0 {
  204. n1, err := psr.Read(p[:readerLen])
  205. if err != nil {
  206. return n1, err
  207. }
  208. n2, err := psr.Read(p[readerLen:])
  209. return n1 + n2, err
  210. }
  211. psr.i++
  212. if psr.i >= len(psr.parts) {
  213. return 0, io.EOF
  214. }
  215. psr.reader = bytes.NewReader(psr.parts[psr.i].Bytes)
  216. return psr.Read(p)
  217. }
  218. func (ps *PartSet) StringShort() string {
  219. if ps == nil {
  220. return "nil-PartSet"
  221. }
  222. ps.mtx.Lock()
  223. defer ps.mtx.Unlock()
  224. return fmt.Sprintf("(%v of %v)", ps.Count(), ps.Total())
  225. }
  226. func (ps *PartSet) MarshalJSON() ([]byte, error) {
  227. if ps == nil {
  228. return []byte("{}"), nil
  229. }
  230. ps.mtx.Lock()
  231. defer ps.mtx.Unlock()
  232. return cdc.MarshalJSON(struct {
  233. CountTotal string `json:"count/total"`
  234. PartsBitArray *cmn.BitArray `json:"parts_bit_array"`
  235. }{
  236. fmt.Sprintf("%d/%d", ps.Count(), ps.Total()),
  237. ps.partsBitArray,
  238. })
  239. }