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.

202 lines
4.7 KiB

  1. package node
  2. import (
  3. "crypto/ecdsa"
  4. "io/ioutil"
  5. "testing"
  6. "github.com/arnaucube/slowlorisdb/core"
  7. "github.com/arnaucube/slowlorisdb/db"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func newTestPoABlockchain() (*ecdsa.PrivateKey, *core.Blockchain, error) {
  11. dir, err := ioutil.TempDir("", "db")
  12. if err != nil {
  13. return nil, nil, err
  14. }
  15. db, err := db.New(dir)
  16. if err != nil {
  17. return nil, nil, err
  18. }
  19. privK, err := core.NewKey()
  20. if err != nil {
  21. return nil, nil, err
  22. }
  23. var authNodes []*ecdsa.PublicKey
  24. authNodes = append(authNodes, &privK.PublicKey)
  25. bc := core.NewPoABlockchain(db, authNodes)
  26. return privK, bc, nil
  27. }
  28. func TestNode(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. privK, err := core.NewKey()
  34. assert.Nil(t, err)
  35. dif := uint64(1)
  36. bc := core.NewBlockchain(db, dif)
  37. node, err := NewNode(privK, bc, true)
  38. assert.Nil(t, err)
  39. assert.Equal(t, node.Addr, core.AddressFromPrivK(node.PrivK))
  40. }
  41. func TestNodeSignature(t *testing.T) {
  42. dir, err := ioutil.TempDir("", "db")
  43. assert.Nil(t, err)
  44. db, err := db.New(dir)
  45. assert.Nil(t, err)
  46. privK, err := core.NewKey()
  47. assert.Nil(t, err)
  48. dif := uint64(1)
  49. bc := core.NewBlockchain(db, dif)
  50. node, err := NewNode(privK, bc, true)
  51. assert.Nil(t, err)
  52. m := []byte("test")
  53. sig, err := node.Sign(m)
  54. assert.Nil(t, err)
  55. pubK := node.PrivK.PublicKey
  56. assert.True(t, core.VerifySignature(&pubK, m, *sig))
  57. }
  58. func TestBlockFromPendingTxs(t *testing.T) {
  59. privK, bc, err := newTestPoABlockchain()
  60. assert.Nil(t, err)
  61. node, err := NewNode(privK, bc, true)
  62. assert.Nil(t, err)
  63. privK0, err := core.NewKey()
  64. assert.Nil(t, err)
  65. pubK0 := privK0.PublicKey
  66. privK1, err := core.NewKey()
  67. assert.Nil(t, err)
  68. pubK1 := privK1.PublicKey
  69. tx := core.NewTx(&pubK0, &pubK1, []core.Input{}, []core.Output{})
  70. node.AddToPendingTxs(*tx)
  71. block, err := node.BlockFromPendingTxs()
  72. assert.Nil(t, err)
  73. assert.True(t, core.CheckBlockPoW(block, node.Bc.Difficulty))
  74. assert.True(t, node.Bc.VerifyBlock(block))
  75. }
  76. func TestBlockFromPendingTxsIteration(t *testing.T) {
  77. privK, bc, err := newTestPoABlockchain()
  78. assert.Nil(t, err)
  79. node, err := NewNode(privK, bc, true)
  80. assert.Nil(t, err)
  81. privK0, err := core.NewKey()
  82. assert.Nil(t, err)
  83. pubK0 := privK0.PublicKey
  84. privK1, err := core.NewKey()
  85. assert.Nil(t, err)
  86. pubK1 := privK1.PublicKey
  87. for i := 0; i < 10; i++ {
  88. tx := core.NewTx(&pubK0, &pubK1, []core.Input{}, []core.Output{})
  89. node.AddToPendingTxs(*tx)
  90. }
  91. block, err := node.BlockFromPendingTxs()
  92. assert.Nil(t, err)
  93. assert.True(t, core.CheckBlockPoW(block, node.Bc.Difficulty))
  94. assert.True(t, node.Bc.VerifyBlock(block))
  95. }
  96. func TestFromGenesisToTenBlocks(t *testing.T) {
  97. privK, bc, err := newTestPoABlockchain()
  98. assert.Nil(t, err)
  99. node, err := NewNode(privK, bc, true)
  100. assert.Nil(t, err)
  101. // create the genesis block
  102. genesisBlock, err := node.CreateGenesis(&privK.PublicKey, uint64(100))
  103. assert.Nil(t, err)
  104. assert.NotEqual(t, genesisBlock.Signature, core.Signature{})
  105. assert.NotEqual(t, genesisBlock.Hash, core.Hash{})
  106. assert.True(t, node.Bc.VerifyBlock(genesisBlock))
  107. // add the genesis block into the blockchain
  108. err = node.Bc.AddBlock(genesisBlock)
  109. assert.Nil(t, err)
  110. assert.NotEqual(t, genesisBlock.Hash, core.Hash{})
  111. assert.Equal(t, genesisBlock.Hash, node.Bc.LastBlock.Hash)
  112. // add another tx sending coins to the pubK0
  113. privK0, err := core.NewKey()
  114. assert.Nil(t, err)
  115. pubK0 := privK0.PublicKey
  116. var ins []core.Input
  117. in := core.Input{
  118. TxId: genesisBlock.Txs[0].TxId,
  119. Vout: 0,
  120. Value: 100,
  121. }
  122. ins = append(ins, in)
  123. var outs []core.Output
  124. out0 := core.Output{
  125. Value: 10,
  126. }
  127. out1 := core.Output{
  128. Value: 90,
  129. }
  130. outs = append(outs, out0)
  131. outs = append(outs, out1)
  132. tx := core.NewTx(&privK.PublicKey, &pubK0, ins, outs)
  133. // verify tx
  134. txVerified := core.CheckTx(tx)
  135. assert.True(t, txVerified)
  136. // then create a new block with the tx and add it to the blockchain
  137. var txs []core.Tx
  138. txs = append(txs, *tx)
  139. block, err := node.NewBlock(txs)
  140. assert.Nil(t, err)
  141. err = node.Bc.AddBlock(block)
  142. assert.Nil(t, err)
  143. // add another tx sending coins to the pubK1
  144. privK1, err := core.NewKey()
  145. assert.Nil(t, err)
  146. pubK1 := privK1.PublicKey
  147. ins = []core.Input{}
  148. in = core.Input{
  149. TxId: block.Txs[0].TxId,
  150. Vout: 0,
  151. Value: 10,
  152. }
  153. ins = append(ins, in)
  154. outs = []core.Output{}
  155. out0 = core.Output{
  156. Value: 10,
  157. }
  158. outs = append(outs, out0)
  159. tx = core.NewTx(&pubK0, &pubK1, ins, outs)
  160. // verify tx
  161. txVerified = core.CheckTx(tx)
  162. assert.True(t, txVerified)
  163. // then create a new block with the tx and add it to the blockchain
  164. txs = []core.Tx{}
  165. txs = append(txs, *tx)
  166. block, err = node.NewBlock(txs)
  167. assert.Nil(t, err)
  168. err = node.Bc.AddBlock(block)
  169. assert.Nil(t, err)
  170. }