forked from febe19/bazo-miner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
260 lines (211 loc) · 10.9 KB
/
main_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package miner
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"fmt"
"github.com/julwil/bazo-miner/crypto"
"io/ioutil"
"log"
"math/big"
"os"
"testing"
"github.com/julwil/bazo-miner/p2p"
"github.com/julwil/bazo-miner/protocol"
"github.com/julwil/bazo-miner/storage"
)
const (
TestDBFileName = "test.db"
TestIpPort = "127.0.0.1:8000"
TestKeyFileName = "test_root"
)
const (
PubA1 = "c2be9abbeaec39a066c2a09cee23bb9ab2a0b88f2880b1e785b4d317adf0dc7c"
PubA2 = "8ce020fde838d9c443f6c93345dafe7fd74f091c4d2f30b37e2453679a257ed5"
PrivA = "ba127fa8f802b008b9cdb58f4e44809d48f1b000cff750dda9cd6b312395c1c5"
CommPubA = "vsl0yfAd3dqJfDEawAl7Xp2/hOvXGN/u0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8"
PubB2 = "ab301a6a77b201c416ddc13a2d33fdf200a5302f6f687e0ea09085debaf8a1d9"
PrivB = "7a0a9babcc97ea7991ed67ed7f800f70c5e04e99718960ad8efab2ca052f00c7"
CommPubB = "n7vb+4YNgTDwjJ1St3/UQP+bXrN/mMmsPgTjKthIMpoMYN7mRhpk6/MGa6Gv0p1Zbw39g6fVsluHSXvyYO6VmsahTQ0gI9MEmxgKt4c6ZQct6M+kWP7E3omXT68NsXXXaZBjBuewfHrJReTz/znbS66HgY8BML55YDRKQBsmDz+cb/H6FWT7/mmPBRXufz7sf6OqvwiMRGXNlRbktbEn3gpumXpndlGhmGL0ZVZj2VklqWSHtgsfBut+rov7uuIN28StPZYZvllnCCvP1DHeImExWHOltWTnZAE0pRUbaX3q3NVAqU4ngL1sbkMSghF8bmz8G26qawM7YNiiDrAmcQ=="
CommPrivB = "P0og9Hz99tVcSmq/boOQpxxgBFrc0L3/qCcplz1RBfOxueQ3m0kz+aU2QwkycCH2YKFLdJHYgy3u4bfhpnSCBGx1VuE/fdJLfeQ9wtAq3ALHNvqm5Lg1avNbZ7A1nb3SVzplckP00q2X+ECqSNM0x7zkZfoyf4zI7MxrKxFWuC1c1BT7zj7EUT1idG+n/yz3WCx4Xr+4XM3CIt1dTrddhCboLdLlNYCOIh4t5JSTfYysp8YR4FSc96vRVCe+QCVtMOfo7RCR8bcZDoIQjat+u5umnyAsyXLetBerh/MABqHq8wOgC6a8vCqRnyAwhLOT+VQbTbFMQzLO9Lw9T8v9EQ=="
CommPrim1B = "zzoomDPTH/WxxtqTIApnecinr+BuAhcxJephkDHOhlRWK1IH16yLIal9V6OmC/REGCLgZpJHzUeesATn7QnsTIFnmEDKxIPVk54etYAXJo8G51pB9mylTUJiXqY1hu5O1GSEgtD+EAdHrIRJZ2E7/Pyp/wFrLG5ymXULZ5BFicU="
CommPrim2B = "xVQiK60JgjOqSdQ6EKEjDxdxfWp1NDGpHbBLElzbqJyAHfd5KCPdwASLIR8V0WHIa12df877xGGL1W+SlXXXOsJaER+FfnlzxzaO5D8a3GqaYMJBYWyUBnf1f0/lgVvnJzh0hKHlKSlvJX2mbObD9mPeuYhEXNO1v7Vo0846sL0="
//Root account for testing
PubRoot1 = "6323cc034597195ae69bcfb628ecdffa5989c7503154c566bab4a87f3e9910ac"
PubRoot2 = "f6115b77a15852764c609c6a5c1739e698ebc6e49bf14617c561b9110039cec7"
PrivRoot = "277ed539f56122c25a6fc115d07d632b47e71416c9aebf1beb54ee704f11842c"
CommPubRoot = "1e2QBjDop/b9Gk4U1YUtxzTpDrMvFTNb4dFIm2mIxhimeiJtHKnc0xDR1LPqkHN9Ke+tCbg6T3csbONoj8NT+ePIYF97DuUUL9d0ok8QZaSoAOGVIQHLbdCE08zwq8qiwzFWsfJSyKVJe1Bwbjsp9OWaxHenA3f2SWALiK1ZHAA13YV+nxm5Jh2O4uSmmz3PLv7Iz7Lfpo1uhpa0qfWap8Eqsp1XSWj60yms+hfy3X/r57FrbHUjJqeVQUPOqPmRRl3r3j1P+l/b+WQNA0WYu1ArjI8T3BEohqLZW3tZcx4NssyVyiS59SU16Yu3qroAdkLnFP4YPBSgQhXRjVzt8w=="
CommPrivRoot = "jKphuoBsaw1wDdzrvB6PJF65JE5UFjeoIgswF+jD46YPyV1bq65RooN7xcXr5cHaujl76Vk3FkuBbbP2bBl+3WCWwC/oRboBlRex/IvKd1tWkQXDvmlkrzeeL3qhggSDE6AcpnN1VbPBZpFU7FaA1yQmqSsYKaK20jaSPvPlFRAllP1adSd+m3ZrJY5rPWzPkPDmeyLRhbTPMp2ke3gAVXn2JdX6hYwYBeZJv2ZnDM/ZQfmWezHJpjsaichnbB8mUiHOOqBnGXaHKKomgmveZ+UjLD7QN9x12NfRyhFM7Aih8iAgbK06CNzBMPvj4J3MGrJrZ1sjqpOw7ljLiGccGQ=="
CommPrimRoot1 = "/dCNZfqFkgE3360DnH+wE9eR1KL0xdjC3XY+0ge2rkg3XxJc2hZsv0MO2JiGuqQBsAfjEtJCmqayJaTemPMHBJABrhJnfLaDL2fHLRzwGzGYvEd2LVTGqOOW5+0qfimEV5dwnCVE7CcZ/uXwH0R2baQzWN2S29DxEq706Bhtpsc="
CommPrimRoot2 = "18UX/SiRcgUsjXlFsurz9Vjuh05vpREyn5wlNIHLjqmm5Rg/A37/gKiVls7o6CArM344Vhc6e5KpTVcM9FY496Xr/CNBA4ujl71hr93D6oCo50fLXdABoGFBjDP9d8eguK/Bi4L9q1MptTf2UM5JV/IkntVy1X4ff50uh1J5o3U="
//Multisig account for testing
MultiSigPub1 = "d5a0c62eeaf699eeba121f92e08becd38577f57b83eba981dc057e92fde1ad22"
MultiSigPub2 = "a480e4ee6ff8b4edbf9470631ec27d3b1eb27f210d5a994a7cbcffa3bfce958e"
MultiSigPriv = "b8d1fa3cc7476eafca970ea222676647da1817d1d9dc602e9446290454ffe1a4"
)
//Globally accessible values for all other tests, (root)account-related
var (
accA, accB, validatorAcc, multiSigAcc, rootAcc *protocol.Account
PrivKeyAccA, PrivKeyAccB, PrivKeyMultiSig, PrivKeyRoot *ecdsa.PrivateKey
CommPrivKeyAccA, CommPrivKeyAccB, CommPrivKeyRoot *rsa.PrivateKey
genesisBlock *protocol.Block
)
//Create some accounts that are used by the tests
func addTestingAccounts() {
accA, accB, validatorAcc, multiSigAcc = new(protocol.Account), new(protocol.Account), new(protocol.Account), new(protocol.Account)
pubAccA1, _ := new(big.Int).SetString(PubA1, 16)
pubAccA2, _ := new(big.Int).SetString(PubA2, 16)
privAccA, _ := new(big.Int).SetString(PrivA, 16)
pubKeyAccA := ecdsa.PublicKey{
elliptic.P256(),
pubAccA1,
pubAccA2,
}
PrivKeyAccA = &ecdsa.PrivateKey{
pubKeyAccA,
privAccA,
}
CommPrivKeyAccA, _ = crypto.CreateRSAPrivKeyFromBase64(CommPubA, CommPrivA, []string{CommPrim1A, CommPrim2A})
copy(accA.Address[0:32], PrivKeyAccA.PublicKey.X.Bytes())
copy(accA.Address[32:64], PrivKeyAccA.PublicKey.Y.Bytes())
copy(accA.CommitmentKey[:], CommPrivKeyAccA.PublicKey.N.Bytes())
hashAccA := protocol.SerializeHashContent(accA.Address)
pubAccB1, _ := new(big.Int).SetString(PubB1, 16)
pubAccB2, _ := new(big.Int).SetString(PubB2, 16)
privAccB, _ := new(big.Int).SetString(PrivB, 16)
pubKeyAccB := ecdsa.PublicKey{
elliptic.P256(),
pubAccB1,
pubAccB2,
}
PrivKeyAccB = &ecdsa.PrivateKey{
pubKeyAccB,
privAccB,
}
CommPrivKeyAccB, _ = crypto.CreateRSAPrivKeyFromBase64(CommPubB, CommPrivB, []string{CommPrim1B, CommPrim2B})
copy(accB.Address[0:32], PrivKeyAccB.PublicKey.X.Bytes())
copy(accB.Address[32:64], PrivKeyAccB.PublicKey.Y.Bytes())
copy(accB.CommitmentKey[:], CommPrivKeyAccB.PublicKey.N.Bytes())
hashAccB := protocol.SerializeHashContent(accB.Address)
privMultiSig, _ := new(big.Int).SetString(MultiSigPriv, 16)
pubKeyMultiSig, _ := crypto.GetPubKeyFromString(MultiSigPub1, MultiSigPub2)
PrivKeyMultiSig = &ecdsa.PrivateKey{
*pubKeyMultiSig,
privMultiSig,
}
copy(multiSigAcc.Address[0:32], PrivKeyMultiSig.PublicKey.X.Bytes())
copy(multiSigAcc.Address[32:64], PrivKeyMultiSig.PublicKey.Y.Bytes())
hashMultiSig := protocol.SerializeHashContent(multiSigAcc.Address)
//Set the global variable in blockchain.go
multisigPubKey = pubKeyMultiSig
privKeyValidator, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
copy(validatorAcc.Address[:32], privKeyValidator.X.Bytes())
copy(validatorAcc.Address[32:64], privKeyValidator.Y.Bytes())
hashValidator := protocol.SerializeHashContent(validatorAcc.Address)
//Create and store an initial commitment key for the validator account.
commPrivKeyValidator, _ := rsa.GenerateMultiPrimeKey(rand.Reader, crypto.COMM_NOF_PRIMES, crypto.COMM_KEY_BITS)
copy(validatorAcc.CommitmentKey[:], commPrivKeyValidator.PublicKey.N.Bytes()[:])
validatorAcc.Balance = activeParameters.StakingMinimum
validatorAcc.IsStaking = true
//Set the global variable in blockchain.go
validatorAccAddress = validatorAcc.Address
commPrivKey = commPrivKeyValidator
storage.State[hashAccA] = accA
storage.State[hashAccB] = accB
storage.State[hashMultiSig] = multiSigAcc
storage.State[hashValidator] = validatorAcc
}
//Create some root accounts that are used by the tests
func addRootAccounts() {
rootAcc = new(protocol.Account)
pubRoot1, _ := new(big.Int).SetString(PubRoot1, 16)
pubRoot2, _ := new(big.Int).SetString(PubRoot2, 16)
privRoot, _ := new(big.Int).SetString(PrivRoot, 16)
pubKeyRoot := ecdsa.PublicKey{
elliptic.P256(),
pubRoot1,
pubRoot2,
}
PrivKeyRoot = &ecdsa.PrivateKey{
pubKeyRoot,
privRoot,
}
copy(rootAcc.Address[:32], PrivKeyRoot.X.Bytes())
copy(rootAcc.Address[32:64], PrivKeyRoot.Y.Bytes())
hashRoot := protocol.SerializeHashContent(rootAcc.Address)
//Create root file
file, _ := os.Create(TestKeyFileName)
_, _ = file.WriteString(PubRoot1 + "\n")
_, _ = file.WriteString(PubRoot2 + "\n")
_, _ = file.WriteString(PrivRoot + "\n")
CommPrivKeyRoot, _ = crypto.CreateRSAPrivKeyFromBase64(CommPubRoot, CommPrivRoot, []string{CommPrimRoot1, CommPrimRoot2})
copy(rootAcc.CommitmentKey[:], CommPrivKeyRoot.PublicKey.N.Bytes()[:])
rootAcc.Balance = activeParameters.StakingMinimum
rootAcc.IsStaking = true
storage.State[hashRoot] = rootAcc
storage.RootKeys[hashRoot] = rootAcc
}
//The state changes (accounts, funds, system parameters etc.) need to be reverted before any new test starts
//So every test has the same view on the blockchain
func cleanAndPrepare() {
storage.DeleteAll()
tmpState := make(map[[32]byte]*protocol.Account)
tmpRootKeys := make(map[[32]byte]*protocol.Account)
storage.State = tmpState
storage.RootKeys = tmpRootKeys
lastBlock = nil
globalBlockCount = -1
localBlockCount = -1
//Prepare system parameters
targetTimes = []timerange{}
currentTargetTime = new(timerange)
target = append(target, 8)
var tmpSlice []Parameters
tmpSlice = append(tmpSlice, NewDefaultParameters())
slashingDict = make(map[[32]byte]SlashingProof)
parameterSlice = tmpSlice
activeParameters = &tmpSlice[0]
slashingDict = make(map[[32]byte]SlashingProof)
//Override some params to ensure tests work correctly.
activeParameters.numIncludedPrevProofs = 0
activeParameters.BlockReward = 1
activeParameters.SlashReward = 1
addTestingAccounts()
addRootAccounts()
genesisCommitmentProof, _ := crypto.SignMessageWithRSAKey(CommPrivKeyRoot, "0")
genesisBlock = newBlock([32]byte{}, genesisCommitmentProof, 0)
collectStatistics(genesisBlock)
if err := storage.WriteClosedBlock(genesisBlock); err != nil {
fmt.Printf("Error: %v\n", err)
}
if err := storage.WriteLastClosedBlock(genesisBlock); err != nil {
fmt.Printf("Error: %v\n", err)
}
//Some meaningful balance to simplify testing
//validatorAcc.Balance = 1000
accA.Balance = 123232345678
accB.Balance = 823237654321
accA.TxCnt = 0
accB.TxCnt = 0
}
func TestMain(m *testing.M) {
storage.Init(TestDBFileName, TestIpPort)
p2p.Init(TestIpPort)
cleanAndPrepare()
addTestingAccounts()
addRootAccounts()
//We don't want logging msgs when testing, we have designated messages
logger = log.New(nil, "", 0)
logger.SetOutput(ioutil.Discard)
retCode := m.Run()
//Teardown
storage.TearDown()
os.Remove(TestDBFileName)
os.Remove(TestKeyFileName)
os.Exit(retCode)
}