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