Преглед изворни кода

default fallback NatSpec messages

zsfelfoldi пре 10 година
родитељ
комит
b46e152171
3 измењених фајлова са 50 додато и 22 уклоњено
  1. 1 8
      cmd/geth/js.go
  2. 28 1
      common/natspec/natspec.go
  3. 21 13
      common/natspec/natspec_e2e_test.go

+ 1 - 8
cmd/geth/js.go

@@ -143,14 +143,7 @@ var net = web3.net;
 var ds, _ = docserver.New(utils.JSpathFlag.String())
 
 func (self *jsre) ConfirmTransaction(tx string) bool {
-	var notice string
-	nat, err := natspec.New(self.xeth, tx, ds)
-	if err == nil {
-		notice, err = nat.Notice()
-	}
-	if err != nil {
-		notice = fmt.Sprintf("About to submit transaction: %v")
-	}
+	notice := natspec.GetNotice(self.xeth, tx, ds)
 	fmt.Println(notice)
 	answer, _ := self.Prompt("Confirm Transaction\n[y/n] ")
 	return strings.HasPrefix(strings.Trim(answer, " "), "y")

+ 28 - 1
common/natspec/natspec.go

@@ -24,6 +24,33 @@ type NatSpec struct {
 	// abiDoc abiDoc
 }
 
+func getFallbackNotice(comment, tx string) string {
+
+	return "About to submit transaction (" + comment + "): " + tx
+
+}
+
+func GetNotice(xeth *xeth.XEth, tx string, http *docserver.DocServer) (notice string) {
+
+	ns, err := New(xeth, tx, http)
+	if err != nil {
+		if ns == nil {
+			return getFallbackNotice("no NatSpec info found for contract", tx)
+		} else {
+			return getFallbackNotice("invalid NatSpec info", tx)
+		}
+	}
+
+	notice, err2 := ns.Notice()
+
+	if err2 != nil {
+		return getFallbackNotice("couldn't create NatSpec notice", tx)
+	}
+
+	return
+
+}
+
 func New(xeth *xeth.XEth, tx string, http *docserver.DocServer) (self *NatSpec, err error) {
 
 	// extract contract address from tx
@@ -46,7 +73,7 @@ func New(xeth *xeth.XEth, tx string, http *docserver.DocServer) (self *NatSpec,
 		return
 	}
 	codehex := xeth.CodeAt(contractAddress)
-	codeHash := common.BytesToHash(crypto.Sha3(common.Hex2Bytes(codehex)))
+	codeHash := common.BytesToHash(crypto.Sha3(common.Hex2Bytes(codehex[2:])))
 	// parse out host/domain
 
 	// set up nameresolver with natspecreg + urlhint contract addresses

+ 21 - 13
common/natspec/natspec_e2e_test.go

@@ -32,7 +32,9 @@ type testFrontend struct {
 }
 
 const testNotice = "Register key `_key` <- content `_content`"
-const testExpNotice = "Register key 8.9477152217924674838424037953991966239322087453347756267410168184682657981552e+76 <- content 2.9345842665291639932787537650068479186757226656217642728276414736233000517704e+76"
+const testExpNotice = "Register key 7.8620552293692941353904828453472309202657763561546399188096247518683806402731e+76 <- content 2.9345842665291639932787537650068479186757226656217642728276414736233000517704e+76"
+const testExpNotice2 = `About to submit transaction (couldn't create NatSpec notice): {"id":6,"jsonrpc":"2.0","method":"eth_transact","params":[{"from":"0xe273f01c99144c438695e10f24926dc1f9fbf62d","to":"0x0000000000000000000000000000000000000009","value":"100000000000","gas":"100000","gasPrice":"100000","data":"0x31e12c20"}]}`
+const testExpNotice3 = `About to submit transaction (no NatSpec info found for contract): {"id":6,"jsonrpc":"2.0","method":"eth_transact","params":[{"from":"0xe273f01c99144c438695e10f24926dc1f9fbf62d","to":"0x0000000000000000000000000000000000000008","value":"100000000000","gas":"100000","gasPrice":"100000","data":"0xd66d6c1040e128891caf6033850eb422796ecd92ca7393052020b64455cf8ac00c4ac04800000000000000000000000066696c653a2f2f2f746573742e636f6e74656e74"}]}`
 
 const testUserDoc = `
 {
@@ -89,15 +91,7 @@ func (f *testFrontend) ConfirmTransaction(tx string) bool {
 		if err != nil {
 			f.t.Errorf("Error creating DocServer: %v", err)
 		}
-		nat, err2 := natspec.New(f.xeth, tx, ds)
-		if err2 == nil {
-			f.lastConfirm, err = nat.Notice()
-			if err != nil {
-				f.t.Errorf("Notice error: %v", err)
-			}
-		} else {
-			f.t.Errorf("Error creating NatSpec: %v", err2)
-		}
+		f.lastConfirm = natspec.GetNotice(f.xeth, tx, ds)
 	}
 	return true
 }
@@ -270,7 +264,7 @@ func TestNatspecE2E(t *testing.T) {
 	dochash := common.BytesToHash(crypto.Sha3([]byte(testDocs)))
 
 	codehex := tf.xeth.CodeAt(core.ContractAddrHashReg)
-	codehash := common.BytesToHash(crypto.Sha3(common.Hex2Bytes(codehex)))
+	codehash := common.BytesToHash(crypto.Sha3(common.Hex2Bytes(codehex[2:])))
 
 	tf.setOwner()
 	tf.registerNatSpec(codehash, dochash)
@@ -288,12 +282,26 @@ func TestNatspecE2E(t *testing.T) {
 	}
 	t.Logf("url = %v  err = %v", url, err2)
 
+	// NatSpec info for register method of HashReg contract installed
+	// now using the same transactions to check confirm messages
+
 	tf.makeNatSpec = true
-	tf.registerNatSpec(codehash, dochash) // call again just to get a confirm message
+	tf.registerNatSpec(codehash, dochash)
 	t.Logf("Confirm message: %v\n", tf.lastConfirm)
-
 	if tf.lastConfirm != testExpNotice {
 		t.Errorf("Wrong confirm message, expected '%v', got '%v'", testExpNotice, tf.lastConfirm)
 	}
 
+	tf.setOwner()
+	t.Logf("Confirm message for unknown method: %v\n", tf.lastConfirm)
+	if tf.lastConfirm != testExpNotice2 {
+		t.Errorf("Wrong confirm message, expected '%v', got '%v'", testExpNotice2, tf.lastConfirm)
+	}
+
+	tf.registerURL(dochash, "file:///test.content")
+	t.Logf("Confirm message for unknown contract: %v\n", tf.lastConfirm)
+	if tf.lastConfirm != testExpNotice3 {
+		t.Errorf("Wrong confirm message, expected '%v', got '%v'", testExpNotice3, tf.lastConfirm)
+	}
+
 }