From 83ee39448e0f23d42dff27bccde27f828afa3707 Mon Sep 17 00:00:00 2001 From: zelig Date: Tue, 23 Jun 2015 15:48:33 +0100 Subject: Registrar and contractInfo handling * resolver -> common/registrar * global registrar name registry interface * add Call to resolver backend interface * the hashReg and UrlHing contracts now initialised from global registry * initialization of contracts uniform * improve errors and more econsistent method names * common/registrar/ethreg: versioned registrar * integrate new naming and registrar in natspec * js console api: setGlobalRegistrar, setHashReg, setUrlHint * js test TestContract uses mining - tests fixed all pass * eth/backend: allow PoW test mode (small ethash DAG) * console jsre refers to resolver.abi/addr, * cmd/geth/contracts.go moved to common/registrar --- rpc/api/admin.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'rpc/api/admin.go') diff --git a/rpc/api/admin.go b/rpc/api/admin.go index b27482cfe..78c75a60b 100644 --- a/rpc/api/admin.go +++ b/rpc/api/admin.go @@ -3,7 +3,9 @@ package api import ( "fmt" "io" + "math/big" "os" + "time" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" @@ -244,3 +246,49 @@ 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 +} + +// sec, err := call.Argument(0).ToInteger() +// if err != nil { +// fmt.Println(err) +// return otto.FalseValue() +// } +// time.Sleep(time.Duration(sec) * time.Second) +// return otto.UndefinedValue() +// } -- cgit v1.2.3 From 27392337198b9287e9f6fe615510a1f30099e3d7 Mon Sep 17 00:00:00 2001 From: zelig Date: Tue, 23 Jun 2015 15:48:33 +0100 Subject: Registrar and contractInfo handling * resolver -> common/registrar * global registrar name registry interface * add Call to resolver backend interface * the hashReg and UrlHing contracts now initialised from global registry * initialization of contracts uniform * improve errors and more econsistent method names * common/registrar/ethreg: versioned registrar * integrate new naming and registrar in natspec * js console api: setGlobalRegistrar, setHashReg, setUrlHint * js test TestContract uses mining - tests fixed all pass * eth/backend: allow PoW test mode (small ethash DAG) * console jsre refers to resolver.abi/addr, * cmd/geth/contracts.go moved to common/registrar --- rpc/api/admin.go | 185 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 174 insertions(+), 11 deletions(-) (limited to 'rpc/api/admin.go') diff --git a/rpc/api/admin.go b/rpc/api/admin.go index 78c75a60b..40199caa9 100644 --- a/rpc/api/admin.go +++ b/rpc/api/admin.go @@ -7,8 +7,14 @@ import ( "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" @@ -26,17 +32,27 @@ 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, } ) @@ -49,6 +65,7 @@ type adminApi struct { ethereum *eth.Ethereum codec codec.Codec coder codec.ApiCoder + ds *docserver.DocServer } // create a new admin api instance @@ -58,6 +75,7 @@ func NewAdminApi(xeth *xeth.XEth, ethereum *eth.Ethereum, codec codec.Codec) *ad ethereum: ethereum, codec: codec, coder: codec.New(nil), + ds: docserver.New("/"), } } @@ -292,3 +310,148 @@ func sleepBlocks(wait chan *big.Int, height *big.Int, timer <-chan time.Time) (n // time.Sleep(time.Duration(sec) * time.Second) // return otto.UndefinedValue() // } +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) + err := reg.SetGlobalRegistrar(args.NameReg, sender) + if err != nil { + return false, err + } + + return registrar.GlobalRegistrarAddr, 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) + err := reg.SetHashReg(args.HashReg, sender) + if err != nil { + return false, err + } + + return registrar.HashRegAddr, 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) + err := reg.SetUrlHint(urlHint, sender) + if err != nil { + return nil, err + } + + return registrar.UrlHintAddr, 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 +} -- cgit v1.2.3 From 6391ec0c8f5ea645d772ede9f4c6fbda3d84105f Mon Sep 17 00:00:00 2001 From: zelig Date: Tue, 30 Jun 2015 16:39:31 +0100 Subject: add missing method to api/admin --- rpc/api/admin.go | 1 + 1 file changed, 1 insertion(+) (limited to 'rpc/api/admin.go') diff --git a/rpc/api/admin.go b/rpc/api/admin.go index 40199caa9..0230937fa 100644 --- a/rpc/api/admin.go +++ b/rpc/api/admin.go @@ -43,6 +43,7 @@ var ( "admin_datadir": (*adminApi).DataDir, "admin_startRPC": (*adminApi).StartRPC, "admin_stopRPC": (*adminApi).StopRPC, + "admin_sleepBlocks": (*adminApi).SleepBlocks, "admin_setGlobalRegistrar": (*adminApi).SetGlobalRegistrar, "admin_setHashReg": (*adminApi).SetHashReg, "admin_setUrlHint": (*adminApi).SetUrlHint, -- cgit v1.2.3 From 518dc87db3dd09aed21f255f448f95dcc746dc12 Mon Sep 17 00:00:00 2001 From: zelig Date: Thu, 2 Jul 2015 16:38:48 +0100 Subject: fix sleepBlocks, implement sleep --- rpc/api/admin.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'rpc/api/admin.go') diff --git a/rpc/api/admin.go b/rpc/api/admin.go index 0230937fa..5d214e8d8 100644 --- a/rpc/api/admin.go +++ b/rpc/api/admin.go @@ -43,7 +43,6 @@ var ( "admin_datadir": (*adminApi).DataDir, "admin_startRPC": (*adminApi).StartRPC, "admin_stopRPC": (*adminApi).StopRPC, - "admin_sleepBlocks": (*adminApi).SleepBlocks, "admin_setGlobalRegistrar": (*adminApi).SetGlobalRegistrar, "admin_setHashReg": (*adminApi).SetHashReg, "admin_setUrlHint": (*adminApi).SetUrlHint, @@ -54,6 +53,8 @@ var ( "admin_stopNatSpec": (*adminApi).StopNatSpec, "admin_getContractInfo": (*adminApi).GetContractInfo, "admin_httpGet": (*adminApi).HttpGet, + "admin_sleepBlocks": (*adminApi).SleepBlocks, + "admin_sleep": (*adminApi).Sleep, } ) @@ -303,14 +304,15 @@ func sleepBlocks(wait chan *big.Int, height *big.Int, timer <-chan time.Time) (n return } -// sec, err := call.Argument(0).ToInteger() -// if err != nil { -// fmt.Println(err) -// return otto.FalseValue() -// } -// time.Sleep(time.Duration(sec) * time.Second) -// return otto.UndefinedValue() -// } +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 { -- cgit v1.2.3 From c5cb6e8e70339b6641b7ce46cda04b83936213b3 Mon Sep 17 00:00:00 2001 From: zelig Date: Tue, 7 Jul 2015 06:00:58 +0100 Subject: fix/skip tests, adapt registrar to no contract address registry initialisers now return the txhash which caller can use to retrieve receipt --- rpc/api/admin.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'rpc/api/admin.go') diff --git a/rpc/api/admin.go b/rpc/api/admin.go index 5d214e8d8..c5f026090 100644 --- a/rpc/api/admin.go +++ b/rpc/api/admin.go @@ -322,12 +322,12 @@ func (self *adminApi) SetGlobalRegistrar(req *shared.Request) (interface{}, erro sender := common.HexToAddress(args.ContractAddress) reg := registrar.New(self.xeth) - err := reg.SetGlobalRegistrar(args.NameReg, sender) + txhash, err := reg.SetGlobalRegistrar(args.NameReg, sender) if err != nil { return false, err } - return registrar.GlobalRegistrarAddr, nil + return txhash, nil } func (self *adminApi) SetHashReg(req *shared.Request) (interface{}, error) { @@ -338,12 +338,12 @@ func (self *adminApi) SetHashReg(req *shared.Request) (interface{}, error) { reg := registrar.New(self.xeth) sender := common.HexToAddress(args.Sender) - err := reg.SetHashReg(args.HashReg, sender) + txhash, err := reg.SetHashReg(args.HashReg, sender) if err != nil { return false, err } - return registrar.HashRegAddr, nil + return txhash, nil } func (self *adminApi) SetUrlHint(req *shared.Request) (interface{}, error) { @@ -356,12 +356,12 @@ func (self *adminApi) SetUrlHint(req *shared.Request) (interface{}, error) { sender := common.HexToAddress(args.Sender) reg := registrar.New(self.xeth) - err := reg.SetUrlHint(urlHint, sender) + txhash, err := reg.SetUrlHint(urlHint, sender) if err != nil { return nil, err } - return registrar.UrlHintAddr, nil + return txhash, nil } func (self *adminApi) SaveInfo(req *shared.Request) (interface{}, error) { -- cgit v1.2.3