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
This commit is contained in:
zelig
2015-06-23 15:48:33 +01:00
committed by Jeffrey Wilcke
parent 83ee39448e
commit 2739233719
6 changed files with 964 additions and 15 deletions

View File

@ -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
}