mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-23 13:16:42 +00:00
added API versioning support
This commit is contained in:
commit
88a0ba0895
12 changed files with 86 additions and 18 deletions
|
|
@ -134,7 +134,7 @@ func (js *jsre) apiBindings(ipcpath string) {
|
||||||
|
|
||||||
// load only supported API's in javascript runtime
|
// load only supported API's in javascript runtime
|
||||||
shortcuts := "var eth = web3.eth; "
|
shortcuts := "var eth = web3.eth; "
|
||||||
for _, apiName := range apis {
|
for apiName, _ := range apis {
|
||||||
if apiName == api.Web3ApiName || apiName == api.EthApiName {
|
if apiName == api.Web3ApiName || apiName == api.EthApiName {
|
||||||
continue // manually mapped
|
continue // manually mapped
|
||||||
}
|
}
|
||||||
|
|
@ -194,7 +194,7 @@ func (self *jsre) exec(filename string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *jsre) suportedApis(ipcpath string) ([]string, error) {
|
func (self *jsre) suportedApis(ipcpath string) (map[string]string, error) {
|
||||||
config := comms.IpcConfig{
|
config := comms.IpcConfig{
|
||||||
Endpoint: ipcpath,
|
Endpoint: ipcpath,
|
||||||
}
|
}
|
||||||
|
|
@ -207,7 +207,7 @@ func (self *jsre) suportedApis(ipcpath string) ([]string, error) {
|
||||||
req := shared.Request{
|
req := shared.Request{
|
||||||
Id: 1,
|
Id: 1,
|
||||||
Jsonrpc: "2.0",
|
Jsonrpc: "2.0",
|
||||||
Method: "support_apis",
|
Method: "modules",
|
||||||
}
|
}
|
||||||
|
|
||||||
err = client.Send(req)
|
err = client.Send(req)
|
||||||
|
|
@ -222,7 +222,7 @@ func (self *jsre) suportedApis(ipcpath string) ([]string, error) {
|
||||||
|
|
||||||
if sucRes, ok := res.(shared.SuccessResponse); ok {
|
if sucRes, ok := res.(shared.SuccessResponse); ok {
|
||||||
data, _ := json.Marshal(sucRes.Result)
|
data, _ := json.Marshal(sucRes.Result)
|
||||||
apis := make([]string, 0)
|
apis := make(map[string]string)
|
||||||
err = json.Unmarshal(data, &apis)
|
err = json.Unmarshal(data, &apis)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return apis, nil
|
return apis, nil
|
||||||
|
|
@ -242,7 +242,11 @@ func (self *jsre) welcome(ipcpath string) {
|
||||||
+ " " + new Date(lastBlockTimestamp).toLocaleTimeString() + ")");`)
|
+ " " + new Date(lastBlockTimestamp).toLocaleTimeString() + ")");`)
|
||||||
|
|
||||||
if modules, err := self.suportedApis(ipcpath); err == nil {
|
if modules, err := self.suportedApis(ipcpath); err == nil {
|
||||||
self.re.Eval(fmt.Sprintf("var modules = '%s';", strings.Join(modules, " ")))
|
modulesVersionString := ""
|
||||||
|
for api, version := range modules {
|
||||||
|
modulesVersionString += fmt.Sprintf("%s:%s ", api, version)
|
||||||
|
}
|
||||||
|
self.re.Eval(fmt.Sprintf("var modules = '%s';", modulesVersionString))
|
||||||
self.re.Eval(`console.log(" modules: " + modules);`)
|
self.re.Eval(`console.log(" modules: " + modules);`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
AdminVersion = "1.0.0"
|
AdminApiversion = "1.0"
|
||||||
importBatchSize = 2500
|
importBatchSize = 2500
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -82,6 +82,10 @@ func (self *adminApi) Name() string {
|
||||||
return AdminApiName
|
return AdminApiName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *adminApi) ApiVersion() string {
|
||||||
|
return AdminApiversion
|
||||||
|
}
|
||||||
|
|
||||||
func (self *adminApi) AddPeer(req *shared.Request) (interface{}, error) {
|
func (self *adminApi) AddPeer(req *shared.Request) (interface{}, error) {
|
||||||
args := new(AddPeerArgs)
|
args := new(AddPeerArgs)
|
||||||
if err := self.codec.Decode(req.Params, &args); err != nil {
|
if err := self.codec.Decode(req.Params, &args); err != nil {
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,9 @@ type EthereumApi interface {
|
||||||
// API identifier
|
// API identifier
|
||||||
Name() string
|
Name() string
|
||||||
|
|
||||||
|
// API version
|
||||||
|
ApiVersion() string
|
||||||
|
|
||||||
// Execute the given request and returns the response or an error
|
// Execute the given request and returns the response or an error
|
||||||
Execute(*shared.Request) (interface{}, error)
|
Execute(*shared.Request) (interface{}, error)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DebugVersion = "1.0.0"
|
DebugApiVersion = "1.0"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -74,6 +74,10 @@ func (self *debugApi) Name() string {
|
||||||
return DebugApiName
|
return DebugApiName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *debugApi) ApiVersion() string {
|
||||||
|
return DebugApiVersion
|
||||||
|
}
|
||||||
|
|
||||||
func (self *debugApi) PrintBlock(req *shared.Request) (interface{}, error) {
|
func (self *debugApi) PrintBlock(req *shared.Request) (interface{}, error) {
|
||||||
args := new(BlockNumArg)
|
args := new(BlockNumArg)
|
||||||
if err := self.codec.Decode(req.Params, &args); err != nil {
|
if err := self.codec.Decode(req.Params, &args); err != nil {
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,10 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/xeth"
|
"github.com/ethereum/go-ethereum/xeth"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
EthApiVersion = "1.0"
|
||||||
|
)
|
||||||
|
|
||||||
// eth api provider
|
// eth api provider
|
||||||
// See https://github.com/ethereum/wiki/wiki/JSON-RPC
|
// See https://github.com/ethereum/wiki/wiki/JSON-RPC
|
||||||
type ethApi struct {
|
type ethApi struct {
|
||||||
|
|
@ -97,6 +101,10 @@ func (self *ethApi) Name() string {
|
||||||
return EthApiName
|
return EthApiName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *ethApi) ApiVersion() string {
|
||||||
|
return EthApiVersion
|
||||||
|
}
|
||||||
|
|
||||||
func (self *ethApi) Accounts(req *shared.Request) (interface{}, error) {
|
func (self *ethApi) Accounts(req *shared.Request) (interface{}, error) {
|
||||||
return self.xeth.Accounts(), nil
|
return self.xeth.Accounts(), nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,27 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import "github.com/ethereum/go-ethereum/rpc/shared"
|
import (
|
||||||
|
"github.com/ethereum/go-ethereum/rpc/shared"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
MergedApiVersion = "1.0"
|
||||||
|
)
|
||||||
|
|
||||||
// combines multiple API's
|
// combines multiple API's
|
||||||
type MergedApi struct {
|
type MergedApi struct {
|
||||||
apis []string
|
apis map[string]string
|
||||||
methods map[string]EthereumApi
|
methods map[string]EthereumApi
|
||||||
}
|
}
|
||||||
|
|
||||||
// create new merged api instance
|
// create new merged api instance
|
||||||
func newMergedApi(apis ...EthereumApi) *MergedApi {
|
func newMergedApi(apis ...EthereumApi) *MergedApi {
|
||||||
mergedApi := new(MergedApi)
|
mergedApi := new(MergedApi)
|
||||||
mergedApi.apis = make([]string, len(apis))
|
mergedApi.apis = make(map[string]string, len(apis))
|
||||||
mergedApi.methods = make(map[string]EthereumApi)
|
mergedApi.methods = make(map[string]EthereumApi)
|
||||||
|
|
||||||
for i, api := range apis {
|
for _, api := range apis {
|
||||||
mergedApi.apis[i] = api.Name()
|
mergedApi.apis[api.Name()] = api.ApiVersion()
|
||||||
for _, method := range api.Methods() {
|
for _, method := range api.Methods() {
|
||||||
mergedApi.methods[method] = api
|
mergedApi.methods[method] = api
|
||||||
}
|
}
|
||||||
|
|
@ -47,8 +53,12 @@ func (self *MergedApi) Name() string {
|
||||||
return MergedApiName
|
return MergedApiName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *MergedApi) ApiVersion() string {
|
||||||
|
return MergedApiVersion
|
||||||
|
}
|
||||||
|
|
||||||
func (self *MergedApi) handle(req *shared.Request) (interface{}, error) {
|
func (self *MergedApi) handle(req *shared.Request) (interface{}, error) {
|
||||||
if req.Method == "support_apis" { // provided API's
|
if req.Method == "modules" { // provided API's
|
||||||
return self.apis, nil
|
return self.apis, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
MinerVersion = "1.0.0"
|
MinerApiVersion = "1.0"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -69,6 +69,10 @@ func (self *minerApi) Name() string {
|
||||||
return MinerApiName
|
return MinerApiName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *minerApi) ApiVersion() string {
|
||||||
|
return MinerApiVersion
|
||||||
|
}
|
||||||
|
|
||||||
func (self *minerApi) StartMiner(req *shared.Request) (interface{}, error) {
|
func (self *minerApi) StartMiner(req *shared.Request) (interface{}, error) {
|
||||||
args := new(StartMinerArgs)
|
args := new(StartMinerArgs)
|
||||||
if err := self.codec.Decode(req.Params, &args); err != nil {
|
if err := self.codec.Decode(req.Params, &args); err != nil {
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,10 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/xeth"
|
"github.com/ethereum/go-ethereum/xeth"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
NetApiVersion = "1.0"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// mapping between methods and handlers
|
// mapping between methods and handlers
|
||||||
netMapping = map[string]nethandler{
|
netMapping = map[string]nethandler{
|
||||||
|
|
@ -62,6 +66,10 @@ func (self *netApi) Name() string {
|
||||||
return NetApiName
|
return NetApiName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *netApi) ApiVersion() string {
|
||||||
|
return NetApiVersion
|
||||||
|
}
|
||||||
|
|
||||||
// Network version
|
// Network version
|
||||||
func (self *netApi) Version(req *shared.Request) (interface{}, error) {
|
func (self *netApi) Version(req *shared.Request) (interface{}, error) {
|
||||||
return self.xeth.NetworkVersion(), nil
|
return self.xeth.NetworkVersion(), nil
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,10 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/xeth"
|
"github.com/ethereum/go-ethereum/xeth"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
PersonalApiVersion = "1.0"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// mapping between methods and handlers
|
// mapping between methods and handlers
|
||||||
personalMapping = map[string]personalhandler{
|
personalMapping = map[string]personalhandler{
|
||||||
|
|
@ -65,6 +69,10 @@ func (self *personalApi) Name() string {
|
||||||
return PersonalApiName
|
return PersonalApiName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *personalApi) ApiVersion() string {
|
||||||
|
return PersonalApiVersion
|
||||||
|
}
|
||||||
|
|
||||||
func (self *personalApi) ListAccounts(req *shared.Request) (interface{}, error) {
|
func (self *personalApi) ListAccounts(req *shared.Request) (interface{}, error) {
|
||||||
return self.xeth.Accounts(), nil
|
return self.xeth.Accounts(), nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,10 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/xeth"
|
"github.com/ethereum/go-ethereum/xeth"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ShhApiVersion = "1.0"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// mapping between methods and handlers
|
// mapping between methods and handlers
|
||||||
shhMapping = map[string]shhhandler{
|
shhMapping = map[string]shhhandler{
|
||||||
|
|
@ -71,6 +75,10 @@ func (self *shhApi) Name() string {
|
||||||
return ShhApiName
|
return ShhApiName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *shhApi) ApiVersion() string {
|
||||||
|
return ShhApiVersion
|
||||||
|
}
|
||||||
|
|
||||||
func (self *shhApi) Version(req *shared.Request) (interface{}, error) {
|
func (self *shhApi) Version(req *shared.Request) (interface{}, error) {
|
||||||
w := self.xeth.Whisper()
|
w := self.xeth.Whisper()
|
||||||
if w == nil {
|
if w == nil {
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,10 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/xeth"
|
"github.com/ethereum/go-ethereum/xeth"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
TxPoolApiVersion = "1.0"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// mapping between methods and handlers
|
// mapping between methods and handlers
|
||||||
txpoolMapping = map[string]txpoolhandler{
|
txpoolMapping = map[string]txpoolhandler{
|
||||||
|
|
@ -59,6 +63,10 @@ func (self *txPoolApi) Name() string {
|
||||||
return TxPoolApiName
|
return TxPoolApiName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *txPoolApi) ApiVersion() string {
|
||||||
|
return TxPoolApiVersion
|
||||||
|
}
|
||||||
|
|
||||||
func (self *txPoolApi) Status(req *shared.Request) (interface{}, error) {
|
func (self *txPoolApi) Status(req *shared.Request) (interface{}, error) {
|
||||||
return map[string]int{
|
return map[string]int{
|
||||||
"pending": self.ethereum.TxPool().GetTransactions().Len(),
|
"pending": self.ethereum.TxPool().GetTransactions().Len(),
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Web3Version = "1.0.0"
|
Web3ApiVersion = "1.0"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -63,9 +63,8 @@ func (self *web3Api) Name() string {
|
||||||
return Web3ApiName
|
return Web3ApiName
|
||||||
}
|
}
|
||||||
|
|
||||||
// Version of the API this instance provides
|
func (self *web3Api) ApiVersion() string {
|
||||||
func (self *web3Api) Version() string {
|
return Web3ApiVersion
|
||||||
return Web3Version
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculates the sha3 over req.Params.Data
|
// Calculates the sha3 over req.Params.Data
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue