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.

113 lines
2.6 KiB

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