mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 02:42:27 +00:00
Merge pull request #24 from flashbots/feature/builder-api-balance
Add GetBalance endpoint
This commit is contained in:
commit
287a1071ac
7 changed files with 60 additions and 0 deletions
|
|
@ -172,6 +172,10 @@ func (b *Builder) AddBundles(bundles []*suavextypes.Bundle) ([]*suavextypes.Simu
|
||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Builder) GetBalance(addr common.Address) *big.Int {
|
||||||
|
return b.env.state.GetBalance(addr)
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Builder) FillPending() error {
|
func (b *Builder) FillPending() error {
|
||||||
if err := b.wrk.commitPendingTxs(b.env); err != nil {
|
if err := b.wrk.commitPendingTxs(b.env); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
|
|
@ -270,6 +270,26 @@ func TestBuilder_Bid(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBuilder_Balance(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
config, backend := newMockBuilderConfig(t)
|
||||||
|
|
||||||
|
builder, err := NewBuilder(config, &BuilderArgs{})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
balance := builder.GetBalance(testBankAddress)
|
||||||
|
require.Equal(t, balance, testBankFunds)
|
||||||
|
|
||||||
|
// make a random txn that consumes gas
|
||||||
|
tx1 := backend.newRandomTx(true)
|
||||||
|
_, err = builder.AddTransaction(tx1)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
balance2 := builder.GetBalance(testBankAddress)
|
||||||
|
require.NotEqual(t, balance2, testBankFunds)
|
||||||
|
}
|
||||||
|
|
||||||
func newMockBuilderConfig(t *testing.T) (*BuilderConfig, *testWorkerBackend) {
|
func newMockBuilderConfig(t *testing.T) (*BuilderConfig, *testWorkerBackend) {
|
||||||
var (
|
var (
|
||||||
db = rawdb.NewMemoryDatabase()
|
db = rawdb.NewMemoryDatabase()
|
||||||
|
|
|
||||||
|
|
@ -96,4 +96,5 @@ type API interface {
|
||||||
AddBundles(ctx context.Context, sessionId string, bundles []*Bundle) ([]*SimulateBundleResult, error)
|
AddBundles(ctx context.Context, sessionId string, bundles []*Bundle) ([]*SimulateBundleResult, error)
|
||||||
BuildBlock(ctx context.Context, sessionId string) error
|
BuildBlock(ctx context.Context, sessionId string) error
|
||||||
Bid(ctx context.Context, sessioId string, blsPubKey phase0.BLSPubKey) (*SubmitBlockRequest, error)
|
Bid(ctx context.Context, sessioId string, blsPubKey phase0.BLSPubKey) (*SubmitBlockRequest, error)
|
||||||
|
GetBalance(ctx context.Context, sessionId string, addr common.Address) (*big.Int, error)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,10 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"math/big"
|
||||||
|
|
||||||
"github.com/attestantio/go-eth2-client/spec/phase0"
|
"github.com/attestantio/go-eth2-client/spec/phase0"
|
||||||
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/ethereum/go-ethereum/rpc"
|
"github.com/ethereum/go-ethereum/rpc"
|
||||||
)
|
)
|
||||||
|
|
@ -63,3 +65,9 @@ func (a *APIClient) Bid(ctx context.Context, sessioId string, blsPubKey phase0.B
|
||||||
err := a.rpc.CallContext(ctx, &req, "suavex_bid", sessioId, blsPubKey)
|
err := a.rpc.CallContext(ctx, &req, "suavex_bid", sessioId, blsPubKey)
|
||||||
return req, err
|
return req, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *APIClient) GetBalance(ctx context.Context, sessionId string, addr common.Address) (*big.Int, error) {
|
||||||
|
var balance *big.Int
|
||||||
|
err := a.rpc.CallContext(ctx, &balance, "suavex_getBalance", sessionId, addr)
|
||||||
|
return balance, err
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,10 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"math/big"
|
||||||
|
|
||||||
"github.com/attestantio/go-eth2-client/spec/phase0"
|
"github.com/attestantio/go-eth2-client/spec/phase0"
|
||||||
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -17,6 +19,7 @@ type SessionManager interface {
|
||||||
AddBundles(sessionId string, bundles []*Bundle) ([]*SimulateBundleResult, error)
|
AddBundles(sessionId string, bundles []*Bundle) ([]*SimulateBundleResult, error)
|
||||||
BuildBlock(sessionId string) error
|
BuildBlock(sessionId string) error
|
||||||
Bid(sessionId string, blsPubKey phase0.BLSPubKey) (*SubmitBlockRequest, error)
|
Bid(sessionId string, blsPubKey phase0.BLSPubKey) (*SubmitBlockRequest, error)
|
||||||
|
GetBalance(sessionId string, addr common.Address) (*big.Int, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer(s SessionManager) *Server {
|
func NewServer(s SessionManager) *Server {
|
||||||
|
|
@ -54,6 +57,10 @@ func (s *Server) Bid(ctx context.Context, sessionId string, blsPubKey phase0.BLS
|
||||||
return s.sessionMngr.Bid(sessionId, blsPubKey)
|
return s.sessionMngr.Bid(sessionId, blsPubKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) GetBalance(ctx context.Context, sessionId string, addr common.Address) (*big.Int, error) {
|
||||||
|
return s.sessionMngr.GetBalance(sessionId, addr)
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Remove
|
// TODO: Remove
|
||||||
type MockServer struct {
|
type MockServer struct {
|
||||||
}
|
}
|
||||||
|
|
@ -69,3 +76,7 @@ func (s *MockServer) AddTransaction(ctx context.Context, sessionId string, tx *t
|
||||||
func (s *MockServer) BuildBlock(ctx context.Context) error {
|
func (s *MockServer) BuildBlock(ctx context.Context) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *MockServer) GetBalance(ctx context.Context, sessionId string, addr common.Address) (*big.Int, error) {
|
||||||
|
return big.NewInt(0), nil
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,10 @@ func TestAPI(t *testing.T) {
|
||||||
txn := types.NewTransaction(0, common.Address{}, big.NewInt(1), 1, big.NewInt(1), []byte{})
|
txn := types.NewTransaction(0, common.Address{}, big.NewInt(1), 1, big.NewInt(1), []byte{})
|
||||||
_, err = c.AddTransaction(context.Background(), "1", txn)
|
_, err = c.AddTransaction(context.Background(), "1", txn)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
_, err = c.GetBalance(context.Background(), "1", common.Address{})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
_, err = c.AddTransactions(context.Background(), "1", []*types.Transaction{txn})
|
_, err = c.AddTransactions(context.Background(), "1", []*types.Transaction{txn})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
|
@ -62,3 +66,7 @@ func (nullSessionManager) BuildBlock(sessionId string) error {
|
||||||
func (nullSessionManager) Bid(sessioId string, blsPubKey phase0.BLSPubKey) (*SubmitBlockRequest, error) {
|
func (nullSessionManager) Bid(sessioId string, blsPubKey phase0.BLSPubKey) (*SubmitBlockRequest, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (nullSessionManager) GetBalance(sessionId string, addr common.Address) (*big.Int, error) {
|
||||||
|
return big.NewInt(0), nil
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -183,6 +183,14 @@ func (s *SessionManager) Bid(sessionId string, blsPubKey phase0.BLSPubKey) (*api
|
||||||
return builder.Bid(blsPubKey)
|
return builder.Bid(blsPubKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SessionManager) GetBalance(sessionId string, addr common.Address) (*big.Int, error) {
|
||||||
|
builder, err := s.getSession(sessionId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return builder.GetBalance(addr), nil
|
||||||
|
}
|
||||||
|
|
||||||
// CalcBaseFee calculates the basefee of the header.
|
// CalcBaseFee calculates the basefee of the header.
|
||||||
func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
|
func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
|
||||||
// If the current block is the first EIP-1559 block, return the InitialBaseFee.
|
// If the current block is the first EIP-1559 block, return the InitialBaseFee.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue