Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow EVM state to be flushed to disk every Nth block instead of every block #1532

Merged
merged 89 commits into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
097dc40
experiment
pathornteng Oct 9, 2019
ea8a66b
fix unit tests
pathornteng Oct 9, 2019
de298e0
only save committed evmroot
pathornteng Oct 9, 2019
5fda6e4
allow flush interval for evm state to be configured in loom.yaml
pathornteng Oct 9, 2019
8c04bfc
fix unit test
pathornteng Oct 9, 2019
9180eb1
fix unit test
pathornteng Oct 9, 2019
4193068
save default root to evm.db
pathornteng Oct 9, 2019
a4863cf
address PR comment
pathornteng Oct 9, 2019
2af31ad
move EVMStateDB from readonlystate to state
pathornteng Oct 9, 2019
28376ff
refactor
pathornteng Oct 11, 2019
393c94e
clean up
pathornteng Oct 11, 2019
15a2264
update tg test
pathornteng Oct 11, 2019
0ca6a89
revert change
pathornteng Oct 11, 2019
7f92b44
only save default root once
pathornteng Oct 17, 2019
749052c
remove legacy evm dump commands and clean up
pathornteng Oct 17, 2019
fc265e7
Merge branch 'master' into in-memory-evmstate
pathornteng Oct 17, 2019
88aafca
fix unit tests
pathornteng Oct 17, 2019
8853c38
fix a unit test
pathornteng Oct 17, 2019
2861d52
add evmstore to pluginvm
pathornteng Oct 17, 2019
ccc4ffd
merge master
pathornteng Oct 18, 2019
8198833
simplify test
pathornteng Oct 18, 2019
56cfef7
Merge branch 'master' into in-memory-evmstate
pathornteng Oct 22, 2019
8a6be85
merge master
pathornteng Oct 30, 2019
1b7540c
simplify commit EVM root logic
pathornteng Nov 1, 2019
0b70a21
address PR comment
pathornteng Nov 4, 2019
3bfac5b
fix unit test
pathornteng Nov 4, 2019
e6ab7ca
address PR comment
pathornteng Nov 6, 2019
89f3fff
commit evm state in application commit
pathornteng Nov 7, 2019
b422604
refactor
pathornteng Nov 7, 2019
c03f7fa
revert tg branch
pathornteng Nov 7, 2019
5e3a4ab
fix unit test
pathornteng Nov 7, 2019
95f91e1
fix broken tests
pathornteng Nov 7, 2019
2e268ef
add dposv2-genesis
pathornteng Nov 8, 2019
82c887d
reset logs after each tx
pathornteng Nov 8, 2019
51c7eeb
remove unused code
pathornteng Nov 8, 2019
792072e
reset state after commit
pathornteng Nov 11, 2019
1dc0463
merge master
pathornteng Nov 18, 2019
24de625
commit state every tx
pathornteng Nov 21, 2019
fca3e54
fix condition
pathornteng Nov 21, 2019
8cf511c
Merge branch 'master' into in-memory-evmstate
pathornteng Nov 21, 2019
dd7f2d0
address PR comment
pathornteng Nov 22, 2019
1ec9fa0
address PR comment
pathornteng Nov 22, 2019
8bb11f0
use clone version of evm state for checktx
pathornteng Nov 22, 2019
6e87a27
merge master
pathornteng Nov 22, 2019
00475d4
finalise state after each tx
pathornteng Nov 23, 2019
43068fe
finalize state if tx succeeds
pathornteng Nov 23, 2019
cbebc15
revert finalise
pathornteng Nov 23, 2019
e47e0ce
revert snapshot if tx fails
pathornteng Nov 24, 2019
5abbc76
address PR comment
pathornteng Nov 25, 2019
3d6d1c8
copying state instead of reverting
pathornteng Nov 25, 2019
c4679f3
address PR comments
pathornteng Nov 25, 2019
bb65d19
fix unit tests
pathornteng Nov 25, 2019
ed9b1bb
fix truffle test
pathornteng Nov 26, 2019
9f8b531
address PR comments
pathornteng Nov 28, 2019
5603a9d
merge master
pathornteng Nov 28, 2019
7c64432
commit missing file
pathornteng Nov 28, 2019
cc2b16c
fix linter error
pathornteng Nov 28, 2019
388b818
address PR comment
pathornteng Dec 2, 2019
e022db9
Simplify
enlight Dec 2, 2019
e2c471a
Tweaks
enlight Dec 2, 2019
b4a243e
make evmstore thread-safe by removing its cache
pathornteng Dec 2, 2019
5c51fc6
fix unit test
pathornteng Dec 2, 2019
e7d93a1
add snapshot truffle test
pathornteng Dec 2, 2019
8576560
use level db batch in ethdb
pathornteng Dec 3, 2019
16cca33
use cache for logbatch
pathornteng Dec 3, 2019
ea2d726
fix wrong import repo
pathornteng Dec 3, 2019
eeeab35
address PR comments
pathornteng Dec 3, 2019
ed6c72d
Remove LoomEthDB.logContext
enlight Dec 3, 2019
b259464
Tweak the LoomEthDB batch a little bit
enlight Dec 3, 2019
548e35c
Increase delay before mapping accounts on Linux
enlight Dec 4, 2019
b53718b
Revert irrelevant changes
enlight Dec 4, 2019
9fe103f
Cleanup LogBatch and remove unused sortKeys func
enlight Dec 4, 2019
4952caa
Add a few comments
enlight Dec 4, 2019
7b0a764
Bump default number of cached EVM state roots to 500
enlight Dec 4, 2019
d6702e7
Don't reuse the cachingDB instance between EVMState instances
enlight Dec 4, 2019
d818dd9
Eliminate PruningIAVLStore
enlight Dec 4, 2019
d4824ff
Ensure that EVM & app state are in sync in Application.ReadOnlyState()
enlight Dec 4, 2019
7335869
Remove bogus implementation of MockStore.GetSnapshotAt
enlight Dec 6, 2019
c65d47f
Verify state roots in EVMState.GetSnapshot()
enlight Dec 6, 2019
6ecf12f
Remove unused Application.EvmStore field
enlight Dec 6, 2019
23f109e
Revert some modifications to the MultiWriterAppStore tests
enlight Dec 6, 2019
45224fc
Hack VersionedCachingStore to get the e2e tests passing
enlight Dec 9, 2019
e996c14
Check that EVMState isn't nil in Application.Commit()
enlight Dec 9, 2019
8b8a3cb
Check that EVMState isn't nil in Application.CheckTx()
enlight Dec 9, 2019
b41cb2a
address PR comments and make evm state snapshot panic if it calls Com…
pathornteng Dec 11, 2019
ad002bb
Add EvmStore.FlushInterval setting to loom.yml
enlight Dec 11, 2019
b1d871b
Switch back to go-ethereum/loomchain branch
enlight Dec 17, 2019
412d987
Add back a lost comment
enlight Dec 17, 2019
c69f3c2
Merge branch 'master' into in-memory-evmstate
enlight Jan 28, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ BINANCE_TGORACLE_DIR=$(GOPATH)/src/$(PKG_BINANCE_TGORACLE)
# specific commit.
GO_LOOM_GIT_REV = HEAD
# Specifies the loomnetwork/transfer-gateway branch/revision to use.
TG_GIT_REV = HEAD
TG_GIT_REV = in-memory-evmstate
# loomnetwork/go-ethereum loomchain branch
ETHEREUM_GIT_REV = 1fb6138d017a4309105d91f187c126cf979c93f9
ETHEREUM_GIT_REV = in-memory-statedb
# use go-plugin we get 'timeout waiting for connection info' error
HASHICORP_GIT_REV = f4c3476bd38585f9ec669d10ed1686abd52b9961
LEVIGO_GIT_REV = c42d9e0ca023e2198120196f842701bb4c55d7b9
Expand Down Expand Up @@ -197,14 +197,15 @@ $(BINANCE_TGORACLE_DIR):
git clone -q git@github.com:loomnetwork/binance-tgoracle.git $@
cd $(BINANCE_TGORACLE_DIR) && git checkout master && git pull && git checkout $(BINANCE_TG_GIT_REV)

validators-tool: $(TRANSFER_GATEWAY_DIR)
go build -tags gateway -o e2e/validators-tool $(PKG)/e2e/cmd

deps: $(PLUGIN_DIR) $(GO_ETHEREUM_DIR) $(SSHA3_DIR)
# Temp workaround for https://github.com/prometheus/procfs/issues/221
$(PROMETHEUS_PROCFS_DIR):
git clone -q git@github.com:prometheus/procfs $(PROMETHEUS_PROCFS_DIR)
cd $(PROMETHEUS_PROCFS_DIR) && git checkout master && git pull && git checkout d3b299e382e6acf1baa852560d862eca4ff643c8

validators-tool: $(TRANSFER_GATEWAY_DIR)
go build -tags gateway -o e2e/validators-tool $(PKG)/e2e/cmd

deps: $(PLUGIN_DIR) $(GO_ETHEREUM_DIR) $(SSHA3_DIR) $(PROMETHEUS_PROCFS_DIR)

go get \
golang.org/x/crypto/ed25519 \
google.golang.org/grpc \
Expand Down
1 change: 1 addition & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ type Application struct {
EventHandler
ReceiptHandlerProvider
EvmAuxStore *evmaux.EvmAuxStore
EvmStore *store.EvmStore
blockindex.BlockIndexStore
CreateValidatorManager ValidatorsManagerFactoryFunc
CreateChainConfigManager ChainConfigManagerFactoryFunc
Expand Down
2 changes: 1 addition & 1 deletion app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func mockMultiWriterStore(flushInterval int64) (*store.MultiWriterAppStore, erro
return nil, err
}
memDb, _ = db.LoadMemDB()
evmStore := store.NewEvmStore(memDb, 100)
evmStore := store.NewEvmStore(memDb, 100, 0)
multiWriterStore, err := store.NewMultiWriterAppStore(iavlStore, evmStore, false)
if err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ func deployContractToEVM(ctx *plugin.FakeContextWithEVM, filename string, caller
}
byteCode := common.FromHex(string(hexByteCode))
byteCode, err = hex.DecodeString(string(hexByteCode))

vm := evm.NewLoomVm(ctx.State, nil, nil, nil, false)
vm := evm.NewLoomVm(ctx.State, ctx.EvmStore, nil, nil, nil, false)
_, contractAddr, err = vm.Create(caller, byteCode, loom.NewBigUIntFromInt(0))
if err != nil {
return contractAddr, err
Expand Down
2 changes: 0 additions & 2 deletions cmd/loom/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ func NewDBCommand() *cobra.Command {
cmd.AddCommand(
newPruneDBCommand(),
newCompactDBCommand(),
newDumpEVMStateCommand(),
newDumpEVMStateMultiWriterAppStoreCommand(),
newDumpEVMStateFromEvmDB(),
newGetEvmHeightCommand(),
newGetAppHeightCommand(),
Expand Down
240 changes: 4 additions & 236 deletions cmd/loom/db/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package db

import (
"context"
"fmt"
"math"
"path"
Expand All @@ -14,235 +13,18 @@ import (
gstate "github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"
"github.com/loomnetwork/loomchain"
"github.com/loomnetwork/loomchain/cmd/loom/common"
cdb "github.com/loomnetwork/loomchain/db"
"github.com/loomnetwork/loomchain/events"
"github.com/loomnetwork/loomchain/evm"
"github.com/loomnetwork/loomchain/log"
"github.com/loomnetwork/loomchain/plugin"
"github.com/loomnetwork/loomchain/receipts"
registry "github.com/loomnetwork/loomchain/registry/factory"
"github.com/loomnetwork/loomchain/store"
"github.com/spf13/cobra"
abci "github.com/tendermint/tendermint/abci/types"
dbm "github.com/tendermint/tendermint/libs/db"
)

func newDumpEVMStateCommand() *cobra.Command {
var appHeight int64

cmd := &cobra.Command{
Use: "evm-dump",
Short: "Dumps EVM state stored at a specific block height",
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := common.ParseConfig()
if err != nil {
return err
}

db, err := dbm.NewGoLevelDB(cfg.DBName, cfg.RootPath())
if err != nil {
return err
}
appStore, err := store.NewIAVLStore(db, 0, appHeight, 0)
if err != nil {
return err
}

eventHandler := loomchain.NewDefaultEventHandler(events.NewLogEventDispatcher())

regVer, err := registry.RegistryVersionFromInt(cfg.RegistryVersion)
if err != nil {
return err
}
createRegistry, err := registry.NewRegistryFactory(regVer)
if err != nil {
return err
}

receiptHandlerProvider := receipts.NewReceiptHandlerProvider(
eventHandler,
cfg.EVMPersistentTxReceiptsMax,
nil,
)

// TODO: This should use snapshot obtained from appStore.ReadOnlyState()
storeTx := store.WrapAtomic(appStore).BeginTx()
state := loomchain.NewStoreState(
context.Background(),
storeTx,
abci.Header{
Height: appStore.Version(),
},
// it is possible to load the block hash from the TM block store, but probably don't
// need it for just dumping the EVM state
nil,
nil,
)

var newABMFactory plugin.NewAccountBalanceManagerFactoryFunc
if evm.EVMEnabled && cfg.EVMAccountsEnabled {
newABMFactory = plugin.NewAccountBalanceManagerFactory
}

var accountBalanceManager evm.AccountBalanceManager
if newABMFactory != nil {
pvm := plugin.NewPluginVM(
common.NewDefaultContractsLoader(cfg),
state,
createRegistry(state),
eventHandler,
log.Default,
newABMFactory,
receiptHandlerProvider.Writer(),
receiptHandlerProvider.Reader(),
)
createABM, err := newABMFactory(pvm)
if err != nil {
return err
}
accountBalanceManager = createABM(true)
if err != nil {
return err
}
}

vm, err := evm.NewLoomEvm(state, accountBalanceManager, nil, false)
if err != nil {
return err
}

fmt.Printf("\n--- EVM state at app height %d ---\n%s\n", appStore.Version(), string(vm.RawDump()))
return nil
},
}

cmdFlags := cmd.Flags()
cmdFlags.Int64Var(&appHeight, "app-height", 0, "Dump EVM state as it was the specified app height")
return cmd
}

func newDumpEVMStateMultiWriterAppStoreCommand() *cobra.Command {
var appHeight int64
var evmDBName string
cmd := &cobra.Command{
Use: "evm-dump-2",
Short: "Dumps EVM state stored at a specific block height from MultiWriterAppStore",
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := common.ParseConfig()
if err != nil {
return err
}

db, err := dbm.NewGoLevelDB(cfg.DBName, cfg.RootPath())
if err != nil {
return err
}
evmDB, err := cdb.LoadDB(
"goleveldb",
evmDBName,
cfg.RootPath(),
256,
4,
false,
)
if err != nil {
return err
}
iavlStore, err := store.NewIAVLStore(db, 0, appHeight, 0)
if err != nil {
return err
}
evmStore := store.NewEvmStore(evmDB, 100)
if err := evmStore.LoadVersion(iavlStore.Version()); err != nil {
return err
}

appStore, err := store.NewMultiWriterAppStore(iavlStore, evmStore, false)
if err != nil {
return err
}
eventHandler := loomchain.NewDefaultEventHandler(events.NewLogEventDispatcher())

regVer, err := registry.RegistryVersionFromInt(cfg.RegistryVersion)
if err != nil {
return err
}
createRegistry, err := registry.NewRegistryFactory(regVer)
if err != nil {
return err
}

receiptHandlerProvider := receipts.NewReceiptHandlerProvider(
eventHandler,
cfg.EVMPersistentTxReceiptsMax,
nil,
)

// TODO: This should use snapshot obtained from appStore.ReadOnlyState()
storeTx := store.WrapAtomic(appStore).BeginTx()
state := loomchain.NewStoreState(
context.Background(),
storeTx,
abci.Header{
Height: appStore.Version(),
},
// it is possible to load the block hash from the TM block store, but probably don't
// need it for just dumping the EVM state
nil,
nil,
)

var newABMFactory plugin.NewAccountBalanceManagerFactoryFunc
if evm.EVMEnabled && cfg.EVMAccountsEnabled {
newABMFactory = plugin.NewAccountBalanceManagerFactory
}

var accountBalanceManager evm.AccountBalanceManager
if newABMFactory != nil {
pvm := plugin.NewPluginVM(
common.NewDefaultContractsLoader(cfg),
state,
createRegistry(state),
eventHandler,
log.Default,
newABMFactory,
receiptHandlerProvider.Writer(),
receiptHandlerProvider.Reader(),
)
createABM, err := newABMFactory(pvm)
if err != nil {
return err
}
accountBalanceManager = createABM(true)
if err != nil {
return err
}
}

vm, err := evm.NewLoomEvm(state, accountBalanceManager, nil, false)
if err != nil {
return err
}

fmt.Printf("\n--- EVM state at app height %d ---\n%s\n", appStore.Version(), string(vm.RawDump()))
return nil
},
}

cmdFlags := cmd.Flags()
cmdFlags.Int64Var(&appHeight, "app-height", 0, "Dump EVM state as it was the specified app height")
cmdFlags.StringVar(&evmDBName, "evmdb-name", "evm", "Name of EVM state database")
return cmd
}

func newDumpEVMStateFromEvmDB() *cobra.Command {
var appHeight int64
var evmDBName string
var dumpStorageTrie bool
cmd := &cobra.Command{
Use: "evm-dump-3",
Use: "evm-dump",
Short: "Dumps EVM state stored at a specific block height from evm.db",
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := common.ParseConfig()
Expand All @@ -262,7 +44,7 @@ func newDumpEVMStateFromEvmDB() *cobra.Command {
return err
}

evmStore := store.NewEvmStore(evmDB, 100)
evmStore := store.NewEvmStore(evmDB, 100, cfg.AppStore.IAVLFlushInterval)
if err := evmStore.LoadVersion(appHeight); err != nil {
return err
}
Expand All @@ -271,21 +53,7 @@ func newDumpEVMStateFromEvmDB() *cobra.Command {

fmt.Printf("version: %d, root: %x\n", version, root)

// TODO: This should use snapshot obtained from appStore.ReadOnlyState()
storeTx := store.WrapAtomic(evmStore).BeginTx()
state := loomchain.NewStoreState(
context.Background(),
storeTx,
abci.Header{
Height: appHeight,
},
// it is possible to load the block hash from the TM block store, but probably don't
// need it for just dumping the EVM state
nil,
nil,
)

srcStateDB := gstate.NewDatabase(evm.NewLoomEthdb(state, nil))
srcStateDB := gstate.NewDatabase(store.NewLoomEthDB(evmStore, nil))
srcStateDBTrie, err := srcStateDB.OpenTrie(evmRoot)
if err != nil {
fmt.Printf("cannot open trie, %s\n", evmRoot.Hex())
Expand Down Expand Up @@ -364,7 +132,7 @@ func newGetEvmHeightCommand() *cobra.Command {
}
defer db.Close()

evmStore := store.NewEvmStore(db, 100)
evmStore := store.NewEvmStore(db, 100, 0)
if err := evmStore.LoadVersion(math.MaxInt64); err != nil {
return err
}
Expand Down
Loading