This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
app_currency_test.go
117 lines (91 loc) · 3.71 KB
/
app_currency_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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package lazyledger
import (
"crypto/rand"
"encoding/binary"
"testing"
"github.com/libp2p/go-libp2p-crypto"
)
func TestAppCurrencySimpleBlock(t *testing.T) {
bs := NewSimpleBlockStore()
b := NewBlockchain(bs)
sb := NewSimpleBlock([]byte{0})
ms := NewSimpleMap()
app := NewCurrency(ms, b)
b.RegisterApplication(&app)
privA, pubA, _ := crypto.GenerateSecp256k1Key(rand.Reader)
_, pubB, _ := crypto.GenerateSecp256k1Key(rand.Reader)
pubABytes, _ := pubA.Bytes()
pubABalanceBytes := make([]byte, binary.MaxVarintLen64)
binary.BigEndian.PutUint64(pubABalanceBytes, 1000)
ms.Put(pubABytes, pubABalanceBytes)
sb.AddMessage(app.(*Currency).GenerateTransaction(privA, pubB, 100, nil))
b.ProcessBlock(sb)
if app.(*Currency).Balance(pubA) != 900 || app.(*Currency).Balance(pubB) != 100 {
t.Error("test tranasaction failed: invalid post-balances")
}
}
func TestAppCurrencySimpleBlockDependency(t *testing.T) {
bs := NewSimpleBlockStore()
b := NewBlockchain(bs)
sb := NewSimpleBlock([]byte{0})
ms := NewSimpleMap()
app := NewCurrency(ms, b)
b.RegisterApplication(&app)
privA, pubA, _ := crypto.GenerateSecp256k1Key(rand.Reader)
_, pubB, _ := crypto.GenerateSecp256k1Key(rand.Reader)
pubABytes, _ := pubA.Bytes()
pubABalanceBytes := make([]byte, binary.MaxVarintLen64)
binary.BigEndian.PutUint64(pubABalanceBytes, 1000)
ms.Put(pubABytes, pubABalanceBytes)
sb.AddMessage(*NewMessage([namespaceSize]byte{0}, []byte("foo")))
hash, _, _ := sb.ProveDependency(0)
sb.AddMessage(app.(*Currency).GenerateTransaction(privA, pubB, 100, hash))
_, proof, _ := sb.ProveDependency(0)
sb.VerifyDependency(0, hash, proof)
b.ProcessBlock(sb)
if app.(*Currency).Balance(pubA) != 900 || app.(*Currency).Balance(pubB) != 100 {
t.Error("test tranasaction failed: invalid post-balances")
}
}
func TestAppCurrencyProbabilisticBlockDependency(t *testing.T) {
bs := NewSimpleBlockStore()
b := NewBlockchain(bs)
pb := NewProbabilisticBlock([]byte{0}, 512)
ms := NewSimpleMap()
app := NewCurrency(ms, b)
b.RegisterApplication(&app)
privA, pubA, _ := crypto.GenerateSecp256k1Key(rand.Reader)
_, pubB, _ := crypto.GenerateSecp256k1Key(rand.Reader)
pubABytes, _ := pubA.Bytes()
pubABalanceBytes := make([]byte, binary.MaxVarintLen64)
binary.BigEndian.PutUint64(pubABalanceBytes, 1000)
ms.Put(pubABytes, pubABalanceBytes)
pb.AddMessage(*NewMessage([namespaceSize]byte{0}, []byte("foo")))
hash, _, _ := pb.ProveDependency(0)
pb.AddMessage(app.(*Currency).GenerateTransaction(privA, pubB, 100, hash))
_, proof, _ := pb.ProveDependency(0)
pb.VerifyDependency(0, hash, proof)
b.ProcessBlock(pb)
if app.(*Currency).Balance(pubA) != 900 || app.(*Currency).Balance(pubB) != 100 {
t.Error("test tranasaction failed: invalid post-balances")
}
}
func TestAppCurrencyProbabilisticBlock(t *testing.T) {
bs := NewSimpleBlockStore()
b := NewBlockchain(bs)
pb := NewProbabilisticBlock([]byte{0}, 512)
ms := NewSimpleMap()
app := NewCurrency(ms, b)
b.RegisterApplication(&app)
privA, pubA, _ := crypto.GenerateSecp256k1Key(rand.Reader)
_, pubB, _ := crypto.GenerateSecp256k1Key(rand.Reader)
pubABytes, _ := pubA.Bytes()
pubABalanceBytes := make([]byte, binary.MaxVarintLen64)
binary.BigEndian.PutUint64(pubABalanceBytes, 1000)
ms.Put(pubABytes, pubABalanceBytes)
pb.AddMessage(app.(*Currency).GenerateTransaction(privA, pubB, 100, nil))
b.ProcessBlock(pb)
if app.(*Currency).Balance(pubA) != 900 || app.(*Currency).Balance(pubB) != 100 {
t.Error("test tranasaction failed: invalid post-balances")
}
}