mirror of
https://github.com/luxfi/evm.git
synced 2026-09-24 00:27:44 +00:00
refactor: rename lux_ RPC → generic evm_ namespace
evm_backend, evm_backends, evm_setBackend — generic, any chain can adopt. No lux-specific branding in the RPC interface.
This commit is contained in:
+29
-35
@@ -11,79 +11,73 @@ import (
|
||||
log "github.com/luxfi/log"
|
||||
)
|
||||
|
||||
// LuxAPI exposes Lux-specific RPC methods under the "lux" namespace.
|
||||
// Read methods are always available; write methods require admin API enabled.
|
||||
// EvmBackendAPI exposes EVM backend switching via RPC.
|
||||
// Generic — works on any chain that embeds this EVM.
|
||||
//
|
||||
// RPC methods:
|
||||
// - lux_evmBackend — returns the active EVM backend name
|
||||
// - lux_evmBackends — returns all available backend names
|
||||
// - lux_setEvmBackend — switches the active backend (admin-only)
|
||||
type LuxAPI struct {
|
||||
// RPC methods (namespace "evm"):
|
||||
// - evm_backend — returns the active EVM backend name
|
||||
// - evm_backends — returns all available backend names
|
||||
// - evm_setBackend — switches the active backend (admin-only)
|
||||
type EvmBackendAPI struct {
|
||||
vm *VM
|
||||
}
|
||||
|
||||
// NewLuxAPI creates a new LuxAPI instance.
|
||||
func NewLuxAPI(vm *VM) *LuxAPI {
|
||||
return &LuxAPI{vm: vm}
|
||||
func NewEvmBackendAPI(vm *VM) *EvmBackendAPI {
|
||||
return &EvmBackendAPI{vm: vm}
|
||||
}
|
||||
|
||||
// EvmBackendResult is the response for lux_evmBackend.
|
||||
type EvmBackendResult struct {
|
||||
type BackendResult struct {
|
||||
Backend string `json:"backend"`
|
||||
}
|
||||
|
||||
// EvmBackendsResult is the response for lux_evmBackends.
|
||||
type EvmBackendsResult struct {
|
||||
type BackendsResult struct {
|
||||
Backends []string `json:"backends"`
|
||||
Active string `json:"active"`
|
||||
}
|
||||
|
||||
// SetEvmBackendArgs is the argument for lux_setEvmBackend.
|
||||
type SetEvmBackendArgs struct {
|
||||
type SetBackendArgs struct {
|
||||
Backend string `json:"backend"`
|
||||
}
|
||||
|
||||
// SetEvmBackendResult is the response for lux_setEvmBackend.
|
||||
type SetEvmBackendResult struct {
|
||||
type SetBackendResult struct {
|
||||
Previous string `json:"previous"`
|
||||
Active string `json:"active"`
|
||||
}
|
||||
|
||||
// EvmBackend returns the currently active EVM backend.
|
||||
// RPC: lux_evmBackend
|
||||
func (api *LuxAPI) EvmBackend(_ context.Context) (*EvmBackendResult, error) {
|
||||
return &EvmBackendResult{
|
||||
// Backend returns the currently active EVM backend.
|
||||
// RPC: evm_backend
|
||||
func (api *EvmBackendAPI) Backend(_ context.Context) (*BackendResult, error) {
|
||||
return &BackendResult{
|
||||
Backend: string(parallel.ActiveBackend()),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// EvmBackends returns all available EVM backends and the active one.
|
||||
// RPC: lux_evmBackends
|
||||
func (api *LuxAPI) EvmBackends(_ context.Context) (*EvmBackendsResult, error) {
|
||||
// Backends returns all available EVM backends and the active one.
|
||||
// RPC: evm_backends
|
||||
func (api *EvmBackendAPI) Backends(_ context.Context) (*BackendsResult, error) {
|
||||
available := parallel.AvailableBackends()
|
||||
names := make([]string, len(available))
|
||||
for i, b := range available {
|
||||
names[i] = string(b)
|
||||
}
|
||||
return &EvmBackendsResult{
|
||||
return &BackendsResult{
|
||||
Backends: names,
|
||||
Active: string(parallel.ActiveBackend()),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SetEvmBackend switches the active EVM backend at runtime.
|
||||
// Only valid backend names are accepted: "gevm", "revm", "cevm", "auto".
|
||||
// Requires admin API to be enabled.
|
||||
// RPC: lux_setEvmBackend
|
||||
func (api *LuxAPI) SetEvmBackend(_ context.Context, args SetEvmBackendArgs) (*SetEvmBackendResult, error) {
|
||||
// SetBackend switches the active EVM backend at runtime.
|
||||
// Valid: "gevm", "revm", "cevm", "auto".
|
||||
// Requires admin API enabled.
|
||||
// RPC: evm_setBackend
|
||||
func (api *EvmBackendAPI) SetBackend(_ context.Context, args SetBackendArgs) (*SetBackendResult, error) {
|
||||
if !api.vm.config.AdminAPIEnabled {
|
||||
return nil, fmt.Errorf("admin API is disabled; lux_setEvmBackend requires admin access")
|
||||
return nil, fmt.Errorf("admin API disabled; evm_setBackend requires admin access")
|
||||
}
|
||||
|
||||
requested := parallel.EVMBackend(args.Backend)
|
||||
switch requested {
|
||||
case parallel.GoEVM, parallel.RustEVM, parallel.CppEVM, parallel.AutoEVM:
|
||||
// valid
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown backend %q; valid: gevm, revm, cevm, auto", args.Backend)
|
||||
}
|
||||
@@ -92,9 +86,9 @@ func (api *LuxAPI) SetEvmBackend(_ context.Context, args SetEvmBackendArgs) (*Se
|
||||
parallel.SetBackend(requested)
|
||||
active := parallel.ActiveBackend()
|
||||
|
||||
log.Info("lux_setEvmBackend", "previous", string(previous), "requested", args.Backend, "active", string(active))
|
||||
log.Info("evm_setBackend", "previous", string(previous), "requested", args.Backend, "active", string(active))
|
||||
|
||||
return &SetEvmBackendResult{
|
||||
return &SetBackendResult{
|
||||
Previous: string(previous),
|
||||
Active: string(active),
|
||||
}, nil
|
||||
|
||||
+25
-36
@@ -11,31 +11,28 @@ import (
|
||||
"github.com/luxfi/evm/plugin/evm/config"
|
||||
)
|
||||
|
||||
func TestLuxAPIEvmBackend(t *testing.T) {
|
||||
vm := &VM{config: config.Config{AdminAPIEnabled: true}}
|
||||
api := NewLuxAPI(vm)
|
||||
func TestEvmBackendAPI_Backend(t *testing.T) {
|
||||
api := NewEvmBackendAPI(&VM{config: config.Config{AdminAPIEnabled: true}})
|
||||
|
||||
result, err := api.EvmBackend(context.Background())
|
||||
result, err := api.Backend(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("EvmBackend returned error: %v", err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Backend != string(parallel.ActiveBackend()) {
|
||||
t.Fatalf("expected %q, got %q", parallel.ActiveBackend(), result.Backend)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLuxAPIEvmBackends(t *testing.T) {
|
||||
vm := &VM{config: config.Config{AdminAPIEnabled: true}}
|
||||
api := NewLuxAPI(vm)
|
||||
func TestEvmBackendAPI_Backends(t *testing.T) {
|
||||
api := NewEvmBackendAPI(&VM{config: config.Config{AdminAPIEnabled: true}})
|
||||
|
||||
result, err := api.EvmBackends(context.Background())
|
||||
result, err := api.Backends(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("EvmBackends returned error: %v", err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(result.Backends) == 0 {
|
||||
t.Fatal("expected at least one backend (gevm)")
|
||||
t.Fatal("expected at least one backend")
|
||||
}
|
||||
// gevm is always present
|
||||
found := false
|
||||
for _, b := range result.Backends {
|
||||
if b == "gevm" {
|
||||
@@ -45,50 +42,42 @@ func TestLuxAPIEvmBackends(t *testing.T) {
|
||||
if !found {
|
||||
t.Fatalf("expected gevm in backends, got %v", result.Backends)
|
||||
}
|
||||
if result.Active == "" {
|
||||
t.Fatal("active backend must not be empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLuxAPISetEvmBackend(t *testing.T) {
|
||||
vm := &VM{config: config.Config{AdminAPIEnabled: true}}
|
||||
api := NewLuxAPI(vm)
|
||||
func TestEvmBackendAPI_SetBackend(t *testing.T) {
|
||||
api := NewEvmBackendAPI(&VM{config: config.Config{AdminAPIEnabled: true}})
|
||||
|
||||
// Switch to gevm (always available)
|
||||
result, err := api.SetEvmBackend(context.Background(), SetEvmBackendArgs{Backend: "gevm"})
|
||||
result, err := api.SetBackend(context.Background(), SetBackendArgs{Backend: "gevm"})
|
||||
if err != nil {
|
||||
t.Fatalf("SetEvmBackend returned error: %v", err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Active != "gevm" {
|
||||
t.Fatalf("expected active=gevm, got %q", result.Active)
|
||||
t.Fatalf("expected gevm, got %q", result.Active)
|
||||
}
|
||||
|
||||
// auto should resolve to gevm when no other backends registered
|
||||
result, err = api.SetEvmBackend(context.Background(), SetEvmBackendArgs{Backend: "auto"})
|
||||
result, err = api.SetBackend(context.Background(), SetBackendArgs{Backend: "auto"})
|
||||
if err != nil {
|
||||
t.Fatalf("SetEvmBackend(auto) returned error: %v", err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Active != "gevm" {
|
||||
t.Fatalf("expected active=gevm after auto, got %q", result.Active)
|
||||
t.Fatalf("expected gevm after auto, got %q", result.Active)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLuxAPISetEvmBackendInvalidName(t *testing.T) {
|
||||
vm := &VM{config: config.Config{AdminAPIEnabled: true}}
|
||||
api := NewLuxAPI(vm)
|
||||
func TestEvmBackendAPI_InvalidBackend(t *testing.T) {
|
||||
api := NewEvmBackendAPI(&VM{config: config.Config{AdminAPIEnabled: true}})
|
||||
|
||||
_, err := api.SetEvmBackend(context.Background(), SetEvmBackendArgs{Backend: "invalid"})
|
||||
_, err := api.SetBackend(context.Background(), SetBackendArgs{Backend: "invalid"})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid backend name")
|
||||
t.Fatal("expected error for invalid backend")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLuxAPISetEvmBackendAdminDisabled(t *testing.T) {
|
||||
vm := &VM{config: config.Config{AdminAPIEnabled: false}}
|
||||
api := NewLuxAPI(vm)
|
||||
func TestEvmBackendAPI_AdminDisabled(t *testing.T) {
|
||||
api := NewEvmBackendAPI(&VM{config: config.Config{AdminAPIEnabled: false}})
|
||||
|
||||
_, err := api.SetEvmBackend(context.Background(), SetEvmBackendArgs{Backend: "gevm"})
|
||||
_, err := api.SetBackend(context.Background(), SetBackendArgs{Backend: "gevm"})
|
||||
if err == nil {
|
||||
t.Fatal("expected error when admin API is disabled")
|
||||
t.Fatal("expected error when admin disabled")
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -1656,9 +1656,9 @@ func (vm *VM) CreateHandlers(context.Context) (map[string]http.Handler, error) {
|
||||
log.Info("CreateHandlers validators API handler created successfully")
|
||||
}
|
||||
|
||||
// Register lux namespace (lux_evmBackend, lux_evmBackends, lux_setEvmBackend)
|
||||
if err := handler.RegisterName("lux", NewLuxAPI(vm)); err != nil {
|
||||
return nil, fmt.Errorf("failed to register lux API: %w", err)
|
||||
// Register evm namespace (evm_backend, evm_backends, evm_setBackend)
|
||||
if err := handler.RegisterName("evm", NewEvmBackendAPI(vm)); err != nil {
|
||||
return nil, fmt.Errorf("failed to register evm backend API: %w", err)
|
||||
}
|
||||
enabledAPIs = append(enabledAPIs, "lux")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user