NatSpec passing end to end test
This commit is contained in:
@ -11,7 +11,7 @@ import (
|
||||
|
||||
/*
|
||||
Resolver implements the Ethereum DNS mapping
|
||||
NameReg : Domain Name (or Code hash of Contract) -> Content Hash
|
||||
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
|
||||
@ -19,13 +19,13 @@ of a url scheme
|
||||
*/
|
||||
const (
|
||||
URLHintContractAddress = core.ContractAddrURLhint
|
||||
NameRegContractAddress = core.ContractAddrHashReg
|
||||
HashRegContractAddress = core.ContractAddrHashReg
|
||||
)
|
||||
|
||||
type Resolver struct {
|
||||
backend Backend
|
||||
urlHintContractAddress string
|
||||
nameRegContractAddress string
|
||||
hashRegContractAddress string
|
||||
}
|
||||
|
||||
type Backend interface {
|
||||
@ -36,17 +36,23 @@ func New(eth Backend, uhca, nrca string) *Resolver {
|
||||
return &Resolver{eth, uhca, nrca}
|
||||
}
|
||||
|
||||
func (self *Resolver) NameToContentHash(name string) (chash common.Hash, err error) {
|
||||
// look up in nameReg
|
||||
key := storageAddress(1, common.Hex2BytesFixed(name, 32))
|
||||
hash := self.backend.StorageAt("0x"+self.nameRegContractAddress, key)
|
||||
copy(chash[:], common.Hex2Bytes(hash))
|
||||
func (self *Resolver) KeyToContentHash(khash common.Hash) (chash common.Hash, err error) {
|
||||
// look up in hashReg
|
||||
key := storageAddress(1, khash[:])
|
||||
hash := self.backend.StorageAt("0x"+self.hashRegContractAddress, key)
|
||||
|
||||
if hash == "0x0" || len(hash) < 3 {
|
||||
err = fmt.Errorf("GetHashReg: content hash not found")
|
||||
return
|
||||
}
|
||||
|
||||
copy(chash[:], common.Hex2BytesFixed(hash[2:], 32))
|
||||
return
|
||||
}
|
||||
|
||||
func (self *Resolver) ContentHashToUrl(chash common.Hash) (uri string, err error) {
|
||||
// look up in nameReg
|
||||
key := storageAddress(2, chash[:])
|
||||
// look up in URL reg
|
||||
key := storageAddress(1, chash[:])
|
||||
hex := self.backend.StorageAt("0x"+self.urlHintContractAddress, key)
|
||||
uri = string(common.Hex2Bytes(hex[2:]))
|
||||
l := len(uri)
|
||||
@ -61,9 +67,9 @@ func (self *Resolver) ContentHashToUrl(chash common.Hash) (uri string, err error
|
||||
return
|
||||
}
|
||||
|
||||
func (self *Resolver) NameToUrl(name string) (uri string, hash common.Hash, err error) {
|
||||
func (self *Resolver) KeyToUrl(key common.Hash) (uri string, hash common.Hash, err error) {
|
||||
// look up in urlHint
|
||||
hash, err = self.NameToContentHash(name)
|
||||
hash, err = self.KeyToContentHash(key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -23,12 +23,12 @@ func NewTestBackend() *testBackend {
|
||||
self := &testBackend{}
|
||||
self.contracts = make(map[string](map[string]string))
|
||||
|
||||
self.contracts[NameRegContractAddress] = make(map[string]string)
|
||||
key := storageAddress(0, common.Hex2Bytes(codehash))
|
||||
self.contracts[NameRegContractAddress][key] = hash
|
||||
self.contracts[HashRegContractAddress] = make(map[string]string)
|
||||
key := storageAddress(1, common.Hex2Bytes(codehash))
|
||||
self.contracts[HashRegContractAddress][key] = hash
|
||||
|
||||
self.contracts[URLHintContractAddress] = make(map[string]string)
|
||||
key = storageAddress(0, common.Hex2Bytes(hash))
|
||||
key = storageAddress(1, common.Hex2Bytes(hash))
|
||||
self.contracts[URLHintContractAddress][key] = url
|
||||
|
||||
return self
|
||||
@ -43,10 +43,12 @@ func (self *testBackend) StorageAt(ca, sa string) (res string) {
|
||||
return
|
||||
}
|
||||
|
||||
func TestNameToContentHash(t *testing.T) {
|
||||
func TestKeyToContentHash(t *testing.T) {
|
||||
b := NewTestBackend()
|
||||
res := New(b, URLHintContractAddress, NameRegContractAddress)
|
||||
got, err := res.NameToContentHash(codehash)
|
||||
res := New(b, URLHintContractAddress, HashRegContractAddress)
|
||||
chash := common.Hash{}
|
||||
copy(chash[:], common.Hex2BytesFixed(codehash, 32))
|
||||
got, err := res.KeyToContentHash(chash)
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got %v", err)
|
||||
} else {
|
||||
@ -58,7 +60,7 @@ func TestNameToContentHash(t *testing.T) {
|
||||
|
||||
func TestContentHashToUrl(t *testing.T) {
|
||||
b := NewTestBackend()
|
||||
res := New(b, URLHintContractAddress, NameRegContractAddress)
|
||||
res := New(b, URLHintContractAddress, HashRegContractAddress)
|
||||
chash := common.Hash{}
|
||||
copy(chash[:], common.Hex2Bytes(hash))
|
||||
got, err := res.ContentHashToUrl(chash)
|
||||
@ -71,5 +73,5 @@ func TestContentHashToUrl(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNameToUrl(t *testing.T) {
|
||||
func TestKeyToUrl(t *testing.T) {
|
||||
}
|
||||
|
Reference in New Issue
Block a user