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.

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