forked from truechain/truechain-consensus-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
85 lines (72 loc) · 2.02 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
package main
import (
"fmt"
"log"
"os"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/triedb"
)
// CheckErr simply checks errors and panics, as a guilty pleasure.
func CheckErr(e error) {
if e != nil {
panic(e)
}
}
// MakeDirIfNot handles dir creation operations
func MakeDirIfNot(dir string) bool {
_, err := os.Stat(dir)
if os.IsNotExist(err) {
err := os.Mkdir(dir, 0750)
CheckErr(err)
return true
}
return false
}
func NewTrieDB(N int) *trie.Trie {
var trieInstance *trie.Trie
dir := fmt.Sprintf("./data-%d", N)
created := MakeDirIfNot(dir)
db, err := rawdb.NewLevelDBDatabase(dir, 16, 16, "leveldb", false)
if err != nil {
log.Fatalf("Failed to create LevelDB database: %v", err)
}
diskdb := triedb.NewDatabase(db, triedb.HashDefaults)
trieInstance = trie.NewEmpty(diskdb)
if created {
// trieInstance = trie.NewEmpty(diskdb)
fmt.Println("Creating new trie")
} else {
}
// rootHash := storeNewRootHash(N, trieInstance)
// fmt.Printf("Trie hash: %x\n", rootHash)
// // print the root of the trie
// fmt.Printf("Trie root: %x\n", rootHash)
return trieInstance
}
func main() {
// Initialize the database
db := rawdb.NewMemoryDatabase()
// Create a new trie database
triedb := triedb.NewDatabase(db, triedb.HashDefaults)
// Initialize the state with an empty root
stateDB, err := state.New(common.Hash{}, state.NewDatabase(triedb), nil)
if err != nil {
log.Fatalf("Failed to create state: %v", err)
}
// Update the state with some data
address := common.HexToAddress("0x123456")
stateDB.AddBalance(address, 1000)
// Commit the state to the trie
root, err := stateDB.Commit(false)
if err != nil {
log.Fatalf("Failed to commit state: %v", err)
}
// Persist the trie to the database
if err := triedb.Commit(root, false); err != nil {
log.Fatalf("Failed to commit trie: %v", err)
}
fmt.Printf("State committed with root: %x\n", root)
}