Skip to content

Commit

Permalink
adding a unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
otherview committed Nov 13, 2023
1 parent 2902b42 commit bc3c43f
Show file tree
Hide file tree
Showing 5 changed files with 460 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go/enclave/enclave.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func NewEnclave(
sharedSecretProcessor := components.NewSharedSecretProcessor(mgmtContractLib, attestationProvider, storage, logger)

blockchain := ethblockchain.NewEthBlockchain(big.NewInt(config.ObscuroChainID), registry, storage, logger)
mempool, err := txpool.NewTxPool(blockchain)
mempool, err := txpool.NewTxPool(blockchain, config.MinGasPrice)
if err != nil {
logger.Crit("unable to init eth tx pool", log.ErrKey, err)
}
Expand Down
3 changes: 2 additions & 1 deletion go/enclave/nodetype/sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,10 @@ func (s *sequencer) createNewHeadBatch(l1HeadBlock *common.L1Block, skipBatchIfE

// todo (@stefan) - limit on receipts too
limiter := limiters.NewBatchSizeLimiter(s.settings.MaxBatchSize)
pendingTransactions := s.mempool.PendingTransactions() // minor does not request tip enforcement
pendingTransactions := s.mempool.PendingTransactions()
var transactions []*types.Transaction
for _, group := range pendingTransactions {
// lazily resolve transactions until the batch runs out of space
for _, lazyTx := range group {
if tx := lazyTx.Resolve(); tx != nil {
err = limiter.AcceptTransaction(tx.Tx)
Expand Down
6 changes: 4 additions & 2 deletions go/enclave/txpool/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@ type TxPool struct {
legacyPool *legacypool.LegacyPool
pool *gethtxpool.TxPool
blockchain *ethblockchain.EthBlockchain
gasTip *big.Int
}

// NewTxPool returns a new instance of the tx pool
func NewTxPool(blockchain *ethblockchain.EthBlockchain) (*TxPool, error) {
func NewTxPool(blockchain *ethblockchain.EthBlockchain, gasTip *big.Int) (*TxPool, error) {
txPoolConfig := ethblockchain.NewLegacyPoolConfig()
legacyPool := legacypool.New(txPoolConfig, blockchain)

return &TxPool{
blockchain: blockchain,
txPoolConfig: txPoolConfig,
legacyPool: legacyPool,
gasTip: gasTip,
}, nil
}

Expand All @@ -40,7 +42,7 @@ func (t *TxPool) Start() error {
return fmt.Errorf("tx pool already started")
}

memp, err := gethtxpool.New(new(big.Int).SetUint64(0), t.blockchain, []gethtxpool.SubPool{t.legacyPool})
memp, err := gethtxpool.New(t.gasTip, t.blockchain, []gethtxpool.SubPool{t.legacyPool})
if err != nil {
return fmt.Errorf("unable to init geth tx pool - %w", err)
}
Expand Down
342 changes: 342 additions & 0 deletions go/enclave/txpool/txpool_mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,342 @@
package txpool

import (
"crypto/ecdsa"
"math/big"

"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/trie"
"github.com/obscuronet/go-obscuro/go/common"
"github.com/obscuronet/go-obscuro/go/common/errutil"
"github.com/obscuronet/go-obscuro/go/common/tracers"
"github.com/obscuronet/go-obscuro/go/enclave/core"
"github.com/obscuronet/go-obscuro/go/enclave/crypto"
"github.com/obscuronet/go-obscuro/go/enclave/limiters"

gethcommon "github.com/ethereum/go-ethereum/common"
)

type mockBatchRegistry struct {
currentBatch *core.Batch
}

func (m *mockBatchRegistry) BatchesAfter(_ uint64, _ uint64, _ limiters.RollupLimiter) ([]*core.Batch, []*types.Block, error) {
// TODO implement me
panic("implement me")
}

func (m *mockBatchRegistry) GetBatchStateAtHeight(_ *rpc.BlockNumber) (*state.StateDB, error) {
// TODO implement me
panic("implement me")
}

func (m *mockBatchRegistry) GetBatchAtHeight(_ rpc.BlockNumber) (*core.Batch, error) {
// TODO implement me
panic("implement me")
}

func (m *mockBatchRegistry) SubscribeForExecutedBatches(_ func(*core.Batch, types.Receipts)) {
// TODO implement me
panic("implement me")
}

func (m *mockBatchRegistry) UnsubscribeFromBatches() {
// TODO implement me
panic("implement me")
}

func (m *mockBatchRegistry) OnBatchExecuted(batch *core.Batch, _ types.Receipts) {
m.currentBatch = batch
}

func (m *mockBatchRegistry) HasGenesisBatch() (bool, error) {
// TODO implement me
panic("implement me")
}

func (m *mockBatchRegistry) HeadBatchSeq() *big.Int {
return m.currentBatch.SeqNo()
}

func newMockBatchRegistry() *mockBatchRegistry {
return &mockBatchRegistry{}
}

type mockStorage struct {
currentBatch *core.Batch
batchesSeqNo map[uint64]*core.Batch
batchesHeight map[uint64]*core.Batch
batchesHash map[gethcommon.Hash]*core.Batch
stateDB state.Database
}

func newMockStorage() *mockStorage {
db := state.NewDatabaseWithConfig(rawdb.NewMemoryDatabase(), &trie.Config{
Cache: 1_000_000,
})
stateDB, err := state.New(types.EmptyRootHash, db, nil)
if err != nil {
panic(err)
}

_, err = stateDB.Commit(0, true)
if err != nil {
panic(err)
}

return &mockStorage{
batchesSeqNo: map[uint64]*core.Batch{},
batchesHeight: map[uint64]*core.Batch{},
batchesHash: map[gethcommon.Hash]*core.Batch{},
stateDB: db,
}
}

func (m *mockStorage) FetchBlock(_ common.L1BlockHash) (*types.Block, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) FetchCanonicaBlockByHeight(_ *big.Int) (*types.Block, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) FetchHeadBlock() (*types.Block, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) StoreBlock(_ *types.Block, _ *common.ChainFork) error {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) IsAncestor(_ *types.Block, _ *types.Block) bool {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) IsBlockAncestor(_ *types.Block, _ common.L1BlockHash) bool {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) FetchBatch(_ common.L2BatchHash) (*core.Batch, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) FetchBatchHeader(_ common.L2BatchHash) (*common.BatchHeader, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) FetchBatchByHeight(height uint64) (*core.Batch, error) {
batch, found := m.batchesHeight[height]
if !found {
return nil, errutil.ErrNotFound
}
return batch, nil
}

func (m *mockStorage) FetchBatchBySeqNo(seqNum uint64) (*core.Batch, error) {
batch, found := m.batchesSeqNo[seqNum]
if !found {
return nil, errutil.ErrNotFound
}
return batch, nil
}

func (m *mockStorage) FetchHeadBatch() (*core.Batch, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) FetchCurrentSequencerNo() (*big.Int, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) FetchBatchesByBlock(_ common.L1BlockHash) ([]*core.Batch, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) FetchNonCanonicalBatchesBetween(_ uint64, _ uint64) ([]*core.Batch, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) FetchCanonicalUnexecutedBatches(_ *big.Int) ([]*core.Batch, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) BatchWasExecuted(_ common.L2BatchHash) (bool, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) FetchHeadBatchForBlock(_ common.L1BlockHash) (*core.Batch, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) StoreBatch(_ *core.Batch) error {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) StoreExecutedBatch(batch *core.Batch, _ []*types.Receipt) error {
m.currentBatch = batch
m.batchesSeqNo[batch.SeqNo().Uint64()] = batch
m.batchesHeight[batch.Number().Uint64()] = batch
m.batchesHash[batch.Hash()] = batch
return nil
}

func (m *mockStorage) StoreRollup(_ *common.ExtRollup, _ *common.CalldataRollupHeader) error {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) FetchReorgedRollup(_ []common.L1BlockHash) (*common.L2BatchHash, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) CreateStateDB(hash common.L2BatchHash) (*state.StateDB, error) {
batch, found := m.batchesHash[hash]
if !found {
return nil, errutil.ErrNotFound
}
return state.New(batch.Header.Root, m.stateDB, nil)
}

func (m *mockStorage) EmptyStateDB() (*state.StateDB, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) FetchSecret() (*crypto.SharedEnclaveSecret, error) {
return &crypto.SharedEnclaveSecret{}, nil
}

func (m *mockStorage) StoreSecret(_ crypto.SharedEnclaveSecret) error {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) GetTransaction(_ common.L2TxHash) (*types.Transaction, gethcommon.Hash, uint64, uint64, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) GetTransactionReceipt(_ common.L2TxHash) (*types.Receipt, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) GetReceiptsByBatchHash(_ common.L2BatchHash) (types.Receipts, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) GetContractCreationTx(_ gethcommon.Address) (*gethcommon.Hash, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) FetchAttestedKey(_ gethcommon.Address) (*ecdsa.PublicKey, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) StoreAttestedKey(_ gethcommon.Address, _ *ecdsa.PublicKey) error {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) StoreL1Messages(_ common.L1BlockHash, _ common.CrossChainMessages) error {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) GetL1Messages(_ common.L1BlockHash) (common.CrossChainMessages, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) StoreValueTransfers(_ common.L1BlockHash, _ common.ValueTransferEvents) error {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) GetL1Transfers(_ common.L1BlockHash) (common.ValueTransferEvents, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) StoreEnclaveKey(_ *ecdsa.PrivateKey) error {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) GetEnclaveKey() (*ecdsa.PrivateKey, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) GetContractCount() (*big.Int, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) GetReceiptsPerAddress(_ *gethcommon.Address, _ *common.QueryPagination) (types.Receipts, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) GetPublicTransactionData(_ *common.QueryPagination) ([]common.PublicTransaction, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) GetPublicTransactionCount() (uint64, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) GetReceiptsPerAddressCount(_ *gethcommon.Address) (uint64, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) Close() error {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) HealthCheck() (bool, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) FilterLogs(_ *gethcommon.Address, _, _ *big.Int, _ *common.L2BatchHash, _ []gethcommon.Address, _ [][]gethcommon.Hash) ([]*types.Log, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) DebugGetLogs(_ common.TxHash) ([]*tracers.DebugLogs, error) {
// TODO implement me
panic("implement me")
}

func (m *mockStorage) TrieDB() *trie.Database {
// TODO implement me
panic("implement me")
}
Loading

0 comments on commit bc3c43f

Please sign in to comment.