From 2cdead6754f63ec3bbe06a0dce96b9b10daf1ce9 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Tue, 1 Oct 2024 13:06:54 +0200 Subject: [PATCH] Allow adding console extensions --- console/console.go | 2 +- internal/web3ext/web3ext.go | 161 ++++++++---------------------------- node/node.go | 3 + rpc/types.go | 11 +-- 4 files changed, 44 insertions(+), 133 deletions(-) diff --git a/console/console.go b/console/console.go index 5acb4cdccb..596240b4b9 100644 --- a/console/console.go +++ b/console/console.go @@ -221,7 +221,7 @@ func (c *Console) initExtensions() error { continue } aliases[api] = struct{}{} - if file, ok := web3ext.Modules[api]; ok { + if file := web3ext.DefaultModules.Get(api); file != "" { if err = c.jsre.Compile(api+".js", file); err != nil { return fmt.Errorf("%s.js: %v", api, err) } diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index 4a53e2c829..973f0b63bc 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -17,20 +17,40 @@ // Package web3ext contains geth specific web3.js extensions. package web3ext -var Modules = map[string]string{ - "admin": AdminJs, - "clique": CliqueJs, - "ethash": EthashJs, - "debug": DebugJs, - "eth": EthJs, - "miner": MinerJs, - "net": NetJs, - "personal": PersonalJs, - "rpc": RpcJs, - "txpool": TxpoolJs, - "les": LESJs, - "vflux": VfluxJs, - "dev": DevJs, +import "fmt" + +type ModuleDirectory struct { + modules map[string]string +} + +var DefaultModules = &ModuleDirectory{ + modules: map[string]string{ + "admin": AdminJs, + "clique": CliqueJs, + "debug": DebugJs, + "eth": EthJs, + "miner": MinerJs, + "net": NetJs, + "personal": PersonalJs, + "rpc": RpcJs, + "txpool": TxpoolJs, + "dev": DevJs, + }, +} + +func (d *ModuleDirectory) Add(name, code string) { + if code == "" { + return + } + prev := "" + if c, ok := d.modules[name]; ok { + prev = fmt.Sprintf("%s\n", c) + } + d.modules[name] = fmt.Sprintf("%s%s", prev, code) +} + +func (d *ModuleDirectory) Get(name string) string { + return d.modules[name] } const CliqueJs = ` @@ -90,34 +110,6 @@ web3._extend({ }); ` -const EthashJs = ` -web3._extend({ - property: 'ethash', - methods: [ - new web3._extend.Method({ - name: 'getWork', - call: 'ethash_getWork', - params: 0 - }), - new web3._extend.Method({ - name: 'getHashrate', - call: 'ethash_getHashrate', - params: 0 - }), - new web3._extend.Method({ - name: 'submitWork', - call: 'ethash_submitWork', - params: 3, - }), - new web3._extend.Method({ - name: 'submitHashrate', - call: 'ethash_submitHashrate', - params: 2, - }), - ] -}); -` - const AdminJs = ` web3._extend({ property: 'admin', @@ -790,91 +782,6 @@ web3._extend({ }); ` -const LESJs = ` -web3._extend({ - property: 'les', - methods: - [ - new web3._extend.Method({ - name: 'getCheckpoint', - call: 'les_getCheckpoint', - params: 1 - }), - new web3._extend.Method({ - name: 'clientInfo', - call: 'les_clientInfo', - params: 1 - }), - new web3._extend.Method({ - name: 'priorityClientInfo', - call: 'les_priorityClientInfo', - params: 3 - }), - new web3._extend.Method({ - name: 'setClientParams', - call: 'les_setClientParams', - params: 2 - }), - new web3._extend.Method({ - name: 'setDefaultParams', - call: 'les_setDefaultParams', - params: 1 - }), - new web3._extend.Method({ - name: 'addBalance', - call: 'les_addBalance', - params: 2 - }), - ], - properties: - [ - new web3._extend.Property({ - name: 'latestCheckpoint', - getter: 'les_latestCheckpoint' - }), - new web3._extend.Property({ - name: 'checkpointContractAddress', - getter: 'les_getCheckpointContractAddress' - }), - new web3._extend.Property({ - name: 'serverInfo', - getter: 'les_serverInfo' - }), - ] -}); -` - -const VfluxJs = ` -web3._extend({ - property: 'vflux', - methods: - [ - new web3._extend.Method({ - name: 'distribution', - call: 'vflux_distribution', - params: 2 - }), - new web3._extend.Method({ - name: 'timeout', - call: 'vflux_timeout', - params: 2 - }), - new web3._extend.Method({ - name: 'value', - call: 'vflux_value', - params: 2 - }), - ], - properties: - [ - new web3._extend.Property({ - name: 'requestStats', - getter: 'vflux_requestStats' - }), - ] -}); -` - const DevJs = ` web3._extend({ property: 'dev', diff --git a/node/node.go b/node/node.go index 633f88f058..f513edfd16 100644 --- a/node/node.go +++ b/node/node.go @@ -36,6 +36,7 @@ import ( "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb/memorydb" "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/internal/web3ext" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/rpc" @@ -386,6 +387,8 @@ func (n *Node) startRPC() error { } } apis = append(apis, api) + // Register console extension. + web3ext.DefaultModules.Add(api.Namespace, api.ConsoleExtension) } if err := n.startInProc(apis); err != nil { return err diff --git a/rpc/types.go b/rpc/types.go index 2e53174b87..6c02dfe83e 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -30,11 +30,12 @@ import ( // API describes the set of methods offered over the RPC interface type API struct { - Namespace string // namespace under which the rpc methods of Service are exposed - Version string // deprecated - this field is no longer used, but retained for compatibility - Service interface{} // receiver instance which holds the methods - Public bool // deprecated - this field is no longer used, but retained for compatibility - Authenticated bool // whether the api should only be available behind authentication. + Namespace string // namespace under which the rpc methods of Service are exposed + Version string // deprecated - this field is no longer used, but retained for compatibility + Service interface{} // receiver instance which holds the methods + Public bool // deprecated - this field is no longer used, but retained for compatibility + Authenticated bool // whether the api should only be available behind authentication. + ConsoleExtension string // web3.js instructions to add methods for console auto-completion. } // ServerCodec implements reading, parsing and writing RPC messages for the server side of