diff options
author | zelig <viktor.tron@gmail.com> | 2015-06-23 22:48:33 +0800 |
---|---|---|
committer | Jeffrey Wilcke <geffobscura@gmail.com> | 2015-07-07 16:43:31 +0800 |
commit | 83ee39448e0f23d42dff27bccde27f828afa3707 (patch) | |
tree | 429fa8f20a4bc017a58ffc4c7c0ed8596d1d206f /common/resolver | |
parent | d764bd058457cd9eb91d205d1ac187d40c4866d6 (diff) | |
download | go-tangerine-83ee39448e0f23d42dff27bccde27f828afa3707.tar go-tangerine-83ee39448e0f23d42dff27bccde27f828afa3707.tar.gz go-tangerine-83ee39448e0f23d42dff27bccde27f828afa3707.tar.bz2 go-tangerine-83ee39448e0f23d42dff27bccde27f828afa3707.tar.lz go-tangerine-83ee39448e0f23d42dff27bccde27f828afa3707.tar.xz go-tangerine-83ee39448e0f23d42dff27bccde27f828afa3707.tar.zst go-tangerine-83ee39448e0f23d42dff27bccde27f828afa3707.zip |
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
Diffstat (limited to 'common/resolver')
-rw-r--r-- | common/resolver/contracts.go | 36 | ||||
-rw-r--r-- | common/resolver/resolver.go | 232 | ||||
-rw-r--r-- | common/resolver/resolver_test.go | 93 |
3 files changed, 0 insertions, 361 deletions
diff --git a/common/resolver/contracts.go b/common/resolver/contracts.go deleted file mode 100644 index 4aad95e43..000000000 --- a/common/resolver/contracts.go +++ /dev/null @@ -1,36 +0,0 @@ -package resolver - -const ( // built-in contracts address and code - ContractCodeURLhint = "0x60c180600c6000396000f30060003560e060020a90048063300a3bbf14601557005b6024600435602435604435602a565b60006000f35b6000600084815260200190815260200160002054600160a060020a0316600014806078575033600160a060020a03166000600085815260200190815260200160002054600160a060020a0316145b607f5760bc565b336000600085815260200190815260200160002081905550806001600085815260200190815260200160002083610100811060b657005b01819055505b50505056" - /* - contract URLhint { - function register(uint256 _hash, uint8 idx, uint256 _url) { - if (owner[_hash] == 0 || owner[_hash] == msg.sender) { - owner[_hash] = msg.sender; - url[_hash][idx] = _url; - } - } - mapping (uint256 => address) owner; - mapping (uint256 => uint256[256]) url; - } - */ - - ContractCodeHashReg = "0x609880600c6000396000f30060003560e060020a9004806331e12c2014601f578063d66d6c1014602b57005b6025603d565b60006000f35b6037600435602435605d565b60006000f35b600054600160a060020a0316600014605357605b565b336000819055505b565b600054600160a060020a031633600160a060020a031614607b576094565b8060016000848152602001908152602001600020819055505b505056" - /* - contract HashReg { - function setowner() { - if (owner == 0) { - owner = msg.sender; - } - } - function register(uint256 _key, uint256 _content) { - if (msg.sender == owner) { - content[_key] = _content; - } - } - address owner; - mapping (uint256 => uint256) content; - } - */ - -) diff --git a/common/resolver/resolver.go b/common/resolver/resolver.go deleted file mode 100644 index 9016547e1..000000000 --- a/common/resolver/resolver.go +++ /dev/null @@ -1,232 +0,0 @@ -package resolver - -import ( - "encoding/binary" - "fmt" - - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/logger" - "github.com/ethereum/go-ethereum/logger/glog" -) - -/* -Resolver implements the Ethereum DNS mapping -HashReg : Key Hash (hash of domain name or contract code) -> Content Hash -UrlHint : Content Hash -> Url Hint - -The resolver is meant to be called by the roundtripper transport implementation -of a url scheme -*/ - -// // contract addresses will be hardcoded after they're created -var UrlHintContractAddress, HashRegContractAddress string - -const ( - txValue = "0" - txGas = "100000" - txGasPrice = "1000000000000" -) - -func abi(s string) string { - return common.ToHex(crypto.Sha3([]byte(s))[:4]) -} - -var ( - registerContentHashAbi = abi("register(uint256,uint256)") - registerUrlAbi = abi("register(uint256,uint8,uint256)") - setOwnerAbi = abi("setowner()") -) - -type Backend interface { - StorageAt(string, string) string - Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) -} - -type Resolver struct { - backend Backend -} - -func New(eth Backend) *Resolver { - return &Resolver{eth} -} - -// for testing and play temporarily -// ideally the HashReg and UrlHint contracts should be in the genesis block -// if we got build-in support for natspec/contract info -// there should be only one of these officially endorsed -// addresses as constants -// TODO: could get around this with namereg, check -func (self *Resolver) CreateContracts(addr common.Address) (err error) { - HashRegContractAddress, err = self.backend.Transact(addr.Hex(), "", "", txValue, txGas, txGasPrice, ContractCodeHashReg) - if err != nil { - return - } - UrlHintContractAddress, err = self.backend.Transact(addr.Hex(), "", "", txValue, txGas, txGasPrice, ContractCodeURLhint) - glog.V(logger.Detail).Infof("HashReg @ %v\nUrlHint @ %v\n", HashRegContractAddress, UrlHintContractAddress) - return -} - -// called as first step in the registration process on HashReg -func (self *Resolver) SetOwner(address common.Address) (txh string, err error) { - return self.backend.Transact( - address.Hex(), - HashRegContractAddress, - "", txValue, txGas, txGasPrice, - setOwnerAbi, - ) -} - -// registers some content hash to a key/code hash -// e.g., the contract Info combined Json Doc's ContentHash -// to CodeHash of a contract or hash of a domain -// kept -func (self *Resolver) RegisterContentHash(address common.Address, codehash, dochash common.Hash) (txh string, err error) { - _, err = self.SetOwner(address) - if err != nil { - return - } - codehex := common.Bytes2Hex(codehash[:]) - dochex := common.Bytes2Hex(dochash[:]) - - data := registerContentHashAbi + codehex + dochex - return self.backend.Transact( - address.Hex(), - HashRegContractAddress, - "", txValue, txGas, txGasPrice, - data, - ) -} - -// registers a url to a content hash so that the content can be fetched -// address is used as sender for the transaction and will be the owner of a new -// registry entry on first time use -// FIXME: silently doing nothing if sender is not the owner -// note that with content addressed storage, this step is no longer necessary -// it could be purely -func (self *Resolver) RegisterUrl(address common.Address, hash common.Hash, url string) (txh string, err error) { - hashHex := common.Bytes2Hex(hash[:]) - var urlHex string - urlb := []byte(url) - var cnt byte - n := len(urlb) - - for n > 0 { - if n > 32 { - n = 32 - } - urlHex = common.Bytes2Hex(urlb[:n]) - urlb = urlb[n:] - n = len(urlb) - bcnt := make([]byte, 32) - bcnt[31] = cnt - data := registerUrlAbi + - hashHex + - common.Bytes2Hex(bcnt) + - common.Bytes2Hex(common.Hex2BytesFixed(urlHex, 32)) - txh, err = self.backend.Transact( - address.Hex(), - UrlHintContractAddress, - "", txValue, txGas, txGasPrice, - data, - ) - if err != nil { - return - } - cnt++ - } - return -} - -func (self *Resolver) Register(address common.Address, codehash, dochash common.Hash, url string) (txh string, err error) { - - _, err = self.RegisterContentHash(address, codehash, dochash) - if err != nil { - return - } - return self.RegisterUrl(address, dochash, url) -} - -// resolution is costless non-transactional -// implemented as direct retrieval from db -func (self *Resolver) KeyToContentHash(khash common.Hash) (chash common.Hash, err error) { - // look up in hashReg - at := common.Bytes2Hex(common.FromHex(HashRegContractAddress)) - key := storageAddress(storageMapping(storageIdx2Addr(1), khash[:])) - hash := self.backend.StorageAt(at, key) - - if hash == "0x0" || len(hash) < 3 { - err = fmt.Errorf("content hash not found for '%v'", khash.Hex()) - return - } - copy(chash[:], common.Hex2BytesFixed(hash[2:], 32)) - return -} - -// retrieves the url-hint for the content hash - -// if we use content addressed storage, this step is no longer necessary -func (self *Resolver) ContentHashToUrl(chash common.Hash) (uri string, err error) { - // look up in URL reg - var str string = " " - var idx uint32 - for len(str) > 0 { - mapaddr := storageMapping(storageIdx2Addr(1), chash[:]) - key := storageAddress(storageFixedArray(mapaddr, storageIdx2Addr(idx))) - hex := self.backend.StorageAt(UrlHintContractAddress, key) - str = string(common.Hex2Bytes(hex[2:])) - l := len(str) - for (l > 0) && (str[l-1] == 0) { - l-- - } - str = str[:l] - uri = uri + str - idx++ - } - - if len(uri) == 0 { - err = fmt.Errorf("GetURLhint: URL hint not found for '%v'", chash.Hex()) - } - return -} - -func (self *Resolver) KeyToUrl(key common.Hash) (uri string, hash common.Hash, err error) { - // look up in urlHint - hash, err = self.KeyToContentHash(key) - if err != nil { - return - } - uri, err = self.ContentHashToUrl(hash) - return -} - -func storageIdx2Addr(varidx uint32) []byte { - data := make([]byte, 32) - binary.BigEndian.PutUint32(data[28:32], varidx) - return data -} - -func storageMapping(addr, key []byte) []byte { - data := make([]byte, 64) - copy(data[0:32], key[0:32]) - copy(data[32:64], addr[0:32]) - sha := crypto.Sha3(data) - return sha -} - -func storageFixedArray(addr, idx []byte) []byte { - var carry byte - for i := 31; i >= 0; i-- { - var b byte = addr[i] + idx[i] + carry - if b < addr[i] { - carry = 1 - } else { - carry = 0 - } - addr[i] = b - } - return addr -} - -func storageAddress(addr []byte) string { - return common.ToHex(addr) -} diff --git a/common/resolver/resolver_test.go b/common/resolver/resolver_test.go deleted file mode 100644 index 02d12592e..000000000 --- a/common/resolver/resolver_test.go +++ /dev/null @@ -1,93 +0,0 @@ -package resolver - -import ( - "testing" - - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" -) - -type testBackend struct { - // contracts mock - contracts map[string](map[string]string) -} - -var ( - text = "test" - codehash = common.StringToHash("1234") - hash = common.BytesToHash(crypto.Sha3([]byte(text))) - url = "bzz://bzzhash/my/path/contr.act" -) - -func NewTestBackend() *testBackend { - HashRegContractAddress = common.BigToAddress(common.Big0).Hex()[2:] - UrlHintContractAddress = common.BigToAddress(common.Big1).Hex()[2:] - self := &testBackend{} - self.contracts = make(map[string](map[string]string)) - - self.contracts[HashRegContractAddress] = make(map[string]string) - key := storageAddress(storageMapping(storageIdx2Addr(1), codehash[:])) - self.contracts[HashRegContractAddress][key] = hash.Hex() - - self.contracts[UrlHintContractAddress] = make(map[string]string) - mapaddr := storageMapping(storageIdx2Addr(1), hash[:]) - - key = storageAddress(storageFixedArray(mapaddr, storageIdx2Addr(0))) - self.contracts[UrlHintContractAddress][key] = common.ToHex([]byte(url)) - key = storageAddress(storageFixedArray(mapaddr, storageIdx2Addr(1))) - self.contracts[UrlHintContractAddress][key] = "0x00" - return self -} - -func (self *testBackend) StorageAt(ca, sa string) (res string) { - c := self.contracts[ca] - if c == nil { - return - } - res = c[sa] - return -} - -func (self *testBackend) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) { - return "", nil -} - -func TestKeyToContentHash(t *testing.T) { - b := NewTestBackend() - res := New(b) - - got, err := res.KeyToContentHash(codehash) - if err != nil { - t.Errorf("expected no error, got %v", err) - } else { - if got != hash { - t.Errorf("incorrect result, expected '%v', got '%v'", hash.Hex(), got.Hex()) - } - } -} - -func TestContentHashToUrl(t *testing.T) { - b := NewTestBackend() - res := New(b) - got, err := res.ContentHashToUrl(hash) - if err != nil { - t.Errorf("expected no error, got %v", err) - } else { - if got != url { - t.Errorf("incorrect result, expected '%v', got '%s'", url, got) - } - } -} - -func TestKeyToUrl(t *testing.T) { - b := NewTestBackend() - res := New(b) - got, _, err := res.KeyToUrl(codehash) - if err != nil { - t.Errorf("expected no error, got %v", err) - } else { - if got != url { - t.Errorf("incorrect result, expected \n'%s', got \n'%s'", url, got) - } - } -} |