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.

115 lines
2.1 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package blocks
  2. import (
  3. "bytes"
  4. . "github.com/tendermint/tendermint/binary"
  5. "math/rand"
  6. "testing"
  7. )
  8. // Distributed pseudo-exponentially to test for various cases
  9. func randVar() UInt64 {
  10. bits := rand.Uint32() % 64
  11. if bits == 0 {
  12. return 0
  13. }
  14. n := uint64(1 << (bits - 1))
  15. n += uint64(rand.Int63()) & ((1 << (bits - 1)) - 1)
  16. return UInt64(n)
  17. }
  18. func randBytes(n int) ByteSlice {
  19. bs := make([]byte, n)
  20. for i := 0; i < n; i++ {
  21. bs[i] = byte(rand.Intn(256))
  22. }
  23. return bs
  24. }
  25. func randSig() Signature {
  26. return Signature{AccountNumber(randVar()), randBytes(32)}
  27. }
  28. func TestBlock(t *testing.T) {
  29. // Txs
  30. sendTx := &SendTx{
  31. Signature: randSig(),
  32. Fee: randVar(),
  33. To: AccountNumber(randVar()),
  34. Amount: randVar(),
  35. }
  36. nameTx := &NameTx{
  37. Signature: randSig(),
  38. Fee: randVar(),
  39. Name: String(randBytes(12)),
  40. PubKey: randBytes(32),
  41. }
  42. // Adjs
  43. bond := &Bond{
  44. Signature: randSig(),
  45. Fee: randVar(),
  46. UnbondTo: AccountNumber(randVar()),
  47. Amount: randVar(),
  48. }
  49. unbond := &Unbond{
  50. Signature: randSig(),
  51. Fee: randVar(),
  52. Amount: randVar(),
  53. }
  54. timeout := &Timeout{
  55. Account: AccountNumber(randVar()),
  56. Penalty: randVar(),
  57. }
  58. dupeout := &Dupeout{
  59. VoteA: Vote{
  60. Height: randVar(),
  61. BlockHash: randBytes(32),
  62. Signature: randSig(),
  63. },
  64. VoteB: Vote{
  65. Height: randVar(),
  66. BlockHash: randBytes(32),
  67. Signature: randSig(),
  68. },
  69. }
  70. // Block
  71. block := &Block{
  72. Header{
  73. Name: "Tendermint",
  74. Height: randVar(),
  75. Fees: randVar(),
  76. Time: randVar(),
  77. PrevHash: randBytes(32),
  78. ValidationHash: randBytes(32),
  79. DataHash: randBytes(32),
  80. },
  81. Validation{
  82. Signatures: []Signature{randSig(), randSig()},
  83. Adjustments: []Adjustment{bond, unbond, timeout, dupeout},
  84. },
  85. Data{
  86. Txs: []Tx{sendTx, nameTx},
  87. },
  88. }
  89. // Write the block, read it in again, write it again.
  90. // Then, compare.
  91. blockBytes := BinaryBytes(block)
  92. block2 := ReadBlock(bytes.NewReader(blockBytes))
  93. blockBytes2 := BinaryBytes(block2)
  94. if !BinaryEqual(blockBytes, blockBytes2) {
  95. t.Fatal("Write->Read of block failed.")
  96. }
  97. }