slowlorisdb/node/node_test.go
node/node_test.goRaw
1package node
2
3import (
4 "crypto/ecdsa"
5 "io/ioutil"
6 "testing"
7
8 "github.com/arnaucube/slowlorisdb/core"
9 "github.com/arnaucube/slowlorisdb/db"
10 "github.com/stretchr/testify/assert"
11)
12
13func newTestPoABlockchain() (*ecdsa.PrivateKey, *core.Blockchain, error) {
14 dir, err := ioutil.TempDir("", "db")
15 if err != nil {
16 return nil, nil, err
17 }
18 db, err := db.New(dir)
19 if err != nil {
20 return nil, nil, err
21 }
22
23 privK, err := core.NewKey()
24 if err != nil {
25 return nil, nil, err
26 }
27
28 var authNodes []*ecdsa.PublicKey
29 authNodes = append(authNodes, &privK.PublicKey)
30
31 bc := core.NewPoABlockchain(db, authNodes)
32 return privK, bc, nil
33}
34func TestNode(t *testing.T) {
35 dir, err := ioutil.TempDir("", "db")
36 assert.Nil(t, err)
37 db, err := db.New(dir)
38 assert.Nil(t, err)
39
40 privK, err := core.NewKey()
41 assert.Nil(t, err)
42
43 dif := uint64(1)
44 bc := core.NewBlockchain(db, dif)
45 node, err := NewNode(privK, bc, true)
46 assert.Nil(t, err)
47
48 assert.Equal(t, node.Addr, core.AddressFromPrivK(node.PrivK))
49}
50
51func TestNodeSignature(t *testing.T) {
52 dir, err := ioutil.TempDir("", "db")
53 assert.Nil(t, err)
54 db, err := db.New(dir)
55 assert.Nil(t, err)
56
57 privK, err := core.NewKey()
58 assert.Nil(t, err)
59
60 dif := uint64(1)
61 bc := core.NewBlockchain(db, dif)
62 node, err := NewNode(privK, bc, true)
63 assert.Nil(t, err)
64
65 m := []byte("test")
66 sig, err := node.Sign(m)
67 assert.Nil(t, err)
68 pubK := node.PrivK.PublicKey
69 assert.True(t, core.VerifySignature(&pubK, m, *sig))
70}
71
72func TestBlockFromPendingTxs(t *testing.T) {
73 privK, bc, err := newTestPoABlockchain()
74 assert.Nil(t, err)
75
76 node, err := NewNode(privK, bc, true)
77 assert.Nil(t, err)
78
79 privK0, err := core.NewKey()
80 assert.Nil(t, err)
81 pubK0 := privK0.PublicKey
82 privK1, err := core.NewKey()
83 assert.Nil(t, err)
84 pubK1 := privK1.PublicKey
85 tx := core.NewTx(&pubK0, &pubK1, []core.Input{}, []core.Output{})
86 node.AddToPendingTxs(*tx)
87 block, err := node.BlockFromPendingTxs()
88 assert.Nil(t, err)
89 assert.True(t, core.CheckBlockPoW(block, node.Bc.Difficulty))
90 assert.True(t, node.Bc.VerifyBlock(block))
91}
92
93func TestBlockFromPendingTxsIteration(t *testing.T) {
94 privK, bc, err := newTestPoABlockchain()
95 assert.Nil(t, err)
96
97 node, err := NewNode(privK, bc, true)
98 assert.Nil(t, err)
99
100 privK0, err := core.NewKey()
101 assert.Nil(t, err)
102 pubK0 := privK0.PublicKey
103 privK1, err := core.NewKey()
104 assert.Nil(t, err)
105 pubK1 := privK1.PublicKey
106
107 for i := 0; i < 10; i++ {
108 tx := core.NewTx(&pubK0, &pubK1, []core.Input{}, []core.Output{})
109 node.AddToPendingTxs(*tx)
110 }
111 block, err := node.BlockFromPendingTxs()
112 assert.Nil(t, err)
113 assert.True(t, core.CheckBlockPoW(block, node.Bc.Difficulty))
114 assert.True(t, node.Bc.VerifyBlock(block))
115}
116
117func TestFromGenesisToTenBlocks(t *testing.T) {
118 privK, bc, err := newTestPoABlockchain()
119 assert.Nil(t, err)
120
121 node, err := NewNode(privK, bc, true)
122 assert.Nil(t, err)
123
124 // create the genesis block
125 genesisBlock, err := node.CreateGenesis(&privK.PublicKey, uint64(100))
126 assert.Nil(t, err)
127 assert.NotEqual(t, genesisBlock.Signature, core.Signature{})
128 assert.NotEqual(t, genesisBlock.Hash, core.Hash{})
129 assert.True(t, node.Bc.VerifyBlock(genesisBlock))
130
131 // add the genesis block into the blockchain
132 err = node.Bc.AddBlock(genesisBlock)
133 assert.Nil(t, err)
134 assert.NotEqual(t, genesisBlock.Hash, core.Hash{})
135 assert.Equal(t, genesisBlock.Hash, node.Bc.LastBlock.Hash)
136
137 // add another tx sending coins to the pubK0
138 privK0, err := core.NewKey()
139 assert.Nil(t, err)
140 pubK0 := privK0.PublicKey
141
142 var ins []core.Input
143 in := core.Input{
144 TxId: genesisBlock.Txs[0].TxId,
145 Vout: 0,
146 Value: 100,
147 }
148 ins = append(ins, in)
149 var outs []core.Output
150 out0 := core.Output{
151 Value: 10,
152 }
153 out1 := core.Output{
154 Value: 90,
155 }
156 outs = append(outs, out0)
157 outs = append(outs, out1)
158 tx := core.NewTx(&privK.PublicKey, &pubK0, ins, outs)
159
160 // verify tx
161 txVerified := core.CheckTx(tx)
162 assert.True(t, txVerified)
163
164 // then create a new block with the tx and add it to the blockchain
165 var txs []core.Tx
166 txs = append(txs, *tx)
167 block, err := node.NewBlock(txs)
168 assert.Nil(t, err)
169 err = node.Bc.AddBlock(block)
170 assert.Nil(t, err)
171
172 // add another tx sending coins to the pubK1
173 privK1, err := core.NewKey()
174 assert.Nil(t, err)
175 pubK1 := privK1.PublicKey
176
177 ins = []core.Input{}
178 in = core.Input{
179 TxId: block.Txs[0].TxId,
180 Vout: 0,
181 Value: 10,
182 }
183 ins = append(ins, in)
184 outs = []core.Output{}
185 out0 = core.Output{
186 Value: 10,
187 }
188 outs = append(outs, out0)
189 tx = core.NewTx(&pubK0, &pubK1, ins, outs)
190
191 // verify tx
192 txVerified = core.CheckTx(tx)
193 assert.True(t, txVerified)
194
195 // then create a new block with the tx and add it to the blockchain
196 txs = []core.Tx{}
197 txs = append(txs, *tx)
198 block, err = node.NewBlock(txs)
199 assert.Nil(t, err)
200 err = node.Bc.AddBlock(block)
201 assert.Nil(t, err)
202}