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.

236 lines
5.1 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 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
10 years ago
11 years ago
  1. package blocks
  2. import (
  3. "bytes"
  4. "crypto/sha256"
  5. "errors"
  6. "io"
  7. "time"
  8. . "github.com/tendermint/tendermint/binary"
  9. . "github.com/tendermint/tendermint/config"
  10. "github.com/tendermint/tendermint/merkle"
  11. )
  12. var (
  13. ErrBlockInvalidNetwork = errors.New("Error block invalid network")
  14. ErrBlockInvalidBlockHeight = errors.New("Error block invalid height")
  15. ErrBlockInvalidLastBlockHash = errors.New("Error block invalid last blockhash")
  16. )
  17. type Block struct {
  18. Header
  19. Validation
  20. Data
  21. // Volatile
  22. hash []byte
  23. }
  24. func ReadBlock(r io.Reader, n *int64, err *error) *Block {
  25. return &Block{
  26. Header: ReadHeader(r, n, err),
  27. Validation: ReadValidation(r, n, err),
  28. Data: ReadData(r, n, err),
  29. }
  30. }
  31. func (b *Block) WriteTo(w io.Writer) (n int64, err error) {
  32. WriteBinary(w, &b.Header, &n, &err)
  33. WriteBinary(w, &b.Validation, &n, &err)
  34. WriteBinary(w, &b.Data, &n, &err)
  35. return
  36. }
  37. // Basic validation that doesn't involve state data.
  38. func (b *Block) ValidateBasic(lastBlockHeight uint32, lastBlockHash []byte) error {
  39. if b.Header.Network != Config.Network {
  40. return ErrBlockInvalidNetwork
  41. }
  42. if b.Header.Height != lastBlockHeight+1 {
  43. return ErrBlockInvalidBlockHeight
  44. }
  45. if !bytes.Equal(b.Header.LastBlockHash, lastBlockHash) {
  46. return ErrBlockInvalidLastBlockHash
  47. }
  48. // XXX more validation
  49. return nil
  50. }
  51. func (b *Block) Hash() []byte {
  52. if b.hash != nil {
  53. return b.hash
  54. } else {
  55. hashes := [][]byte{
  56. b.Header.Hash(),
  57. b.Validation.Hash(),
  58. b.Data.Hash(),
  59. }
  60. // Merkle hash from sub-hashes.
  61. return merkle.HashFromHashes(hashes)
  62. }
  63. }
  64. // Convenience.
  65. // A nil block never hashes to anything.
  66. // Nothing hashes to a nil hash.
  67. func (b *Block) HashesTo(hash []byte) bool {
  68. if len(hash) == 0 {
  69. return false
  70. }
  71. if b == nil {
  72. return false
  73. }
  74. return bytes.Equal(b.Hash(), hash)
  75. }
  76. // Makes an empty next block.
  77. func (b *Block) MakeNextBlock() *Block {
  78. return &Block{
  79. Header: Header{
  80. Network: b.Header.Network,
  81. Height: b.Header.Height + 1,
  82. //Fees: uint64(0),
  83. Time: time.Now(),
  84. LastBlockHash: b.Hash(),
  85. //ValidationStateHash: nil,
  86. //AccountStateHash: nil,
  87. },
  88. }
  89. }
  90. //-----------------------------------------------------------------------------
  91. type Header struct {
  92. Network string
  93. Height uint32
  94. Fees uint64
  95. Time time.Time
  96. LastBlockHash []byte
  97. ValidationStateHash []byte
  98. AccountStateHash []byte
  99. // Volatile
  100. hash []byte
  101. }
  102. func ReadHeader(r io.Reader, n *int64, err *error) (h Header) {
  103. if *err != nil {
  104. return Header{}
  105. }
  106. return Header{
  107. Network: ReadString(r, n, err),
  108. Height: ReadUInt32(r, n, err),
  109. Fees: ReadUInt64(r, n, err),
  110. Time: ReadTime(r, n, err),
  111. LastBlockHash: ReadByteSlice(r, n, err),
  112. ValidationStateHash: ReadByteSlice(r, n, err),
  113. AccountStateHash: ReadByteSlice(r, n, err),
  114. }
  115. }
  116. func (h *Header) WriteTo(w io.Writer) (n int64, err error) {
  117. WriteString(w, h.Network, &n, &err)
  118. WriteUInt32(w, h.Height, &n, &err)
  119. WriteUInt64(w, h.Fees, &n, &err)
  120. WriteTime(w, h.Time, &n, &err)
  121. WriteByteSlice(w, h.LastBlockHash, &n, &err)
  122. WriteByteSlice(w, h.ValidationStateHash, &n, &err)
  123. WriteByteSlice(w, h.AccountStateHash, &n, &err)
  124. return
  125. }
  126. func (h *Header) Hash() []byte {
  127. if h.hash != nil {
  128. return h.hash
  129. } else {
  130. hasher := sha256.New()
  131. _, err := h.WriteTo(hasher)
  132. if err != nil {
  133. panic(err)
  134. }
  135. h.hash = hasher.Sum(nil)
  136. return h.hash
  137. }
  138. }
  139. //-----------------------------------------------------------------------------
  140. type Validation struct {
  141. Signatures []Signature
  142. // Volatile
  143. hash []byte
  144. }
  145. func ReadValidation(r io.Reader, n *int64, err *error) Validation {
  146. numSigs := ReadUInt32(r, n, err)
  147. sigs := make([]Signature, 0, numSigs)
  148. for i := uint32(0); i < numSigs; i++ {
  149. sigs = append(sigs, ReadSignature(r, n, err))
  150. }
  151. return Validation{
  152. Signatures: sigs,
  153. }
  154. }
  155. func (v *Validation) WriteTo(w io.Writer) (n int64, err error) {
  156. WriteUInt32(w, uint32(len(v.Signatures)), &n, &err)
  157. for _, sig := range v.Signatures {
  158. WriteBinary(w, sig, &n, &err)
  159. }
  160. return
  161. }
  162. func (v *Validation) Hash() []byte {
  163. if v.hash != nil {
  164. return v.hash
  165. } else {
  166. hasher := sha256.New()
  167. _, err := v.WriteTo(hasher)
  168. if err != nil {
  169. panic(err)
  170. }
  171. v.hash = hasher.Sum(nil)
  172. return v.hash
  173. }
  174. }
  175. //-----------------------------------------------------------------------------
  176. type Data struct {
  177. Txs []Tx
  178. // Volatile
  179. hash []byte
  180. }
  181. func ReadData(r io.Reader, n *int64, err *error) Data {
  182. numTxs := ReadUInt32(r, n, err)
  183. txs := make([]Tx, 0, numTxs)
  184. for i := uint32(0); i < numTxs; i++ {
  185. txs = append(txs, ReadTx(r, n, err))
  186. }
  187. return Data{Txs: txs}
  188. }
  189. func (data *Data) WriteTo(w io.Writer) (n int64, err error) {
  190. WriteUInt32(w, uint32(len(data.Txs)), &n, &err)
  191. for _, tx := range data.Txs {
  192. WriteBinary(w, tx, &n, &err)
  193. }
  194. return
  195. }
  196. func (data *Data) Hash() []byte {
  197. if data.hash != nil {
  198. return data.hash
  199. } else {
  200. bs := make([]Binary, 0, len(data.Txs))
  201. for i, tx := range data.Txs {
  202. bs[i] = Binary(tx)
  203. }
  204. data.hash = merkle.HashFromBinaries(bs)
  205. return data.hash
  206. }
  207. }