forked from Fantom-foundation/Substate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
static_substate_db.go
139 lines (114 loc) · 4.21 KB
/
static_substate_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
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
package substate
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/urfave/cli/v2"
)
const substateNamespace = "substatedir"
var (
SubstateDbFlag = cli.StringFlag{
Name: "substate-db",
Usage: "Data directory for substate recorder/replayer",
}
substateDir = SubstateDbFlag.Value
staticSubstateDB *DB
RecordReplay bool = false
)
// Deprecated: Please use NewDb instead. This function
// relies on a global variable which will be removed in the future.
func OpenSubstateDB() {
fmt.Println("record-replay: OpenSubstateDB")
backend, err := rawdb.NewLevelDBDatabase(substateDir, 1024, 100, substateNamespace, false)
if err != nil {
panic(fmt.Errorf("error opening substate leveldb %s: %v", substateDir, err))
}
fmt.Println("record-replay: opened successfully")
staticSubstateDB = newSubstateDB(backend)
}
// Deprecated: Please use NewDb instead. This function
// relies on a global variable which will be removed in the future.
func OpenSubstateDBReadOnly() {
fmt.Println("record-replay: OpenSubstateDB")
backend, err := rawdb.NewLevelDBDatabase(substateDir, 1024, 100, substateNamespace, true)
if err != nil {
panic(fmt.Errorf("error opening substate leveldb %s: %v", substateDir, err))
}
staticSubstateDB = newSubstateDB(backend)
}
// Deprecated: Please use MakeDb instead. This function
// relies on a global variable which will be removed in the future.
func SetSubstateDbBackend(backend ethdb.Database) {
fmt.Println("record-replay: SetSubstateDB")
staticSubstateDB = newSubstateDB(backend)
}
// Deprecated: Please use NewDb or MakeDb to create new DB and call the method Close() from
// returned object. This function relies on a global variable which will be removed in the future.
func CloseSubstateDB() {
defer fmt.Println("record-replay: CloseSubstateDB")
err := staticSubstateDB.Close()
if err != nil {
panic(fmt.Errorf("error closing substate leveldb %s: %v", substateDir, err))
}
}
// Deprecated: Please use NewDb or MakeDb to create new DB and call the method Compact() from
// returned object. This function relies on a global variable which will be removed in the future.
func CompactSubstateDB() {
fmt.Println("record-replay: CompactSubstateDB")
// compact entire DB
err := staticSubstateDB.Compact(nil, nil)
if err != nil {
panic(fmt.Errorf("error compacting substate leveldb %s: %v", substateDir, err))
}
}
// Deprecated: Please use NewInMemoryDb instead. This function
// relies on a global variable which will be removed in the future.
func OpenFakeSubstateDB() {
backend := rawdb.NewMemoryDatabase()
staticSubstateDB = newSubstateDB(backend)
}
// Deprecated: Please use NewInMemoryDb to create new in-memory DB and call the method Close() from
// returned object. This function relies on a global variable which will be removed in the future.
func CloseFakeSubstateDB() {
staticSubstateDB.Close()
}
// Deprecated: This function relies on a global variable which will be removed in the future.
func SetSubstateDbFlags(ctx *cli.Context) {
substateDir = ctx.String(SubstateDbFlag.Name)
fmt.Printf("record-replay: --substatedir=%s\n", substateDir)
}
// Deprecated: This function relies on a global variable which will be removed in the future.
func SetSubstateDb(dir string) {
substateDir = dir
}
func HasCode(codeHash common.Hash) bool {
return staticSubstateDB.HasCode(codeHash)
}
func GetCode(codeHash common.Hash) []byte {
return staticSubstateDB.GetCode(codeHash)
}
func PutCode(code []byte) {
staticSubstateDB.PutCode(code)
}
func HasSubstate(block uint64, tx int) bool {
return staticSubstateDB.HasSubstate(block, tx)
}
func GetSubstate(block uint64, tx int) *Substate {
return staticSubstateDB.GetSubstate(block, tx)
}
func GetBlockSubstates(block uint64) map[int]*Substate {
return staticSubstateDB.GetBlockSubstates(block)
}
func PutSubstate(block uint64, tx int, substate *Substate) {
staticSubstateDB.PutSubstate(block, tx, substate)
}
func DeleteSubstate(block uint64, tx int) {
staticSubstateDB.DeleteSubstate(block, tx)
}
func GetFirstSubstate() *Substate {
return staticSubstateDB.GetFirstSubstate()
}
func GetLastSubstate() (*Substate, error) {
return staticSubstateDB.GetLastSubstate()
}