reorg:
- statereg methods move to natspec/resolver/docserver - fix failing test on invalid js input
This commit is contained in:
parent
5b0ea1044a
commit
3a540425a3
@ -14,21 +14,6 @@ var (
|
|||||||
schemes = map[string]func(*DocServer) http.RoundTripper{
|
schemes = map[string]func(*DocServer) http.RoundTripper{
|
||||||
// Simple File server from local disk file:///etc/passwd :)
|
// Simple File server from local disk file:///etc/passwd :)
|
||||||
"file": fileServerOnDocRoot,
|
"file": fileServerOnDocRoot,
|
||||||
|
|
||||||
// Swarm RoundTripper for bzz scheme bzz://mydomain/contact/twitter.json
|
|
||||||
// "bzz": &bzz.FileServer{},
|
|
||||||
|
|
||||||
// swarm remote file system call bzzfs:///etc/passwd
|
|
||||||
// "bzzfs": &bzzfs.FileServer{},
|
|
||||||
|
|
||||||
// restful peer protocol RoundTripper for enode scheme:
|
|
||||||
// POST suggestPeer DELETE removePeer PUT switch protocols
|
|
||||||
// RPC - standard protocol provides arc API for self enode (very safe remote server only connects to registered enode)
|
|
||||||
// POST enode://ae234dfgc56b..@128.56.68.5:3456/eth/getBlock/356eac67890fe
|
|
||||||
// and remote peer protocol
|
|
||||||
// POST enode://ae234dfgc56b..@128.56.68.5:3456/eth/getBlock/356eac67890fe
|
|
||||||
// proxy protoocol
|
|
||||||
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -8,6 +8,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"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/crypto"
|
||||||
"github.com/ethereum/go-ethereum/xeth"
|
"github.com/ethereum/go-ethereum/xeth"
|
||||||
)
|
)
|
||||||
@ -22,8 +24,7 @@ type NatSpec struct {
|
|||||||
// abiDoc abiDoc
|
// abiDoc abiDoc
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: should initialise with abi and userdoc jsons
|
func New(xeth *xeth.XEth, tx string, http *docserver.DocServer) (self *NatSpec, err error) {
|
||||||
func New(xeth *xeth.XEth, tx string) (self *NatSpec, err error) {
|
|
||||||
|
|
||||||
// extract contract address from tx
|
// extract contract address from tx
|
||||||
|
|
||||||
@ -45,21 +46,26 @@ func New(xeth *xeth.XEth, tx string) (self *NatSpec, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
codeHash := xeth.CodeAt(contractAddress)
|
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)
|
// resolve host via nameReg/UrlHint Resolver
|
||||||
|
uri, hash, err := res.NameToUrl(codeHash)
|
||||||
natspecHash, err1 := statereg.GetNatSpec(codeHash)
|
if err != nil {
|
||||||
if err1 != nil {
|
return
|
||||||
return nil, err1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// retrieve content
|
// get content via http client and authenticate content using hash
|
||||||
|
content, err := http.GetAuthContent(uri, hash)
|
||||||
content, err2 := statereg.GetContent(natspecHash)
|
if err != nil {
|
||||||
if err2 != nil {
|
return
|
||||||
return nil, err2
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// get abi, userdoc
|
// get abi, userdoc
|
||||||
|
@ -107,29 +107,23 @@ func TestMissingMethod(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// test invalid desc
|
// test invalid desc
|
||||||
/*
|
|
||||||
func TestInvalidDesc(t *testing.T) {
|
func TestInvalidDesc(t *testing.T) {
|
||||||
|
|
||||||
desc := "Will multiply 122 by \"7\" and return 854."
|
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)
|
userdoc := makeUserdoc(desc)
|
||||||
|
|
||||||
ns, err := NewWithDocs(abi, userdoc, tx)
|
_, err := NewWithDocs(abi, userdoc, tx)
|
||||||
if err != nil {
|
|
||||||
t.Errorf("New: error: %v", err)
|
|
||||||
}
|
|
||||||
notice, err := ns.Notice()
|
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("expected error, got nothing (notice: '%v')", err, notice)
|
t.Errorf("expected error, got nothing", err)
|
||||||
} else {
|
} else {
|
||||||
if err.Error() != expected {
|
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
|
// test wrong input params
|
||||||
func TestWrongInputParams(t *testing.T) {
|
func TestWrongInputParams(t *testing.T) {
|
||||||
|
@ -1,13 +1,7 @@
|
|||||||
package natspec
|
package natspec
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"fmt"
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
|
||||||
"github.com/ethereum/go-ethereum/xeth"
|
"github.com/ethereum/go-ethereum/xeth"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type StateReg struct {
|
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
|
|
||||||
|
|
||||||
}
|
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package resolver
|
package resolver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
// "net/url"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/xeth"
|
"github.com/ethereum/go-ethereum/xeth"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -12,6 +14,9 @@ import (
|
|||||||
Resolver implements the Ethereum DNS mapping
|
Resolver implements the Ethereum DNS mapping
|
||||||
NameReg : Domain Name (or Code hash of Contract) -> Content Hash
|
NameReg : Domain Name (or Code hash of Contract) -> Content Hash
|
||||||
UrlHint : Content Hash -> Url Hint
|
UrlHint : Content Hash -> Url Hint
|
||||||
|
|
||||||
|
The resolver is meant to be called by the roundtripper transport implementation
|
||||||
|
of a url scheme
|
||||||
*/
|
*/
|
||||||
const (
|
const (
|
||||||
urlHintContractAddress = "urlhint"
|
urlHintContractAddress = "urlhint"
|
||||||
@ -20,26 +25,52 @@ const (
|
|||||||
|
|
||||||
type Resolver struct {
|
type Resolver struct {
|
||||||
xeth *xeth.XEth
|
xeth *xeth.XEth
|
||||||
|
urlHintContractAddress string
|
||||||
|
nameRegContractAddress string
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(_xeth *xeth.XEth, uhca, nrca string) *Resolver {
|
||||||
|
return &Resolver{_xeth, uhca, nrca}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Resolver) NameToContentHash(name string) (hash common.Hash, err error) {
|
func (self *Resolver) NameToContentHash(name string) (hash common.Hash, err error) {
|
||||||
// look up in nameReg
|
// look up in nameReg
|
||||||
copy(hash[:], []byte(name)[:32])
|
hashbytes := self.xeth.StorageAt(self.nameRegContractAddress, storageAddress(0, common.Hex2Bytes(name)))
|
||||||
|
copy(hash[:], hashbytes[:32])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Resolver) ContentHashToUrl(hash common.Hash) (uri *url.URL, err error) {
|
func (self *Resolver) ContentHashToUrl(hash common.Hash) (uri string, err error) {
|
||||||
// look up in nameReg
|
// look up in nameReg
|
||||||
rawurl := fmt.Sprintf("bzz://%x/my/path/mycontract.sud", hash[:])
|
|
||||||
|
urlHex := self.xeth.StorageAt(self.urlHintContractAddress, storageAddress(0, hash.Bytes()))
|
||||||
|
uri = string(common.Hex2Bytes(urlHex))
|
||||||
|
l := len(uri)
|
||||||
|
for (l > 0) && (uri[l-1] == 0) {
|
||||||
|
l--
|
||||||
|
}
|
||||||
|
uri = uri[:l]
|
||||||
|
if l == 0 {
|
||||||
|
err = fmt.Errorf("GetURLhint: URL hint not found")
|
||||||
|
}
|
||||||
|
// rawurl := fmt.Sprintf("bzz://%x/my/path/mycontract.s ud", hash[:])
|
||||||
// mime type?
|
// mime type?
|
||||||
return url.Parse(rawurl)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Resolver) NameToUrl(name string) (uri *url.URL, err error) {
|
func (self *Resolver) NameToUrl(name string) (uri string, hash common.Hash, err error) {
|
||||||
// look up in urlHint
|
// look up in urlHint
|
||||||
hash, err := self.NameToContentHash(name)
|
hash, err = self.NameToContentHash(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return self.ContentHashToUrl(hash)
|
uri, err = self.ContentHashToUrl(hash)
|
||||||
|
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))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user