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