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.

71 lines
1.6 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. blockParsed, err := BlockFromBytes(block.Bytes())
  18. assert.Nil(t, err)
  19. assert.Equal(t, blockParsed.Bytes(), block.Bytes())
  20. blockCopy := block.Copy()
  21. assert.Equal(t, blockCopy.Bytes(), block.Bytes())
  22. difficulty := uint64(2)
  23. nonce, err := CalculatePoW(block, difficulty)
  24. assert.Nil(t, err)
  25. block.Nonce = nonce
  26. h := HashBytes(block.Bytes())
  27. // CheckPoW
  28. assert.True(t, CheckPoW(h, difficulty))
  29. }
  30. func TestNewBlock(t *testing.T) {
  31. block := &Block{
  32. PrevHash: HashBytes([]byte("prevhash")),
  33. NextHash: HashBytes([]byte("nextHash")),
  34. Txs: []Tx{},
  35. Miner: Address(HashBytes([]byte("addrfromminer"))),
  36. Timestamp: time.Now(),
  37. Nonce: 0,
  38. Hash: HashBytes([]byte("blockhash")),
  39. }
  40. block2, err := BlockFromBytes(block.Bytes())
  41. assert.Nil(t, err)
  42. assert.Equal(t, block2.Bytes(), block.Bytes())
  43. difficulty := uint64(2)
  44. nonce, err := CalculatePoW(block, difficulty)
  45. assert.Nil(t, err)
  46. block.Nonce = nonce
  47. h := HashBytes(block.Bytes())
  48. // CheckPoW
  49. assert.True(t, CheckPoW(h, difficulty))
  50. }
  51. func TestTx(t *testing.T) {
  52. addr0 := Address(HashBytes([]byte("addr0")))
  53. addr1 := Address(HashBytes([]byte("addr1")))
  54. tx := NewTx(addr0, addr1, []Input{}, []Output{})
  55. assert.Equal(t, tx.From, addr0)
  56. assert.Equal(t, tx.To, addr1)
  57. }