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.

126 lines
3.6 KiB

11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
  1. package blocks
  2. import (
  3. "bytes"
  4. . "github.com/tendermint/tendermint/binary"
  5. . "github.com/tendermint/tendermint/common"
  6. "testing"
  7. )
  8. func randSig() Signature {
  9. return Signature{RandUInt64Exp(), RandBytes(32)}
  10. }
  11. func randRoundSig() RoundSignature {
  12. return RoundSignature{RandUInt16(), randSig()}
  13. }
  14. func randBaseTx() BaseTx {
  15. return BaseTx{0, RandUInt64Exp(), randSig()}
  16. }
  17. func randBlock() *Block {
  18. // Account Txs
  19. sendTx := &SendTx{
  20. BaseTx: randBaseTx(),
  21. To: RandUInt64Exp(),
  22. Amount: RandUInt64Exp(),
  23. }
  24. nameTx := &NameTx{
  25. BaseTx: randBaseTx(),
  26. Name: string(RandBytes(12)),
  27. PubKey: RandBytes(32),
  28. }
  29. // Validation Txs
  30. bondTx := &BondTx{
  31. BaseTx: randBaseTx(),
  32. //UnbondTo: RandUInt64Exp(),
  33. }
  34. unbondTx := &UnbondTx{
  35. BaseTx: randBaseTx(),
  36. }
  37. dupeoutTx := &DupeoutTx{
  38. VoteA: Vote{
  39. Height: RandUInt32Exp(),
  40. Round: RandUInt16Exp(),
  41. Type: VoteTypePrevote,
  42. BlockHash: RandBytes(32),
  43. Signature: randSig(),
  44. },
  45. VoteB: Vote{
  46. Height: RandUInt32Exp(),
  47. Round: RandUInt16Exp(),
  48. Type: VoteTypePrevote,
  49. BlockHash: RandBytes(32),
  50. Signature: randSig(),
  51. },
  52. }
  53. // Block
  54. block := &Block{
  55. Header: Header{
  56. Network: "Tendermint",
  57. Height: RandUInt32Exp(),
  58. Fees: RandUInt64Exp(),
  59. Time: RandTime(),
  60. LastBlockHash: RandBytes(32),
  61. StateHash: RandBytes(32),
  62. },
  63. Validation: Validation{
  64. Commits: []RoundSignature{randRoundSig(), randRoundSig()},
  65. },
  66. Data: Data{
  67. Txs: []Tx{sendTx, nameTx, bondTx, unbondTx, dupeoutTx},
  68. },
  69. }
  70. return block
  71. }
  72. func TestBlock(t *testing.T) {
  73. block := randBlock()
  74. // Mutate the block and ensure that the hash changed.
  75. lastHash := block.Hash()
  76. expectChange := func(mutateFn func(b *Block), message string) {
  77. // mutate block
  78. mutateFn(block)
  79. // nuke hashes
  80. block.hash = nil
  81. block.Header.hash = nil
  82. block.Validation.hash = nil
  83. block.Data.hash = nil
  84. // compare
  85. if bytes.Equal(lastHash, block.Hash()) {
  86. t.Error(message)
  87. } else {
  88. lastHash = block.Hash()
  89. }
  90. }
  91. expectChange(func(b *Block) { b.Header.Network = "blah" }, "Expected hash to depend on Network")
  92. expectChange(func(b *Block) { b.Header.Height += 1 }, "Expected hash to depend on Height")
  93. expectChange(func(b *Block) { b.Header.Fees += 1 }, "Expected hash to depend on Fees")
  94. expectChange(func(b *Block) { b.Header.Time = RandTime() }, "Expected hash to depend on Time")
  95. expectChange(func(b *Block) { b.Header.LastBlockHash = RandBytes(32) }, "Expected hash to depend on LastBlockHash")
  96. expectChange(func(b *Block) { b.Header.StateHash = RandBytes(32) }, "Expected hash to depend on StateHash")
  97. expectChange(func(b *Block) { b.Validation.Commits[0].Round += 1 }, "Expected hash to depend on Validation Commit")
  98. expectChange(func(b *Block) { b.Validation.Commits[0].SignerId += 1 }, "Expected hash to depend on Validation Commit")
  99. expectChange(func(b *Block) { b.Validation.Commits[0].Bytes = RandBytes(32) }, "Expected hash to depend on Validation Commit")
  100. expectChange(func(b *Block) { b.Data.Txs[0].(*SendTx).Signature.SignerId += 1 }, "Expected hash to depend on tx Signature")
  101. expectChange(func(b *Block) { b.Data.Txs[0].(*SendTx).Amount += 1 }, "Expected hash to depend on send tx Amount")
  102. // Write the block, read it in again, check hash.
  103. block1 := randBlock()
  104. block1Bytes := BinaryBytes(block1)
  105. var n int64
  106. var err error
  107. block2 := ReadBlock(bytes.NewReader(block1Bytes), &n, &err)
  108. if err != nil {
  109. t.Errorf("Reading block failed: %v", err)
  110. }
  111. if !bytes.Equal(block1.Hash(), block2.Hash()) {
  112. t.Errorf("Expected write/read to preserve original hash")
  113. t.Logf("\nBlock1:\n%v", block1)
  114. t.Logf("\nBlock2:\n%v", block2)
  115. }
  116. }