Fix tests

This commit is contained in:
Ferran Borreguero 2024-02-15 11:15:04 +00:00
parent 977f127c33
commit f338f50c30
6 changed files with 2 additions and 133 deletions

View file

@ -1,14 +1,12 @@
package miner
import (
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
)
@ -30,8 +28,6 @@ type Builder struct {
env *environment
wrk *worker
args *BuilderArgs
profitPre *big.Int
}
func NewBuilder(config *BuilderConfig, args *BuilderArgs) (*Builder, error) {
@ -62,7 +58,6 @@ func NewBuilder(config *BuilderConfig, args *BuilderArgs) (*Builder, error) {
env.gasPool = new(core.GasPool).AddGas(env.header.GasLimit)
b.env = env
b.profitPre = env.state.GetBalance(env.coinbase)
return b, nil
}
@ -85,77 +80,6 @@ func (b *Builder) AddTransaction(txn *types.Transaction) (*types.SimulateTransac
return receiptToSimResult(&types.Receipt{Logs: logs}), nil
}
func (b *Builder) AddBundles(bundles []*SBundle) error {
for _, bundle := range bundles {
if err := b.AddBundle(bundle); err != nil {
return err
}
}
return nil
}
func (b *Builder) AddBundle(bundle *SBundle) error {
work := b.env
// Assume static 28000 gas transfers for both mev-share and proposer payments
refundTransferCost := new(big.Int).Mul(big.NewInt(28000), work.header.BaseFee)
// create ephemeral addr and private key for payment txn
ephemeralPrivKey, err := crypto.GenerateKey()
if err != nil {
return err
}
ephemeralAddr := crypto.PubkeyToAddress(ephemeralPrivKey.PublicKey)
// apply bundle
profitPreBundle := work.state.GetBalance(b.env.coinbase)
if err := b.wrk.rawCommitTransactions(work, bundle.Txs); err != nil {
return err
}
profitPostBundle := work.state.GetBalance(b.env.coinbase)
// calc & refund user if bundle has multiple txns and wants refund
if len(bundle.Txs) > 1 && bundle.RefundPercent != nil {
// Note: PoC logic, this could be gamed by not sending any eth to coinbase
refundPrct := *bundle.RefundPercent
if refundPrct == 0 {
// default refund
refundPrct = 10
}
bundleProfit := new(big.Int).Sub(profitPostBundle, profitPreBundle)
refundAmt := new(big.Int).Div(bundleProfit, big.NewInt(int64(refundPrct)))
// subtract payment txn transfer costs
refundAmt = new(big.Int).Sub(refundAmt, refundTransferCost)
currNonce := work.state.GetNonce(ephemeralAddr)
// HACK to include payment txn
// multi refund block untested
userTx := bundle.Txs[0] // NOTE : assumes first txn is refund recipient
refundAddr, err := types.Sender(types.LatestSignerForChainID(userTx.ChainId()), userTx)
if err != nil {
return err
}
paymentTx, err := types.SignTx(types.NewTx(&types.LegacyTx{
Nonce: currNonce,
To: &refundAddr,
Value: refundAmt,
Gas: 28000,
GasPrice: work.header.BaseFee,
}), work.signer, ephemeralPrivKey)
if err != nil {
return err
}
// commit payment txn
if err := b.wrk.rawCommitTransactions(work, types.Transactions{paymentTx}); err != nil {
return err
}
}
return nil
}
func (b *Builder) FillPending() error {
if err := b.wrk.commitPendingTxs(b.env); err != nil {
return err
@ -166,38 +90,6 @@ func (b *Builder) FillPending() error {
func (b *Builder) BuildBlock() (*types.Block, error) {
work := b.env
// Assume static 28000 gas transfers for both mev-share and proposer payments
refundTransferCost := new(big.Int).Mul(big.NewInt(28000), work.header.BaseFee)
// create ephemeral addr and private key for payment txn
ephemeralPrivKey, err := crypto.GenerateKey()
if err != nil {
return nil, err
}
ephemeralAddr := crypto.PubkeyToAddress(ephemeralPrivKey.PublicKey)
profitPost := work.state.GetBalance(b.env.coinbase)
proposerProfit := new(big.Int).Set(profitPost) // = post-pre-transfer_cost
proposerProfit = proposerProfit.Sub(profitPost, b.profitPre)
proposerProfit = proposerProfit.Sub(proposerProfit, refundTransferCost)
currNonce := work.state.GetNonce(ephemeralAddr)
paymentTx, err := types.SignTx(types.NewTx(&types.LegacyTx{
Nonce: currNonce,
To: &b.args.FeeRecipient,
Value: proposerProfit,
Gas: 28000,
GasPrice: work.header.BaseFee,
}), work.signer, ephemeralPrivKey)
if err != nil {
return nil, fmt.Errorf("could not sign proposer payment: %w", err)
}
// commit payment txn
if err := b.wrk.rawCommitTransactions(work, types.Transactions{paymentTx}); err != nil {
return nil, fmt.Errorf("could not sign proposer payment: %w", err)
}
block, err := b.wrk.engine.FinalizeAndAssemble(b.wrk.chain, work.header, work.state, work.txs, nil, work.receipts, nil)
if err != nil {
return nil, err

View file

@ -1,7 +1,6 @@
package miner
import (
"fmt"
"testing"
"github.com/ethereum/go-ethereum/consensus/clique"
@ -57,7 +56,6 @@ func TestBuilder_FillTransactions(t *testing.T) {
func TestBuilder_BuildBlock(t *testing.T) {
t.Parallel()
t.Skip("TODO")
config, backend := newMockBuilderConfig(t)
@ -71,7 +69,8 @@ func TestBuilder_BuildBlock(t *testing.T) {
block, err := builder.BuildBlock()
require.NoError(t, err)
fmt.Println(block)
require.NotNil(t, block)
require.Len(t, block.Transactions(), 1)
}
func newMockBuilderConfig(t *testing.T) (*BuilderConfig, *testWorkerBackend) {

View file

@ -32,6 +32,5 @@ type BuildBlockArgs struct {
type API interface {
NewSession(ctx context.Context, args *BuildBlockArgs) (string, error)
AddTransaction(ctx context.Context, sessionId string, tx *types.Transaction) (*types.SimulateTransactionResult, error)
AddBundle(ctx context.Context, sessionId string, bundle Bundle) error
BuildBlock(ctx context.Context, sessionId string) error
}

View file

@ -41,10 +41,6 @@ func (a *APIClient) AddTransaction(ctx context.Context, sessionId string, tx *ty
return receipt, err
}
func (a *APIClient) AddBundle(ctx context.Context, sessionId string, bundle Bundle) error {
return a.rpc.CallContext(ctx, nil, "suavex_addBundle", sessionId, bundle)
}
func (a *APIClient) BuildBlock(ctx context.Context, sessionId string) error {
return a.rpc.CallContext(ctx, nil, "suavex_buildBlock", sessionId)
}

View file

@ -10,7 +10,6 @@ import (
type SessionManager interface {
NewSession(context.Context, *BuildBlockArgs) (string, error)
AddTransaction(sessionId string, tx *types.Transaction) (*types.SimulateTransactionResult, error)
AddBundle(sessionId string, bundle Bundle) error
BuildBlock(sessionId string) error
}
@ -33,10 +32,6 @@ func (s *Server) AddTransaction(ctx context.Context, sessionId string, tx *types
return s.sessionMngr.AddTransaction(sessionId, tx)
}
func (s *Server) AddBundle(ctx context.Context, sessionId string, bundle Bundle) error {
return s.sessionMngr.AddBundle(sessionId, bundle)
}
func (s *Server) BuildBlock(ctx context.Context, sessionId string) error {
return s.sessionMngr.BuildBlock(sessionId)
}
@ -52,10 +47,6 @@ func (s *MockServer) AddTransaction(ctx context.Context, sessionId string, tx *t
return &types.SimulateTransactionResult{}, nil
}
func (s *MockServer) AddBundle(ctx context.Context, sessionId string, bundle Bundle) error {
return nil
}
func (s *MockServer) BuildBlock(ctx context.Context) error {
return nil
}

View file

@ -145,14 +145,6 @@ func (s *SessionManager) AddTransaction(sessionId string, tx *types.Transaction)
return builder.AddTransaction(tx)
}
func (s *SessionManager) AddBundle(sessionId string, bundle api.Bundle) error {
builder, err := s.getSession(sessionId)
if err != nil {
return err
}
return builder.AddBundle(nil) // TODO: Use api.Bundle type
}
func (s *SessionManager) BuildBlock(sessionId string) error {
builder, err := s.getSession(sessionId)
if err != nil {