This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* basic sbundle * sbundle pool * sbundle api * local builder * move sim bundle to core * working builder * db for sbundles * report sbundle stat * mev_simBundle nested logs * refundConfig * pay kickback from refundable value * lints * percentof * sbundle pool with separate lock * don't wait for error when adding sbundle
- Loading branch information
Showing
31 changed files
with
1,231 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package core | ||
|
||
import ( | ||
"errors" | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/state" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/core/vm" | ||
"github.com/ethereum/go-ethereum/params" | ||
) | ||
|
||
var ( | ||
ErrInvalidInclusion = errors.New("invalid inclusion") | ||
|
||
ErrTxFailed = errors.New("tx failed") | ||
ErrNegativeProfit = errors.New("negative profit") | ||
ErrInvalidBundle = errors.New("invalid bundle") | ||
|
||
SbundlePayoutMaxCostInt uint64 = 30_000 | ||
SbundlePayoutMaxCost = big.NewInt(30_000) | ||
) | ||
|
||
type SimBundleResult struct { | ||
TotalProfit *big.Int | ||
RefundableValue *big.Int | ||
GasUsed uint64 | ||
MevGasPrice *big.Int | ||
BodyLogs []SimBundleBodyLogs | ||
} | ||
|
||
type SimBundleBodyLogs struct { | ||
TxLogs []*types.Log `json:"txLogs,omitempty"` | ||
BundleLogs []SimBundleBodyLogs `json:"bundleLogs,omitempty"` | ||
} | ||
|
||
func NewSimBundleResult() SimBundleResult { | ||
return SimBundleResult{ | ||
TotalProfit: big.NewInt(0), | ||
RefundableValue: big.NewInt(0), | ||
GasUsed: 0, | ||
MevGasPrice: big.NewInt(0), | ||
BodyLogs: nil, | ||
} | ||
} | ||
|
||
func SimBundle(chainConfig *params.ChainConfig, chain *BlockChain, gp *GasPool, statedb *state.StateDB, header *types.Header, b *types.SBundle, logs bool) (SimBundleResult, error) { | ||
res := NewSimBundleResult() | ||
|
||
currBlock := header.Number.Uint64() | ||
if currBlock < b.Inclusion.BlockNumber || currBlock > b.Inclusion.MaxBlockNumber { | ||
return res, ErrInvalidInclusion | ||
} | ||
|
||
// extract constraints into convenient format | ||
refundIdx := make([]bool, len(b.Body)) | ||
refundPercents := make([]int, len(b.Body)) | ||
for _, el := range b.Validity.Refund { | ||
refundIdx[el.BodyIdx] = true | ||
refundPercents[el.BodyIdx] = el.Percent | ||
} | ||
|
||
var ( | ||
coinbaseDelta = new(big.Int) | ||
coinbaseBefore *big.Int | ||
) | ||
for i, el := range b.Body { | ||
coinbaseDelta.Set(common.Big0) | ||
coinbaseBefore = statedb.GetBalance(header.Coinbase) | ||
|
||
if el.Tx != nil { | ||
vmconfig := vm.Config{} | ||
receipt, err := ApplyTransaction(chainConfig, chain, &header.Coinbase, gp, statedb, header, el.Tx, &header.GasUsed, vmconfig, nil) | ||
if err != nil { | ||
return res, err | ||
} | ||
if receipt.Status != types.ReceiptStatusSuccessful && !el.CanRevert { | ||
return res, ErrTxFailed | ||
} | ||
res.GasUsed += receipt.GasUsed | ||
if logs { | ||
res.BodyLogs = append(res.BodyLogs, SimBundleBodyLogs{TxLogs: receipt.Logs}) | ||
} | ||
} else if el.Bundle != nil { | ||
innerRes, err := SimBundle(chainConfig, chain, gp, statedb, header, el.Bundle, logs) | ||
if err != nil { | ||
return res, err | ||
} | ||
res.GasUsed += innerRes.GasUsed | ||
if logs { | ||
res.BodyLogs = append(res.BodyLogs, SimBundleBodyLogs{BundleLogs: innerRes.BodyLogs}) | ||
} | ||
} else { | ||
return res, ErrInvalidBundle | ||
} | ||
|
||
coinbaseDelta.Set(statedb.GetBalance(header.Coinbase)) | ||
coinbaseDelta.Sub(coinbaseDelta, coinbaseBefore) | ||
|
||
res.TotalProfit.Add(res.TotalProfit, coinbaseDelta) | ||
if !refundIdx[i] { | ||
res.RefundableValue.Add(res.RefundableValue, coinbaseDelta) | ||
} | ||
} | ||
|
||
// estimate payout value and subtract from total profit | ||
signer := types.MakeSigner(chainConfig, header.Number) | ||
for i, el := range refundPercents { | ||
if !refundIdx[i] { | ||
continue | ||
} | ||
// we pay tx cost out of the refundable value | ||
|
||
// cost | ||
refundConfig, err := types.GetRefundConfig(&b.Body[i], signer) | ||
if err != nil { | ||
return res, err | ||
} | ||
payoutTxFee := new(big.Int).Mul(header.BaseFee, SbundlePayoutMaxCost) | ||
payoutTxFee.Mul(payoutTxFee, new(big.Int).SetInt64(int64(len(refundConfig)))) | ||
res.GasUsed += SbundlePayoutMaxCost.Uint64() * uint64(len(refundConfig)) | ||
|
||
// allocated refundable value | ||
payoutValue := common.PercentOf(res.RefundableValue, el) | ||
|
||
if payoutTxFee.Cmp(payoutValue) > 0 { | ||
return res, ErrNegativeProfit | ||
} | ||
|
||
res.TotalProfit.Sub(res.TotalProfit, payoutValue) | ||
} | ||
|
||
if res.TotalProfit.Sign() < 0 { | ||
res.TotalProfit.Set(common.Big0) | ||
return res, ErrNegativeProfit | ||
} | ||
res.MevGasPrice.Div(res.TotalProfit, new(big.Int).SetUint64(res.GasUsed)) | ||
return res, nil | ||
} |
Oops, something went wrong.