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.

68 lines
1.5 KiB

  1. package core
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestBlock(t *testing.T) {
  8. block := &Block{
  9. PrevHash: HashBytes([]byte("prevhash")),
  10. NextHash: HashBytes([]byte("nextHash")),
  11. Txs: []Tx{},
  12. Miner: Address(HashBytes([]byte("addrfromminer"))),
  13. Timestamp: time.Now(),
  14. Nonce: 0,
  15. Hash: HashBytes([]byte("blockhash")),
  16. }
  17. block, err := BlockFromBytes(block.Bytes())
  18. assert.Nil(t, err)
  19. assert.Equal(t, block.Bytes(), block.Bytes())
  20. difficulty := 2
  21. nonce, err := CalculatePoW(block, difficulty)
  22. assert.Nil(t, err)
  23. block.Nonce = nonce
  24. h := HashBytes(block.Bytes())
  25. // CheckPoW
  26. assert.True(t, CheckPoW(h, difficulty))
  27. }
  28. func TestNewBlock(t *testing.T) {
  29. block := &Block{
  30. PrevHash: HashBytes([]byte("prevhash")),
  31. NextHash: HashBytes([]byte("nextHash")),
  32. Txs: []Tx{},
  33. Miner: Address(HashBytes([]byte("addrfromminer"))),
  34. Timestamp: time.Now(),
  35. Nonce: 0,
  36. Hash: HashBytes([]byte("blockhash")),
  37. }
  38. block2, err := BlockFromBytes(block.Bytes())
  39. assert.Nil(t, err)
  40. assert.Equal(t, block2.Bytes(), block.Bytes())
  41. difficulty := 2
  42. nonce, err := CalculatePoW(block, difficulty)
  43. assert.Nil(t, err)
  44. block.Nonce = nonce
  45. h := HashBytes(block.Bytes())
  46. // CheckPoW
  47. assert.True(t, CheckPoW(h, difficulty))
  48. }
  49. func TestTx(t *testing.T) {
  50. addr0 := Address(HashBytes([]byte("addr0")))
  51. addr1 := Address(HashBytes([]byte("addr1")))
  52. tx := NewTx(addr0, addr1, []Input{}, []Output{})
  53. assert.Equal(t, tx.From, addr0)
  54. assert.Equal(t, tx.To, addr1)
  55. }