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.

133 lines
2.5 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
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 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. // Consensus 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. Name: "Tendermint",
  87. Height: randUInt32(),
  88. Fees: randUInt64(),
  89. Time: randTime(),
  90. PrevHash: randBytes(32),
  91. ValidationHash: randBytes(32),
  92. TxsHash: randBytes(32),
  93. },
  94. Validation: Validation{
  95. Signatures: []Signature{randSig(), randSig()},
  96. Txs: []Txs{bond, unbond, timeout, dupeout},
  97. },
  98. Txs: Txs{
  99. Txs: []Tx{sendTx, nameTx},
  100. hash: nil,
  101. },
  102. }
  103. // Write the block, read it in again, write it again.
  104. // Then, compare.
  105. blockBytes := BinaryBytes(block)
  106. var n int64
  107. var err error
  108. block2 := ReadBlock(bytes.NewReader(blockBytes), &n, &err)
  109. blockBytes2 := BinaryBytes(block2)
  110. if !bytes.Equal(blockBytes, blockBytes2) {
  111. t.Fatal("Write->Read of block failed.")
  112. }
  113. }