-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move suave-enabled api backends to suave package (#56)
- Loading branch information
Showing
5 changed files
with
134 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package backends | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/beacon/engine" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
suave "github.com/ethereum/go-ethereum/suave/core" | ||
) | ||
|
||
// EthBackend is the set of functions exposed from the SUAVE-enabled node | ||
type EthBackend interface { | ||
BuildEth2Block(ctx context.Context, buildArgs *types.BuildBlockArgs, txs types.Transactions) (*engine.ExecutionPayloadEnvelope, error) | ||
BuildEth2BlockFromBundles(ctx context.Context, buildArgs *types.BuildBlockArgs, bundles []types.SBundle) (*engine.ExecutionPayloadEnvelope, error) | ||
} | ||
|
||
var _ EthBackend = &EthBackendServer{} | ||
|
||
// EthBackendServerBackend is the interface implemented by the SUAVE-enabled node | ||
// to resolve the EthBackend server queries | ||
type EthBackendServerBackend interface { | ||
CurrentHeader() *types.Header | ||
BuildBlockFromTxs(ctx context.Context, buildArgs *suave.BuildBlockArgs, txs types.Transactions) (*types.Block, *big.Int, error) | ||
BuildBlockFromBundles(ctx context.Context, buildArgs *suave.BuildBlockArgs, bundles []types.SBundle) (*types.Block, *big.Int, error) | ||
} | ||
|
||
type EthBackendServer struct { | ||
b EthBackendServerBackend | ||
} | ||
|
||
func NewEthBackendServer(b EthBackendServerBackend) *EthBackendServer { | ||
return &EthBackendServer{b} | ||
} | ||
|
||
func (e *EthBackendServer) BuildEth2Block(ctx context.Context, buildArgs *types.BuildBlockArgs, txs types.Transactions) (*engine.ExecutionPayloadEnvelope, error) { | ||
if buildArgs == nil { | ||
head := e.b.CurrentHeader() | ||
buildArgs = &types.BuildBlockArgs{ | ||
Parent: head.Hash(), | ||
Timestamp: head.Time + uint64(12), | ||
FeeRecipient: common.Address{0x42}, | ||
GasLimit: 30000000, | ||
Random: head.Root, | ||
Withdrawals: nil, | ||
} | ||
} | ||
|
||
block, profit, err := e.b.BuildBlockFromTxs(ctx, buildArgs, txs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return engine.BlockToExecutableData(block, profit), nil | ||
} | ||
|
||
func (e *EthBackendServer) BuildEth2BlockFromBundles(ctx context.Context, buildArgs *types.BuildBlockArgs, bundles []types.SBundle) (*engine.ExecutionPayloadEnvelope, error) { | ||
if buildArgs == nil { | ||
head := e.b.CurrentHeader() | ||
buildArgs = &types.BuildBlockArgs{ | ||
Parent: head.Hash(), | ||
Timestamp: head.Time + uint64(12), | ||
FeeRecipient: common.Address{0x42}, | ||
GasLimit: 30000000, | ||
Random: head.Root, | ||
Withdrawals: nil, | ||
} | ||
} | ||
|
||
block, profit, err := e.b.BuildBlockFromBundles(ctx, buildArgs, bundles) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return engine.BlockToExecutableData(block, profit), nil | ||
} |
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,50 @@ | ||
package backends | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/rpc" | ||
"github.com/ethereum/go-ethereum/trie" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ethereum/go-ethereum/core/types" | ||
suave "github.com/ethereum/go-ethereum/suave/core" | ||
) | ||
|
||
func TestEthBackend_Compatibility(t *testing.T) { | ||
// This test ensures that the client is able to call to the server. | ||
// It does not cover the internal logic implemention of the endpoints. | ||
srv := rpc.NewServer() | ||
require.NoError(t, srv.RegisterName("eth", NewEthBackendServer(&mockBackend{}))) | ||
|
||
clt := &RemoteEthBackend{client: rpc.DialInProc(srv)} | ||
|
||
_, err := clt.BuildEthBlock(context.Background(), &types.BuildBlockArgs{}, nil) | ||
require.NoError(t, err) | ||
|
||
_, err = clt.BuildEthBlockFromBundles(context.Background(), &types.BuildBlockArgs{}, nil) | ||
require.NoError(t, err) | ||
} | ||
|
||
// mockBackend is a backend for the EthBackendServer that returns mock data | ||
type mockBackend struct{} | ||
|
||
func (n *mockBackend) CurrentHeader() *types.Header { | ||
return &types.Header{} | ||
} | ||
|
||
func (n *mockBackend) BuildBlockFromTxs(ctx context.Context, buildArgs *suave.BuildBlockArgs, txs types.Transactions) (*types.Block, *big.Int, error) { | ||
block := types.NewBlock(&types.Header{GasUsed: 1000, BaseFee: big.NewInt(1)}, txs, nil, nil, trie.NewStackTrie(nil)) | ||
return block, big.NewInt(11000), nil | ||
} | ||
|
||
func (n *mockBackend) BuildBlockFromBundles(ctx context.Context, buildArgs *suave.BuildBlockArgs, bundles []types.SBundle) (*types.Block, *big.Int, error) { | ||
var txs types.Transactions | ||
for _, bundle := range bundles { | ||
txs = append(txs, bundle.Txs...) | ||
} | ||
block := types.NewBlock(&types.Header{GasUsed: 1000, BaseFee: big.NewInt(1)}, txs, nil, nil, trie.NewStackTrie(nil)) | ||
return block, big.NewInt(11000), nil | ||
} |