diff options
Diffstat (limited to 'common/natspec')
-rw-r--r-- | common/natspec/natspec.go | 24 | ||||
-rw-r--r-- | common/natspec/natspec_e2e_test.go | 166 | ||||
-rw-r--r-- | common/natspec/natspec_e2e_test.go.orig | 253 |
3 files changed, 393 insertions, 50 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..db5ef2527 100644 --- a/common/natspec/natspec_e2e_test.go +++ b/common/natspec/natspec_e2e_test.go @@ -3,16 +3,18 @@ package natspec import ( "fmt" "io/ioutil" + "math/big" "os" + "runtime" "strings" "testing" + "time" "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" "github.com/ethereum/go-ethereum/eth" xe "github.com/ethereum/go-ethereum/xeth" @@ -73,13 +75,10 @@ const ( ) type testFrontend struct { - t *testing.T - // resolver *resolver.Resolver + t *testing.T ethereum *eth.Ethereum xeth *xe.XEth - coinbase common.Address - stateDb *state.StateDB - txc uint64 + wait chan *big.Int lastConfirm string wantNatSpec bool } @@ -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 @@ -128,6 +124,8 @@ func testEth(t *testing.T) (ethereum *eth.Ethereum, err error) { DataDir: "/tmp/eth-natspec", AccountManager: am, MaxPeers: 0, + PowTest: true, + Etherbase: testAddress, }) if err != nil { @@ -153,30 +151,41 @@ func testInit(t *testing.T) (self *testFrontend) { // mock frontend self = &testFrontend{t: t, ethereum: ethereum} self.xeth = xe.New(ethereum, self) - - addr, _ := ethereum.Etherbase() - self.coinbase = addr - self.stateDb = self.ethereum.ChainManager().State().Copy() + self.wait = self.xeth.UpdateState() + addr, _ := self.ethereum.Etherbase() // initialise the registry contracts - // self.resolver.CreateContracts(addr) - resolver.New(self.xeth).CreateContracts(addr) - self.applyTxs() - // t.Logf("HashReg contract registered at %v", resolver.HashRegContractAddress) - // t.Logf("URLHint contract registered at %v", resolver.UrlHintContractAddress) + reg := registrar.New(self.xeth) + var registrarTxhash, hashRegTxhash, urlHintTxhash string + registrarTxhash, err = reg.SetGlobalRegistrar("", addr) + if err != nil { + t.Errorf("error creating GlobalRegistrar: %v", err) + } - return + hashRegTxhash, err = reg.SetHashReg("", addr) + if err != nil { + t.Errorf("error creating HashReg: %v", err) + } + urlHintTxhash, err = reg.SetUrlHint("", addr) + if err != nil { + t.Errorf("error creating UrlHint: %v", err) + } + if !processTxs(self, t, 3) { + t.Errorf("error mining txs") + } + _ = registrarTxhash + _ = hashRegTxhash + _ = urlHintTxhash -} + /* TODO: + * lookup receipt and contract addresses by tx hash + * name registration for HashReg and UrlHint addresses + * mine those transactions + * then set once more SetHashReg SetUrlHint + */ -// this is needed for transaction to be applied to the state in testing -// the heavy lifing is done in XEth.ApplyTestTxs -// this is fragile, -// and does process leaking since xeth loops cannot quit safely -// should be replaced by proper mining with testDAG for easy full integration tests -func (self *testFrontend) applyTxs() { - self.txc, self.xeth = self.xeth.ApplyTestTxs(self.stateDb, self.coinbase, self.txc) return + } // end to end test @@ -185,54 +194,63 @@ func TestNatspecE2E(t *testing.T) { tf := testInit(t) defer tf.ethereum.Stop() + addr, _ := tf.ethereum.Etherbase() // create a contractInfo file (mock cloud-deployed contract metadocs) // incidentally this is the info for the registry contract itself ioutil.WriteFile("/tmp/"+testFileName, []byte(testContractInfo), os.ModePerm) - dochash := common.BytesToHash(crypto.Sha3([]byte(testContractInfo))) + dochash := crypto.Sha3Hash([]byte(testContractInfo)) // take the codehash for the contract we wanna test - // codehex := tf.xeth.CodeAt(resolver.HashRegContractAddress) - codeb := tf.xeth.CodeAtBytes(resolver.HashRegContractAddress) - codehash := common.BytesToHash(crypto.Sha3(codeb)) + codeb := tf.xeth.CodeAtBytes(registrar.HashRegAddr) + codehash := crypto.Sha3Hash(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(addr, codehash, dochash) if err != nil { t.Errorf("error registering: %v", err) } - // apply txs to the state - tf.applyTxs() + _, err = reg.SetUrlToHash(addr, dochash, "file:///"+testFileName) + if err != nil { + t.Errorf("error registering: %v", err) + } + if !processTxs(tf, t, 5) { + return + } // NatSpec info for register method of HashReg contract installed // 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(addr, 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) + t.Errorf("Wrong confirm message. expected\n'%v', got\n'%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(addr) if err != nil { t.Errorf("error setting owner: %v", err) } if tf.lastConfirm != exp { - t.Errorf("Wrong confirm message, expected '%v', got '%v'", exp, tf.lastConfirm) + t.Errorf("Wrong confirm message, expected\n'%v', got\n'%v'", exp, tf.lastConfirm) } // 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(addr, dochash, "file:///test.content") if err != nil { t.Errorf("error registering: %v", err) } @@ -242,3 +260,63 @@ func TestNatspecE2E(t *testing.T) { } } + +func pendingTransactions(repl *testFrontend, t *testing.T) (txc int64, err error) { + txs := repl.ethereum.TxPool().GetTransactions() + return int64(len(txs)), nil +} + +func processTxs(repl *testFrontend, t *testing.T, expTxc int) bool { + var txc int64 + var err error + for i := 0; i < 50; i++ { + txc, err = pendingTransactions(repl, t) + if err != nil { + t.Errorf("unexpected error checking pending transactions: %v", err) + return false + } + if expTxc < int(txc) { + t.Errorf("too many pending transactions: expected %v, got %v", expTxc, txc) + return false + } else if expTxc == int(txc) { + break + } + time.Sleep(100 * time.Millisecond) + } + if int(txc) != expTxc { + t.Errorf("incorrect number of pending transactions, expected %v, got %v", expTxc, txc) + return false + } + + err = repl.ethereum.StartMining(runtime.NumCPU()) + if err != nil { + t.Errorf("unexpected error mining: %v", err) + return false + } + defer repl.ethereum.StopMining() + + timer := time.NewTimer(100 * time.Second) + height := new(big.Int).Add(repl.xeth.CurrentBlock().Number(), big.NewInt(1)) + repl.wait <- height + select { + case <-timer.C: + // if times out make sure the xeth loop does not block + go func() { + select { + case repl.wait <- nil: + case <-repl.wait: + } + }() + case <-repl.wait: + } + txc, err = pendingTransactions(repl, t) + if err != nil { + t.Errorf("unexpected error checking pending transactions: %v", err) + return false + } + if txc != 0 { + t.Errorf("%d trasactions were not mined", txc) + return false + } + return true +} diff --git a/common/natspec/natspec_e2e_test.go.orig b/common/natspec/natspec_e2e_test.go.orig new file mode 100644 index 000000000..ae8e17ad9 --- /dev/null +++ b/common/natspec/natspec_e2e_test.go.orig @@ -0,0 +1,253 @@ +package natspec + +import ( + "fmt" + "io/ioutil" + "os" + "strings" + "testing" + + "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/registrar" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/eth" + xe "github.com/ethereum/go-ethereum/xeth" +) + +const ( + testBalance = "10000000000000000000" + + testFileName = "long_file_name_for_testing_registration_of_URLs_longer_than_32_bytes.content" + + testNotice = "Register key `utils.toHex(_key)` <- content `utils.toHex(_content)`" + + testExpNotice = "Register key 0xadd1a7d961cff0242089674ec2ef6fca671ab15e1fe80e38859fc815b98d88ab <- content 0xb3a2dea218de5d8bbe6c4645aadbf67b5ab00ecb1a9ec95dbdad6a0eed3e41a7" + + testExpNotice2 = `About to submit transaction (NatSpec notice error: abi key does not match any method): {"params":[{"to":"%s","data": "0x31e12c20"}]}` + + testExpNotice3 = `About to submit transaction (no NatSpec info found for contract: content hash not found for '0x1392c62d05b2d149e22a339c531157ae06b44d39a674cce500064b12b9aeb019'): {"params":[{"to":"%s","data": "0x300a3bbfb3a2dea218de5d8bbe6c4645aadbf67b5ab00ecb1a9ec95dbdad6a0eed3e41a7000000000000000000000000000000000000000000000000000000000000000000000000000000000000000066696c653a2f2f2f746573742e636f6e74656e74"}]}` +) + +const ( + testUserDoc = ` +{ + "methods": { + "register(uint256,uint256)": { + "notice": "` + testNotice + `" + } + }, + "invariants": [ + { "notice": "" } + ], + "construction": [ + { "notice": "" } + ] +} +` + testAbiDefinition = ` +[{ + "name": "register", + "constant": false, + "type": "function", + "inputs": [{ + "name": "_key", + "type": "uint256" + }, { + "name": "_content", + "type": "uint256" + }], + "outputs": [] +}] +` + + testContractInfo = ` +{ + "userDoc": ` + testUserDoc + `, + "abiDefinition": ` + testAbiDefinition + ` +} +` +) + +type testFrontend struct { + t *testing.T + ethereum *eth.Ethereum + xeth *xe.XEth + coinbase common.Address + stateDb *state.StateDB + txc uint64 + lastConfirm string + wantNatSpec bool +} + +func (self *testFrontend) UnlockAccount(acc []byte) bool { + self.ethereum.AccountManager().Unlock(common.BytesToAddress(acc), "password") + return true +} + +func (self *testFrontend) ConfirmTransaction(tx string) bool { + if self.wantNatSpec { + ds := docserver.New("/tmp/") + self.lastConfirm = GetNotice(self.xeth, tx, ds) + } + return true +} + +func testEth(t *testing.T) (ethereum *eth.Ethereum, err error) { + + os.RemoveAll("/tmp/eth-natspec/") + + err = os.MkdirAll("/tmp/eth-natspec/keystore", os.ModePerm) + if err != nil { + panic(err) + } + + // create a testAddress + ks := crypto.NewKeyStorePassphrase("/tmp/eth-natspec/keystore") + am := accounts.NewManager(ks) + testAccount, err := am.NewAccount("password") + if err != nil { + panic(err) + } + testAddress := strings.TrimPrefix(testAccount.Address.Hex(), "0x") + + // set up mock genesis with balance on the testAddress + core.GenesisAccounts = []byte(`{ + "` + testAddress + `": {"balance": "` + testBalance + `"} + }`) + + // only use minimalistic stack with no networking + ethereum, err = eth.New(ð.Config{ + DataDir: "/tmp/eth-natspec", + AccountManager: am, + MaxPeers: 0, + }) + + if err != nil { + panic(err) + } + + return +} + +func testInit(t *testing.T) (self *testFrontend) { + // initialise and start minimal ethereum stack + ethereum, err := testEth(t) + if err != nil { + t.Errorf("error creating ethereum: %v", err) + return + } + err = ethereum.Start() + if err != nil { + t.Errorf("error starting ethereum: %v", err) + return + } + + // mock frontend + self = &testFrontend{t: t, ethereum: ethereum} + self.xeth = xe.New(ethereum, self) + + addr, _ := ethereum.Etherbase() + self.coinbase = addr + self.stateDb = self.ethereum.ChainManager().State().Copy() + + // initialise the registry contracts + 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() + + return + +} + +// this is needed for transaction to be applied to the state in testing +// the heavy lifing is done in XEth.ApplyTestTxs +// this is fragile, +// and does process leaking since xeth loops cannot quit safely +// should be replaced by proper mining with testDAG for easy full integration tests +func (self *testFrontend) applyTxs() { + self.txc, self.xeth = self.xeth.ApplyTestTxs(self.stateDb, self.coinbase, self.txc) + return +} + +// end to end test +func TestNatspecE2E(t *testing.T) { + t.Skip() + + tf := testInit(t) + defer tf.ethereum.Stop() + + // create a contractInfo file (mock cloud-deployed contract metadocs) + // incidentally this is the info for the registry contract itself + ioutil.WriteFile("/tmp/"+testFileName, []byte(testContractInfo), os.ModePerm) + dochash := common.BytesToHash(crypto.Sha3([]byte(testContractInfo))) + + // take the codehash for the contract we wanna test + // 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 + // 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) + } + // apply txs to the state + tf.applyTxs() + + // NatSpec info for register method of HashReg contract installed + // now using the same transactions to check confirm messages + + tf.wantNatSpec = true // this is set so now the backend uses natspec confirmation + _, 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, registrar.HashRegAddr) + _, err = reg.SetOwner(tf.coinbase) + if err != nil { + t.Errorf("error setting owner: %v", err) + } + + if tf.lastConfirm != exp { + t.Errorf("Wrong confirm message, expected '%v', got '%v'", exp, tf.lastConfirm) + } + + // test unknown contract + exp = fmt.Sprintf(testExpNotice3, registrar.UrlHintAddr) + + _, err = reg.SetUrlToHash(tf.coinbase, dochash, "file:///test.content") + if err != nil { + t.Errorf("error registering: %v", err) + } + + if tf.lastConfirm != exp { + t.Errorf("Wrong confirm message, expected '%v', got '%v'", exp, tf.lastConfirm) + } + +} |