From 299866b4129f910723cab906c1f16108ab3e8f96 Mon Sep 17 00:00:00 2001 From: chengzhinei Date: Mon, 15 May 2023 15:03:56 +0800 Subject: [PATCH 1/5] thread to PushData2Database --- libs/cosmos-sdk/store/mpt/store.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/libs/cosmos-sdk/store/mpt/store.go b/libs/cosmos-sdk/store/mpt/store.go index 0a492e3ce..2c9f28ee7 100644 --- a/libs/cosmos-sdk/store/mpt/store.go +++ b/libs/cosmos-sdk/store/mpt/store.go @@ -90,6 +90,8 @@ type MptStore struct { // for time statistics statisticsBeginTime time.Time + + pushDBHeightChan chan int64 } func (ms *MptStore) CommitterCommitMap(deltaMap iavl.TreeDeltaMap) (_ types.CommitID, _ iavl.TreeDeltaMap) { @@ -126,6 +128,7 @@ func generateMptStore(logger tmlog.Logger, id types.CommitID, db ethstate.Databa logger: logger, retriever: retriever, exitSignal: make(chan struct{}), + pushDBHeightChan: make(chan int64, 10), } if mptStore.logger == nil { mptStore.logger = tmlog.NewNopLogger() @@ -143,6 +146,8 @@ func generateMptStore(logger tmlog.Logger, id types.CommitID, db ethstate.Databa mptStore.logger.Info("open snapshot successfully", "snapshot", "ok") } + go mptStore.threadPushData2DB() + return mptStore, err } @@ -446,9 +451,8 @@ func (ms *MptStore) CommitterCommit(inputDelta interface{}) (rootHash types.Comm ms.SetMptRootHash(uint64(ms.version), root) ms.originalRoot = root - // TODO: use a thread to push data to database // push data to database - ms.PushData2Database(ms.version) + ms.pushDBHeightChan <- ms.version ms.sprintDebugLog(ms.version) @@ -461,6 +465,12 @@ func (ms *MptStore) CommitterCommit(inputDelta interface{}) (rootHash types.Comm }, outputDelta } +func (ms *MptStore) threadPushData2DB() { + for h := range ms.pushDBHeightChan { + ms.PushData2Database(h) + } +} + func (ms *MptStore) LastCommitID() types.CommitID { return types.CommitID{ Version: ms.version, From 217598f5dbd87a88c3bb286092ad2db91d89cb9b Mon Sep 17 00:00:00 2001 From: chengzhinei Date: Mon, 15 May 2023 15:49:56 +0800 Subject: [PATCH 2/5] fix ut --- libs/cosmos-sdk/store/mpt/snapshot_test.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/libs/cosmos-sdk/store/mpt/snapshot_test.go b/libs/cosmos-sdk/store/mpt/snapshot_test.go index d9794429d..907db3438 100644 --- a/libs/cosmos-sdk/store/mpt/snapshot_test.go +++ b/libs/cosmos-sdk/store/mpt/snapshot_test.go @@ -20,13 +20,15 @@ func TestMptStoreSnapshotDeleteAccount(t *testing.T) { require.NoError(t, err) mptStore := &MptStore{ - trie: trie, - db: stateDb, - logger: tmlog.NewNopLogger(), - originalRoot: trie.Hash(), - retriever: EmptyStateRootRetriever{}, - triegc: prque.New(nil), + trie: trie, + db: stateDb, + logger: tmlog.NewNopLogger(), + originalRoot: trie.Hash(), + retriever: EmptyStateRootRetriever{}, + triegc: prque.New(nil), + pushDBHeightChan: make(chan int64, 10), } + go mptStore.threadPushData2DB() SetSnapshotRebuild(true) err = mptStore.openSnapshot() require.NoError(t, err) From 653e6dfbf4cda079bdee14a2e1cbfed80dea4d6e Mon Sep 17 00:00:00 2001 From: chengzhinei Date: Mon, 12 Jun 2023 17:57:39 +0800 Subject: [PATCH 3/5] lock commitSnap --- libs/cosmos-sdk/store/mpt/snapshot.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libs/cosmos-sdk/store/mpt/snapshot.go b/libs/cosmos-sdk/store/mpt/snapshot.go index 45e52d141..f2acaa859 100644 --- a/libs/cosmos-sdk/store/mpt/snapshot.go +++ b/libs/cosmos-sdk/store/mpt/snapshot.go @@ -82,6 +82,8 @@ func (ms *MptStore) commitSnap(root common.Hash) { if ms.snap == nil { return } + ms.snapRWLock.Lock() + defer ms.snapRWLock.Unlock() // Only update if there's a state transition if parent := ms.snap.Root(); parent != root { if err := ms.snaps.Update(root, parent, ms.snapDestructs, ms.snapAccounts, ms.snapStorage); err != nil { From 9a67194a02f2cfccecf76f4bf99c1c858122a448 Mon Sep 17 00:00:00 2001 From: chengzhinei Date: Mon, 12 Jun 2023 18:13:08 +0800 Subject: [PATCH 4/5] use flag trie.async-push-db --- libs/cosmos-sdk/server/start.go | 1 + libs/cosmos-sdk/server/start_okchain.go | 1 + libs/cosmos-sdk/store/mpt/params.go | 4 ++++ libs/cosmos-sdk/store/mpt/store.go | 10 ++++++++-- 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/libs/cosmos-sdk/server/start.go b/libs/cosmos-sdk/server/start.go index ef80dbca2..94c8658bf 100644 --- a/libs/cosmos-sdk/server/start.go +++ b/libs/cosmos-sdk/server/start.go @@ -362,4 +362,5 @@ func SetExternalPackageValue(cmd *cobra.Command) { mptstore.TrieAsyncDBInitCap = viper.GetInt(mptstore.FlagTrieAsyncDBInitCap) mptstore.TrieAsyncDBAutoPruningOff = viper.GetBool(mptstore.FlagTrieAsyncDBAutoPruningOff) mptstore.TrieAsyncDBSyncPruning = viper.GetBool(mptstore.FlagTrieAsyncDBSyncPruning) + mptstore.TrieAsyncPushDB = viper.GetBool(mptstore.FlagTrieAsyncPushDB) } diff --git a/libs/cosmos-sdk/server/start_okchain.go b/libs/cosmos-sdk/server/start_okchain.go index 6f568df76..086db3785 100644 --- a/libs/cosmos-sdk/server/start_okchain.go +++ b/libs/cosmos-sdk/server/start_okchain.go @@ -260,6 +260,7 @@ func RegisterServerFlags(cmd *cobra.Command) *cobra.Command { cmd.Flags().Int(mpt.FlagTrieAsyncDBInitCap, 200_0000, "Init cap of trie async db") cmd.Flags().Bool(mpt.FlagTrieAsyncDBAutoPruningOff, false, "Disable auto prune of trie async db") cmd.Flags().Bool(mpt.FlagTrieAsyncDBSyncPruning, false, "if auto pruning is off and this is on, trie async db will be pruned every block in sync mode") + cmd.Flags().Bool(mpt.FlagTrieAsyncPushDB, false, "Enable to Async PushData2DB") cmd.Flags().Int64(FlagCommitGapHeight, 10, "Block interval to commit cached data into db, affects iavl & mpt") cmd.Flags().Int64(FlagFastSyncGap, 20, "Block height interval to switch fast-sync mode") diff --git a/libs/cosmos-sdk/store/mpt/params.go b/libs/cosmos-sdk/store/mpt/params.go index 5ca978eaf..655690af6 100644 --- a/libs/cosmos-sdk/store/mpt/params.go +++ b/libs/cosmos-sdk/store/mpt/params.go @@ -24,6 +24,8 @@ const ( FlagTrieAsyncDBInitCap = "trie.asyncdb.init-cap" FlagTrieAsyncDBAutoPruningOff = "trie.asyncdb.auto-pruning-off" FlagTrieAsyncDBSyncPruning = "trie.asyncdb.sync-pruning" + + FlagTrieAsyncPushDB = "trie.async-push-db" ) var ( @@ -38,6 +40,8 @@ var ( TrieAsyncDBSyncPruning = false EnableAsyncCommit = false + + TrieAsyncPushDB = false ) var ( diff --git a/libs/cosmos-sdk/store/mpt/store.go b/libs/cosmos-sdk/store/mpt/store.go index 9d3defba7..8ad3ad8c9 100644 --- a/libs/cosmos-sdk/store/mpt/store.go +++ b/libs/cosmos-sdk/store/mpt/store.go @@ -146,7 +146,9 @@ func generateMptStore(logger tmlog.Logger, id types.CommitID, db ethstate.Databa mptStore.logger.Info("open snapshot successfully", "snapshot", "ok") } - go mptStore.threadPushData2DB() + if TrieAsyncPushDB { + go mptStore.threadPushData2DB() + } return mptStore, err } @@ -469,7 +471,11 @@ func (ms *MptStore) CommitterCommit(inputDelta interface{}) (rootHash types.Comm ms.originalRoot = root // push data to database - ms.pushDBHeightChan <- ms.version + if TrieAsyncPushDB { + ms.pushDBHeightChan <- ms.version + } else { + ms.PushData2Database(ms.version) + } ms.sprintDebugLog(ms.version) From 95b66e78af4a99d76a481f5c2fe30a836333e211 Mon Sep 17 00:00:00 2001 From: chengzhinei Date: Mon, 12 Jun 2023 18:25:48 +0800 Subject: [PATCH 5/5] change depend of eth --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index e4b836ae1..45d8f23e8 100644 --- a/go.mod +++ b/go.mod @@ -177,7 +177,7 @@ replace ( github.com/buger/jsonparser => github.com/buger/jsonparser v1.0.0 // imported by nacos-go-sdk, upgraded to v1.0.0 in case of a known vulnerable bug github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8.0 github.com/cosmos/gorocksdb => github.com/okx/grocksdb v1.6.45-okc2 - github.com/ethereum/go-ethereum => github.com/okx/go-ethereum v1.10.26-0.20230602095011-cb24c06ec795 + github.com/ethereum/go-ethereum => github.com/okx/go-ethereum v1.10.26-0.20230612041309-c8164aa43e39 github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1 github.com/keybase/go-keychain => github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 github.com/tendermint/go-amino => github.com/okx/go-amino v0.15.1-okc4 diff --git a/go.sum b/go.sum index 8347f8b59..645857013 100644 --- a/go.sum +++ b/go.sum @@ -627,8 +627,8 @@ github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+ github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= github.com/okx/go-amino v0.15.1-okc4 h1:K0xPoVjkz33ySNC4VLStyit4dblMeVlWJQQ6sq+aUBY= github.com/okx/go-amino v0.15.1-okc4/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME= -github.com/okx/go-ethereum v1.10.26-0.20230602095011-cb24c06ec795 h1:Bgk/fHPGNfbzctqq8lwlrtdkFUm0O8je6rKYm8ATSe0= -github.com/okx/go-ethereum v1.10.26-0.20230602095011-cb24c06ec795/go.mod h1:f9/CleoGkiU7NWF9tdpWGfEYmFbN7zUi17jGick31KM= +github.com/okx/go-ethereum v1.10.26-0.20230612041309-c8164aa43e39 h1:Tf6ICF8bO19BAlK7snq53nPlFbk9RfE1asa2HqRbmCo= +github.com/okx/go-ethereum v1.10.26-0.20230612041309-c8164aa43e39/go.mod h1:f9/CleoGkiU7NWF9tdpWGfEYmFbN7zUi17jGick31KM= github.com/okx/grocksdb v1.6.45-okc2 h1:Dbmax+uY71YuGTkJftpF55dbGW7hfIVOfNz2mnFqXl0= github.com/okx/grocksdb v1.6.45-okc2/go.mod h1:+/BHUY+mT0tbaVXwO2wTtD9eytazyw1W5n2O7AGyXZA= github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=