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