diff options
Diffstat (limited to 'rpc/api/admin.go')
-rw-r--r-- | rpc/api/admin.go | 236 |
1 files changed, 225 insertions, 11 deletions
diff --git a/rpc/api/admin.go b/rpc/api/admin.go index b27482cfe..c5f026090 100644 --- a/rpc/api/admin.go +++ b/rpc/api/admin.go @@ -3,10 +3,18 @@ package api import ( "fmt" "io" + "math/big" "os" + "time" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/compiler" + "github.com/ethereum/go-ethereum/common/docserver" + "github.com/ethereum/go-ethereum/common/natspec" + "github.com/ethereum/go-ethereum/common/registrar" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/rlp" @@ -24,17 +32,29 @@ const ( var ( // mapping between methods and handlers AdminMapping = map[string]adminhandler{ - "admin_addPeer": (*adminApi).AddPeer, - "admin_peers": (*adminApi).Peers, - "admin_nodeInfo": (*adminApi).NodeInfo, - "admin_exportChain": (*adminApi).ExportChain, - "admin_importChain": (*adminApi).ImportChain, - "admin_verbosity": (*adminApi).Verbosity, - "admin_chainSyncStatus": (*adminApi).ChainSyncStatus, - "admin_setSolc": (*adminApi).SetSolc, - "admin_datadir": (*adminApi).DataDir, - "admin_startRPC": (*adminApi).StartRPC, - "admin_stopRPC": (*adminApi).StopRPC, + "admin_addPeer": (*adminApi).AddPeer, + "admin_peers": (*adminApi).Peers, + "admin_nodeInfo": (*adminApi).NodeInfo, + "admin_exportChain": (*adminApi).ExportChain, + "admin_importChain": (*adminApi).ImportChain, + "admin_verbosity": (*adminApi).Verbosity, + "admin_chainSyncStatus": (*adminApi).ChainSyncStatus, + "admin_setSolc": (*adminApi).SetSolc, + "admin_datadir": (*adminApi).DataDir, + "admin_startRPC": (*adminApi).StartRPC, + "admin_stopRPC": (*adminApi).StopRPC, + "admin_setGlobalRegistrar": (*adminApi).SetGlobalRegistrar, + "admin_setHashReg": (*adminApi).SetHashReg, + "admin_setUrlHint": (*adminApi).SetUrlHint, + "admin_saveInfo": (*adminApi).SaveInfo, + "admin_register": (*adminApi).Register, + "admin_registerUrl": (*adminApi).RegisterUrl, + "admin_startNatSpec": (*adminApi).StartNatSpec, + "admin_stopNatSpec": (*adminApi).StopNatSpec, + "admin_getContractInfo": (*adminApi).GetContractInfo, + "admin_httpGet": (*adminApi).HttpGet, + "admin_sleepBlocks": (*adminApi).SleepBlocks, + "admin_sleep": (*adminApi).Sleep, } ) @@ -47,6 +67,7 @@ type adminApi struct { ethereum *eth.Ethereum codec codec.Codec coder codec.ApiCoder + ds *docserver.DocServer } // create a new admin api instance @@ -56,6 +77,7 @@ func NewAdminApi(xeth *xeth.XEth, ethereum *eth.Ethereum, codec codec.Codec) *ad ethereum: ethereum, codec: codec, coder: codec.New(nil), + ds: docserver.New("/"), } } @@ -244,3 +266,195 @@ func (self *adminApi) StopRPC(req *shared.Request) (interface{}, error) { comms.StopHttp() return true, nil } + +func (self *adminApi) SleepBlocks(req *shared.Request) (interface{}, error) { + args := new(SleepBlocksArgs) + if err := self.coder.Decode(req.Params, &args); err != nil { + return nil, shared.NewDecodeParamError(err.Error()) + } + var timer <-chan time.Time + var height *big.Int + var err error + if args.Timeout > 0 { + timer = time.NewTimer(time.Duration(args.Timeout) * time.Second).C + } + + height = new(big.Int).Add(self.xeth.CurrentBlock().Number(), big.NewInt(args.N)) + height, err = sleepBlocks(self.xeth.UpdateState(), height, timer) + if err != nil { + return nil, err + } + return height.Uint64(), nil +} + +func sleepBlocks(wait chan *big.Int, height *big.Int, timer <-chan time.Time) (newHeight *big.Int, err error) { + wait <- height + select { + case <-timer: + // if times out make sure the xeth loop does not block + go func() { + select { + case wait <- nil: + case <-wait: + } + }() + return nil, fmt.Errorf("timeout") + case newHeight = <-wait: + } + return +} + +func (self *adminApi) Sleep(req *shared.Request) (interface{}, error) { + args := new(SleepArgs) + if err := self.coder.Decode(req.Params, &args); err != nil { + return nil, shared.NewDecodeParamError(err.Error()) + } + time.Sleep(time.Duration(args.S) * time.Second) + return nil, nil +} + +func (self *adminApi) SetGlobalRegistrar(req *shared.Request) (interface{}, error) { + args := new(SetGlobalRegistrarArgs) + if err := self.coder.Decode(req.Params, &args); err != nil { + return nil, shared.NewDecodeParamError(err.Error()) + } + + sender := common.HexToAddress(args.ContractAddress) + + reg := registrar.New(self.xeth) + txhash, err := reg.SetGlobalRegistrar(args.NameReg, sender) + if err != nil { + return false, err + } + + return txhash, nil +} + +func (self *adminApi) SetHashReg(req *shared.Request) (interface{}, error) { + args := new(SetHashRegArgs) + if err := self.coder.Decode(req.Params, &args); err != nil { + return nil, shared.NewDecodeParamError(err.Error()) + } + + reg := registrar.New(self.xeth) + sender := common.HexToAddress(args.Sender) + txhash, err := reg.SetHashReg(args.HashReg, sender) + if err != nil { + return false, err + } + + return txhash, nil +} + +func (self *adminApi) SetUrlHint(req *shared.Request) (interface{}, error) { + args := new(SetUrlHintArgs) + if err := self.coder.Decode(req.Params, &args); err != nil { + return nil, shared.NewDecodeParamError(err.Error()) + } + + urlHint := args.UrlHint + sender := common.HexToAddress(args.Sender) + + reg := registrar.New(self.xeth) + txhash, err := reg.SetUrlHint(urlHint, sender) + if err != nil { + return nil, err + } + + return txhash, nil +} + +func (self *adminApi) SaveInfo(req *shared.Request) (interface{}, error) { + args := new(SaveInfoArgs) + if err := self.coder.Decode(req.Params, &args); err != nil { + return nil, shared.NewDecodeParamError(err.Error()) + } + + contenthash, err := compiler.SaveInfo(&args.ContractInfo, args.Filename) + if err != nil { + return nil, err + } + + return contenthash.Hex(), nil +} + +func (self *adminApi) Register(req *shared.Request) (interface{}, error) { + args := new(RegisterArgs) + if err := self.coder.Decode(req.Params, &args); err != nil { + return nil, shared.NewDecodeParamError(err.Error()) + } + + sender := common.HexToAddress(args.Sender) + // sender and contract address are passed as hex strings + codeb := self.xeth.CodeAtBytes(args.Address) + codeHash := common.BytesToHash(crypto.Sha3(codeb)) + contentHash := common.HexToHash(args.ContentHashHex) + registry := registrar.New(self.xeth) + + _, err := registry.SetHashToHash(sender, codeHash, contentHash) + if err != nil { + return false, err + } + + return true, nil +} + +func (self *adminApi) RegisterUrl(req *shared.Request) (interface{}, error) { + args := new(RegisterUrlArgs) + if err := self.coder.Decode(req.Params, &args); err != nil { + return nil, shared.NewDecodeParamError(err.Error()) + } + + sender := common.HexToAddress(args.Sender) + registry := registrar.New(self.xeth) + _, err := registry.SetUrlToHash(sender, common.HexToHash(args.ContentHash), args.Url) + if err != nil { + return false, err + } + + return true, nil +} + +func (self *adminApi) StartNatSpec(req *shared.Request) (interface{}, error) { + self.ethereum.NatSpec = true + return true, nil +} + +func (self *adminApi) StopNatSpec(req *shared.Request) (interface{}, error) { + self.ethereum.NatSpec = false + return true, nil +} + +func (self *adminApi) GetContractInfo(req *shared.Request) (interface{}, error) { + args := new(GetContractInfoArgs) + if err := self.coder.Decode(req.Params, &args); err != nil { + return nil, shared.NewDecodeParamError(err.Error()) + } + + infoDoc, err := natspec.FetchDocsForContract(args.Contract, self.xeth, self.ds) + if err != nil { + return nil, err + } + + var info interface{} + err = self.coder.Decode(infoDoc, &info) + if err != nil { + return nil, err + } + + return info, nil +} + +func (self *adminApi) HttpGet(req *shared.Request) (interface{}, error) { + args := new(HttpGetArgs) + if err := self.coder.Decode(req.Params, &args); err != nil { + return nil, shared.NewDecodeParamError(err.Error()) + } + + resp, err := self.ds.Get(args.Uri, args.Path) + if err != nil { + return nil, err + } + + return string(resp), nil +} |