diff options
Diffstat (limited to 'common/natspec')
-rw-r--r-- | common/natspec/natspec.go | 24 | ||||
-rw-r--r-- | common/natspec/natspec_e2e_test.go | 49 |
2 files changed, 47 insertions, 26 deletions
diff --git a/common/natspec/natspec.go b/common/natspec/natspec.go index 7e5f053c7..9965a2227 100644 --- a/common/natspec/natspec.go +++ b/common/natspec/natspec.go @@ -9,7 +9,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/docserver" - "github.com/ethereum/go-ethereum/common/resolver" + "github.com/ethereum/go-ethereum/common/registrar" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/xeth" ) @@ -88,7 +88,7 @@ func New(xeth *xeth.XEth, jsontx string, http *docserver.DocServer) (self *NatSp } // also called by admin.contractInfo.get -func FetchDocsForContract(contractAddress string, xeth *xeth.XEth, http *docserver.DocServer) (content []byte, err error) { +func FetchDocsForContract(contractAddress string, xeth *xeth.XEth, ds *docserver.DocServer) (content []byte, err error) { // retrieve contract hash from state codehex := xeth.CodeAt(contractAddress) codeb := xeth.CodeAtBytes(contractAddress) @@ -99,20 +99,32 @@ func FetchDocsForContract(contractAddress string, xeth *xeth.XEth, http *docserv } codehash := common.BytesToHash(crypto.Sha3(codeb)) // set up nameresolver with natspecreg + urlhint contract addresses - res := resolver.New(xeth) + reg := registrar.New(xeth) // resolve host via HashReg/UrlHint Resolver - uri, hash, err := res.KeyToUrl(codehash) + hash, err := reg.HashToHash(codehash) if err != nil { return } + if ds.HasScheme("bzz") { + content, err = ds.Get("bzz://"+hash.Hex()[2:], "") + if err == nil { // non-fatal + return + } + err = nil + //falling back to urlhint + } - // get content via http client and authenticate content using hash - content, err = http.GetAuthContent(uri, hash) + uri, err := reg.HashToUrl(hash) if err != nil { return } + // get content via http client and authenticate content using hash + content, err = ds.GetAuthContent(uri, hash) + if err != nil { + return + } return } diff --git a/common/natspec/natspec_e2e_test.go b/common/natspec/natspec_e2e_test.go index 7e9172649..a941acbba 100644 --- a/common/natspec/natspec_e2e_test.go +++ b/common/natspec/natspec_e2e_test.go @@ -10,7 +10,7 @@ import ( "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/docserver" - "github.com/ethereum/go-ethereum/common/resolver" + "github.com/ethereum/go-ethereum/common/registrar" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/crypto" @@ -73,8 +73,7 @@ const ( ) type testFrontend struct { - t *testing.T - // resolver *resolver.Resolver + t *testing.T ethereum *eth.Ethereum xeth *xe.XEth coinbase common.Address @@ -91,10 +90,7 @@ func (self *testFrontend) UnlockAccount(acc []byte) bool { func (self *testFrontend) ConfirmTransaction(tx string) bool { if self.wantNatSpec { - ds, err := docserver.New("/tmp/") - if err != nil { - self.t.Errorf("Error creating DocServer: %v", err) - } + ds := docserver.New("/tmp/") self.lastConfirm = GetNotice(self.xeth, tx, ds) } return true @@ -159,11 +155,16 @@ func testInit(t *testing.T) (self *testFrontend) { self.stateDb = self.ethereum.ChainManager().State().Copy() // initialise the registry contracts - // self.resolver.CreateContracts(addr) - resolver.New(self.xeth).CreateContracts(addr) + reg := registrar.New(self.xeth) + err = reg.SetHashReg("", addr) + if err != nil { + t.Errorf("error creating HashReg: %v", err) + } + err = reg.SetUrlHint("", addr) + if err != nil { + t.Errorf("error creating UrlHint: %v", err) + } self.applyTxs() - // t.Logf("HashReg contract registered at %v", resolver.HashRegContractAddress) - // t.Logf("URLHint contract registered at %v", resolver.UrlHintContractAddress) return @@ -192,13 +193,20 @@ func TestNatspecE2E(t *testing.T) { dochash := common.BytesToHash(crypto.Sha3([]byte(testContractInfo))) // take the codehash for the contract we wanna test - // codehex := tf.xeth.CodeAt(resolver.HashRegContractAddress) - codeb := tf.xeth.CodeAtBytes(resolver.HashRegContractAddress) + // codehex := tf.xeth.CodeAt(registar.HashRegAddr) + codeb := tf.xeth.CodeAtBytes(registrar.HashRegAddr) codehash := common.BytesToHash(crypto.Sha3(codeb)) // use resolver to register codehash->dochash->url - registry := resolver.New(tf.xeth) - _, err := registry.Register(tf.coinbase, codehash, dochash, "file:///"+testFileName) + // test if globalregistry works + // registrar.HashRefAddr = "0x0" + // registrar.UrlHintAddr = "0x0" + reg := registrar.New(tf.xeth) + _, err := reg.SetHashToHash(tf.coinbase, codehash, dochash) + if err != nil { + t.Errorf("error registering: %v", err) + } + _, err = reg.SetUrlToHash(tf.coinbase, dochash, "file:///"+testFileName) if err != nil { t.Errorf("error registering: %v", err) } @@ -209,18 +217,19 @@ func TestNatspecE2E(t *testing.T) { // now using the same transactions to check confirm messages tf.wantNatSpec = true // this is set so now the backend uses natspec confirmation - _, err = registry.RegisterContentHash(tf.coinbase, codehash, dochash) + _, err = reg.SetHashToHash(tf.coinbase, codehash, dochash) if err != nil { t.Errorf("error calling contract registry: %v", err) } + fmt.Printf("GlobalRegistrar: %v, HashReg: %v, UrlHint: %v\n", registrar.GlobalRegistrarAddr, registrar.HashRegAddr, registrar.UrlHintAddr) if tf.lastConfirm != testExpNotice { t.Errorf("Wrong confirm message. expected '%v', got '%v'", testExpNotice, tf.lastConfirm) } // test unknown method - exp := fmt.Sprintf(testExpNotice2, resolver.HashRegContractAddress) - _, err = registry.SetOwner(tf.coinbase) + exp := fmt.Sprintf(testExpNotice2, registrar.HashRegAddr) + _, err = reg.SetOwner(tf.coinbase) if err != nil { t.Errorf("error setting owner: %v", err) } @@ -230,9 +239,9 @@ func TestNatspecE2E(t *testing.T) { } // test unknown contract - exp = fmt.Sprintf(testExpNotice3, resolver.UrlHintContractAddress) + exp = fmt.Sprintf(testExpNotice3, registrar.UrlHintAddr) - _, err = registry.RegisterUrl(tf.coinbase, dochash, "file:///test.content") + _, err = reg.SetUrlToHash(tf.coinbase, dochash, "file:///test.content") if err != nil { t.Errorf("error registering: %v", err) } |