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.

275 lines
5.5 KiB

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