Skip to content

Commit

Permalink
Restructure simulated backend (#6028)
Browse files Browse the repository at this point in the history
  • Loading branch information
connorwstein authored Feb 17, 2022
1 parent 90c69c4 commit 53bb13a
Show file tree
Hide file tree
Showing 7 changed files with 709 additions and 676 deletions.
10 changes: 5 additions & 5 deletions core/chains/evm/bulletprooftxmanager/eth_confirmer.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,13 +380,13 @@ func (ec *EthConfirmer) getNonceForLatestBlock(ctx context.Context, from gethCom

// Note this function will increment promRevertedTxCount upon receiving
// a reverted transaction receipt. Should only be called with unconfirmed attempts.
func (ec *EthConfirmer) batchFetchReceipts(ctx context.Context, attempts []EthTxAttempt) (receipts []Receipt, err error) {
func (ec *EthConfirmer) batchFetchReceipts(ctx context.Context, attempts []EthTxAttempt) (receipts []evmtypes.Receipt, err error) {
var reqs []rpc.BatchElem
for _, attempt := range attempts {
req := rpc.BatchElem{
Method: "eth_getTransactionReceipt",
Args: []interface{}{attempt.Hash},
Result: &Receipt{},
Result: &evmtypes.Receipt{},
}
reqs = append(reqs, req)
}
Expand All @@ -402,9 +402,9 @@ func (ec *EthConfirmer) batchFetchReceipts(ctx context.Context, attempts []EthTx
attempt := attempts[i]
result, err := req.Result, req.Error

receipt, is := result.(*Receipt)
receipt, is := result.(*evmtypes.Receipt)
if !is {
return nil, errors.Errorf("expected result to be a %T, got %T", (*Receipt)(nil), result)
return nil, errors.Errorf("expected result to be a %T, got %T", (*evmtypes.Receipt)(nil), result)
}

l := lggr.With(
Expand Down Expand Up @@ -461,7 +461,7 @@ func (ec *EthConfirmer) batchFetchReceipts(ctx context.Context, attempts []EthTx
return
}

func (ec *EthConfirmer) saveFetchedReceipts(receipts []Receipt) (err error) {
func (ec *EthConfirmer) saveFetchedReceipts(receipts []evmtypes.Receipt) (err error) {
if len(receipts) == 0 {
return nil
}
Expand Down
74 changes: 37 additions & 37 deletions core/chains/evm/bulletprooftxmanager/eth_confirmer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func TestEthConfirmer_CheckForReceipts(t *testing.T) {
return len(b) == 1 && cltest.BatchElemMatchesHash(b[0], attempt1_1.Hash)
})).Return(nil).Run(func(args mock.Arguments) {
elems := args.Get(1).([]rpc.BatchElem)
elems[0].Result = &bulletprooftxmanager.Receipt{}
elems[0].Result = &evmtypes.Receipt{}
}).Once()

// Do the thing
Expand All @@ -217,7 +217,7 @@ func TestEthConfirmer_CheckForReceipts(t *testing.T) {
})

t.Run("saves nothing if returned receipt does not match the attempt", func(t *testing.T) {
bptxmReceipt := bulletprooftxmanager.Receipt{
bptxmReceipt := evmtypes.Receipt{
TxHash: utils.NewHash(),
BlockHash: utils.NewHash(),
BlockNumber: big.NewInt(42),
Expand All @@ -244,7 +244,7 @@ func TestEthConfirmer_CheckForReceipts(t *testing.T) {
})

t.Run("saves nothing if query returns error", func(t *testing.T) {
bptxmReceipt := bulletprooftxmanager.Receipt{
bptxmReceipt := evmtypes.Receipt{
TxHash: attempt1_1.Hash,
BlockHash: utils.NewHash(),
BlockNumber: big.NewInt(42),
Expand Down Expand Up @@ -277,7 +277,7 @@ func TestEthConfirmer_CheckForReceipts(t *testing.T) {
require.Len(t, attempt2_1.EthReceipts, 0)

t.Run("saves eth_receipt and marks eth_tx as confirmed when geth client returns valid receipt", func(t *testing.T) {
bptxmReceipt := bulletprooftxmanager.Receipt{
bptxmReceipt := evmtypes.Receipt{
TxHash: attempt1_1.Hash,
BlockHash: utils.NewHash(),
BlockNumber: big.NewInt(42),
Expand All @@ -295,7 +295,7 @@ func TestEthConfirmer_CheckForReceipts(t *testing.T) {
// First transaction confirmed
elems[0].Result = &bptxmReceipt
// Second transaction still unconfirmed
elems[1].Result = &bulletprooftxmanager.Receipt{}
elems[1].Result = &evmtypes.Receipt{}
}).Once()

// Do the thing
Expand Down Expand Up @@ -336,7 +336,7 @@ func TestEthConfirmer_CheckForReceipts(t *testing.T) {
require.NoError(t, borm.InsertEthTxAttempt(&attempt2_3))
require.NoError(t, borm.InsertEthTxAttempt(&attempt2_2))

bptxmReceipt := bulletprooftxmanager.Receipt{
bptxmReceipt := evmtypes.Receipt{
TxHash: attempt2_2.Hash,
BlockHash: utils.NewHash(),
BlockNumber: big.NewInt(42),
Expand All @@ -353,11 +353,11 @@ func TestEthConfirmer_CheckForReceipts(t *testing.T) {
})).Return(nil).Run(func(args mock.Arguments) {
elems := args.Get(1).([]rpc.BatchElem)
// Most expensive attempt still unconfirmed
elems[2].Result = &bulletprooftxmanager.Receipt{}
elems[2].Result = &evmtypes.Receipt{}
// Second most expensive attempt is confirmed
elems[1].Result = &bptxmReceipt
// Cheapest attempt still unconfirmed
elems[0].Result = &bulletprooftxmanager.Receipt{}
elems[0].Result = &evmtypes.Receipt{}
}).Once()

// Do the thing
Expand All @@ -379,7 +379,7 @@ func TestEthConfirmer_CheckForReceipts(t *testing.T) {

t.Run("ignores receipt missing BlockHash that comes from querying parity too early", func(t *testing.T) {
ethClient.On("NonceAt", mock.Anything, mock.Anything, mock.Anything).Return(uint64(10), nil)
receipt := bulletprooftxmanager.Receipt{
receipt := evmtypes.Receipt{
TxHash: attempt3_1.Hash,
}
ethClient.On("BatchCallContext", mock.Anything, mock.MatchedBy(func(b []rpc.BatchElem) bool {
Expand All @@ -404,7 +404,7 @@ func TestEthConfirmer_CheckForReceipts(t *testing.T) {

t.Run("does not panic if receipt has BlockHash but is missing some other fields somehow", func(t *testing.T) {
// NOTE: This should never happen, but we shouldn't panic regardless
receipt := bulletprooftxmanager.Receipt{
receipt := evmtypes.Receipt{
TxHash: attempt3_1.Hash,
BlockHash: utils.NewHash(),
}
Expand All @@ -431,7 +431,7 @@ func TestEthConfirmer_CheckForReceipts(t *testing.T) {
t.Run("handles case where eth_receipt already exists somehow", func(t *testing.T) {
ethReceipt := cltest.MustInsertEthReceipt(t, borm, 42, utils.NewHash(), attempt3_1.Hash)

bptxmReceipt := bulletprooftxmanager.Receipt{
bptxmReceipt := evmtypes.Receipt{
TxHash: attempt3_1.Hash,
BlockHash: ethReceipt.BlockHash,
BlockNumber: big.NewInt(ethReceipt.BlockNumber),
Expand Down Expand Up @@ -477,7 +477,7 @@ func TestEthConfirmer_CheckForReceipts(t *testing.T) {

require.NoError(t, borm.InsertEthTxAttempt(&attempt4_2))

bptxmReceipt := bulletprooftxmanager.Receipt{
bptxmReceipt := evmtypes.Receipt{
TxHash: attempt4_2.Hash,
BlockHash: utils.NewHash(),
BlockNumber: big.NewInt(42),
Expand All @@ -492,7 +492,7 @@ func TestEthConfirmer_CheckForReceipts(t *testing.T) {
})).Return(nil).Run(func(args mock.Arguments) {
elems := args.Get(1).([]rpc.BatchElem)
// First attempt still unconfirmed
elems[1].Result = &bulletprooftxmanager.Receipt{}
elems[1].Result = &evmtypes.Receipt{}
// Second attempt is confirmed
elems[0].Result = &bptxmReceipt
}).Once()
Expand Down Expand Up @@ -560,24 +560,24 @@ func TestEthConfirmer_CheckForReceipts_batching(t *testing.T) {
cltest.BatchElemMatchesHash(b[1], attempts[3].Hash)
})).Return(nil).Run(func(args mock.Arguments) {
elems := args.Get(1).([]rpc.BatchElem)
elems[0].Result = &bulletprooftxmanager.Receipt{}
elems[1].Result = &bulletprooftxmanager.Receipt{}
elems[0].Result = &evmtypes.Receipt{}
elems[1].Result = &evmtypes.Receipt{}
}).Once()
ethClient.On("BatchCallContext", mock.Anything, mock.MatchedBy(func(b []rpc.BatchElem) bool {
return len(b) == 2 &&
cltest.BatchElemMatchesHash(b[0], attempts[2].Hash) &&
cltest.BatchElemMatchesHash(b[1], attempts[1].Hash)
})).Return(nil).Run(func(args mock.Arguments) {
elems := args.Get(1).([]rpc.BatchElem)
elems[0].Result = &bulletprooftxmanager.Receipt{}
elems[1].Result = &bulletprooftxmanager.Receipt{}
elems[0].Result = &evmtypes.Receipt{}
elems[1].Result = &evmtypes.Receipt{}
}).Once()
ethClient.On("BatchCallContext", mock.Anything, mock.MatchedBy(func(b []rpc.BatchElem) bool {
return len(b) == 1 &&
cltest.BatchElemMatchesHash(b[0], attempts[0].Hash)
})).Return(nil).Run(func(args mock.Arguments) {
elems := args.Get(1).([]rpc.BatchElem)
elems[0].Result = &bulletprooftxmanager.Receipt{}
elems[0].Result = &evmtypes.Receipt{}
}).Once()

require.NoError(t, ec.CheckForReceipts(ctx, 42))
Expand Down Expand Up @@ -628,10 +628,10 @@ func TestEthConfirmer_CheckForReceipts_only_likely_confirmed(t *testing.T) {
})).Return(nil).Run(func(args mock.Arguments) {
elems := args.Get(1).([]rpc.BatchElem)
captured = append(captured, elems...)
elems[0].Result = &bulletprooftxmanager.Receipt{}
elems[1].Result = &bulletprooftxmanager.Receipt{}
elems[2].Result = &bulletprooftxmanager.Receipt{}
elems[3].Result = &bulletprooftxmanager.Receipt{}
elems[0].Result = &evmtypes.Receipt{}
elems[1].Result = &evmtypes.Receipt{}
elems[2].Result = &evmtypes.Receipt{}
elems[3].Result = &evmtypes.Receipt{}
}).Once()

require.NoError(t, ec.CheckForReceipts(ctx, 42))
Expand Down Expand Up @@ -729,13 +729,13 @@ func TestEthConfirmer_CheckForReceipts_confirmed_missing_receipt(t *testing.T) {
pgtest.MustExec(t, db, `UPDATE eth_tx_attempts SET broadcast_before_block_num = 41 WHERE broadcast_before_block_num IS NULL`)

t.Run("marks buried eth_txes as 'confirmed_missing_receipt'", func(t *testing.T) {
bptxmReceipt0 := bulletprooftxmanager.Receipt{
bptxmReceipt0 := evmtypes.Receipt{
TxHash: attempt0_2.Hash,
BlockHash: utils.NewHash(),
BlockNumber: big.NewInt(42),
TransactionIndex: uint(1),
}
bptxmReceipt3 := bulletprooftxmanager.Receipt{
bptxmReceipt3 := evmtypes.Receipt{
TxHash: attempt3_1.Hash,
BlockHash: utils.NewHash(),
BlockNumber: big.NewInt(42),
Expand All @@ -755,12 +755,12 @@ func TestEthConfirmer_CheckForReceipts_confirmed_missing_receipt(t *testing.T) {
elems := args.Get(1).([]rpc.BatchElem)
// First transaction confirmed
elems[0].Result = &bptxmReceipt0
elems[1].Result = &bulletprooftxmanager.Receipt{}
elems[1].Result = &evmtypes.Receipt{}
// Second transaction stil unconfirmed
elems[2].Result = &bulletprooftxmanager.Receipt{}
elems[3].Result = &bulletprooftxmanager.Receipt{}
elems[2].Result = &evmtypes.Receipt{}
elems[3].Result = &evmtypes.Receipt{}
// Third transaction still unconfirmed
elems[4].Result = &bulletprooftxmanager.Receipt{}
elems[4].Result = &evmtypes.Receipt{}
// Fourth transaction is confirmed
elems[5].Result = &bptxmReceipt3
}).Once()
Expand Down Expand Up @@ -804,7 +804,7 @@ func TestEthConfirmer_CheckForReceipts_confirmed_missing_receipt(t *testing.T) {
// eth_txes with nonce 3 is confirmed

t.Run("marks eth_txes with state 'confirmed_missing_receipt' as 'confirmed' if a receipt finally shows up", func(t *testing.T) {
bptxmReceipt := bulletprooftxmanager.Receipt{
bptxmReceipt := evmtypes.Receipt{
TxHash: attempt2_1.Hash,
BlockHash: utils.NewHash(),
BlockNumber: big.NewInt(43),
Expand All @@ -820,8 +820,8 @@ func TestEthConfirmer_CheckForReceipts_confirmed_missing_receipt(t *testing.T) {
})).Return(nil).Run(func(args mock.Arguments) {
elems := args.Get(1).([]rpc.BatchElem)
// First transaction still unconfirmed
elems[0].Result = &bulletprooftxmanager.Receipt{}
elems[1].Result = &bulletprooftxmanager.Receipt{}
elems[0].Result = &evmtypes.Receipt{}
elems[1].Result = &evmtypes.Receipt{}
// Second transaction confirmed
elems[2].Result = &bptxmReceipt
}).Once()
Expand Down Expand Up @@ -868,8 +868,8 @@ func TestEthConfirmer_CheckForReceipts_confirmed_missing_receipt(t *testing.T) {
})).Return(nil).Run(func(args mock.Arguments) {
elems := args.Get(1).([]rpc.BatchElem)
// Both attempts still unconfirmed
elems[0].Result = &bulletprooftxmanager.Receipt{}
elems[1].Result = &bulletprooftxmanager.Receipt{}
elems[0].Result = &evmtypes.Receipt{}
elems[1].Result = &evmtypes.Receipt{}
}).Once()

// PERFORM
Expand Down Expand Up @@ -910,8 +910,8 @@ func TestEthConfirmer_CheckForReceipts_confirmed_missing_receipt(t *testing.T) {
})).Return(nil).Run(func(args mock.Arguments) {
elems := args.Get(1).([]rpc.BatchElem)
// Both attempts still unconfirmed
elems[0].Result = &bulletprooftxmanager.Receipt{}
elems[1].Result = &bulletprooftxmanager.Receipt{}
elems[0].Result = &evmtypes.Receipt{}
elems[1].Result = &evmtypes.Receipt{}
}).Once()

// PERFORM
Expand Down Expand Up @@ -1501,7 +1501,7 @@ func TestEthConfirmer_RebroadcastWhereNecessary(t *testing.T) {
require.Greater(t, expectedBumpedGasPrice.Int64(), attempt1_2.GasPrice.ToInt().Int64())

ethTx := *types.NewTx(&types.LegacyTx{})
receipt := bulletprooftxmanager.Receipt{BlockNumber: big.NewInt(40)}
receipt := evmtypes.Receipt{BlockNumber: big.NewInt(40)}
kst.On("SignTx",
fromAddress,
mock.MatchedBy(func(tx *types.Transaction) bool {
Expand Down Expand Up @@ -2541,7 +2541,7 @@ func TestEthConfirmer_ResumePendingRuns(t *testing.T) {
case data := <-ch:
require.IsType(t, []byte{}, data)

var r bulletprooftxmanager.Receipt
var r evmtypes.Receipt
err := json.Unmarshal(data.([]byte), &r)
require.NoError(t, err)
require.Equal(t, receipt.TxHash, r.TxHash)
Expand Down
Loading

0 comments on commit 53bb13a

Please sign in to comment.