aboutsummaryrefslogtreecommitdiffstats
path: root/common/natspec
diff options
context:
space:
mode:
authorzelig <viktor.tron@gmail.com>2015-03-31 23:02:55 +0800
committerzelig <viktor.tron@gmail.com>2015-04-20 03:57:48 +0800
commit3a540425a3d7fc29233b5cd3e7b79b4866f35d57 (patch)
treed1e101d7dcf79d362d087d431f5c4cefafbd30fd /common/natspec
parent5b0ea1044ab7c14e11489bc6c612bb7f2e5a023e (diff)
downloaddexon-3a540425a3d7fc29233b5cd3e7b79b4866f35d57.tar
dexon-3a540425a3d7fc29233b5cd3e7b79b4866f35d57.tar.gz
dexon-3a540425a3d7fc29233b5cd3e7b79b4866f35d57.tar.bz2
dexon-3a540425a3d7fc29233b5cd3e7b79b4866f35d57.tar.lz
dexon-3a540425a3d7fc29233b5cd3e7b79b4866f35d57.tar.xz
dexon-3a540425a3d7fc29233b5cd3e7b79b4866f35d57.tar.zst
dexon-3a540425a3d7fc29233b5cd3e7b79b4866f35d57.zip
reorg:
- statereg methods move to natspec/resolver/docserver - fix failing test on invalid js input
Diffstat (limited to 'common/natspec')
-rw-r--r--common/natspec/natspec.go36
-rw-r--r--common/natspec/natspec_test.go16
-rw-r--r--common/natspec/statereg.go66
3 files changed, 26 insertions, 92 deletions
diff --git a/common/natspec/natspec.go b/common/natspec/natspec.go
index 883e27ef3..a9ac2d1b2 100644
--- a/common/natspec/natspec.go
+++ b/common/natspec/natspec.go
@@ -8,6 +8,8 @@ import (
"strings"
"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/crypto"
"github.com/ethereum/go-ethereum/xeth"
)
@@ -22,8 +24,7 @@ type NatSpec struct {
// abiDoc abiDoc
}
-// TODO: should initialise with abi and userdoc jsons
-func New(xeth *xeth.XEth, tx string) (self *NatSpec, err error) {
+func New(xeth *xeth.XEth, tx string, http *docserver.DocServer) (self *NatSpec, err error) {
// extract contract address from tx
@@ -45,21 +46,26 @@ func New(xeth *xeth.XEth, tx string) (self *NatSpec, err error) {
return
}
codeHash := xeth.CodeAt(contractAddress)
-
- // retrieve natspec info content hash
-
- statereg := NewStateReg(xeth)
-
- natspecHash, err1 := statereg.GetNatSpec(codeHash)
- if err1 != nil {
- return nil, err1
+ // parse out host/domain
+
+ // set up nameresolver with natspecreg + urlhint contract addresses
+ stateReg := NewStateReg(xeth)
+ res := resolver.New(
+ xeth,
+ stateReg.caNatSpec,
+ stateReg.caURL,
+ )
+
+ // resolve host via nameReg/UrlHint Resolver
+ uri, hash, err := res.NameToUrl(codeHash)
+ if err != nil {
+ return
}
- // retrieve content
-
- content, err2 := statereg.GetContent(natspecHash)
- if err2 != nil {
- return nil, err2
+ // get content via http client and authenticate content using hash
+ content, err := http.GetAuthContent(uri, hash)
+ if err != nil {
+ return
}
// get abi, userdoc
diff --git a/common/natspec/natspec_test.go b/common/natspec/natspec_test.go
index ac894e9b1..35a59469a 100644
--- a/common/natspec/natspec_test.go
+++ b/common/natspec/natspec_test.go
@@ -107,29 +107,23 @@ func TestMissingMethod(t *testing.T) {
}
// test invalid desc
-/*
+
func TestInvalidDesc(t *testing.T) {
desc := "Will multiply 122 by \"7\" and return 854."
- expected := "natspec.js error setting expression: (anonymous): Line 1:41 Unexpected number"
+ expected := "invalid character '7' after object key:value pair"
userdoc := makeUserdoc(desc)
- ns, err := NewWithDocs(abi, userdoc, tx)
- if err != nil {
- t.Errorf("New: error: %v", err)
- }
- notice, err := ns.Notice()
-
+ _, err := NewWithDocs(abi, userdoc, tx)
if err == nil {
- t.Errorf("expected error, got nothing (notice: '%v')", err, notice)
+ t.Errorf("expected error, got nothing", err)
} else {
if err.Error() != expected {
- t.Errorf("expected error '%s' got '%v' (notice: '%v')", expected, err, notice)
+ t.Errorf("expected error '%s' got '%v'", expected, err)
}
}
}
-*/
// test wrong input params
func TestWrongInputParams(t *testing.T) {
diff --git a/common/natspec/statereg.go b/common/natspec/statereg.go
index cb5dd4ce1..b76990540 100644
--- a/common/natspec/statereg.go
+++ b/common/natspec/statereg.go
@@ -1,13 +1,7 @@
package natspec
import (
- "encoding/binary"
- "fmt"
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/xeth"
- "io/ioutil"
- "net/http"
)
type StateReg struct {
@@ -48,63 +42,3 @@ func (self *StateReg) testCreateContracts() {
}
}
-
-func (self *StateReg) GetURLhint(hash string) (url string, err error) {
-
- url_hex := self.xeth.StorageAt(self.caURL, storageAddress(0, common.Hex2Bytes(hash)))
- url = string(common.Hex2Bytes(url_hex))
- l := len(url)
- for (l > 0) && (url[l-1] == 0) {
- l--
- }
- url = url[:l]
- if l == 0 {
- err = fmt.Errorf("GetURLhint: URL hint not found")
- }
- return
-
-}
-
-func storageAddress(varidx uint32, key []byte) string {
- data := make([]byte, 64)
- binary.BigEndian.PutUint32(data[28:32], varidx)
- copy(data[32:64], key[0:32])
- return common.Bytes2Hex(crypto.Sha3(data))
-}
-
-func (self *StateReg) GetNatSpec(codehash string) (hash string, err error) {
-
- hash = self.xeth.StorageAt(self.caNatSpec, storageAddress(0, common.Hex2Bytes(codehash)))
- return
-
-}
-
-func (self *StateReg) GetContent(hash string) (content []byte, err error) {
-
- // get URL
- url, err := self.GetURLhint(hash)
- if err != nil {
- return
- }
-
- // retrieve content
- resp, err := http.Get(url)
- defer resp.Body.Close()
- if err != nil {
- return
- }
- content, err = ioutil.ReadAll(resp.Body)
- if err != nil {
- return
- }
-
- // check hash
-
- if common.Bytes2Hex(crypto.Sha3(content)) != hash {
- content = nil
- err = fmt.Errorf("GetContent error: content hash mismatch")
- }
-
- return
-
-}