mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 02:12:23 +00:00
Allow adding console extensions
This commit is contained in:
parent
9ae1444d48
commit
2cdead6754
4 changed files with 44 additions and 133 deletions
|
|
@ -221,7 +221,7 @@ func (c *Console) initExtensions() error {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
aliases[api] = struct{}{}
|
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 {
|
if err = c.jsre.Compile(api+".js", file); err != nil {
|
||||||
return fmt.Errorf("%s.js: %v", api, err)
|
return fmt.Errorf("%s.js: %v", api, err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,10 +17,16 @@
|
||||||
// Package web3ext contains geth specific web3.js extensions.
|
// Package web3ext contains geth specific web3.js extensions.
|
||||||
package web3ext
|
package web3ext
|
||||||
|
|
||||||
var Modules = map[string]string{
|
import "fmt"
|
||||||
|
|
||||||
|
type ModuleDirectory struct {
|
||||||
|
modules map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
var DefaultModules = &ModuleDirectory{
|
||||||
|
modules: map[string]string{
|
||||||
"admin": AdminJs,
|
"admin": AdminJs,
|
||||||
"clique": CliqueJs,
|
"clique": CliqueJs,
|
||||||
"ethash": EthashJs,
|
|
||||||
"debug": DebugJs,
|
"debug": DebugJs,
|
||||||
"eth": EthJs,
|
"eth": EthJs,
|
||||||
"miner": MinerJs,
|
"miner": MinerJs,
|
||||||
|
|
@ -28,9 +34,23 @@ var Modules = map[string]string{
|
||||||
"personal": PersonalJs,
|
"personal": PersonalJs,
|
||||||
"rpc": RpcJs,
|
"rpc": RpcJs,
|
||||||
"txpool": TxpoolJs,
|
"txpool": TxpoolJs,
|
||||||
"les": LESJs,
|
|
||||||
"vflux": VfluxJs,
|
|
||||||
"dev": DevJs,
|
"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 = `
|
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 = `
|
const AdminJs = `
|
||||||
web3._extend({
|
web3._extend({
|
||||||
property: 'admin',
|
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 = `
|
const DevJs = `
|
||||||
web3._extend({
|
web3._extend({
|
||||||
property: 'dev',
|
property: 'dev',
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/ethdb"
|
"github.com/ethereum/go-ethereum/ethdb"
|
||||||
"github.com/ethereum/go-ethereum/ethdb/memorydb"
|
"github.com/ethereum/go-ethereum/ethdb/memorydb"
|
||||||
"github.com/ethereum/go-ethereum/event"
|
"github.com/ethereum/go-ethereum/event"
|
||||||
|
"github.com/ethereum/go-ethereum/internal/web3ext"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/ethereum/go-ethereum/p2p"
|
"github.com/ethereum/go-ethereum/p2p"
|
||||||
"github.com/ethereum/go-ethereum/rpc"
|
"github.com/ethereum/go-ethereum/rpc"
|
||||||
|
|
@ -386,6 +387,8 @@ func (n *Node) startRPC() error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
apis = append(apis, api)
|
apis = append(apis, api)
|
||||||
|
// Register console extension.
|
||||||
|
web3ext.DefaultModules.Add(api.Namespace, api.ConsoleExtension)
|
||||||
}
|
}
|
||||||
if err := n.startInProc(apis); err != nil {
|
if err := n.startInProc(apis); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,7 @@ type API struct {
|
||||||
Service interface{} // receiver instance which holds the methods
|
Service interface{} // receiver instance which holds the methods
|
||||||
Public bool // deprecated - this field is no longer used, but retained for compatibility
|
Public bool // deprecated - this field is no longer used, but retained for compatibility
|
||||||
Authenticated bool // whether the api should only be available behind authentication.
|
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
|
// ServerCodec implements reading, parsing and writing RPC messages for the server side of
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue