diff --git a/go.mod b/go.mod index 101f8aed9..6add257e2 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.20230605041300-9db59c1d91e6 + github.com/ethereum/go-ethereum => github.com/okx/go-ethereum v1.10.26-0.20230627015235-d0b4e45610b7 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 6c6e80f85..73744dc50 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.20230605041300-9db59c1d91e6 h1:pXsdHnWHejHIc1cJZ625U44eBN3x12Sr5H8Qm9EQCtY= -github.com/okx/go-ethereum v1.10.26-0.20230605041300-9db59c1d91e6/go.mod h1:f9/CleoGkiU7NWF9tdpWGfEYmFbN7zUi17jGick31KM= +github.com/okx/go-ethereum v1.10.26-0.20230627015235-d0b4e45610b7 h1:l+OUB+AvdmiYiTOshKjTY9//eBMnffeqRL+odaxpFGM= +github.com/okx/go-ethereum v1.10.26-0.20230627015235-d0b4e45610b7/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= diff --git a/libs/cosmos-sdk/server/start.go b/libs/cosmos-sdk/server/start.go index cf52937ac..da24c44da 100644 --- a/libs/cosmos-sdk/server/start.go +++ b/libs/cosmos-sdk/server/start.go @@ -362,5 +362,6 @@ 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) mptstore.SetSnapshotJournal(viper.GetBool(mptstore.FlagTrieEnableSnapshotJournal)) } diff --git a/libs/cosmos-sdk/server/start_okchain.go b/libs/cosmos-sdk/server/start_okchain.go index 9af7a5832..55bd46946 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().Bool(mpt.FlagTrieEnableSnapshotJournal, false, "Enable record snapshot's journal. So that snapshot can be repaired within certain version") cmd.Flags().Int64(FlagCommitGapHeight, 10, "Block interval to commit cached data into db, affects iavl & mpt") diff --git a/libs/cosmos-sdk/store/mpt/params.go b/libs/cosmos-sdk/store/mpt/params.go index 0deb7a9ed..cafa6e965 100644 --- a/libs/cosmos-sdk/store/mpt/params.go +++ b/libs/cosmos-sdk/store/mpt/params.go @@ -25,6 +25,8 @@ const ( FlagTrieAsyncDBAutoPruningOff = "trie.asyncdb.auto-pruning-off" FlagTrieAsyncDBSyncPruning = "trie.asyncdb.sync-pruning" + FlagTrieAsyncPushDB = "trie.async-push-db" + FlagTrieEnableSnapshotJournal = "trie.enable-snapshot-journal" ) @@ -40,6 +42,8 @@ var ( TrieAsyncDBSyncPruning = false EnableAsyncCommit = false + + TrieAsyncPushDB = false ) var ( diff --git a/libs/cosmos-sdk/store/mpt/snapshot.go b/libs/cosmos-sdk/store/mpt/snapshot.go index c89e530fe..17b196061 100644 --- a/libs/cosmos-sdk/store/mpt/snapshot.go +++ b/libs/cosmos-sdk/store/mpt/snapshot.go @@ -102,6 +102,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 { 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) diff --git a/libs/cosmos-sdk/store/mpt/store.go b/libs/cosmos-sdk/store/mpt/store.go index 9c66f5530..8ad3ad8c9 100644 --- a/libs/cosmos-sdk/store/mpt/store.go +++ b/libs/cosmos-sdk/store/mpt/store.go @@ -89,7 +89,8 @@ type MptStore struct { // for time statistics statisticsBeginTime time.Time - outputDelta *trie.MptDelta + pushDBHeightChan chan int64 + outputDelta *trie.MptDelta } func (ms *MptStore) CommitterCommitMap(deltaMap iavl.TreeDeltaMap) (_ types.CommitID, _ iavl.TreeDeltaMap) { @@ -126,6 +127,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), outputDelta: trie.NewMptDelta(), } if mptStore.logger == nil { @@ -144,6 +146,10 @@ func generateMptStore(logger tmlog.Logger, id types.CommitID, db ethstate.Databa mptStore.logger.Info("open snapshot successfully", "snapshot", "ok") } + if TrieAsyncPushDB { + go mptStore.threadPushData2DB() + } + return mptStore, err } @@ -464,9 +470,12 @@ 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) + if TrieAsyncPushDB { + ms.pushDBHeightChan <- ms.version + } else { + ms.PushData2Database(ms.version) + } ms.sprintDebugLog(ms.version) @@ -479,6 +488,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,