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.

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