Skip to content

Commit 05f7407

Browse files
committed
Merge pull request ethereum#1919 from ethersphere/getnatspec
rpc api: eth_getNatSpec
2 parents 2e4fdce + 4d005a2 commit 05f7407

File tree

14 files changed

+120
-120
lines changed

14 files changed

+120
-120
lines changed

cmd/geth/js.go

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030

3131
"github.com/ethereum/go-ethereum/cmd/utils"
3232
"github.com/ethereum/go-ethereum/common"
33-
"github.com/ethereum/go-ethereum/common/docserver"
3433
"github.com/ethereum/go-ethereum/common/natspec"
3534
"github.com/ethereum/go-ethereum/common/registrar"
3635
"github.com/ethereum/go-ethereum/eth"
@@ -77,8 +76,6 @@ func (r dumbterm) PasswordPrompt(p string) (string, error) {
7776
func (r dumbterm) AppendHistory(string) {}
7877

7978
type jsre struct {
80-
docRoot string
81-
ds *docserver.DocServer
8279
re *re.JSRE
8380
ethereum *eth.Ethereum
8481
xeth *xeth.XEth
@@ -153,7 +150,6 @@ func newLightweightJSRE(docRoot string, client comms.EthereumClient, datadir str
153150
js := &jsre{ps1: "> "}
154151
js.wait = make(chan *big.Int)
155152
js.client = client
156-
js.ds = docserver.New(docRoot)
157153

158154
// update state in separare forever blocks
159155
js.re = re.New(docRoot)
@@ -181,18 +177,17 @@ func newLightweightJSRE(docRoot string, client comms.EthereumClient, datadir str
181177
}
182178

183179
func newJSRE(ethereum *eth.Ethereum, docRoot, corsDomain string, client comms.EthereumClient, interactive bool, f xeth.Frontend) *jsre {
184-
js := &jsre{ethereum: ethereum, ps1: "> ", docRoot: docRoot}
180+
js := &jsre{ethereum: ethereum, ps1: "> "}
185181
// set default cors domain used by startRpc from CLI flag
186182
js.corsDomain = corsDomain
187183
if f == nil {
188184
f = js
189185
}
190-
js.ds = docserver.New(docRoot)
191186
js.xeth = xeth.New(ethereum, f)
192187
js.wait = js.xeth.UpdateState()
193188
js.client = client
194189
if clt, ok := js.client.(*comms.InProcClient); ok {
195-
if offeredApis, err := api.ParseApiString(shared.AllApis, codec.JSON, js.xeth, ethereum, docRoot); err == nil {
190+
if offeredApis, err := api.ParseApiString(shared.AllApis, codec.JSON, js.xeth, ethereum); err == nil {
196191
clt.Initialize(api.Merge(offeredApis...))
197192
}
198193
}
@@ -248,14 +243,14 @@ func (self *jsre) batch(statement string) {
248243
// show summary of current geth instance
249244
func (self *jsre) welcome() {
250245
self.re.Run(`
251-
(function () {
252-
console.log('instance: ' + web3.version.client);
253-
console.log(' datadir: ' + admin.datadir);
254-
console.log("coinbase: " + eth.coinbase);
255-
var ts = 1000 * eth.getBlock(eth.blockNumber).timestamp;
256-
console.log("at block: " + eth.blockNumber + " (" + new Date(ts) + ")");
257-
})();
258-
`)
246+
(function () {
247+
console.log('instance: ' + web3.version.client);
248+
console.log(' datadir: ' + admin.datadir);
249+
console.log("coinbase: " + eth.coinbase);
250+
var ts = 1000 * eth.getBlock(eth.blockNumber).timestamp;
251+
console.log("at block: " + eth.blockNumber + " (" + new Date(ts) + ")");
252+
})();
253+
`)
259254
if modules, err := self.supportedApis(); err == nil {
260255
loadedModules := make([]string, 0)
261256
for api, version := range modules {
@@ -281,7 +276,7 @@ func (js *jsre) apiBindings(f xeth.Frontend) error {
281276
apiNames = append(apiNames, a)
282277
}
283278

284-
apiImpl, err := api.ParseApiString(strings.Join(apiNames, ","), codec.JSON, js.xeth, js.ethereum, js.docRoot)
279+
apiImpl, err := api.ParseApiString(strings.Join(apiNames, ","), codec.JSON, js.xeth, js.ethereum)
285280
if err != nil {
286281
utils.Fatalf("Unable to determine supported api's: %v", err)
287282
}
@@ -334,7 +329,7 @@ func (js *jsre) apiBindings(f xeth.Frontend) error {
334329
utils.Fatalf("Error setting namespaces: %v", err)
335330
}
336331

337-
js.re.Run(`var GlobalRegistrar = eth.contract(` + registrar.GlobalRegistrarAbi + `); registrar = GlobalRegistrar.at("` + registrar.GlobalRegistrarAddr + `");`)
332+
js.re.Run(`var GlobalRegistrar = eth.contract(` + registrar.GlobalRegistrarAbi + `); registrar = GlobalRegistrar.at("` + registrar.GlobalRegistrarAddr + `");`)
338333
return nil
339334
}
340335

@@ -348,7 +343,7 @@ func (self *jsre) AskPassword() (string, bool) {
348343

349344
func (self *jsre) ConfirmTransaction(tx string) bool {
350345
if self.ethereum.NatSpec {
351-
notice := natspec.GetNotice(self.xeth, tx, self.ds)
346+
notice := natspec.GetNotice(self.xeth, tx, self.ethereum.HTTPClient())
352347
fmt.Println(notice)
353348
answer, _ := self.Prompt("Confirm Transaction [y/n]")
354349
return strings.HasPrefix(strings.Trim(answer, " "), "y")

cmd/geth/js_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
"github.com/ethereum/go-ethereum/accounts"
3232
"github.com/ethereum/go-ethereum/common"
3333
"github.com/ethereum/go-ethereum/common/compiler"
34-
"github.com/ethereum/go-ethereum/common/docserver"
34+
"github.com/ethereum/go-ethereum/common/httpclient"
3535
"github.com/ethereum/go-ethereum/common/natspec"
3636
"github.com/ethereum/go-ethereum/common/registrar"
3737
"github.com/ethereum/go-ethereum/core"
@@ -62,7 +62,7 @@ var (
6262
type testjethre struct {
6363
*jsre
6464
lastConfirm string
65-
ds *docserver.DocServer
65+
client *httpclient.HTTPClient
6666
}
6767

6868
func (self *testjethre) UnlockAccount(acc []byte) bool {
@@ -75,7 +75,7 @@ func (self *testjethre) UnlockAccount(acc []byte) bool {
7575

7676
func (self *testjethre) ConfirmTransaction(tx string) bool {
7777
if self.ethereum.NatSpec {
78-
self.lastConfirm = natspec.GetNotice(self.xeth, tx, self.ds)
78+
self.lastConfirm = natspec.GetNotice(self.xeth, tx, self.client)
7979
}
8080
return true
8181
}
@@ -101,6 +101,7 @@ func testREPL(t *testing.T, config func(*eth.Config)) (string, *testjethre, *eth
101101
AccountManager: am,
102102
MaxPeers: 0,
103103
Name: "test",
104+
DocRoot: "/",
104105
SolcPath: testSolcPath,
105106
PowTest: true,
106107
NewDB: func(path string) (ethdb.Database, error) { return db, nil },
@@ -130,8 +131,7 @@ func testREPL(t *testing.T, config func(*eth.Config)) (string, *testjethre, *eth
130131

131132
assetPath := filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext")
132133
client := comms.NewInProcClient(codec.JSON)
133-
ds := docserver.New("/")
134-
tf := &testjethre{ds: ds}
134+
tf := &testjethre{client: ethereum.HTTPClient()}
135135
repl := newJSRE(ethereum, assetPath, "", client, false, tf)
136136
tf.jsre = repl
137137
return tmp, tf, ethereum

cmd/utils/flags.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ var (
139139
Name: "natspec",
140140
Usage: "Enable NatSpec confirmation notice",
141141
}
142+
DocRootFlag = DirectoryFlag{
143+
Name: "docroot",
144+
Usage: "Document Root for HTTPClient file scheme",
145+
Value: DirectoryString{common.HomeDir()},
146+
}
142147
CacheFlag = cli.IntFlag{
143148
Name: "cache",
144149
Usage: "Megabytes of memory allocated to internal caching",
@@ -452,6 +457,7 @@ func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
452457
Olympic: ctx.GlobalBool(OlympicFlag.Name),
453458
NAT: MakeNAT(ctx),
454459
NatSpec: ctx.GlobalBool(NatspecEnabledFlag.Name),
460+
DocRoot: ctx.GlobalString(DocRootFlag.Name),
455461
Discovery: !ctx.GlobalBool(NoDiscoverFlag.Name),
456462
NodeKey: MakeNodeKey(ctx),
457463
Shh: ctx.GlobalBool(WhisperEnabledFlag.Name),
@@ -616,7 +622,7 @@ func StartIPC(eth *eth.Ethereum, ctx *cli.Context) error {
616622
xeth := xeth.New(eth, fe)
617623
codec := codec.JSON
618624

619-
apis, err := api.ParseApiString(ctx.GlobalString(IPCApiFlag.Name), codec, xeth, eth, ctx.GlobalString(JSpathFlag.Name))
625+
apis, err := api.ParseApiString(ctx.GlobalString(IPCApiFlag.Name), codec, xeth, eth)
620626
if err != nil {
621627
return nil, err
622628
}
@@ -637,7 +643,7 @@ func StartRPC(eth *eth.Ethereum, ctx *cli.Context) error {
637643
xeth := xeth.New(eth, nil)
638644
codec := codec.JSON
639645

640-
apis, err := api.ParseApiString(ctx.GlobalString(RpcApiFlag.Name), codec, xeth, eth, ctx.GlobalString(JSpathFlag.Name))
646+
apis, err := api.ParseApiString(ctx.GlobalString(RpcApiFlag.Name), codec, xeth, eth)
641647
if err != nil {
642648
return err
643649
}

common/docserver/docserver.go renamed to common/httpclient/httpclient.go

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616

17-
package docserver
17+
package httpclient
1818

1919
import (
2020
"fmt"
@@ -26,14 +26,14 @@ import (
2626
"github.com/ethereum/go-ethereum/crypto"
2727
)
2828

29-
type DocServer struct {
29+
type HTTPClient struct {
3030
*http.Transport
3131
DocRoot string
3232
schemes []string
3333
}
3434

35-
func New(docRoot string) (self *DocServer) {
36-
self = &DocServer{
35+
func New(docRoot string) (self *HTTPClient) {
36+
self = &HTTPClient{
3737
Transport: &http.Transport{},
3838
DocRoot: docRoot,
3939
schemes: []string{"file"},
@@ -46,18 +46,18 @@ func New(docRoot string) (self *DocServer) {
4646

4747
// A Client is higher-level than a RoundTripper (such as Transport) and additionally handles HTTP details such as cookies and redirects.
4848

49-
func (self *DocServer) Client() *http.Client {
49+
func (self *HTTPClient) Client() *http.Client {
5050
return &http.Client{
5151
Transport: self,
5252
}
5353
}
5454

55-
func (self *DocServer) RegisterScheme(scheme string, rt http.RoundTripper) {
55+
func (self *HTTPClient) RegisterScheme(scheme string, rt http.RoundTripper) {
5656
self.schemes = append(self.schemes, scheme)
5757
self.RegisterProtocol(scheme, rt)
5858
}
5959

60-
func (self *DocServer) HasScheme(scheme string) bool {
60+
func (self *HTTPClient) HasScheme(scheme string) bool {
6161
for _, s := range self.schemes {
6262
if s == scheme {
6363
return true
@@ -66,43 +66,41 @@ func (self *DocServer) HasScheme(scheme string) bool {
6666
return false
6767
}
6868

69-
func (self *DocServer) GetAuthContent(uri string, hash common.Hash) (content []byte, err error) {
69+
func (self *HTTPClient) GetAuthContent(uri string, hash common.Hash) ([]byte, error) {
7070
// retrieve content
71-
content, err = self.Get(uri, "")
71+
content, err := self.Get(uri, "")
7272
if err != nil {
73-
return
73+
return nil, err
7474
}
7575

7676
// check hash to authenticate content
7777
chash := crypto.Sha3Hash(content)
7878
if chash != hash {
79-
content = nil
80-
err = fmt.Errorf("content hash mismatch %x != %x (exp)", hash[:], chash[:])
79+
return nil, fmt.Errorf("content hash mismatch %x != %x (exp)", hash[:], chash[:])
8180
}
8281

83-
return
82+
return content, nil
8483

8584
}
8685

8786
// Get(uri, path) downloads the document at uri, if path is non-empty it
8887
// is interpreted as a filepath to which the contents are saved
89-
func (self *DocServer) Get(uri, path string) (content []byte, err error) {
88+
func (self *HTTPClient) Get(uri, path string) ([]byte, error) {
9089
// retrieve content
9190
resp, err := self.Client().Get(uri)
92-
91+
if err != nil {
92+
return nil, err
93+
}
9394
defer func() {
9495
if resp != nil {
9596
resp.Body.Close()
9697
}
9798
}()
9899

99-
if err != nil {
100-
return
101-
}
102-
100+
var content []byte
103101
content, err = ioutil.ReadAll(resp.Body)
104102
if err != nil {
105-
return
103+
return nil, err
106104
}
107105

108106
if resp.StatusCode/100 != 2 {
@@ -112,9 +110,15 @@ func (self *DocServer) Get(uri, path string) (content []byte, err error) {
112110
if path != "" {
113111
var abspath string
114112
abspath, err = filepath.Abs(path)
115-
ioutil.WriteFile(abspath, content, 0700)
113+
if err != nil {
114+
return nil, err
115+
}
116+
err = ioutil.WriteFile(abspath, content, 0600)
117+
if err != nil {
118+
return nil, err
119+
}
116120
}
117121

118-
return
122+
return content, nil
119123

120124
}

common/docserver/docserver_test.go renamed to common/httpclient/httpclient_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616

17-
package docserver
17+
package httpclient
1818

1919
import (
2020
"io/ioutil"
@@ -28,19 +28,19 @@ import (
2828
)
2929

3030
func TestGetAuthContent(t *testing.T) {
31-
dir, err := ioutil.TempDir("", "docserver-test")
31+
dir, err := ioutil.TempDir("", "httpclient-test")
3232
if err != nil {
3333
t.Fatal("cannot create temporary directory:", err)
3434
}
3535
defer os.RemoveAll(dir)
36-
ds := New(dir)
36+
client := New(dir)
3737

3838
text := "test"
3939
hash := crypto.Sha3Hash([]byte(text))
4040
if err := ioutil.WriteFile(path.Join(dir, "test.content"), []byte(text), os.ModePerm); err != nil {
4141
t.Fatal("could not write test file", err)
4242
}
43-
content, err := ds.GetAuthContent("file:///test.content", hash)
43+
content, err := client.GetAuthContent("file:///test.content", hash)
4444
if err != nil {
4545
t.Errorf("no error expected, got %v", err)
4646
}
@@ -49,7 +49,7 @@ func TestGetAuthContent(t *testing.T) {
4949
}
5050

5151
hash = common.Hash{}
52-
content, err = ds.GetAuthContent("file:///test.content", hash)
52+
content, err = client.GetAuthContent("file:///test.content", hash)
5353
expected := "content hash mismatch 0000000000000000000000000000000000000000000000000000000000000000 != 9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658 (exp)"
5454
if err == nil {
5555
t.Errorf("expected error, got nothing")
@@ -66,12 +66,12 @@ type rt struct{}
6666
func (rt) RoundTrip(req *http.Request) (resp *http.Response, err error) { return }
6767

6868
func TestRegisterScheme(t *testing.T) {
69-
ds := New("/tmp/")
70-
if ds.HasScheme("scheme") {
69+
client := New("/tmp/")
70+
if client.HasScheme("scheme") {
7171
t.Errorf("expected scheme not to be registered")
7272
}
73-
ds.RegisterScheme("scheme", rt{})
74-
if !ds.HasScheme("scheme") {
73+
client.RegisterScheme("scheme", rt{})
74+
if !client.HasScheme("scheme") {
7575
t.Errorf("expected scheme to be registered")
7676
}
77-
}
77+
}

0 commit comments

Comments
 (0)