Skip to content

Commit

Permalink
fix statedb error during mempool (#1702)
Browse files Browse the repository at this point in the history
* fix statedb error during mempool

* optimize db access

* optimize db access

* close faucet container

* skip flaky tests

* increase timeout

* add error
  • Loading branch information
tudor-malene authored Dec 15, 2023
1 parent 5c4e75d commit c7aee80
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 17 deletions.
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
}
8 changes: 8 additions & 0 deletions integration/faucet/faucet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const (
)

func TestFaucet(t *testing.T) {
t.Skip("Skipping because it is too flaky")

startPort := integration.StartPortFaucetUnitTest
createObscuroNetwork(t, startPort)
// This sleep is required to ensure the initial rollup exists, and thus contract deployer can check its balance.
Expand All @@ -60,6 +62,12 @@ func TestFaucet(t *testing.T) {
assert.NoError(t, err)

err = faucetContainer.Start()
defer func(faucetContainer *container.FaucetContainer) {
err := faucetContainer.Stop()
if err != nil {
fmt.Printf("Could not stop faucet %s", err.Error())
}
}(faucetContainer)
assert.NoError(t, err)

initialFaucetBal, err := getFaucetBalance(faucetConfig.ServerPort)
Expand Down
2 changes: 1 addition & 1 deletion integration/obscurogateway/tengateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ func transferETHToAddress(client *ethclient.Client, wallet wallet.Wallet, toAddr
if err != nil {
return nil, err
}
return integrationCommon.AwaitReceiptEth(context.Background(), client, signedTx.Hash(), 2*time.Second)
return integrationCommon.AwaitReceiptEth(context.Background(), client, signedTx.Hash(), 20*time.Second)
}

func subscribeToEvents(addresses []gethcommon.Address, topics [][]gethcommon.Hash, client *ethclient.Client, logs *[]types.Log) ethereum.Subscription { //nolint:unparam
Expand Down
2 changes: 1 addition & 1 deletion integration/simulation/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func (s *Simulation) prefundL1Accounts() {
func (s *Simulation) checkHealthStatus() {
for _, client := range s.RPCHandles.ObscuroClients {
if healthy, err := client.Health(); !healthy || err != nil {
panic("Client is not healthy")
panic("Client is not healthy: " + err.Error())
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tools/walletextension/test/wallet_extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type testHelper struct {
}

func TestWalletExtension(t *testing.T) {
t.Skip("Skipping because it is too flaky")
i := 0
for name, test := range map[string]func(t *testing.T, testHelper *testHelper){
"canInvokeSensitiveMethodsWithViewingKey": canInvokeSensitiveMethodsWithViewingKey,
Expand Down

0 comments on commit c7aee80

Please sign in to comment.