Writing Ethereum genesis block and reading from it

This commit is contained in:
Alexey Akhunov 2018-06-18 22:10:29 +01:00
parent a80f473128
commit ee8d406f54
2 changed files with 65 additions and 7 deletions

41
Gopkg.lock generated
View File

@ -1,6 +1,12 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. # This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
branch = "master"
name = "github.com/aristanetworks/goarista"
packages = ["monotime"]
revision = "59944ff78bc1de686b0aba1444dfd380f48f03d4"
[[projects]] [[projects]]
branch = "master" branch = "master"
name = "github.com/btcsuite/btcd" name = "github.com/btcsuite/btcd"
@ -29,16 +35,28 @@
"common", "common",
"common/hexutil", "common/hexutil",
"common/math", "common/math",
"common/mclock",
"consensus",
"consensus/misc",
"core",
"core/rawdb",
"core/state", "core/state",
"core/types", "core/types",
"core/vm",
"crypto", "crypto",
"crypto/bn256",
"crypto/bn256/cloudflare",
"crypto/bn256/google",
"crypto/secp256k1", "crypto/secp256k1",
"crypto/sha3", "crypto/sha3",
"ethdb", "ethdb",
"event",
"log", "log",
"metrics", "metrics",
"p2p/netutil",
"params", "params",
"rlp", "rlp",
"rpc",
"trie" "trie"
] ]
revision = "dea1ce052a10cd7d401a5c04f83f371a06fe293c" revision = "dea1ce052a10cd7d401a5c04f83f371a06fe293c"
@ -124,6 +142,12 @@
revision = "645ef00459ed84a119197bfb8d8205042c6df63d" revision = "645ef00459ed84a119197bfb8d8205042c6df63d"
version = "v0.8.0" version = "v0.8.0"
[[projects]]
name = "github.com/rs/cors"
packages = ["."]
revision = "ca016a06a5753f8ba03029c0aa5e54afb1bf713f"
version = "v1.4.0"
[[projects]] [[projects]]
branch = "master" branch = "master"
name = "github.com/syndtr/goleveldb" name = "github.com/syndtr/goleveldb"
@ -214,7 +238,8 @@
"http2/hpack", "http2/hpack",
"idna", "idna",
"internal/timeseries", "internal/timeseries",
"trace" "trace",
"websocket"
] ]
revision = "db08ff08e8622530d9ed3a0e8ac279f6d4c02196" revision = "db08ff08e8622530d9ed3a0e8ac279f6d4c02196"
@ -268,15 +293,27 @@
revision = "5b3c4e850e90a4cf6a20ebd46c8b32a0a3afcb9e" revision = "5b3c4e850e90a4cf6a20ebd46c8b32a0a3afcb9e"
version = "v1.7.5" version = "v1.7.5"
[[projects]]
name = "gopkg.in/fatih/set.v0"
packages = ["."]
revision = "57907de300222151a123d29255ed17f5ed43fad3"
version = "v0.1.0"
[[projects]] [[projects]]
branch = "v2" branch = "v2"
name = "gopkg.in/karalabe/cookiejar.v2" name = "gopkg.in/karalabe/cookiejar.v2"
packages = ["collections/prque"] packages = ["collections/prque"]
revision = "8dcd6a7f4951f6ff3ee9cbb919a06d8925822e57" revision = "8dcd6a7f4951f6ff3ee9cbb919a06d8925822e57"
[[projects]]
branch = "v2"
name = "gopkg.in/natefinch/npipe.v2"
packages = ["."]
revision = "c1b8fa8bdccecb0b8db834ee0b92fdbcfa606dd6"
[solve-meta] [solve-meta]
analyzer-name = "dep" analyzer-name = "dep"
analyzer-version = 1 analyzer-version = 1
inputs-digest = "405903b5cdec5fe831757bd4289ad96436109112bb3ef41be36992969312e8f1" inputs-digest = "471d91e2c0a3b0b799ddfd79266deb2f4e6e46a1721f9c00680c370f1eb5662f"
solver-name = "gps-cdcl" solver-name = "gps-cdcl"
solver-version = 1 solver-version = 1

31
main.go
View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
eth_common "github.com/ethereum/go-ethereum/common" eth_common "github.com/ethereum/go-ethereum/common"
eth_core "github.com/ethereum/go-ethereum/core"
eth_state "github.com/ethereum/go-ethereum/core/state" eth_state "github.com/ethereum/go-ethereum/core/state"
eth_ethdb "github.com/ethereum/go-ethereum/ethdb" eth_ethdb "github.com/ethereum/go-ethereum/ethdb"
eth_trie "github.com/ethereum/go-ethereum/trie" eth_trie "github.com/ethereum/go-ethereum/trie"
@ -184,9 +185,7 @@ func main() {
lookupDb := dbm.NewDB("lookup" /* name */, dbm.MemDBBackend, "" /* dir */) lookupDb := dbm.NewDB("lookup" /* name */, dbm.MemDBBackend, "" /* dir */)
addrPreimageDb := dbm.NewDB("addrPreimage" /* name */, dbm.MemDBBackend, "" /* dir */) addrPreimageDb := dbm.NewDB("addrPreimage" /* name */, dbm.MemDBBackend, "" /* dir */)
codeDb := dbm.NewDB("code" /* name */, dbm.MemDBBackend, "" /* dir */) codeDb := dbm.NewDB("code" /* name */, dbm.MemDBBackend, "" /* dir */)
var d eth_state.Database d, err := OurNewDatabase(stateDb, lookupDb, addrPreimageDb, codeDb)
var err error
d, err = OurNewDatabase(stateDb, lookupDb, addrPreimageDb, codeDb)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -196,7 +195,29 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
// Try something g := eth_core.DefaultGenesisBlock()
b := statedb.GetBalance(eth_common.HexToAddress("0x829BD824B016326A401d083B33D092293333A830")) for addr, account := range g.Alloc {
statedb.AddBalance(addr, account.Balance)
statedb.SetCode(addr, account.Code)
statedb.SetNonce(addr, account.Nonce)
for key, value := range account.Storage {
statedb.SetState(addr, key, value)
}
}
// One of the genesis account having 200 ETH
b := statedb.GetBalance(eth_common.HexToAddress("0x756F45E3FA69347A9A973A725E3C98bC4db0b5a0"))
fmt.Printf("Balance: %s\n", b) fmt.Printf("Balance: %s\n", b)
root, err := statedb.Commit(false /* deleteEmptyObjects */)
if err != nil {
panic(err)
}
fmt.Printf("Genesis state root hash: %x\n", root[:])
// Try to create a new statedb from genesis hash
genesis_state, err := eth_state.New(root, d)
if err != nil {
panic(err)
}
b1 := genesis_state.GetBalance(eth_common.HexToAddress("0x756F45E3FA69347A9A973A725E3C98bC4db0b5a0"))
fmt.Printf("Balance reloaded: %s\n", b1)
} }