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