-
Notifications
You must be signed in to change notification settings - Fork 0
/
tangle.go
160 lines (124 loc) · 3.61 KB
/
tangle.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
package tangle
import (
"errors"
"os"
"path"
"go.etcd.io/bbolt"
"github.com/iotaledger/hive.go/kvstore"
"github.com/iotaledger/hive.go/kvstore/bolt"
"github.com/gohornet/hornet/pkg/profile"
)
const (
TangleDbFilename = "tangle.db"
SnapshotDbFilename = "snapshot.db"
SpentAddressesDbFilename = "spent.db"
)
var (
dbDir string
tangleDb *bbolt.DB
snapshotDb *bbolt.DB
spentDb *bbolt.DB
ErrNothingToCleanUp = errors.New("Nothing to clean up in the databases")
)
func boltDB(directory string, filename string) *bbolt.DB {
opts := &bbolt.Options{
NoSync: true,
}
db, err := bolt.CreateDB(directory, filename, opts)
if err != nil {
panic(err)
}
return db
}
func ConfigureDatabases(directory string) {
dbDir = directory
tangleDb = boltDB(directory, TangleDbFilename)
tangleStore := bolt.New(tangleDb)
snapshotDb = boltDB(directory, SnapshotDbFilename)
snapshotStore := bolt.New(snapshotDb)
spentDb = boltDB(directory, SpentAddressesDbFilename)
spentStore := bolt.New(spentDb)
ConfigureStorages(tangleStore, snapshotStore, spentStore, profile.LoadProfile().Caches)
}
func ConfigureStorages(tangleStore kvstore.KVStore, snapshotStore kvstore.KVStore, spentStore kvstore.KVStore, caches profile.Caches) {
configureHealthStore(tangleStore)
configureTransactionStorage(tangleStore, caches.Transactions)
configureBundleTransactionsStorage(tangleStore, caches.BundleTransactions)
configureBundleStorage(tangleStore, caches.Bundles)
configureApproversStorage(tangleStore, caches.Approvers)
configureTagsStorage(tangleStore, caches.Tags)
configureAddressesStorage(tangleStore, caches.Addresses)
configureMilestoneStorage(tangleStore, caches.Milestones)
configureUnconfirmedTxStorage(tangleStore, caches.UnconfirmedTx)
configureLedgerStore(tangleStore)
configureSnapshotStore(snapshotStore)
configureSpentAddressesStorage(spentStore, caches.SpentAddresses)
}
func FlushStorages() {
FlushMilestoneStorage()
FlushBundleStorage()
FlushBundleTransactionsStorage()
FlushTransactionStorage()
FlushApproversStorage()
FlushTagsStorage()
FlushAddressStorage()
FlushUnconfirmedTxsStorage()
FlushSpentAddressesStorage()
}
func ShutdownStorages() {
ShutdownMilestoneStorage()
ShutdownBundleStorage()
ShutdownBundleTransactionsStorage()
ShutdownTransactionStorage()
ShutdownApproversStorage()
ShutdownTagsStorage()
ShutdownAddressStorage()
ShutdownUnconfirmedTxsStorage()
ShutdownSpentAddressesStorage()
}
func LoadInitialValuesFromDatabase() {
loadSnapshotInfo()
loadSolidEntryPoints()
}
func CloseDatabases() error {
if err := tangleDb.Sync(); err != nil {
return err
}
if err := tangleDb.Close(); err != nil {
return err
}
if err := snapshotDb.Sync(); err != nil {
return err
}
if err := snapshotDb.Close(); err != nil {
return err
}
if err := spentDb.Sync(); err != nil {
return err
}
if err := spentDb.Close(); err != nil {
return err
}
return nil
}
func DatabaseSupportsCleanup() bool {
// Bolt does not support cleaning up anything
return false
}
func CleanupDatabases() error {
// Bolt does not support cleaning up anything
return ErrNothingToCleanUp
}
// GetDatabaseSizes returns the size of the different databases.
func GetDatabaseSizes() (tangle int64, snapshot int64, spent int64) {
if tangleDbFile, err := os.Stat(path.Join(dbDir, TangleDbFilename)); err == nil {
tangle = tangleDbFile.Size()
}
if snapshotDbFile, err := os.Stat(path.Join(dbDir, SnapshotDbFilename)); err == nil {
snapshot = snapshotDbFile.Size()
}
if spentDbFile, err := os.Stat(path.Join(dbDir, SpentAddressesDbFilename)); err == nil {
spent = spentDbFile.Size()
}
return
}