- statereg methods move to natspec/resolver/docserver
- fix failing test on invalid js input
This commit is contained in:
zelig
2015-03-31 16:02:55 +01:00
parent 5b0ea1044a
commit 3a540425a3
5 changed files with 64 additions and 114 deletions

View File

@ -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)
// parse out host/domain
// retrieve natspec info content hash
// set up nameresolver with natspecreg + urlhint contract addresses
stateReg := NewStateReg(xeth)
res := resolver.New(
xeth,
stateReg.caNatSpec,
stateReg.caURL,
)
statereg := NewStateReg(xeth)
natspecHash, err1 := statereg.GetNatSpec(codeHash)
if err1 != nil {
return nil, err1
// 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

View File

@ -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) {

View File

@ -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
}