Allow adding console extensions

This commit is contained in:
Sina Mahmoodi 2024-10-01 13:06:54 +02:00
parent 9ae1444d48
commit 2cdead6754
4 changed files with 44 additions and 133 deletions

View file

@ -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)
}

View file

@ -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',

View file

@ -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

View file

@ -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