diff --git a/oracle/block.go b/oracle/block.go index 43cc97c..b886476 100644 --- a/oracle/block.go +++ b/oracle/block.go @@ -60,6 +60,17 @@ var ForcedPaymentActivationSlot = map[uint64]uint64{ MainnetChainId: 14950448, } +// Preserve the detector used for published history. From the first affected +// slot onwards, a forced payment can occur anywhere in the block. +var AnyPositionForcedPaymentActivationSlot = map[uint64]uint64{ + MainnetChainId: 15194839, +} + +func (b *FullBlock) IsAnyPositionForcedPaymentDetectionActive() bool { + activation, found := AnyPositionForcedPaymentActivationSlot[b.ChainId] + return !found || b.GetSlotUint64() >= activation +} + // Whether forced payment detection applies to this block. Chains with no // recorded activation slot have no published history to preserve, so it applies // from genesis. @@ -382,6 +393,24 @@ func (b *FullBlock) SetEvents(events *Events) { } } +// Returns the lowercase sender of the tx at the given index of the block, or an +// empty string if it cannot be recovered. Only meant for logging. +func (b *FullBlock) GetTxSender(index uint) string { + txs := b.GetBlockTransactions() + if int(index) >= len(txs) { + return "" + } + tx, err := utils.DecodeTx(txs[index]) + if err != nil { + return "" + } + sender, err := utils.GetTxSender(tx, b.ChainId) + if err != nil { + return "" + } + return strings.ToLower(sender.String()) +} + // Returns the value and recipient of the last tx of the block, which is where // the MEV payment is placed. Deliberately says nothing about who sent it: the // sender heuristic in MevRewardInWei misses payments routed through an address @@ -461,6 +490,7 @@ func (b *FullBlock) SetForcedMevPayment(payment *ForcedMevPayment, poolAddress s Data: []byte{}, BlockNumber: payment.BlockNumber, TxHash: common.HexToHash(payment.TxHash), + TxIndex: payment.TxIndex, BlockHash: common.HexToHash(payment.BlockHash), Removed: false, }, @@ -759,6 +789,20 @@ func (b *FullBlock) GetDonations(poolAddress string) []*contract.ContractEtherRe filteredEvents := make([]*contract.ContractEtherReceived, 0) foundMev := false for _, etherRxEvent := range b.Events.EtherReceived { + // Forced payments have a synthetic event with no topics. Match its + // provenance as well as its amount so an equal-sized real donation is + // not discarded. Keep the historical filtering before activation. + if b.IsAnyPositionForcedPaymentDetectionActive() && b.ForcedMevPayment != nil && b.ForcedMevPayment.Delivered { + if !foundMev && len(etherRxEvent.Raw.Topics) == 0 && + etherRxEvent.Raw.TxHash == common.HexToHash(b.ForcedMevPayment.TxHash) && + etherRxEvent.Raw.TxIndex == b.ForcedMevPayment.TxIndex && + etherRxEvent.DonationAmount.Cmp(mevReward) == 0 { + foundMev = true + continue + } + filteredEvents = append(filteredEvents, etherRxEvent) + continue + } if etherRxEvent.DonationAmount.Cmp(mevReward) == 0 { foundMev = true continue diff --git a/oracle/forced_payment.go b/oracle/forced_payment.go new file mode 100644 index 0000000..bbf029d --- /dev/null +++ b/oracle/forced_payment.go @@ -0,0 +1,149 @@ +package oracle + +import ( + "context" + "fmt" + "math/big" + + "github.com/attestantio/go-eth2-client/spec" + "github.com/avast/retry-go/v4" + "github.com/dappnode/mev-sp-oracle/utils" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" +) + +// DetectForcedMevPayment is called before advancing the oracle state. Balance +// reads happen once per block, not once per candidate. Ordinary blocks return +// before decoding transactions; only an unexplained inflow requires a scan. +// Like the original detector, this proves the amount at block granularity, not +// a call trace. A unique transaction with exactly that value identifies the +// candidate. Ambiguous or unsupported inflows stop processing instead of +// silently banning a validator or allocating an arbitrary amount. +func (o *Onchain) DetectForcedMevPayment(b *FullBlock, opts ...retry.Option) error { + if b.ConsensusBlock == nil || !b.IsForcedPaymentDetectionActive() || + !b.IsAnyPositionForcedPaymentDetectionActive() { + return nil + } + return o.detectAnyPositionForcedMevPayment(b, opts...) +} + +func (o *Onchain) detectAnyPositionForcedMevPayment(b *FullBlock, opts ...retry.Option) error { + if _, _, exception := b.mevRewardException(); exception { + return nil + } + // Protocol tips also increase the balance without an event. Keep the + // existing vanilla-reward path for blocks whose fee recipient is the pool. + if utils.Equals(b.GetFeeRecipient(), o.PoolAddress) { + return nil + } + blockNumber := b.GetBlockNumber() + if blockNumber == 0 { + return fmt.Errorf("cannot verify forced payment for block 0") + } + if b.ForcedMevPayment != nil && b.ForcedMevPayment.Delivered { + return nil // Do not insert a second synthetic event on repeated calls. + } + after, err := o.GetPoolEthBalance(new(big.Int).SetUint64(blockNumber), opts...) + if err != nil { + return err + } + before, err := o.GetPoolEthBalance(new(big.Int).SetUint64(blockNumber-1), opts...) + if err != nil { + return err + } + inflow := unexplainedPoolInflow(new(big.Int).Sub(after, before), b.Events) + if inflow.Sign() == 0 { + return nil + } + if inflow.Sign() < 0 { + return fmt.Errorf("block %d has unexplained pool outflow %s wei", blockNumber, inflow) + } + if _, isMev, recipient := b.MevRewardInWei(); isMev && utils.Equals(recipient, o.PoolAddress) { + return fmt.Errorf("block %d has both a direct MEV payment and unexplained inflow %s wei; cannot select a single proposer reward", blockNumber, inflow) + } + // A consensus withdrawal can credit any address, including the pool. It + // must not be mistaken for a proposer payment of the same value. + if b.ConsensusBlock.Version >= spec.DataVersionCapella { + withdrawals, err := b.ConsensusBlock.Withdrawals() + if err != nil { + return fmt.Errorf("read withdrawals at block %d: %w", blockNumber, err) + } + for _, withdrawal := range withdrawals { + if withdrawal.Amount > 0 && utils.Equals(withdrawal.Address.String(), o.PoolAddress) { + return fmt.Errorf("block %d credits a consensus withdrawal to the pool; cannot attribute unexplained inflow %s wei to MEV", blockNumber, inflow) + } + } + } + tx, index, err := b.findForcedPaymentCandidate(o.PoolAddress, inflow) + if err != nil { + return err + } + receipt, err := o.forcedPaymentReceipt(b, tx, index, opts...) + if err != nil { + return err + } + payer := receipt.ContractAddress + if tx.To() != nil { + payer = *tx.To() + } + if payer == (common.Address{}) { + return fmt.Errorf("forced payment transaction %s has no recipient or created contract", tx.Hash()) + } + b.SetForcedMevPayment(&ForcedMevPayment{ + Delivered: true, AmountWei: new(big.Int).Set(inflow), + Payer: payer.Hex(), TxHash: tx.Hash().Hex(), TxIndex: uint(index), + BlockNumber: blockNumber, BlockHash: receipt.BlockHash.Hex(), + }, o.PoolAddress) + return nil +} + +func (b *FullBlock) findForcedPaymentCandidate(poolAddress string, inflow *big.Int) (*types.Transaction, int, error) { + var candidate *types.Transaction + candidateIndex := 0 + for index, rawTx := range b.GetBlockTransactions() { + tx, err := utils.DecodeTx(rawTx) + if err != nil { + return nil, 0, fmt.Errorf("decode transaction %d in block %d: %w", index, b.GetBlockNumber(), err) + } + if tx.Value().Sign() <= 0 || tx.Value().Cmp(inflow) != 0 || + (tx.To() != nil && utils.Equals(tx.To().Hex(), poolAddress)) { + continue + } + if candidate != nil { + return nil, 0, fmt.Errorf("block %d has ambiguous forced payment: transactions %s and %s both match unexplained inflow %s wei", b.GetBlockNumber(), candidate.Hash(), tx.Hash(), inflow) + } + candidate, candidateIndex = tx, index + } + if candidate == nil { + return nil, 0, fmt.Errorf("block %d has unexplained pool inflow %s wei with no exact transaction match", b.GetBlockNumber(), inflow) + } + return candidate, candidateIndex, nil +} + +// Reuse already fetched receipts. An unknown proposer only needs this one +// receipt, rather than the old detector's header plus every block receipt. +func (o *Onchain) forcedPaymentReceipt(b *FullBlock, tx *types.Transaction, index int, opts ...retry.Option) (*types.Receipt, error) { + var receipt *types.Receipt + if index < len(b.ExecutionReceipts) { + receipt = b.ExecutionReceipts[index] + } else { + err := retry.Do(func() error { + var err error + receipt, err = o.ExecutionClient.TransactionReceipt(context.Background(), tx.Hash()) + return err + }, o.GetRetryOpts(opts)...) + if err != nil { + return nil, fmt.Errorf("read forced payment receipt %s: %w", tx.Hash(), err) + } + } + blockHash, err := b.ConsensusBlock.ExecutionBlockHash() + if err != nil { + return nil, err + } + if receipt == nil || receipt.TxHash != tx.Hash() || receipt.BlockNumber == nil || + receipt.BlockNumber.Uint64() != b.GetBlockNumber() || receipt.BlockHash != common.Hash(blockHash) || + receipt.TransactionIndex != uint(index) || receipt.Status != types.ReceiptStatusSuccessful { + return nil, fmt.Errorf("invalid or unsuccessful forced payment receipt for transaction %s in block %d at index %d", tx.Hash(), b.GetBlockNumber(), index) + } + return receipt, nil +} diff --git a/oracle/forced_payment_rpc_test.go b/oracle/forced_payment_rpc_test.go new file mode 100644 index 0000000..16bc07f --- /dev/null +++ b/oracle/forced_payment_rpc_test.go @@ -0,0 +1,428 @@ +package oracle + +import ( + "context" + "crypto/ecdsa" + "fmt" + "math/big" + "sync" + "testing" + + v1 "github.com/attestantio/go-eth2-client/api/v1" + "github.com/attestantio/go-eth2-client/spec" + "github.com/attestantio/go-eth2-client/spec/bellatrix" + "github.com/attestantio/go-eth2-client/spec/capella" + "github.com/attestantio/go-eth2-client/spec/deneb" + "github.com/attestantio/go-eth2-client/spec/electra" + "github.com/attestantio/go-eth2-client/spec/phase0" + "github.com/avast/retry-go/v4" + "github.com/dappnode/mev-sp-oracle/contract" + "github.com/dappnode/mev-sp-oracle/utils" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/ethclient" + "github.com/ethereum/go-ethereum/rpc" + "github.com/stretchr/testify/require" +) + +// Exercise the real ethclient JSON-RPC calls, including block parameters and +// receipt decoding. No external services run in CI. +type forcedPaymentRPC struct { + mu sync.Mutex + balances map[string]*big.Int + receipts map[common.Hash]*types.Receipt + calls []string + fail string +} + +func (s *forcedPaymentRPC) GetBalance(_ context.Context, address common.Address, block string) (*hexutil.Big, error) { + s.mu.Lock() + defer s.mu.Unlock() + s.calls = append(s.calls, "balance:"+block) + if address != common.HexToAddress(testPoolAddress) || s.fail == "balance:"+block { + return nil, fmt.Errorf("balance unavailable") + } + balance, ok := s.balances[block] + if !ok { + return nil, fmt.Errorf("unexpected balance block %s", block) + } + return (*hexutil.Big)(balance), nil +} + +func (s *forcedPaymentRPC) GetTransactionReceipt(_ context.Context, hash common.Hash) (*types.Receipt, error) { + s.mu.Lock() + defer s.mu.Unlock() + s.calls = append(s.calls, "receipt:"+hash.Hex()) + if s.fail == "receipt" { + return nil, fmt.Errorf("receipt unavailable") + } + return s.receipts[hash], nil +} + +func (s *forcedPaymentRPC) callCount() int { + s.mu.Lock() + defer s.mu.Unlock() + return len(s.calls) +} + +func newForcedPaymentRPC(t testing.TB, b *FullBlock, before, after *big.Int, receipt *types.Receipt) (*Onchain, *forcedPaymentRPC) { + t.Helper() + backend := &forcedPaymentRPC{ + balances: map[string]*big.Int{ + hexutil.EncodeUint64(b.GetBlockNumber() - 1): before, + hexutil.EncodeUint64(b.GetBlockNumber()): after, + }, + receipts: make(map[common.Hash]*types.Receipt), + } + if receipt != nil { + backend.receipts[receipt.TxHash] = receipt + } + server := rpc.NewServer() + require.NoError(t, server.RegisterName("eth", backend)) + client := ethclient.NewClient(rpc.DialInProc(server)) + t.Cleanup(client.Close) + t.Cleanup(server.Stop) + return &Onchain{ExecutionClient: client, PoolAddress: testPoolAddress, NumRetries: 1}, backend +} + +type forcedPaymentFixture struct { + Slot uint64 + ProposerIndex phase0.ValidatorIndex + Payload *deneb.ExecutionPayload + BalanceBefore string + BalanceAfter string + EtherReceived []*contract.ContractEtherReceived + ExpectedReward string + Receipt *types.Receipt +} + +func decimal(t testing.TB, value string) *big.Int { + t.Helper() + n, ok := new(big.Int).SetString(value, 10) + require.True(t, ok) + return n +} + +func (f forcedPaymentFixture) block(t testing.TB) *FullBlock { + t.Helper() + credentials := make([]byte, 32) + credentials[0] = 1 + copy(credentials[12:], common.HexToAddress("0x1111111111111111111111111111111111111111").Bytes()) + b := NewFullBlock(&v1.ProposerDuty{Slot: phase0.Slot(f.Slot), ValidatorIndex: f.ProposerIndex}, + &v1.Validator{Index: f.ProposerIndex, Validator: &phase0.Validator{WithdrawalCredentials: credentials}}, MainnetChainId) + b.SetConsensusBlock(&spec.VersionedSignedBeaconBlock{Version: spec.DataVersionFulu, + Fulu: &electra.SignedBeaconBlock{Message: &electra.BeaconBlock{ + Slot: phase0.Slot(f.Slot), ProposerIndex: f.ProposerIndex, + Body: &electra.BeaconBlockBody{ExecutionPayload: f.Payload}, + }}, + }) + for _, event := range f.EtherReceived { + copied := *event + copied.DonationAmount = new(big.Int).Set(event.DonationAmount) + b.Events.EtherReceived = append(b.Events.EtherReceived, &copied) + } + return b +} + +// Index of the payment to the pool in the incident block +const incidentPaymentIndex = 3 + +// Fixed keys so every build of the incident block has the same tx hashes, which +// the receipt refers to +var ( + incidentBuilderKey = mustHexKey("4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318") + incidentOtherKey = mustHexKey("8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63") +) + +func mustHexKey(hex string) *ecdsa.PrivateKey { + key, err := crypto.HexToECDSA(hex) + if err != nil { + panic(err) + } + return key +} + +// Shaped like mainnet block 25956850 (slot 15194839), with its real amounts: the +// builder, which is the fee recipient, pays four parties through the forwarder, +// the fourth being the pool. Unrelated txs follow, the last of them with zero +// value and emitting a donation to the pool. +func incidentFixture(t testing.TB) forcedPaymentFixture { + t.Helper() + + const blockNumber = 25956850 + reward := decimal(t, "13131532323905851") + donation := decimal(t, "103797680070839") + before := decimal(t, "64806968647549848541") + after := new(big.Int).Add(before, reward) + after.Add(after, donation) + + signer := types.LatestSignerForChainID(new(big.Int).SetUint64(MainnetChainId)) + nonces := make(map[*ecdsa.PrivateKey]uint64) + sign := func(key *ecdsa.PrivateKey, to string, value *big.Int) *types.Transaction { + toAddress := common.HexToAddress(to) + tx, err := types.SignNewTx(key, signer, &types.DynamicFeeTx{ + ChainID: new(big.Int).SetUint64(MainnetChainId), + Nonce: nonces[key], + GasTipCap: big.NewInt(0), + GasFeeCap: big.NewInt(0), + Gas: 21000, + To: &toAddress, + Value: value, + }) + require.NoError(t, err) + nonces[key]++ + return tx + } + + txs := []*types.Transaction{ + sign(incidentBuilderKey, titanForwarder, big.NewInt(4622541964425610)), + sign(incidentBuilderKey, titanForwarder, big.NewInt(527426380532538)), + sign(incidentBuilderKey, titanForwarder, big.NewInt(6206743076091)), + sign(incidentBuilderKey, titanForwarder, reward), + sign(incidentOtherKey, "0xf4e8b391eae59bc848848a73230b7b174a785d5c", big.NewInt(137183620275739076)), + sign(incidentOtherKey, "0x5f2876944247f302dff857431e26951c2b8dfa33", big.NewInt(0)), + } + rawTxs := make([]bellatrix.Transaction, 0, len(txs)) + for _, tx := range txs { + raw, err := tx.MarshalBinary() + require.NoError(t, err) + rawTxs = append(rawTxs, raw) + } + + var feeRecipient bellatrix.ExecutionAddress + copy(feeRecipient[:], crypto.PubkeyToAddress(incidentBuilderKey.PublicKey).Bytes()) + var blockHash phase0.Hash32 + copy(blockHash[:], common.HexToHash("0x25956850").Bytes()) + + abi, err := contract.ContractMetaData.GetAbi() + require.NoError(t, err) + lastTx := len(txs) - 1 + donationEvent := etherReceivedEvent(donation) + donationEvent.Raw = types.Log{ + Address: common.HexToAddress(testPoolAddress), + Topics: []common.Hash{abi.Events["EtherReceived"].ID}, + BlockNumber: blockNumber, + TxHash: txs[lastTx].Hash(), + TxIndex: uint(lastTx), + BlockHash: common.Hash(blockHash), + } + + return forcedPaymentFixture{ + Slot: 15194839, + ProposerIndex: 2245769, + Payload: &deneb.ExecutionPayload{ + FeeRecipient: feeRecipient, + BlockNumber: blockNumber, + BlockHash: blockHash, + Transactions: rawTxs, + }, + BalanceBefore: before.String(), + BalanceAfter: after.String(), + EtherReceived: []*contract.ContractEtherReceived{donationEvent}, + ExpectedReward: reward.String(), + Receipt: &types.Receipt{ + Status: types.ReceiptStatusSuccessful, + TxHash: txs[incidentPaymentIndex].Hash(), + BlockHash: common.Hash(blockHash), + BlockNumber: big.NewInt(blockNumber), + TransactionIndex: incidentPaymentIndex, + Logs: []*types.Log{}, + }, + } +} + +func Test_AnyPositionForcedPayment_IncidentStateAndReconciliation(t *testing.T) { + f := incidentFixture(t) + b := f.block(t) + last, _ := b.GetLastTxValueAndRecipient() + require.Zero(t, last.Sign(), "v1.2.12 skipped this block") + require.Less(t, incidentPaymentIndex, len(b.GetBlockTransactions())-1, "the payment is not the last tx") + o, backend := newForcedPaymentRPC(t, b, decimal(t, f.BalanceBefore), decimal(t, f.BalanceAfter), f.Receipt) + // Production has already fetched receipts for a subscribed validator. + b.ExecutionReceipts = make([]*types.Receipt, len(b.GetBlockTransactions())) + b.ExecutionReceipts[incidentPaymentIndex] = f.Receipt + require.NoError(t, o.DetectForcedMevPayment(b)) + require.Equal(t, 2, backend.callCount(), "reuse the correct receipt, no extra receipt RPC") + require.Equal(t, f.Receipt.TxHash, b.Events.EtherReceived[1].Raw.TxHash) + require.Equal(t, uint(incidentPaymentIndex), b.Events.EtherReceived[1].Raw.TxIndex) + require.Len(t, b.GetDonations(testPoolAddress), 1) + require.Equal(t, "103797680070839", b.GetDonations(testPoolAddress)[0].DonationAmount.String()) + + oracle := NewOracle(&Config{Network: Mainnet, PoolAddress: testPoolAddress, DeployedSlot: f.Slot, + PoolFeesPercentOver10000: 500, PoolFeesAddress: "0x2222222222222222222222222222222222222222"}) + oracle.addSubscription(uint64(f.ProposerIndex), "0x1111111111111111111111111111111111111111", "0x") + oracle.addSubscription(42, "0x3333333333333333333333333333333333333333", "0x") + oracle.SetGetSetOfValidatorsFunc(func(indices []phase0.ValidatorIndex, _ string, _ ...retry.Option) (map[phase0.ValidatorIndex]*v1.Validator, error) { + validators := make(map[phase0.ValidatorIndex]*v1.Validator) + for _, index := range indices { + validators[index] = &v1.Validator{Index: index, Validator: &phase0.Validator{EffectiveBalance: 32000000000}} + } + return validators, nil + }) + // Synthetic starting accounts with the real pre-block total, including + // the pending balance which the bug wrongly confiscated from this proposer. + pending := decimal(t, "4096459189661146") + oracle.state.Validators[uint64(f.ProposerIndex)].PendingRewardsWei.Set(pending) + oracle.state.PoolAccumulatedFees.Sub(decimal(t, f.BalanceBefore), pending) + oracle.state.EtherReceivedEvents = append(oracle.state.EtherReceivedEvents, etherReceivedEvent(decimal(t, f.BalanceBefore))) + require.NoError(t, oracle.RunOnchainReconciliation(decimal(t, f.BalanceBefore), nil)) + _, err := oracle.AdvanceStateToNextSlot(b) + require.NoError(t, err) + require.Equal(t, Active, oracle.state.Validators[uint64(f.ProposerIndex)].ValidatorStatus) + require.Empty(t, oracle.state.WrongFeeBlocks) + require.Len(t, oracle.state.ProposedBlocks, 1) + require.Equal(t, f.ExpectedReward, oracle.state.ProposedBlocks[0].Reward.String()) + require.Greater(t, oracle.state.Validators[uint64(f.ProposerIndex)].AccumulatedRewardsWei.Cmp(pending), 0) + require.NoError(t, oracle.RunOffchainReconciliation()) + require.NoError(t, oracle.RunOnchainReconciliation(decimal(t, f.BalanceAfter), nil)) + // Re-running detection must not duplicate the event or make more RPCs. + require.NoError(t, o.DetectForcedMevPayment(b)) + require.Len(t, b.Events.EtherReceived, 2) + require.Equal(t, 2, backend.callCount()) +} + +func Test_AnyPositionForcedPayment_FailuresDoNotCredit(t *testing.T) { + tests := []struct { + name string + mutate func(*FullBlock, *forcedPaymentRPC, *types.Receipt) + want string + }{ + {"after balance unavailable", func(b *FullBlock, s *forcedPaymentRPC, _ *types.Receipt) { + s.fail = "balance:" + hexutil.EncodeUint64(b.GetBlockNumber()) + }, "balance"}, + {"before balance unavailable", func(b *FullBlock, s *forcedPaymentRPC, _ *types.Receipt) { + s.fail = "balance:" + hexutil.EncodeUint64(b.GetBlockNumber()-1) + }, "balance"}, + {"receipt unavailable", func(_ *FullBlock, s *forcedPaymentRPC, _ *types.Receipt) { s.fail = "receipt" }, "receipt"}, + {"receipt missing", func(_ *FullBlock, s *forcedPaymentRPC, _ *types.Receipt) { s.receipts = nil }, "receipt"}, + {"receipt reverted", func(_ *FullBlock, _ *forcedPaymentRPC, r *types.Receipt) { r.Status = 0 }, "unsuccessful"}, + {"wrong receipt index", func(_ *FullBlock, _ *forcedPaymentRPC, r *types.Receipt) { + r.TransactionIndex = incidentPaymentIndex + 1 + }, "invalid"}, + {"wrong receipt block hash", func(_ *FullBlock, _ *forcedPaymentRPC, r *types.Receipt) { r.BlockHash = common.Hash{} }, "invalid"}, + {"wrong receipt block number", func(_ *FullBlock, _ *forcedPaymentRPC, r *types.Receipt) { r.BlockNumber = big.NewInt(1) }, "invalid"}, + {"wrong receipt tx hash", func(_ *FullBlock, _ *forcedPaymentRPC, r *types.Receipt) { r.TxHash = common.Hash{} }, "invalid"}, + {"ambiguous equal values", func(b *FullBlock, _ *forcedPaymentRPC, _ *types.Receipt) { + b.ConsensusBlock.Fulu.Message.Body.ExecutionPayload.Transactions = append(b.GetBlockTransactions(), b.GetBlockTransactions()[incidentPaymentIndex]) + }, "ambiguous"}, + {"partial or split payment", func(b *FullBlock, s *forcedPaymentRPC, _ *types.Receipt) { + s.balances[hexutil.EncodeUint64(b.GetBlockNumber())].Sub(s.balances[hexutil.EncodeUint64(b.GetBlockNumber())], big.NewInt(1)) + }, "no exact transaction match"}, + {"unexplained outflow", func(b *FullBlock, s *forcedPaymentRPC, _ *types.Receipt) { + s.balances[hexutil.EncodeUint64(b.GetBlockNumber())] = big.NewInt(0) + }, "outflow"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + f := incidentFixture(t) + b := f.block(t) + o, backend := newForcedPaymentRPC(t, b, decimal(t, f.BalanceBefore), decimal(t, f.BalanceAfter), f.Receipt) + tt.mutate(b, backend, f.Receipt) + err := o.DetectForcedMevPayment(b) + require.ErrorContains(t, err, tt.want) + require.Nil(t, b.ForcedMevPayment) + require.Len(t, b.Events.EtherReceived, 1, "no synthetic credit on failure") + }) + } +} + +func Test_AnyPositionForcedPayment_ActivationAndVanilla(t *testing.T) { + for _, slot := range []uint64{14950447, 14950448, 15194838, 15194839, 15194840} { + t.Run(fmt.Sprint(slot), func(t *testing.T) { + f := incidentFixture(t) + f.Slot = slot + b := f.block(t) + o, backend := newForcedPaymentRPC(t, b, decimal(t, f.BalanceBefore), decimal(t, f.BalanceAfter), f.Receipt) + require.Equal(t, slot >= 15194839, b.IsAnyPositionForcedPaymentDetectionActive()) + require.NoError(t, o.DetectForcedMevPayment(b)) + if slot < 15194839 { + require.Zero(t, backend.callCount()) + require.Nil(t, b.ForcedMevPayment) + } else { + require.Equal(t, 3, backend.callCount()) + require.NotNil(t, b.ForcedMevPayment) + } + }) + } + f := incidentFixture(t) + b := f.block(t) + copy(b.ConsensusBlock.Fulu.Message.Body.ExecutionPayload.FeeRecipient[:], common.HexToAddress(testPoolAddress).Bytes()) + o, backend := newForcedPaymentRPC(t, b, decimal(t, f.BalanceBefore), decimal(t, f.BalanceAfter), f.Receipt) + require.NoError(t, o.DetectForcedMevPayment(b)) + require.Nil(t, b.ForcedMevPayment) + require.Zero(t, backend.callCount(), "vanilla fee-recipient accounting remains on the existing path") + b.ConsensusBlock = nil + require.NoError(t, o.DetectForcedMevPayment(b)) + require.Zero(t, backend.callCount(), "missed slots do not cause balance RPCs") +} + +func Test_AnyPositionForcedPayment_EqualDonationIsPreserved(t *testing.T) { + f := incidentFixture(t) + b := f.block(t) + donation := decimal(t, f.ExpectedReward) + difference := new(big.Int).Sub(donation, b.Events.EtherReceived[0].DonationAmount) + b.Events.EtherReceived[0].DonationAmount.Set(donation) + after := new(big.Int).Add(decimal(t, f.BalanceAfter), difference) + o, _ := newForcedPaymentRPC(t, b, decimal(t, f.BalanceBefore), after, f.Receipt) + require.NoError(t, o.DetectForcedMevPayment(b)) + require.Len(t, b.GetDonations(testPoolAddress), 1) + require.Equal(t, donation, b.GetDonations(testPoolAddress)[0].DonationAmount) +} + +func Test_AnyPositionForcedPayment_Positions(t *testing.T) { + for _, index := range []int{0, 1, 2} { + t.Run(fmt.Sprint(index), func(t *testing.T) { + b := buildMevBlock(t, 15195000, 123, 42, titanForwarder, big.NewInt(1000)) + paymentRaw := b.GetBlockTransactions()[0] + tail := buildMevBlock(t, 15195000, 123, 42, "0x1111111111111111111111111111111111111111", big.NewInt(0)).GetBlockTransactions()[0] + txs := []bellatrix.Transaction{tail, tail, tail} + txs[index] = paymentRaw + b.ConsensusBlock.Bellatrix.Message.Body.ExecutionPayload.Transactions = txs + tx, err := utils.DecodeTx(paymentRaw) + require.NoError(t, err) + receipt := &types.Receipt{TxHash: tx.Hash(), BlockNumber: big.NewInt(123), TransactionIndex: uint(index), Status: 1, Logs: []*types.Log{}} + o, backend := newForcedPaymentRPC(t, b, big.NewInt(0), big.NewInt(1000), receipt) + require.NoError(t, o.DetectForcedMevPayment(b)) + require.Equal(t, tx.Hash().Hex(), b.ForcedMevPayment.TxHash) + require.Equal(t, uint(index), b.ForcedMevPayment.TxIndex) + require.Equal(t, 3, backend.callCount()) + }) + } +} + +func Test_AnyPositionForcedPayment_ExplainedFlows(t *testing.T) { + tests := []struct { + name string + delta int64 + events *Events + }{ + {"unrelated transfers", 0, &Events{}}, + {"donation", 1000, &Events{EtherReceived: []*contract.ContractEtherReceived{etherReceivedEvent(big.NewInt(1000))}}}, + {"subscription", 1000, &Events{SubscribeValidator: []*contract.ContractSubscribeValidator{subscribeEvent(big.NewInt(1000))}}}, + {"claim", -1000, &Events{ClaimRewards: []*contract.ContractClaimRewards{claimRewardsEvent(big.NewInt(1000))}}}, + {"donation and claim", -1000, &Events{EtherReceived: []*contract.ContractEtherReceived{etherReceivedEvent(big.NewInt(1000))}, ClaimRewards: []*contract.ContractClaimRewards{claimRewardsEvent(big.NewInt(2000))}}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := buildMevBlock(t, 15195000, 123, 42, titanForwarder, big.NewInt(1000)) + b.Events = tt.events + o, backend := newForcedPaymentRPC(t, b, big.NewInt(2000), big.NewInt(2000+tt.delta), nil) + require.NoError(t, o.DetectForcedMevPayment(b)) + require.Nil(t, b.ForcedMevPayment, "a coincidentally equal tx value must not turn explained flows into MEV") + require.Equal(t, 2, backend.callCount()) + }) + } +} + +func Test_AnyPositionForcedPayment_ConsensusWithdrawalIsNotMev(t *testing.T) { + f := incidentFixture(t) + b := f.block(t) + withdrawal := &capella.Withdrawal{Amount: 1} + copy(withdrawal.Address[:], common.HexToAddress(testPoolAddress).Bytes()) + b.ConsensusBlock.Fulu.Message.Body.ExecutionPayload.Withdrawals = append(b.ConsensusBlock.Fulu.Message.Body.ExecutionPayload.Withdrawals, withdrawal) + o, _ := newForcedPaymentRPC(t, b, decimal(t, f.BalanceBefore), decimal(t, f.BalanceAfter), f.Receipt) + require.ErrorContains(t, o.DetectForcedMevPayment(b), "consensus withdrawal") + require.Nil(t, b.ForcedMevPayment) +} diff --git a/oracle/forced_payment_test.go b/oracle/forced_payment_test.go index 7032fc4..f8689b7 100644 --- a/oracle/forced_payment_test.go +++ b/oracle/forced_payment_test.go @@ -2,6 +2,7 @@ package oracle import ( "math/big" + "strings" "testing" v1 "github.com/attestantio/go-eth2-client/api/v1" @@ -510,3 +511,11 @@ func Test_ForcedMevPayment_ExceptionsStillShortCircuit(t *testing.T) { require.Nil(t, fullBlock.ForcedMevPayment) } } + +// The sender is only logged, but it must point at the tx that paid, which in +// buildMevBlock is the block fee recipient +func Test_ForcedMevPayment_GetTxSender(t *testing.T) { + fullBlock := buildMevBlock(t, 15195000, 123, 42, titanForwarder, big.NewInt(1000)) + require.Equal(t, strings.ToLower(fullBlock.GetFeeRecipient()), fullBlock.GetTxSender(0)) + require.Equal(t, "", fullBlock.GetTxSender(1)) +} diff --git a/oracle/onchain.go b/oracle/onchain.go index dbe09f5..0283473 100644 --- a/oracle/onchain.go +++ b/oracle/onchain.go @@ -1157,26 +1157,40 @@ func (o *Onchain) FetchFullBlock(slot uint64, oracle *Oracle, opt ...bool) *Full fullBlock.SetHeaderAndReceipts(header, receipts) } - // A MEV payment whose apparent recipient is not the pool may still have - // reached the pool, forwarded by an intermediate contract through a path - // that executes no pool code and therefore emits no EtherReceived event, - // eg SELFDESTRUCT. Only the pool balance can prove it either way. + // Forced transfers emit no EtherReceived. The new detector checks the + // unexplained balance once and searches the whole block, regardless of + // sender or subscription. Retain the last-tx detector below exclusively + // for history before activation, so published allocations stay stable. // - // The candidate is the last tx of the block whatever its sender. We do - // NOT reuse MevRewardInWei here: it only recognises a payment sent by - // the block fee recipient or a whitelisted builder, and builders route - // payouts through other addresses. On mainnet, six payments were missed - // this way in a single day, all sent by 0x9fc3da86 while the fee - // recipient was Titan at 0x4838b106. + // We do NOT reuse MevRewardInWei to pick the payment: it only recognises + // a payment sent by the block fee recipient or a whitelisted builder, and + // builders route payouts through other addresses. On mainnet, six + // payments were missed this way in a single day, all sent by 0x9fc3da86 + // while the fee recipient was Titan at 0x4838b106. // // We also do not require the proposer to be subscribed, since the pool // auto subscribes any validator whose reward reaches it. - // - // Whether the pool was actually paid is decided by the balance delta - // alone. For unrelated blocks it will not match the candidate value and - // the verdict is simply false, at the cost of two eth_getBalance calls. candidateReward, candidateRecipient := fullBlock.GetLastTxValueAndRecipient() - if fullBlock.IsForcedPaymentDetectionActive() && + if fullBlock.IsForcedPaymentDetectionActive() && fullBlock.IsAnyPositionForcedPaymentDetectionActive() { + if err := o.DetectForcedMevPayment(fullBlock); err != nil { + log.Fatal("could not verify forced mev payment at slot ", fullBlock.GetSlotUint64(), ": ", err) + } + + // Legit when a validator's first reward reaches the pool this way, + // but it is also what a forced transfer that is not a MEV payment + // would look like, so leave a trail to audit it. A sender equal to + // the fee recipient means the builder itself paid. + if payment := fullBlock.ForcedMevPayment; payment != nil && payment.Delivered && !isFromSubscriber { + log.WithFields(log.Fields{ + "Slot": fullBlock.GetSlotUint64(), + "ValIndex": fullBlock.GetProposerIndexUint64(), + "AmountWei": payment.AmountWei, + "TxHash": payment.TxHash, + "TxSender": fullBlock.GetTxSender(payment.TxIndex), + "FeeRecipient": strings.ToLower(fullBlock.GetFeeRecipient()), + }).Warn("Forced MEV payment from a validator not subscribed to the pool, it will be auto subscribed unless banned") + } + } else if fullBlock.IsForcedPaymentDetectionActive() && candidateReward.Sign() > 0 && !utils.Equals(candidateRecipient, o.PoolAddress) && // Guard: if the pool were also the fee recipient it would collect diff --git a/oracle/types.go b/oracle/types.go index ed3f156..2c3e547 100644 --- a/oracle/types.go +++ b/oracle/types.go @@ -143,6 +143,7 @@ type ForcedMevPayment struct { // Provenance, so the evidence can be audited and replayed TxHash string `json:"tx_hash"` + TxIndex uint `json:"tx_index"` BlockNumber uint64 `json:"block_number"` BlockHash string `json:"block_hash"` }