-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·146 lines (124 loc) · 3.41 KB
/
main.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
/*
Copyright 2023 Milan Suk
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"log"
"os"
"runtime"
"strconv"
"time"
)
func main() {
InitBLS()
//MinerTest()
//file paths
os.Mkdir("data", os.ModePerm)
dbPathA := "data/dbA.sqlite"
dbPathB := "data/dbB.sqlite"
genesisPath := "data/genesis.bin"
txnsPath := "data/txns_"
blocksPath := "data/blocks.bin"
const NUMBER_TXNS = 40000
const NUMBER_TXNS_IN_BLOCK = 10000
// inits Db
var genesis_amount int64
var genesis_privKey BLSPrivKey
err := Client_getOrGenerateGenesis(genesisPath, &genesis_amount, &genesis_privKey)
if err != nil {
log.Printf("Client_getOrGenerateGenesis() failed: %v\n", err)
return
}
var genesis_pubKey BLSPubKey
genesis_privKey.ExportPublicKey(&genesis_pubKey)
// generates txns into write them into file
{
if !OsFileExists(txnsPath + "0") {
err = Client_generateTxnsFile(NUMBER_TXNS, NUMBER_TXNS_IN_BLOCK, genesis_amount, &genesis_privKey, txnsPath)
if err != nil {
log.Printf("Client_generateTxnsFile() failed: %v\n", err)
return
}
}
}
const PORT = 4879
// recvs txns and build blocks
{
OsFileRemove(dbPathA)
OsFileRemove(blocksPath)
node, err := NewNode(false, PORT, dbPathA, NUMBER_TXNS_IN_BLOCK, genesis_amount, &genesis_pubKey, blocksPath) //blocksPath=write blocks into file
if err != nil {
log.Printf("NewNode() failed: %v\n", err)
return
}
time.Sleep(100 * time.Millisecond)
var conns []*Connections
for i := 0; i < runtime.NumCPU(); i++ {
conns = append(conns, NewConnections())
err = conns[i].Add("localhost", PORT, "data", false)
if err != nil {
log.Printf("onnections.Add() failed: %v\n", err)
return
}
}
n, err := Client_sendTxns(conns[0], txnsPath+"0")
if err != nil {
log.Printf("Client_sendTxns() failed: %v\n", err)
return
}
nn := n
for node.stat.sum_txns < nn {
time.Sleep(1 * time.Millisecond)
}
for bi := 0; bi < (NUMBER_TXNS/NUMBER_TXNS_IN_BLOCK)-1; bi++ {
time.Sleep(1 * time.Second) //? ...
n, err := Client_sendTxnsMT(conns, txnsPath+strconv.Itoa(bi+1))
if err != nil {
log.Printf("Client_sendTxns() failed: %v\n", err)
return
}
nn += n
for node.stat.sum_txns < nn {
time.Sleep(1 * time.Millisecond)
}
}
for _, c := range conns {
c.Destroy()
}
node.Destroy()
}
// recvs blocks and verify them
{
OsFileRemove(dbPathB)
node, err := NewNode(false, PORT, dbPathB, NUMBER_TXNS_IN_BLOCK, genesis_amount, &genesis_pubKey, "")
if err != nil {
log.Printf("NewNode() failed: %v\n", err)
return
}
conns := NewConnections()
err = conns.Add("localhost", PORT, "data", false)
if err != nil {
log.Printf("Connections.Add() failed: %v\n", err)
return
}
n, err := Client_sendBlocks(conns, blocksPath)
if err != nil {
log.Printf("Client_sendBlocks() failed: %v\n", err)
return
}
for node.stat.num_blocks < n {
time.Sleep(1 * time.Millisecond)
}
conns.Destroy()
node.Destroy()
}
}