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.

132 lines
2.4 KiB

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