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.

59 lines
1.3 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. Txs: []Tx{},
  11. Miner: Address(HashBytes([]byte("addrfromminer"))),
  12. Timestamp: time.Now(),
  13. Nonce: 0,
  14. Hash: HashBytes([]byte("blockhash")),
  15. }
  16. blockParsed, err := BlockFromBytes(block.Bytes())
  17. assert.Nil(t, err)
  18. assert.Equal(t, blockParsed.Bytes(), block.Bytes())
  19. blockCopy := block.Copy()
  20. assert.Equal(t, blockCopy.Bytes(), block.Bytes())
  21. difficulty := uint64(2)
  22. nonce, err := CalculatePoW(block, difficulty)
  23. assert.Nil(t, err)
  24. block.Nonce = nonce
  25. h := HashBytes(block.Bytes())
  26. // CheckPoW
  27. assert.True(t, CheckPoW(h, difficulty))
  28. }
  29. func TestNewBlock(t *testing.T) {
  30. block := &Block{
  31. PrevHash: HashBytes([]byte("prevhash")),
  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 := uint64(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. }