added API versioning support

This commit is contained in:
Bas van Kervel 2015-06-10 15:57:47 +02:00
commit 88a0ba0895
12 changed files with 86 additions and 18 deletions

View file

@ -134,7 +134,7 @@ func (js *jsre) apiBindings(ipcpath string) {
// load only supported API's in javascript runtime
shortcuts := "var eth = web3.eth; "
for _, apiName := range apis {
for apiName, _ := range apis {
if apiName == api.Web3ApiName || apiName == api.EthApiName {
continue // manually mapped
}
@ -194,7 +194,7 @@ func (self *jsre) exec(filename string) error {
return nil
}
func (self *jsre) suportedApis(ipcpath string) ([]string, error) {
func (self *jsre) suportedApis(ipcpath string) (map[string]string, error) {
config := comms.IpcConfig{
Endpoint: ipcpath,
}
@ -207,7 +207,7 @@ func (self *jsre) suportedApis(ipcpath string) ([]string, error) {
req := shared.Request{
Id: 1,
Jsonrpc: "2.0",
Method: "support_apis",
Method: "modules",
}
err = client.Send(req)
@ -222,7 +222,7 @@ func (self *jsre) suportedApis(ipcpath string) ([]string, error) {
if sucRes, ok := res.(shared.SuccessResponse); ok {
data, _ := json.Marshal(sucRes.Result)
apis := make([]string, 0)
apis := make(map[string]string)
err = json.Unmarshal(data, &apis)
if err == nil {
return apis, nil
@ -242,7 +242,11 @@ func (self *jsre) welcome(ipcpath string) {
+ " " + new Date(lastBlockTimestamp).toLocaleTimeString() + ")");`)
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);`)
}
}

View file

@ -16,7 +16,7 @@ import (
)
const (
AdminVersion = "1.0.0"
AdminApiversion = "1.0"
importBatchSize = 2500
)
@ -82,6 +82,10 @@ func (self *adminApi) Name() string {
return AdminApiName
}
func (self *adminApi) ApiVersion() string {
return AdminApiversion
}
func (self *adminApi) AddPeer(req *shared.Request) (interface{}, error) {
args := new(AddPeerArgs)
if err := self.codec.Decode(req.Params, &args); err != nil {

View file

@ -32,6 +32,9 @@ type EthereumApi interface {
// API identifier
Name() string
// API version
ApiVersion() string
// Execute the given request and returns the response or an error
Execute(*shared.Request) (interface{}, error)

View file

@ -14,7 +14,7 @@ import (
)
const (
DebugVersion = "1.0.0"
DebugApiVersion = "1.0"
)
var (
@ -74,6 +74,10 @@ func (self *debugApi) Name() string {
return DebugApiName
}
func (self *debugApi) ApiVersion() string {
return DebugApiVersion
}
func (self *debugApi) PrintBlock(req *shared.Request) (interface{}, error) {
args := new(BlockNumArg)
if err := self.codec.Decode(req.Params, &args); err != nil {

View file

@ -11,6 +11,10 @@ import (
"github.com/ethereum/go-ethereum/xeth"
)
const (
EthApiVersion = "1.0"
)
// eth api provider
// See https://github.com/ethereum/wiki/wiki/JSON-RPC
type ethApi struct {
@ -97,6 +101,10 @@ func (self *ethApi) Name() string {
return EthApiName
}
func (self *ethApi) ApiVersion() string {
return EthApiVersion
}
func (self *ethApi) Accounts(req *shared.Request) (interface{}, error) {
return self.xeth.Accounts(), nil
}

View file

@ -1,21 +1,27 @@
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
type MergedApi struct {
apis []string
apis map[string]string
methods map[string]EthereumApi
}
// create new merged api instance
func newMergedApi(apis ...EthereumApi) *MergedApi {
mergedApi := new(MergedApi)
mergedApi.apis = make([]string, len(apis))
mergedApi.apis = make(map[string]string, len(apis))
mergedApi.methods = make(map[string]EthereumApi)
for i, api := range apis {
mergedApi.apis[i] = api.Name()
for _, api := range apis {
mergedApi.apis[api.Name()] = api.ApiVersion()
for _, method := range api.Methods() {
mergedApi.methods[method] = api
}
@ -47,8 +53,12 @@ func (self *MergedApi) Name() string {
return MergedApiName
}
func (self *MergedApi) ApiVersion() string {
return MergedApiVersion
}
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
}

View file

@ -9,7 +9,7 @@ import (
)
const (
MinerVersion = "1.0.0"
MinerApiVersion = "1.0"
)
var (
@ -69,6 +69,10 @@ func (self *minerApi) Name() string {
return MinerApiName
}
func (self *minerApi) ApiVersion() string {
return MinerApiVersion
}
func (self *minerApi) StartMiner(req *shared.Request) (interface{}, error) {
args := new(StartMinerArgs)
if err := self.codec.Decode(req.Params, &args); err != nil {

View file

@ -7,6 +7,10 @@ import (
"github.com/ethereum/go-ethereum/xeth"
)
const (
NetApiVersion = "1.0"
)
var (
// mapping between methods and handlers
netMapping = map[string]nethandler{
@ -62,6 +66,10 @@ func (self *netApi) Name() string {
return NetApiName
}
func (self *netApi) ApiVersion() string {
return NetApiVersion
}
// Network version
func (self *netApi) Version(req *shared.Request) (interface{}, error) {
return self.xeth.NetworkVersion(), nil

View file

@ -10,6 +10,10 @@ import (
"github.com/ethereum/go-ethereum/xeth"
)
const (
PersonalApiVersion = "1.0"
)
var (
// mapping between methods and handlers
personalMapping = map[string]personalhandler{
@ -65,6 +69,10 @@ func (self *personalApi) Name() string {
return PersonalApiName
}
func (self *personalApi) ApiVersion() string {
return PersonalApiVersion
}
func (self *personalApi) ListAccounts(req *shared.Request) (interface{}, error) {
return self.xeth.Accounts(), nil
}

View file

@ -9,6 +9,10 @@ import (
"github.com/ethereum/go-ethereum/xeth"
)
const (
ShhApiVersion = "1.0"
)
var (
// mapping between methods and handlers
shhMapping = map[string]shhhandler{
@ -71,6 +75,10 @@ func (self *shhApi) Name() string {
return ShhApiName
}
func (self *shhApi) ApiVersion() string {
return ShhApiVersion
}
func (self *shhApi) Version(req *shared.Request) (interface{}, error) {
w := self.xeth.Whisper()
if w == nil {

View file

@ -7,6 +7,10 @@ import (
"github.com/ethereum/go-ethereum/xeth"
)
const (
TxPoolApiVersion = "1.0"
)
var (
// mapping between methods and handlers
txpoolMapping = map[string]txpoolhandler{
@ -59,6 +63,10 @@ func (self *txPoolApi) Name() string {
return TxPoolApiName
}
func (self *txPoolApi) ApiVersion() string {
return TxPoolApiVersion
}
func (self *txPoolApi) Status(req *shared.Request) (interface{}, error) {
return map[string]int{
"pending": self.ethereum.TxPool().GetTransactions().Len(),

View file

@ -9,7 +9,7 @@ import (
)
const (
Web3Version = "1.0.0"
Web3ApiVersion = "1.0"
)
var (
@ -63,9 +63,8 @@ func (self *web3Api) Name() string {
return Web3ApiName
}
// Version of the API this instance provides
func (self *web3Api) Version() string {
return Web3Version
func (self *web3Api) ApiVersion() string {
return Web3ApiVersion
}
// Calculates the sha3 over req.Params.Data