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

fix statedb error during mempool #1702

Merged
merged 7 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
37 changes: 22 additions & 15 deletions go/enclave/evm/ethchainadapter/eth_chainadapter.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ethchainadapter

import (
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -83,13 +82,31 @@ func (e *EthChainAdapter) SubscribeChainHeadEvent(ch chan<- gethcore.ChainHeadEv

// GetBlock retrieves a specific block, used during pool resets.
func (e *EthChainAdapter) GetBlock(_ common.Hash, number uint64) *gethtypes.Block {
nbatch, err := e.storage.FetchBatchByHeight(number)
var batch *core.Batch

// to avoid a costly select to the db, check whether the batches requested are the last ones which are cached
headBatch, err := e.storage.FetchBatchBySeqNo(e.batchRegistry.HeadBatchSeq().Uint64())
if err != nil {
e.logger.Warn("unable to get batch by height", "number", number, log.ErrKey, err)
e.logger.Error("unable to get head batch", log.ErrKey, err)
return nil
}
if headBatch.Number().Uint64() == number {
batch = headBatch
} else if headBatch.Number().Uint64()-1 == number {
batch, err = e.storage.FetchBatch(headBatch.Header.ParentHash)
if err != nil {
e.logger.Error("unable to get parent of head batch", log.ErrKey, err, log.BatchHashKey, headBatch.Header.ParentHash)
return nil
}
} else {
batch, err = e.storage.FetchBatchByHeight(number)
if err != nil {
e.logger.Error("unable to get batch by height", log.BatchHeightKey, number, log.ErrKey, err)
return nil
}
}

nfromBatch, err := gethencoding.CreateEthBlockFromBatch(nbatch)
nfromBatch, err := gethencoding.CreateEthBlockFromBatch(batch)
if err != nil {
e.logger.Error("unable to convert batch to eth block", log.ErrKey, err)
return nil
Expand All @@ -104,17 +121,7 @@ func (e *EthChainAdapter) StateAt(root common.Hash) (*state.StateDB, error) {
return nil, nil //nolint:nilnil
}

currentBatchSeqNo := e.batchRegistry.HeadBatchSeq()
if currentBatchSeqNo == nil {
return nil, fmt.Errorf("not ready yet")
}
currentBatch, err := e.storage.FetchBatchBySeqNo(currentBatchSeqNo.Uint64())
if err != nil {
e.logger.Warn("unable to get batch by height", "currentBatchSeqNo", currentBatchSeqNo, log.ErrKey, err)
return nil, nil //nolint:nilnil
}

return e.storage.CreateStateDB(currentBatch.Hash())
return state.New(root, e.storage.StateDB(), nil)
}

func (e *EthChainAdapter) IngestNewBlock(batch *core.Batch) error {
Expand Down
3 changes: 3 additions & 0 deletions go/enclave/storage/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ type Storage interface {

// TrieDB - return the underlying trie database
TrieDB() *trie.Database

// StateDB - return the underlying state database
StateDB() state.Database
}

type ScanStorage interface {
Expand Down
4 changes: 4 additions & 0 deletions go/enclave/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ func (s *storageImpl) TrieDB() *trie.Database {
return s.stateDB.TrieDB()
}

func (s *storageImpl) StateDB() state.Database {
return s.stateDB
}

func (s *storageImpl) Close() error {
return s.db.GetSQLDB().Close()
}
Expand Down
4 changes: 4 additions & 0 deletions go/enclave/txpool/txpool_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,7 @@ func (m *mockStorage) TrieDB() *trie.Database {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) StateDB() state.Database {
return m.stateDB
}
Loading