This repository has been archived by the owner on Apr 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
107 lines (89 loc) · 2.65 KB
/
db.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
package main
import (
"fmt"
"time"
"github.com/dgraph-io/badger/v2"
"github.com/dgraph-io/badger/v2/options"
"github.com/iotaledger/hive.go/daemon"
"github.com/iotaledger/hive.go/database"
"github.com/iotaledger/hive.go/lru_cache"
"github.com/iotaledger/hive.go/timeutil"
"github.com/iotaledger/iota.go/trinary"
)
var spentAddrCache *lru_cache.LRUCache
func initCache() {
spentAddrCache = lru_cache.NewLRUCache(config.GetInt("cache.size"), &lru_cache.LRUCacheOptions{
EvictionCallback: onEvictSpentAddresses,
EvictionBatchSize: config.GetUint64("cache.batchEvictionSize"),
})
}
func getDatabaseOpts(dbDir string) badger.Options {
opts := badger.DefaultOptions(dbDir)
opts.CompactL0OnClose = false
opts.KeepL0InMemory = false
opts.VerifyValueChecksum = false
opts.ZSTDCompressionLevel = 1
opts.Compression = options.None
opts.MaxCacheSize = 50000000
opts.Truncate = false
opts.EventLogging = false
opts.Logger = nil
return opts
}
func spawnBadgerGC() {
if err := daemon.BackgroundWorker("badger-db-gc", func(shutdownSignal <-chan struct{}) {
timeutil.Ticker(func() {
db := database.GetBadgerInstance()
log.Info("running BadgerDB garbage collection")
var err error
for err == nil {
err = db.RunValueLogGC(0.7)
}
}, 5*time.Minute, shutdownSignal)
}, PriorityBadgerGC); err != nil {
log.Fatal(err)
}
}
func wasAddressSpentFrom(address trinary.Hash) (result bool, err error) {
if spentAddrCache.Contains(address) {
result = true
} else {
result, err = spentDatabaseContainsAddress(address)
}
return
}
func onEvictSpentAddresses(keys interface{}, _ interface{}) {
keyT := keys.([]interface{})
var addresses []trinary.Hash
for _, obj := range keyT {
addresses = append(addresses, obj.(trinary.Hash))
}
if err := storeSpentAddressesInDatabase(addresses); err != nil {
panic(err)
}
}
func databaseKeyForAddress(address trinary.Hash) []byte {
return trinary.MustTrytesToBytes(address)
}
func spentDatabaseContainsAddress(address trinary.Hash) (bool, error) {
if contains, err := db.Contains(databaseKeyForAddress(address)); err != nil {
return contains, fmt.Errorf("failed to check if the address exists in the spent addresses database: %w", err)
} else {
return contains, nil
}
}
func storeSpentAddressesInDatabase(spent []trinary.Hash) error {
var entries []database.Entry
for _, address := range spent {
key := databaseKeyForAddress(address)
entries = append(entries, database.Entry{
Key: key,
Value: []byte{},
})
}
// Now batch insert/delete all entries
if err := db.Apply(entries, []database.Key{}); err != nil {
return fmt.Errorf("failed to mark addresses as spent: %w", err)
}
return nil
}