forked from bazo-blockchain/bazo-miner
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstorage.go
140 lines (131 loc) · 3.65 KB
/
storage.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
package storage
import (
"fmt"
"log"
"sync"
"time"
"github.com/bazo-blockchain/bazo-miner/protocol"
"github.com/boltdb/bolt"
)
var (
db *bolt.DB
logger *log.Logger
State = make(map[[32]byte]*protocol.Account)
RootKeys = make(map[[32]byte]*protocol.Account)
txMemPool = make(map[[32]byte]protocol.Transaction)
txINVALIDMemPool = make(map[[32]byte]protocol.Transaction)
bootstrapReceivedMemPool = make(map[[32]byte]protocol.Transaction)
DifferentSenders = make(map[[32]byte]uint32)
DifferentReceivers = make(map[[32]byte]uint32)
FundsTxBeforeAggregation = make([]*protocol.FundsTx, 0)
ReceivedBlockStash = make([]*protocol.Block, 0)
TxcntToTxMap = make(map[uint32][][32]byte)
AllClosedBlocksAsc []*protocol.Block
Bootstrap_Server string
averageTxSize float32 = 0
totalTransactionSize float32 = 0
nrClosedTransactions float32 = 0
openTxMutex = &sync.Mutex{}
openINVALIDTxMutex = &sync.Mutex{}
openFundsTxBeforeAggregationMutex = &sync.Mutex{}
txcntToTxMapMutex = &sync.Mutex{}
ReceivedBlockStashMutex = &sync.Mutex{}
)
const (
ERROR_MSG = "Initiate storage aborted: "
)
//Entry function for the storage package
func Init(dbname string, bootstrapIpport string) {
Bootstrap_Server = bootstrapIpport
logger = InitLogger()
var err error
db, err = bolt.Open(dbname, 0600, &bolt.Options{Timeout: 5 * time.Second})
if err != nil {
logger.Fatal(ERROR_MSG, err)
}
//Check if db file is empty for all non-bootstraping miners
//if ipport != BOOTSTRAP_SERVER_PORT {
// err := db.View(func(tx *bolt.Tx) error {
// err := tx.ForEach(func(name []byte, bkt *bolt.Bucket) error {
// err := bkt.ForEach(func(k, v []byte) error {
// if k != nil && v != nil {
// return errors.New("Non-empty database given.")
// }
// return nil
// })
// return err
// })
// return err
// })
//
// if err != nil {
// logger.Fatal(ERROR_MSG, err)
// }
//}
db.Update(func(tx *bolt.Tx) error {
_, err = tx.CreateBucket([]byte("openblocks"))
if err != nil {
return fmt.Errorf(ERROR_MSG+"Create bucket: %s", err)
}
return nil
})
db.Update(func(tx *bolt.Tx) error {
_, err = tx.CreateBucket([]byte("closedblocks"))
if err != nil {
return fmt.Errorf(ERROR_MSG+"Create bucket: %s", err)
}
return nil
})
db.Update(func(tx *bolt.Tx) error {
_, err = tx.CreateBucket([]byte("closedblockswithouttx"))
if err != nil {
return fmt.Errorf(ERROR_MSG+"Create bucket: %s", err)
}
return nil
})
db.Update(func(tx *bolt.Tx) error {
_, err = tx.CreateBucket([]byte("closedfunds"))
if err != nil {
return fmt.Errorf(ERROR_MSG+"Create bucket: %s", err)
}
return nil
})
db.Update(func(tx *bolt.Tx) error {
_, err = tx.CreateBucket([]byte("closedaccs"))
if err != nil {
return fmt.Errorf(ERROR_MSG+"Create bucket: %s", err)
}
return nil
})
db.Update(func(tx *bolt.Tx) error {
_, err = tx.CreateBucket([]byte("closedstakes"))
if err != nil {
return fmt.Errorf(ERROR_MSG+"Create bucket: %s", err)
}
return nil
})
db.Update(func(tx *bolt.Tx) error {
_, err = tx.CreateBucket([]byte("closedaggregations"))
if err != nil {
return fmt.Errorf(ERROR_MSG+"Create bucket: %s", err)
}
return nil
})
db.Update(func(tx *bolt.Tx) error {
_, err = tx.CreateBucket([]byte("closedconfigs"))
if err != nil {
return fmt.Errorf(ERROR_MSG+"Create bucket: %s", err)
}
return nil
})
db.Update(func(tx *bolt.Tx) error {
_, err = tx.CreateBucket([]byte("lastclosedblock"))
if err != nil {
return fmt.Errorf(ERROR_MSG+"Create bucket: %s", err)
}
return nil
})
}
func TearDown() {
db.Close()
}