forked from pony-maggie/blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchain_test.go
80 lines (52 loc) · 1.26 KB
/
blockchain_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"fmt"
"testing"
)
func TestValidNodes(t *testing.T) {
blockchainIns := NewBlockchain()
blockchainIns.Register_Node("http://127.0.0.1:8002")
neighbours := blockchainIns.nodes
found := false
for _, node := range neighbours {
fmt.Println("node:", node)
if node == "127.0.0.1:8002" {
found = true
}
}
if !found {
t.Error("not found added node")
}
}
func TestCreateBlock(t *testing.T) {
blockchainIns := NewBlockchain()
blockchainIns.New_Block(123, "abc")
last_block := blockchainIns.last_block()
if len(blockchainIns.chain) != 2 {
t.Error("error length at genesis block")
}
if last_block.index != 2 {
t.Error("genesis block index must be 2")
}
if last_block.proof != 123 {
t.Error("wrong proof")
}
if last_block.previous_hash != "abc" {
t.Error("wrong previous hash")
}
}
func TestCreateTransaction(t *testing.T) {
blockchainIns := NewBlockchain()
blockchainIns.New_Transaction("aa", "bb", 10)
height := len(blockchainIns.current_transactions)
transaction := blockchainIns.current_transactions[height-1]
if transaction.Sender != "aa" {
t.Error("wrong sender")
}
if transaction.Recipient != "bb" {
t.Error("wrong recipient")
}
if transaction.Amount != 10 {
t.Error("wrong amount")
}
}