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.

132 lines
2.9 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
11 years ago
  1. package blocks
  2. import (
  3. . "github.com/tendermint/tendermint/binary"
  4. "github.com/tendermint/tendermint/merkle"
  5. "io"
  6. )
  7. /* Block */
  8. type Block struct {
  9. Header
  10. Validation
  11. Txs
  12. }
  13. func ReadBlock(r io.Reader) *Block {
  14. return &Block{
  15. Header: ReadHeader(r),
  16. Validation: ReadValidation(r),
  17. Txs: ReadTxs(r),
  18. }
  19. }
  20. func (self *Block) Validate() bool {
  21. return false
  22. }
  23. func (self *Block) WriteTo(w io.Writer) (n int64, err error) {
  24. n, err = WriteTo(&self.Header, w, n, err)
  25. n, err = WriteTo(&self.Validation, w, n, err)
  26. n, err = WriteTo(&self.Txs, w, n, err)
  27. return
  28. }
  29. /* Block > Header */
  30. type Header struct {
  31. Name String
  32. Height UInt64
  33. Fees UInt64
  34. Time UInt64
  35. PrevHash ByteSlice
  36. ValidationHash ByteSlice
  37. TxsHash ByteSlice
  38. }
  39. func ReadHeader(r io.Reader) Header {
  40. return Header{
  41. Name: ReadString(r),
  42. Height: ReadUInt64(r),
  43. Fees: ReadUInt64(r),
  44. Time: ReadUInt64(r),
  45. PrevHash: ReadByteSlice(r),
  46. ValidationHash: ReadByteSlice(r),
  47. TxsHash: ReadByteSlice(r),
  48. }
  49. }
  50. func (self *Header) WriteTo(w io.Writer) (n int64, err error) {
  51. n, err = WriteTo(self.Name, w, n, err)
  52. n, err = WriteTo(self.Height, w, n, err)
  53. n, err = WriteTo(self.Fees, w, n, err)
  54. n, err = WriteTo(self.Time, w, n, err)
  55. n, err = WriteTo(self.PrevHash, w, n, err)
  56. n, err = WriteTo(self.ValidationHash, w, n, err)
  57. n, err = WriteTo(self.TxsHash, w, n, err)
  58. return
  59. }
  60. /* Block > Validation */
  61. type Validation struct {
  62. Signatures []Signature
  63. Adjustments []Adjustment
  64. }
  65. func ReadValidation(r io.Reader) Validation {
  66. numSigs := int(ReadUInt64(r))
  67. numAdjs := int(ReadUInt64(r))
  68. sigs := make([]Signature, 0, numSigs)
  69. for i := 0; i < numSigs; i++ {
  70. sigs = append(sigs, ReadSignature(r))
  71. }
  72. adjs := make([]Adjustment, 0, numAdjs)
  73. for i := 0; i < numAdjs; i++ {
  74. adjs = append(adjs, ReadAdjustment(r))
  75. }
  76. return Validation{
  77. Signatures: sigs,
  78. Adjustments: adjs,
  79. }
  80. }
  81. func (self *Validation) WriteTo(w io.Writer) (n int64, err error) {
  82. n, err = WriteTo(UInt64(len(self.Signatures)), w, n, err)
  83. n, err = WriteTo(UInt64(len(self.Adjustments)), w, n, err)
  84. for _, sig := range self.Signatures {
  85. n, err = WriteTo(sig, w, n, err)
  86. }
  87. for _, adj := range self.Adjustments {
  88. n, err = WriteTo(adj, w, n, err)
  89. }
  90. return
  91. }
  92. /* Block > Txs */
  93. type Txs struct {
  94. Txs []Tx
  95. }
  96. func ReadTxs(r io.Reader) Txs {
  97. numTxs := int(ReadUInt64(r))
  98. txs := make([]Tx, 0, numTxs)
  99. for i := 0; i < numTxs; i++ {
  100. txs = append(txs, ReadTx(r))
  101. }
  102. return Txs{txs}
  103. }
  104. func (self *Txs) WriteTo(w io.Writer) (n int64, err error) {
  105. n, err = WriteTo(UInt64(len(self.Txs)), w, n, err)
  106. for _, tx := range self.Txs {
  107. n, err = WriteTo(tx, w, n, err)
  108. }
  109. return
  110. }
  111. func (self *Txs) MerkleHash() ByteSlice {
  112. bs := make([]Binary, 0, len(self.Txs))
  113. for i, tx := range self.Txs {
  114. bs[i] = Binary(tx)
  115. }
  116. return merkle.HashFromBinarySlice(bs)
  117. }