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.

266 lines
6.0 KiB

10 years ago
11 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
11 years ago
10 years ago
11 years ago
  1. package block
  2. import (
  3. "bytes"
  4. "crypto/sha256"
  5. "errors"
  6. "fmt"
  7. "strings"
  8. "time"
  9. "github.com/tendermint/tendermint/account"
  10. "github.com/tendermint/tendermint/binary"
  11. . "github.com/tendermint/tendermint/common"
  12. "github.com/tendermint/tendermint/config"
  13. "github.com/tendermint/tendermint/merkle"
  14. )
  15. type Block struct {
  16. *Header
  17. *Validation
  18. *Data
  19. }
  20. // Basic validation that doesn't involve state data.
  21. func (b *Block) ValidateBasic(lastBlockHeight uint, lastBlockHash []byte,
  22. lastBlockParts PartSetHeader, lastBlockTime time.Time) error {
  23. if b.Network != config.App.GetString("Network") {
  24. return errors.New("Wrong Block.Header.Network")
  25. }
  26. if b.Height != lastBlockHeight+1 {
  27. return errors.New("Wrong Block.Header.Height")
  28. }
  29. if b.NumTxs != uint(len(b.Data.Txs)) {
  30. return errors.New("Wrong Block.Header.NumTxs")
  31. }
  32. if !bytes.Equal(b.LastBlockHash, lastBlockHash) {
  33. return errors.New("Wrong Block.Header.LastBlockHash")
  34. }
  35. if !b.LastBlockParts.Equals(lastBlockParts) {
  36. return errors.New("Wrong Block.Header.LastBlockParts")
  37. }
  38. /* TODO: Determine bounds.
  39. if !b.Time.After(lastBlockTime) {
  40. return errors.New("Invalid Block.Header.Time")
  41. }
  42. */
  43. if b.Header.Height != 1 {
  44. if err := b.Validation.ValidateBasic(); err != nil {
  45. return err
  46. }
  47. }
  48. // XXX more validation
  49. return nil
  50. }
  51. func (b *Block) Hash() []byte {
  52. hashes := [][]byte{
  53. b.Header.Hash(),
  54. b.Validation.Hash(),
  55. b.Data.Hash(),
  56. }
  57. // Merkle hash from sub-hashes.
  58. return merkle.HashFromHashes(hashes)
  59. }
  60. // Convenience.
  61. // A nil block never hashes to anything.
  62. // Nothing hashes to a nil hash.
  63. func (b *Block) HashesTo(hash []byte) bool {
  64. if len(hash) == 0 {
  65. return false
  66. }
  67. if b == nil {
  68. return false
  69. }
  70. return bytes.Equal(b.Hash(), hash)
  71. }
  72. func (b *Block) String() string {
  73. return b.StringIndented("")
  74. }
  75. func (b *Block) StringIndented(indent string) string {
  76. return fmt.Sprintf(`Block{
  77. %s %v
  78. %s %v
  79. %s %v
  80. %s}#%X`,
  81. indent, b.Header.StringIndented(indent+" "),
  82. indent, b.Validation.StringIndented(indent+" "),
  83. indent, b.Data.StringIndented(indent+" "),
  84. indent, b.Hash())
  85. }
  86. func (b *Block) StringShort() string {
  87. if b == nil {
  88. return "nil-Block"
  89. } else {
  90. return fmt.Sprintf("Block#%X", b.Hash())
  91. }
  92. }
  93. //-----------------------------------------------------------------------------
  94. type Header struct {
  95. Network string
  96. Height uint
  97. Time time.Time
  98. Fees uint64
  99. NumTxs uint
  100. LastBlockHash []byte
  101. LastBlockParts PartSetHeader
  102. StateHash []byte
  103. }
  104. func (h *Header) Hash() []byte {
  105. buf := new(bytes.Buffer)
  106. hasher, n, err := sha256.New(), new(int64), new(error)
  107. binary.WriteBinary(h, buf, n, err)
  108. if *err != nil {
  109. panic(err)
  110. }
  111. hasher.Write(buf.Bytes())
  112. hash := hasher.Sum(nil)
  113. return hash
  114. }
  115. func (h *Header) StringIndented(indent string) string {
  116. return fmt.Sprintf(`Header{
  117. %s Network: %v
  118. %s Height: %v
  119. %s Time: %v
  120. %s Fees: %v
  121. %s NumTxs: %v
  122. %s LastBlockHash: %X
  123. %s LastBlockParts: %v
  124. %s StateHash: %X
  125. %s}#%X`,
  126. indent, h.Network,
  127. indent, h.Height,
  128. indent, h.Time,
  129. indent, h.Fees,
  130. indent, h.NumTxs,
  131. indent, h.LastBlockHash,
  132. indent, h.LastBlockParts,
  133. indent, h.StateHash,
  134. indent, h.Hash())
  135. }
  136. //-----------------------------------------------------------------------------
  137. type Commit struct {
  138. Address []byte
  139. Round uint
  140. Signature account.SignatureEd25519
  141. }
  142. func (commit Commit) IsZero() bool {
  143. return commit.Round == 0 && commit.Signature.IsZero()
  144. }
  145. func (commit Commit) String() string {
  146. return fmt.Sprintf("Commit{A:%X R:%v %X}", commit.Address, commit.Round, Fingerprint(commit.Signature))
  147. }
  148. //-------------------------------------
  149. // NOTE: The Commits are in order of address to preserve the bonded ValidatorSet order.
  150. // Any peer with a block can gossip commits by index with a peer without recalculating the
  151. // active ValidatorSet.
  152. type Validation struct {
  153. Commits []Commit // Commits (or nil) of all active validators in address order.
  154. // Volatile
  155. hash []byte
  156. bitArray BitArray
  157. }
  158. func (v *Validation) ValidateBasic() error {
  159. if len(v.Commits) == 0 {
  160. return errors.New("No commits in validation")
  161. }
  162. lastAddress := []byte{}
  163. for i := 0; i < len(v.Commits); i++ {
  164. commit := v.Commits[i]
  165. if commit.IsZero() {
  166. if len(commit.Address) > 0 {
  167. return errors.New("Zero commits should not have an address")
  168. }
  169. } else {
  170. if len(commit.Address) == 0 {
  171. return errors.New("Nonzero commits should have an address")
  172. }
  173. if len(lastAddress) > 0 && bytes.Compare(lastAddress, commit.Address) != -1 {
  174. return errors.New("Invalid commit order")
  175. }
  176. lastAddress = commit.Address
  177. }
  178. }
  179. return nil
  180. }
  181. func (v *Validation) Hash() []byte {
  182. if v.hash == nil {
  183. bs := make([]interface{}, len(v.Commits))
  184. for i, commit := range v.Commits {
  185. bs[i] = commit
  186. }
  187. v.hash = merkle.HashFromBinaries(bs)
  188. }
  189. return v.hash
  190. }
  191. func (v *Validation) StringIndented(indent string) string {
  192. commitStrings := make([]string, len(v.Commits))
  193. for i, commit := range v.Commits {
  194. commitStrings[i] = commit.String()
  195. }
  196. return fmt.Sprintf(`Validation{
  197. %s %v
  198. %s}#%X`,
  199. indent, strings.Join(commitStrings, "\n"+indent+" "),
  200. indent, v.hash)
  201. }
  202. func (v *Validation) BitArray() BitArray {
  203. if v.bitArray.IsZero() {
  204. v.bitArray = NewBitArray(uint(len(v.Commits)))
  205. for i, commit := range v.Commits {
  206. v.bitArray.SetIndex(uint(i), !commit.IsZero())
  207. }
  208. }
  209. return v.bitArray
  210. }
  211. //-----------------------------------------------------------------------------
  212. type Data struct {
  213. Txs []Tx
  214. // Volatile
  215. hash []byte
  216. }
  217. func (data *Data) Hash() []byte {
  218. if data.hash == nil {
  219. bs := make([]interface{}, len(data.Txs))
  220. for i, tx := range data.Txs {
  221. bs[i] = tx
  222. }
  223. data.hash = merkle.HashFromBinaries(bs)
  224. }
  225. return data.hash
  226. }
  227. func (data *Data) StringIndented(indent string) string {
  228. txStrings := make([]string, len(data.Txs))
  229. for i, tx := range data.Txs {
  230. txStrings[i] = fmt.Sprintf("Tx:%v", tx)
  231. }
  232. return fmt.Sprintf(`Data{
  233. %s %v
  234. %s}#%X`,
  235. indent, strings.Join(txStrings, "\n"+indent+" "),
  236. indent, data.hash)
  237. }