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