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.

277 lines
5.5 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
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/go-common"
  10. "github.com/tendermint/go-merkle"
  11. "github.com/tendermint/go-wire"
  12. )
  13. const (
  14. partSize = 65536 // 64KB ... 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. Index int `json:"index"`
  22. Bytes []byte `json:"bytes"`
  23. Proof merkle.SimpleProof `json:"proof"`
  24. // Cache
  25. hash []byte
  26. }
  27. func (part *Part) Hash() []byte {
  28. if part.hash != nil {
  29. return part.hash
  30. } else {
  31. hasher := ripemd160.New()
  32. hasher.Write(part.Bytes) // doesn't err
  33. part.hash = hasher.Sum(nil)
  34. return part.hash
  35. }
  36. }
  37. func (part *Part) String() string {
  38. return part.StringIndented("")
  39. }
  40. func (part *Part) StringIndented(indent string) string {
  41. return fmt.Sprintf(`Part{#%v
  42. %s Bytes: %X...
  43. %s Proof: %v
  44. %s}`,
  45. part.Index,
  46. indent, Fingerprint(part.Bytes),
  47. indent, part.Proof.StringIndented(indent+" "),
  48. indent)
  49. }
  50. //-------------------------------------
  51. type PartSetHeader struct {
  52. Total int `json:"total"`
  53. Hash []byte `json:"hash"`
  54. }
  55. func (psh PartSetHeader) String() string {
  56. return fmt.Sprintf("%v:%X", psh.Total, Fingerprint(psh.Hash))
  57. }
  58. func (psh PartSetHeader) IsZero() bool {
  59. return psh.Total == 0
  60. }
  61. func (psh PartSetHeader) Equals(other PartSetHeader) bool {
  62. return psh.Total == other.Total && bytes.Equal(psh.Hash, other.Hash)
  63. }
  64. func (psh PartSetHeader) WriteSignBytes(w io.Writer, n *int, err *error) {
  65. wire.WriteTo([]byte(Fmt(`{"hash":"%X","total":%v}`, psh.Hash, psh.Total)), w, n, err)
  66. }
  67. //-------------------------------------
  68. type PartSet struct {
  69. total int
  70. hash []byte
  71. mtx sync.Mutex
  72. parts []*Part
  73. partsBitArray *BitArray
  74. count int
  75. }
  76. // Returns an immutable, full PartSet from the data bytes.
  77. // The data bytes are split into "partSize" chunks, and merkle tree computed.
  78. func NewPartSetFromData(data []byte) *PartSet {
  79. // divide data into 4kb parts.
  80. total := (len(data) + partSize - 1) / partSize
  81. parts := make([]*Part, total)
  82. parts_ := make([]merkle.Hashable, total)
  83. partsBitArray := NewBitArray(total)
  84. for i := 0; i < total; i++ {
  85. part := &Part{
  86. Index: i,
  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. root, 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: root,
  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, verify bool) (bool, error) {
  163. ps.mtx.Lock()
  164. defer ps.mtx.Unlock()
  165. // Invalid part index
  166. if part.Index >= ps.total {
  167. return false, ErrPartSetUnexpectedIndex
  168. }
  169. // If part already exists, return false.
  170. if ps.parts[part.Index] != nil {
  171. return false, nil
  172. }
  173. // Check hash proof
  174. if verify {
  175. if !part.Proof.Verify(part.Index, ps.total, part.Hash(), ps.Hash()) {
  176. return false, ErrPartSetInvalidProof
  177. }
  178. }
  179. // Add part
  180. ps.parts[part.Index] = part
  181. ps.partsBitArray.SetIndex(part.Index, true)
  182. ps.count++
  183. return true, nil
  184. }
  185. func (ps *PartSet) GetPart(index int) *Part {
  186. ps.mtx.Lock()
  187. defer ps.mtx.Unlock()
  188. return ps.parts[index]
  189. }
  190. func (ps *PartSet) IsComplete() bool {
  191. return ps.count == ps.total
  192. }
  193. func (ps *PartSet) GetReader() io.Reader {
  194. if !ps.IsComplete() {
  195. PanicSanity("Cannot GetReader() on incomplete PartSet")
  196. }
  197. return NewPartSetReader(ps.parts)
  198. }
  199. type PartSetReader struct {
  200. i int
  201. parts []*Part
  202. reader *bytes.Reader
  203. }
  204. func NewPartSetReader(parts []*Part) *PartSetReader {
  205. return &PartSetReader{
  206. i: 0,
  207. parts: parts,
  208. reader: bytes.NewReader(parts[0].Bytes),
  209. }
  210. }
  211. func (psr *PartSetReader) Read(p []byte) (n int, err error) {
  212. readerLen := psr.reader.Len()
  213. if readerLen >= len(p) {
  214. return psr.reader.Read(p)
  215. } else if readerLen > 0 {
  216. n1, err := psr.Read(p[:readerLen])
  217. if err != nil {
  218. return n1, err
  219. }
  220. n2, err := psr.Read(p[readerLen:])
  221. return n1 + n2, err
  222. }
  223. psr.i += 1
  224. if psr.i >= len(psr.parts) {
  225. return 0, io.EOF
  226. }
  227. psr.reader = bytes.NewReader(psr.parts[psr.i].Bytes)
  228. return psr.Read(p)
  229. }
  230. func (ps *PartSet) StringShort() string {
  231. if ps == nil {
  232. return "nil-PartSet"
  233. } else {
  234. return fmt.Sprintf("(%v of %v)", ps.Count(), ps.Total())
  235. }
  236. }