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.

278 lines
6.1 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. package blocks
  2. import (
  3. "crypto/sha256"
  4. "io"
  5. "time"
  6. . "github.com/tendermint/tendermint/binary"
  7. . "github.com/tendermint/tendermint/common"
  8. "github.com/tendermint/tendermint/merkle"
  9. )
  10. const (
  11. defaultBlockPartSizeBytes = 4096
  12. )
  13. type Block struct {
  14. Header
  15. Validation
  16. Txs
  17. // Volatile
  18. hash []byte
  19. }
  20. func ReadBlock(r io.Reader, n *int64, err *error) *Block {
  21. return &Block{
  22. Header: ReadHeader(r, n, err),
  23. Validation: ReadValidation(r, n, err),
  24. Txs: ReadTxs(r, n, err),
  25. }
  26. }
  27. func (b *Block) WriteTo(w io.Writer) (n int64, err error) {
  28. WriteBinary(w, &b.Header, &n, &err)
  29. WriteBinary(w, &b.Validation, &n, &err)
  30. WriteBinary(w, &b.Txs, &n, &err)
  31. return
  32. }
  33. func (b *Block) ValidateBasic() error {
  34. // TODO Basic validation that doesn't involve context.
  35. return nil
  36. }
  37. func (b *Block) Hash() []byte {
  38. if b.hash != nil {
  39. return b.hash
  40. } else {
  41. hashes := [][]byte{
  42. b.Header.Hash(),
  43. b.Validation.Hash(),
  44. b.Txs.Hash(),
  45. }
  46. // Merkle hash from sub-hashes.
  47. return merkle.HashFromByteSlices(hashes)
  48. }
  49. }
  50. // The returns parts must be signed afterwards.
  51. func (b *Block) ToBlockPartSet() *BlockPartSet {
  52. var parts []*BlockPart
  53. blockBytes := BinaryBytes(b)
  54. total := (len(blockBytes) + defaultBlockPartSizeBytes - 1) / defaultBlockPartSizeBytes
  55. for i := 0; i < total; i++ {
  56. start := defaultBlockPartSizeBytes * i
  57. end := MinInt(start+defaultBlockPartSizeBytes, len(blockBytes))
  58. partBytes := make([]byte, end-start)
  59. copy(partBytes, blockBytes[start:end]) // Do not ref the original byteslice.
  60. part := &BlockPart{
  61. Height: b.Height,
  62. Index: uint16(i),
  63. Total: uint16(total),
  64. Bytes: partBytes,
  65. Signature: Signature{}, // No signature.
  66. }
  67. parts = append(parts, part)
  68. }
  69. return NewBlockPartSet(b.Height, parts)
  70. }
  71. //-----------------------------------------------------------------------------
  72. /*
  73. BlockPart represents a chunk of the bytes of a block.
  74. Each block is divided into fixed length chunks (e.g. 4Kb)
  75. for faster propagation across the gossip network.
  76. */
  77. type BlockPart struct {
  78. Height uint32
  79. Round uint16 // Add Round? Well I need to know...
  80. Index uint16
  81. Total uint16
  82. Bytes []byte
  83. Signature
  84. // Volatile
  85. hash []byte
  86. }
  87. func ReadBlockPart(r io.Reader, n *int64, err *error) *BlockPart {
  88. return &BlockPart{
  89. Height: ReadUInt32(r, n, err),
  90. Round: ReadUInt16(r, n, err),
  91. Index: ReadUInt16(r, n, err),
  92. Total: ReadUInt16(r, n, err),
  93. Bytes: ReadByteSlice(r, n, err),
  94. Signature: ReadSignature(r, n, err),
  95. }
  96. }
  97. func (bp *BlockPart) WriteTo(w io.Writer) (n int64, err error) {
  98. WriteUInt32(w, bp.Height, &n, &err)
  99. WriteUInt16(w, bp.Round, &n, &err)
  100. WriteUInt16(w, bp.Index, &n, &err)
  101. WriteUInt16(w, bp.Total, &n, &err)
  102. WriteByteSlice(w, bp.Bytes, &n, &err)
  103. WriteBinary(w, bp.Signature, &n, &err)
  104. return
  105. }
  106. // Hash returns the hash of the block part data bytes.
  107. func (bp *BlockPart) Hash() []byte {
  108. if bp.hash != nil {
  109. return bp.hash
  110. } else {
  111. hasher := sha256.New()
  112. hasher.Write(bp.Bytes)
  113. bp.hash = hasher.Sum(nil)
  114. return bp.hash
  115. }
  116. }
  117. //-----------------------------------------------------------------------------
  118. /* Header is part of a Block */
  119. type Header struct {
  120. Name string
  121. Height uint32
  122. Fees uint64
  123. Time time.Time
  124. PrevHash []byte
  125. ValidationHash []byte
  126. TxsHash []byte
  127. // Volatile
  128. hash []byte
  129. }
  130. func ReadHeader(r io.Reader, n *int64, err *error) (h Header) {
  131. if *err != nil {
  132. return Header{}
  133. }
  134. return Header{
  135. Name: ReadString(r, n, err),
  136. Height: ReadUInt32(r, n, err),
  137. Fees: ReadUInt64(r, n, err),
  138. Time: ReadTime(r, n, err),
  139. PrevHash: ReadByteSlice(r, n, err),
  140. ValidationHash: ReadByteSlice(r, n, err),
  141. TxsHash: ReadByteSlice(r, n, err),
  142. }
  143. }
  144. func (h *Header) WriteTo(w io.Writer) (n int64, err error) {
  145. WriteString(w, h.Name, &n, &err)
  146. WriteUInt32(w, h.Height, &n, &err)
  147. WriteUInt64(w, h.Fees, &n, &err)
  148. WriteTime(w, h.Time, &n, &err)
  149. WriteByteSlice(w, h.PrevHash, &n, &err)
  150. WriteByteSlice(w, h.ValidationHash, &n, &err)
  151. WriteByteSlice(w, h.TxsHash, &n, &err)
  152. return
  153. }
  154. func (h *Header) Hash() []byte {
  155. if h.hash != nil {
  156. return h.hash
  157. } else {
  158. hasher := sha256.New()
  159. _, err := h.WriteTo(hasher)
  160. if err != nil {
  161. panic(err)
  162. }
  163. h.hash = hasher.Sum(nil)
  164. return h.hash
  165. }
  166. }
  167. /* Validation is part of a block */
  168. type Validation struct {
  169. Signatures []Signature
  170. Txs []Tx
  171. // Volatile
  172. hash []byte
  173. }
  174. func ReadValidation(r io.Reader, n *int64, err *error) Validation {
  175. numSigs := ReadUInt32(r, n, err)
  176. numAdjs := ReadUInt32(r, n, err)
  177. sigs := make([]Signature, 0, numSigs)
  178. for i := uint32(0); i < numSigs; i++ {
  179. sigs = append(sigs, ReadSignature(r, n, err))
  180. }
  181. tx := make([]Tx, 0, numAdjs)
  182. for i := uint32(0); i < numAdjs; i++ {
  183. tx = append(tx, ReadTx(r, n, err))
  184. }
  185. return Validation{
  186. Signatures: sigs,
  187. Txs: tx,
  188. }
  189. }
  190. func (v *Validation) WriteTo(w io.Writer) (n int64, err error) {
  191. WriteUInt32(w, uint32(len(v.Signatures)), &n, &err)
  192. WriteUInt32(w, uint32(len(v.Txs)), &n, &err)
  193. for _, sig := range v.Signatures {
  194. WriteBinary(w, sig, &n, &err)
  195. }
  196. for _, tx := range v.Txs {
  197. WriteBinary(w, tx, &n, &err)
  198. }
  199. return
  200. }
  201. func (v *Validation) Hash() []byte {
  202. if v.hash != nil {
  203. return v.hash
  204. } else {
  205. hasher := sha256.New()
  206. _, err := v.WriteTo(hasher)
  207. if err != nil {
  208. panic(err)
  209. }
  210. v.hash = hasher.Sum(nil)
  211. return v.hash
  212. }
  213. }
  214. /* Txs is part of a block */
  215. type Txs struct {
  216. Txs []Tx
  217. // Volatile
  218. hash []byte
  219. }
  220. func ReadTxs(r io.Reader, n *int64, err *error) Txs {
  221. numTxs := ReadUInt32(r, n, err)
  222. txs := make([]Tx, 0, numTxs)
  223. for i := uint32(0); i < numTxs; i++ {
  224. txs = append(txs, ReadTx(r, n, err))
  225. }
  226. return Txs{Txs: txs}
  227. }
  228. func (txs *Txs) WriteTo(w io.Writer) (n int64, err error) {
  229. WriteUInt32(w, uint32(len(txs.Txs)), &n, &err)
  230. for _, tx := range txs.Txs {
  231. WriteBinary(w, tx, &n, &err)
  232. }
  233. return
  234. }
  235. func (txs *Txs) Hash() []byte {
  236. if txs.hash != nil {
  237. return txs.hash
  238. } else {
  239. bs := make([]Binary, 0, len(txs.Txs))
  240. for i, tx := range txs.Txs {
  241. bs[i] = Binary(tx)
  242. }
  243. txs.hash = merkle.HashFromBinarySlice(bs)
  244. return txs.hash
  245. }
  246. }