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.

140 lines
3.3 KiB

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