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.

160 lines
3.5 KiB

  1. package core
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestBlockchainDataStructure(t *testing.T) {
  8. // dir, err := ioutil.TempDir("", "db")
  9. // assert.Nil(t, err)
  10. // db, err := db.New(dir)
  11. // assert.Nil(t, err)
  12. //
  13. // bc := NewBlockchain(db, uint64(1))
  14. block := &Block{
  15. PrevHash: Hash{},
  16. Txs: []Tx{},
  17. Miner: Address{},
  18. Timestamp: time.Now(),
  19. Nonce: 0,
  20. Hash: HashBytes([]byte("blockhash")),
  21. }
  22. block2, err := BlockFromBytes(block.Bytes())
  23. assert.Nil(t, err)
  24. assert.Equal(t, block2.Bytes(), block.Bytes())
  25. }
  26. /*
  27. func TestGetBlock(t *testing.T) {
  28. // dir, err := ioutil.TempDir("", "db")
  29. // assert.Nil(t, err)
  30. // db, err := db.New(dir)
  31. // assert.Nil(t, err)
  32. //
  33. // bc := NewBlockchain(db, uint64(1))
  34. block := &Block{
  35. Height: uint64(1),
  36. PrevHash: Hash{},
  37. Txs: []Tx{},
  38. Miner: Address{},
  39. Timestamp: time.Now(),
  40. Nonce: 0,
  41. Hash: HashBytes([]byte("blockhash")),
  42. }
  43. assert.Equal(t, block.Height, uint64(1))
  44. // block.CalculateHash()
  45. // err = bc.AddBlock(block)
  46. // assert.Nil(t, err)
  47. //
  48. // block2, err := bc.GetBlock(block.Hash)
  49. // assert.Nil(t, err)
  50. // assert.Equal(t, block.Bytes(), block2.Bytes())
  51. }
  52. func TestGetPrevBlock(t *testing.T) {
  53. dir, err := ioutil.TempDir("", "db")
  54. assert.Nil(t, err)
  55. db, err := db.New(dir)
  56. assert.Nil(t, err)
  57. bc := NewBlockchain(db, uint64(1))
  58. var prevHash Hash
  59. for i := 0; i < 10; i++ {
  60. block := &Block{
  61. Height: uint64(i + 1),
  62. PrevHash: prevHash,
  63. Txs: []Tx{},
  64. Miner: Address{},
  65. Timestamp: time.Now(),
  66. Nonce: 0,
  67. Hash: Hash{},
  68. }
  69. block.CalculatePoW(bc.Difficulty)
  70. assert.Equal(t, block.Height, uint64(i+1))
  71. prevHash = block.Hash
  72. err = bc.AddBlock(block)
  73. assert.Nil(t, err)
  74. assert.Equal(t, bc.LastBlock.Height, block.Height)
  75. }
  76. block9, err := bc.GetPrevBlock(bc.LastBlock.Hash)
  77. assert.Nil(t, err)
  78. assert.Equal(t, block9.Height, uint64(9))
  79. block8, err := bc.GetPrevBlock(block9.Hash)
  80. assert.Nil(t, err)
  81. assert.Equal(t, block8.Height, uint64(8))
  82. currentBlock := bc.LastBlock
  83. for err == nil {
  84. currentBlock, err = bc.GetPrevBlock(currentBlock.Hash)
  85. }
  86. assert.Equal(t, err.Error(), "This was the oldest block")
  87. }
  88. func TestAddBlockWithTx(t *testing.T) {
  89. addr0 := Address(HashBytes([]byte("addr0")))
  90. addr1 := Address(HashBytes([]byte("addr1")))
  91. dir, err := ioutil.TempDir("", "db")
  92. assert.Nil(t, err)
  93. db, err := db.New(dir)
  94. assert.Nil(t, err)
  95. bc := NewBlockchain(db, uint64(1))
  96. var txs []Tx
  97. tx := NewTx(addr0, addr1, []Input{}, []Output{})
  98. txs = append(txs, *tx)
  99. block := &Block{
  100. PrevHash: Hash{},
  101. NextHash: Hash{},
  102. Txs: txs,
  103. Miner: Address{},
  104. Timestamp: time.Now(),
  105. Nonce: 0,
  106. Hash: HashBytes([]byte("blockhash")),
  107. }
  108. block2, err := BlockFromBytes(block.Bytes())
  109. assert.Nil(t, err)
  110. assert.Equal(t, block2.Bytes(), block.Bytes())
  111. }
  112. func TestPoABlockchainDataStructure(t *testing.T) {
  113. dir, err := ioutil.TempDir("", "db")
  114. assert.Nil(t, err)
  115. db, err := db.New(dir)
  116. assert.Nil(t, err)
  117. var authNodesPubK []*ecdsa.PublicKey
  118. for i := 0; i < 3; i++ {
  119. privK, err := NewKey()
  120. assert.Nil(t, err)
  121. authNodesPubK = append(authNodesPubK, &privK.PublicKey)
  122. }
  123. bc := NewPoABlockchain(db, authNodesPubK)
  124. block := &Block{
  125. PrevHash: Hash{},
  126. NextHash: Hash{},
  127. Txs: []Tx{},
  128. Miner: Address{},
  129. Timestamp: time.Now(),
  130. Nonce: 0,
  131. Hash: HashBytes([]byte("blockhash")),
  132. }
  133. block2, err := BlockFromBytes(block.Bytes())
  134. assert.Nil(t, err)
  135. assert.Equal(t, block2.Bytes(), block.Bytes())
  136. }
  137. */