aboutsummaryrefslogtreecommitdiffstats
path: root/common/registrar
diff options
context:
space:
mode:
Diffstat (limited to 'common/registrar')
-rw-r--r--common/registrar/registrar.go52
-rw-r--r--common/registrar/registrar_test.go62
2 files changed, 94 insertions, 20 deletions
diff --git a/common/registrar/registrar.go b/common/registrar/registrar.go
index d891e161e..24e45edb3 100644
--- a/common/registrar/registrar.go
+++ b/common/registrar/registrar.go
@@ -48,17 +48,16 @@ The Registrar uses 3 contracts on the blockchain:
These contracts are (currently) not included in the genesis block.
Each Set<X> needs to be called once on each blockchain/network once.
-Contract addresses need to be set (HashReg and UrlHint retrieved from the global
-registrar the first time any Registrar method is called in a client session
-
-So the caller needs to make sure the relevant environment initialised the desired
-contracts
+Contract addresses need to be set the first time any Registrar method is called
+in a client session.
+This is done for frontier by default, otherwise the caller needs to make sure
+the relevant environment initialised the desired contracts
*/
var (
- UrlHintAddr = "0x0"
- HashRegAddr = "0x0"
- GlobalRegistrarAddr = "0x0"
- // GlobalRegistrarAddr = "0xc6d9d2cd449a754c494264e1809c50e34d64562b"
+ // GlobalRegistrarAddr = "0xc6d9d2cd449a754c494264e1809c50e34d64562b" // olympic
+ GlobalRegistrarAddr = "0x33990122638b9132ca29c723bdf037f1a891a70c" // frontier
+ HashRegAddr = "0x23bf622b5a65f6060d855fca401133ded3520620" // frontier
+ UrlHintAddr = "0x73ed5ef6c010727dfd2671dbb70faac19ec18626" // frontier
zero = regexp.MustCompile("^(0x)?0*$")
)
@@ -113,7 +112,7 @@ func (self *Registrar) SetGlobalRegistrar(namereg string, addr common.Address) (
GlobalRegistrarAddr = namereg
return
}
- if GlobalRegistrarAddr == "0x0" || GlobalRegistrarAddr == "0x" {
+ if zero.MatchString(GlobalRegistrarAddr) {
if (addr == common.Address{}) {
err = fmt.Errorf("GlobalRegistrar address not found and sender for creation not given")
return
@@ -200,6 +199,9 @@ func (self *Registrar) SetUrlHint(urlhint string, addr common.Address) (txhash s
// ReserveName(from, name) reserves name for the sender address in the globalRegistrar
// the tx needs to be mined to take effect
func (self *Registrar) ReserveName(address common.Address, name string) (txh string, err error) {
+ if zero.MatchString(GlobalRegistrarAddr) {
+ return "", fmt.Errorf("GlobalRegistrar address is not set")
+ }
nameHex, extra := encodeName(name, 2)
abi := reserveAbi + nameHex + extra
glog.V(logger.Detail).Infof("Reserve data: %s", abi)
@@ -215,6 +217,10 @@ func (self *Registrar) ReserveName(address common.Address, name string) (txh str
// in the globalRegistrar using from as the sender of the transaction
// the tx needs to be mined to take effect
func (self *Registrar) SetAddressToName(from common.Address, name string, address common.Address) (txh string, err error) {
+ if zero.MatchString(GlobalRegistrarAddr) {
+ return "", fmt.Errorf("GlobalRegistrar address is not set")
+ }
+
nameHex, extra := encodeName(name, 6)
addrHex := encodeAddress(address)
@@ -231,6 +237,10 @@ func (self *Registrar) SetAddressToName(from common.Address, name string, addres
// NameToAddr(from, name) queries the registrar for the address on name
func (self *Registrar) NameToAddr(from common.Address, name string) (address common.Address, err error) {
+ if zero.MatchString(GlobalRegistrarAddr) {
+ return address, fmt.Errorf("GlobalRegistrar address is not set")
+ }
+
nameHex, extra := encodeName(name, 2)
abi := resolveAbi + nameHex + extra
glog.V(logger.Detail).Infof("NameToAddr data: %s", abi)
@@ -249,6 +259,9 @@ func (self *Registrar) NameToAddr(from common.Address, name string) (address com
// called as first step in the registration process on HashReg
func (self *Registrar) SetOwner(address common.Address) (txh string, err error) {
+ if zero.MatchString(HashRegAddr) {
+ return "", fmt.Errorf("HashReg address is not set")
+ }
return self.backend.Transact(
address.Hex(),
HashRegAddr,
@@ -261,6 +274,10 @@ func (self *Registrar) SetOwner(address common.Address) (txh string, err error)
// e.g., the contract Info combined Json Doc's ContentHash
// to CodeHash of a contract or hash of a domain
func (self *Registrar) SetHashToHash(address common.Address, codehash, dochash common.Hash) (txh string, err error) {
+ if zero.MatchString(HashRegAddr) {
+ return "", fmt.Errorf("HashReg address is not set")
+ }
+
_, err = self.SetOwner(address)
if err != nil {
return
@@ -284,6 +301,10 @@ func (self *Registrar) SetHashToHash(address common.Address, codehash, dochash c
// FIXME: silently doing nothing if sender is not the owner
// note that with content addressed storage, this step is no longer necessary
func (self *Registrar) SetUrlToHash(address common.Address, hash common.Hash, url string) (txh string, err error) {
+ if zero.MatchString(UrlHintAddr) {
+ return "", fmt.Errorf("UrlHint address is not set")
+ }
+
hashHex := common.Bytes2Hex(hash[:])
var urlHex string
urlb := []byte(url)
@@ -321,13 +342,17 @@ func (self *Registrar) SetUrlToHash(address common.Address, hash common.Hash, ur
// resolution is costless non-transactional
// implemented as direct retrieval from db
func (self *Registrar) HashToHash(khash common.Hash) (chash common.Hash, err error) {
+ if zero.MatchString(HashRegAddr) {
+ return common.Hash{}, fmt.Errorf("HashReg address is not set")
+ }
+
// look up in hashReg
at := HashRegAddr[2:]
key := storageAddress(storageMapping(storageIdx2Addr(1), khash[:]))
hash := self.backend.StorageAt(at, key)
if hash == "0x0" || len(hash) < 3 || (hash == common.Hash{}.Hex()) {
- err = fmt.Errorf("content hash not found for '%v'", khash.Hex())
+ err = fmt.Errorf("HashToHash: content hash not found for '%v'", khash.Hex())
return
}
copy(chash[:], common.Hex2BytesFixed(hash[2:], 32))
@@ -339,6 +364,9 @@ func (self *Registrar) HashToHash(khash common.Hash) (chash common.Hash, err err
// implemented as direct retrieval from db
// if we use content addressed storage, this step is no longer necessary
func (self *Registrar) HashToUrl(chash common.Hash) (uri string, err error) {
+ if zero.MatchString(UrlHintAddr) {
+ return "", fmt.Errorf("UrlHint address is not set")
+ }
// look up in URL reg
var str string = " "
var idx uint32
@@ -358,7 +386,7 @@ func (self *Registrar) HashToUrl(chash common.Hash) (uri string, err error) {
}
if len(uri) == 0 {
- err = fmt.Errorf("GetURLhint: URL hint not found for '%v'", chash.Hex())
+ err = fmt.Errorf("HashToUrl: URL hint not found for '%v'", chash.Hex())
}
return
}
diff --git a/common/registrar/registrar_test.go b/common/registrar/registrar_test.go
index 63f283563..68ee65ab4 100644
--- a/common/registrar/registrar_test.go
+++ b/common/registrar/registrar_test.go
@@ -36,29 +36,31 @@ var (
)
func NewTestBackend() *testBackend {
- HashRegAddr = common.BigToAddress(common.Big0).Hex() //[2:]
- UrlHintAddr = common.BigToAddress(common.Big1).Hex() //[2:]
self := &testBackend{}
self.contracts = make(map[string](map[string]string))
+ return self
+}
+func (self *testBackend) initHashReg() {
self.contracts[HashRegAddr[2:]] = make(map[string]string)
key := storageAddress(storageMapping(storageIdx2Addr(1), codehash[:]))
self.contracts[HashRegAddr[2:]][key] = hash.Hex()
+}
+func (self *testBackend) initUrlHint() {
self.contracts[UrlHintAddr[2:]] = make(map[string]string)
mapaddr := storageMapping(storageIdx2Addr(1), hash[:])
- key = storageAddress(storageFixedArray(mapaddr, storageIdx2Addr(0)))
+ key := storageAddress(storageFixedArray(mapaddr, storageIdx2Addr(0)))
self.contracts[UrlHintAddr[2:]][key] = common.ToHex([]byte(url))
key = storageAddress(storageFixedArray(mapaddr, storageIdx2Addr(1)))
self.contracts[UrlHintAddr[2:]][key] = "0x0"
- return self
}
func (self *testBackend) StorageAt(ca, sa string) (res string) {
c := self.contracts[ca]
if c == nil {
- return
+ return "0x0"
}
res = c[sa]
return
@@ -84,9 +86,31 @@ func TestSetGlobalRegistrar(t *testing.T) {
func TestHashToHash(t *testing.T) {
b := NewTestBackend()
res := New(b)
- // res.SetHashReg()
+ HashRegAddr = "0x0"
got, err := res.HashToHash(codehash)
+ if err == nil {
+ t.Errorf("expected error")
+ } else {
+ exp := "HashReg address is not set"
+ if err.Error() != exp {
+ t.Errorf("incorrect error, expected '%v', got '%v'", exp, err.Error())
+ }
+ }
+
+ HashRegAddr = common.BigToAddress(common.Big1).Hex() //[2:]
+ got, err = res.HashToHash(codehash)
+ if err == nil {
+ t.Errorf("expected error")
+ } else {
+ exp := "HashToHash: content hash not found for '" + codehash.Hex() + "'"
+ if err.Error() != exp {
+ t.Errorf("incorrect error, expected '%v', got '%v'", exp, err.Error())
+ }
+ }
+
+ b.initHashReg()
+ got, err = res.HashToHash(codehash)
if err != nil {
t.Errorf("expected no error, got %v", err)
} else {
@@ -99,11 +123,33 @@ func TestHashToHash(t *testing.T) {
func TestHashToUrl(t *testing.T) {
b := NewTestBackend()
res := New(b)
- // res.SetUrlHint()
+ UrlHintAddr = "0x0"
got, err := res.HashToUrl(hash)
+ if err == nil {
+ t.Errorf("expected error")
+ } else {
+ exp := "UrlHint address is not set"
+ if err.Error() != exp {
+ t.Errorf("incorrect error, expected '%v', got '%v'", exp, err.Error())
+ }
+ }
+
+ UrlHintAddr = common.BigToAddress(common.Big2).Hex() //[2:]
+ got, err = res.HashToUrl(hash)
+ if err == nil {
+ t.Errorf("expected error")
+ } else {
+ exp := "HashToUrl: URL hint not found for '" + hash.Hex() + "'"
+ if err.Error() != exp {
+ t.Errorf("incorrect error, expected '%v', got '%v'", exp, err.Error())
+ }
+ }
+
+ b.initUrlHint()
+ got, err = res.HashToUrl(hash)
if err != nil {
- t.Errorf("expected error, got %v", err)
+ t.Errorf("expected no error, got %v", err)
} else {
if got != url {
t.Errorf("incorrect result, expected '%v', got '%s'", url, got)