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.

78 lines
2.0 KiB

  1. package node
  2. import (
  3. "io/ioutil"
  4. "testing"
  5. "github.com/arnaucube/slowlorisdb/core"
  6. "github.com/arnaucube/slowlorisdb/db"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestNode(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. node, err := NewNode(db, uint64(1))
  15. assert.Nil(t, err)
  16. assert.Equal(t, node.Addr, core.AddressFromPrivK(node.PrivK))
  17. }
  18. func TestNodeSignature(t *testing.T) {
  19. dir, err := ioutil.TempDir("", "db")
  20. assert.Nil(t, err)
  21. db, err := db.New(dir)
  22. assert.Nil(t, err)
  23. node, err := NewNode(db, uint64(1))
  24. assert.Nil(t, err)
  25. m := []byte("test")
  26. sig, err := node.Sign(m)
  27. assert.Nil(t, err)
  28. pubK := node.PrivK.PublicKey
  29. assert.True(t, core.VerifySignature(&pubK, m, *sig))
  30. }
  31. func TestBlockFromPendingTxs(t *testing.T) {
  32. dir, err := ioutil.TempDir("", "db")
  33. assert.Nil(t, err)
  34. db, err := db.New(dir)
  35. assert.Nil(t, err)
  36. node, err := NewNode(db, uint64(1))
  37. assert.Nil(t, err)
  38. addr0 := core.Address(core.HashBytes([]byte("addr0")))
  39. addr1 := core.Address(core.HashBytes([]byte("addr1")))
  40. tx := core.NewTx(addr0, addr1, []core.Input{}, []core.Output{})
  41. node.AddToPendingTxs(*tx)
  42. block, err := node.BlockFromPendingTxs()
  43. assert.Nil(t, err)
  44. assert.True(t, core.CheckBlockPoW(block, node.Bc.Difficulty))
  45. assert.True(t, core.VerifyBlockSignature(&node.PrivK.PublicKey, block))
  46. }
  47. func TestBlockFromPendingTxsIteration(t *testing.T) {
  48. dir, err := ioutil.TempDir("", "db")
  49. assert.Nil(t, err)
  50. db, err := db.New(dir)
  51. assert.Nil(t, err)
  52. node, err := NewNode(db, uint64(1))
  53. assert.Nil(t, err)
  54. addr0 := core.Address(core.HashBytes([]byte("addr0")))
  55. addr1 := core.Address(core.HashBytes([]byte("addr1")))
  56. for i := 0; i < 10; i++ {
  57. tx := core.NewTx(addr0, addr1, []core.Input{}, []core.Output{})
  58. node.AddToPendingTxs(*tx)
  59. }
  60. block, err := node.BlockFromPendingTxs()
  61. assert.Nil(t, err)
  62. assert.True(t, core.CheckBlockPoW(block, node.Bc.Difficulty))
  63. assert.True(t, core.VerifyBlockSignature(&node.PrivK.PublicKey, block))
  64. }