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.

110 lines
2.2 KiB

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
10 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
10 years ago
11 years ago
10 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
10 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. . "github.com/tendermint/tendermint/common"
  6. "testing"
  7. )
  8. func randSig() Signature {
  9. return Signature{RandUInt64Exp(), RandBytes(32)}
  10. }
  11. func randBaseTx() BaseTx {
  12. return BaseTx{0, randSig()}
  13. }
  14. func TestBlock(t *testing.T) {
  15. // Account Txs
  16. sendTx := &SendTx{
  17. BaseTx: randBaseTx(),
  18. Fee: RandUInt64Exp(),
  19. To: RandUInt64Exp(),
  20. Amount: RandUInt64Exp(),
  21. }
  22. nameTx := &NameTx{
  23. BaseTx: randBaseTx(),
  24. Fee: RandUInt64Exp(),
  25. Name: string(RandBytes(12)),
  26. PubKey: RandBytes(32),
  27. }
  28. // Validation Txs
  29. bondTx := &BondTx{
  30. BaseTx: randBaseTx(),
  31. Fee: RandUInt64Exp(),
  32. UnbondTo: RandUInt64Exp(),
  33. Amount: RandUInt64Exp(),
  34. }
  35. unbondTx := &UnbondTx{
  36. BaseTx: randBaseTx(),
  37. Fee: RandUInt64Exp(),
  38. Amount: RandUInt64Exp(),
  39. }
  40. timeoutTx := &TimeoutTx{
  41. AccountId: RandUInt64Exp(),
  42. Penalty: RandUInt64Exp(),
  43. }
  44. dupeoutTx := &DupeoutTx{
  45. VoteA: Vote{
  46. Height: RandUInt32Exp(),
  47. Round: RandUInt16Exp(),
  48. Type: VoteTypeBare,
  49. BlockHash: RandBytes(32),
  50. Signature: randSig(),
  51. },
  52. VoteB: Vote{
  53. Height: RandUInt32Exp(),
  54. Round: RandUInt16Exp(),
  55. Type: VoteTypeBare,
  56. BlockHash: RandBytes(32),
  57. Signature: randSig(),
  58. },
  59. }
  60. // Block
  61. block := &Block{
  62. Header: Header{
  63. Network: "Tendermint",
  64. Height: RandUInt32Exp(),
  65. Fees: RandUInt64Exp(),
  66. Time: RandTime(),
  67. LastBlockHash: RandBytes(32),
  68. ValidationStateHash: RandBytes(32),
  69. AccountStateHash: RandBytes(32),
  70. },
  71. Validation: Validation{
  72. Signatures: []Signature{randSig(), randSig()},
  73. },
  74. Data: Data{
  75. Txs: []Tx{sendTx, nameTx, bondTx, unbondTx, timeoutTx, dupeoutTx},
  76. },
  77. }
  78. // Write the block, read it in again, write it again.
  79. // Then, compare.
  80. // TODO We should compute the hash instead, so Block -> Bytes -> Block and compare hashes.
  81. blockBytes := BinaryBytes(block)
  82. var n int64
  83. var err error
  84. block2 := ReadBlock(bytes.NewReader(blockBytes), &n, &err)
  85. if err != nil {
  86. t.Errorf("Reading block failed: %v", err)
  87. }
  88. blockBytes2 := BinaryBytes(block2)
  89. if !bytes.Equal(blockBytes, blockBytes2) {
  90. t.Fatal("Write->Read of block failed.")
  91. }
  92. }