From 97dc4edb4569225f4b819717a2fabf0209d312e0 Mon Sep 17 00:00:00 2001 From: zelig Date: Sun, 8 Mar 2015 20:09:13 +0700 Subject: [PATCH 1/4] natspec for poc-9 - put natspec.js in a heredoc string (to help binary) - constructor takes no args, just sets up js vm - API: only exports Notice(transaction, abi, method, expression string) (string, error) - effort to turn natspec.js error strings to proper go errors - test errors --- ethutil/natspec/natspec.go | 58 +++++----- ethutil/natspec/{natspec.js => natspec_js.go} | 91 ++++++++-------- ethutil/natspec/natspec_test.go | 103 +++++++++++++----- 3 files changed, 149 insertions(+), 103 deletions(-) rename ethutil/natspec/{natspec.js => natspec_js.go} (98%) diff --git a/ethutil/natspec/natspec.go b/ethutil/natspec/natspec.go index 00e6f8720f..ebc2658dab 100644 --- a/ethutil/natspec/natspec.go +++ b/ethutil/natspec/natspec.go @@ -1,24 +1,21 @@ package natspec import ( + "fmt" "github.com/obscuren/otto" - "io/ioutil" ) type NatSpec struct { jsvm *otto.Otto } -func NewNATSpec(transaction string) (self *NatSpec, err error) { +// TODO: should initialise with abi and userdoc jsons +func NewNATSpec() (self *NatSpec, err error) { self = new(NatSpec) self.jsvm = otto.New() - code, err := ioutil.ReadFile("natspec.js") - if err != nil { - return - } - _, err = self.jsvm.Run(string(code)) + _, err = self.jsvm.Run(natspecJS) if err != nil { return } @@ -27,39 +24,40 @@ func NewNATSpec(transaction string) (self *NatSpec, err error) { return } - self.jsvm.Run("var transaction = " + transaction + ";") - - return -} - -func (self *NatSpec) SetDescription(desc string) (err error) { - - _, err = self.jsvm.Run("var expression = \"" + desc + "\";") return - } -func (self *NatSpec) SetABI(abi string) (err error) { - - _, err = self.jsvm.Run("var abi = " + abi + ";") - return - -} - -func (self *NatSpec) SetMethod(method string) (err error) { +func (self *NatSpec) Notice(transaction, abi, method, expression string) (string, error) { + var err error + if _, err = self.jsvm.Run("var transaction = " + transaction + ";"); err != nil { + return "", fmt.Errorf("natspec.js error setting transaction: %v", err) + } - _, err = self.jsvm.Run("var method = '" + method + "';") - return + if _, err = self.jsvm.Run("var abi = " + abi + ";"); err != nil { + return "", fmt.Errorf("natspec.js error setting abi: %v", err) + } -} + if _, err = self.jsvm.Run("var method = '" + method + "';"); err != nil { + return "", fmt.Errorf("natspec.js error setting method: %v", err) + } -func (self *NatSpec) Parse() string { + if _, err = self.jsvm.Run("var expression = \"" + expression + "\";"); err != nil { + return "", fmt.Errorf("natspec.js error setting expression: %v", err) + } self.jsvm.Run("var call = {method: method,abi: abi,transaction: transaction};") value, err := self.jsvm.Run("natspec.evaluateExpression(expression, call);") if err != nil { - return err.Error() + return "", fmt.Errorf("natspec.js error evaluating expression: %v", err) + } + evalError := "Natspec evaluation failed, wrong input params" + if value.String() == evalError { + return "", fmt.Errorf("natspec.js error evaluating expression: wrong input params in expression '%s'", expression) } - return value.String() + if len(value.String()) == 0 { + return "", fmt.Errorf("natspec.js error evaluating expression") + } + + return value.String(), nil } diff --git a/ethutil/natspec/natspec.js b/ethutil/natspec/natspec_js.go similarity index 98% rename from ethutil/natspec/natspec.js rename to ethutil/natspec/natspec_js.go index 419ccd5c94..7acaaee4fc 100644 --- a/ethutil/natspec/natspec.js +++ b/ethutil/natspec/natspec_js.go @@ -1,4 +1,6 @@ -require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o multiply var extractDisplayName = function (name) { - var length = name.indexOf('('); + var length = name.indexOf('('); return length !== -1 ? name.substr(0, length) : name; }; @@ -657,8 +659,8 @@ var extractTypeName = function (name) { /// @returns abi array with filtered objects of type 'function' var filterFunctions = function (json) { return json.filter(function (current) { - return current.type === 'function'; - }); + return current.type === 'function'; + }); }; /// Filters all events form input abi @@ -3404,7 +3406,7 @@ module.exports = { * @date 2015 */ -var abi = require('./node_modules/ethereum.js/lib/abi.js'); +var abi = require('./node_modules/ethereum.js/lib/abi.js'); /** * This object should be used to evaluate natspec expression @@ -3418,7 +3420,7 @@ var natspec = (function () { context[key] = obj[key]; }); } - + /// generate codes, which will be evaluated var generateCode = function (obj) { return Object.keys(obj).reduce(function (acc, key) { @@ -3440,20 +3442,20 @@ var natspec = (function () { /// @returns hashmap with all contract's method input variables var getMethodInputParams = function (method, transaction) { // do it with output formatter (cause we have to decode) - var params = abi.formatOutput(method.inputs, '0x' + transaction.params[0].data.slice(10)); + var params = abi.formatOutput(method.inputs, '0x' + transaction.params[0].data.slice(10)); return method.inputs.reduce(function (acc, current, index) { acc[current.name] = params[index]; return acc; }, {}); }; - + /// Should be called to evaluate expression var mapExpressionsToEvaluate = function (expression, cb) { var evaluatedExpression = ""; - // match everything in `` quotes - var pattern = /\`(?:\\.|[^`\\])*\`/gim + // match everything in backtick + var pattern = /\` + "`" + `(?:\\.|[^` + "`" + `\\])*\` + "`" + `/gim var match; var lastIndex = 0; while ((match = pattern.exec(expression)) !== null) { @@ -3464,9 +3466,9 @@ var natspec = (function () { evaluatedExpression += evaluatedPart; lastIndex = pattern.lastIndex; } - + evaluatedExpression += expression.slice(lastIndex); - + return evaluatedExpression; }; @@ -3478,11 +3480,11 @@ var natspec = (function () { var evaluateExpression = function (expression, call) { //var self = this; var context = {}; - + if (!!call) { try { var method = getMethodWithName(call.abi, call.method); - var params = getMethodInputParams(method, call.transaction); + var params = getMethodInputParams(method, call.transaction); copyToContext(params, context); } catch (err) { @@ -3498,7 +3500,7 @@ var natspec = (function () { return fn(context).toString(); } catch (err) { - return 'undefined'; + return 'undefined'; } }); @@ -3511,7 +3513,8 @@ var natspec = (function () { })(); -module.exports = natspec; +module.exports = natspec; },{"./node_modules/ethereum.js/lib/abi.js":3}]},{},[]); +` diff --git a/ethutil/natspec/natspec_test.go b/ethutil/natspec/natspec_test.go index 48a9cb25c9..3c70a08400 100644 --- a/ethutil/natspec/natspec_test.go +++ b/ethutil/natspec/natspec_test.go @@ -6,41 +6,48 @@ import ( func TestNotice(t *testing.T) { - ns, err := NewNATSpec(` + tx := ` { - "jsonrpc": "2.0", - "method": "eth_call", - "params": [{ - "to": "0x8521742d3f456bd237e312d6e30724960f72517a", - "data": "0xc6888fa1000000000000000000000000000000000000000000000000000000000000007a" - }], - "id": 6 - } - `) + "jsonrpc": "2.0", + "method": "eth_call", + "params": [{ + "to": "0x8521742d3f456bd237e312d6e30724960f72517a", + "data": "0xc6888fa1000000000000000000000000000000000000000000000000000000000000007a" + }], + "id": 6 + } + ` + abi := ` + [{ + "name": "multiply", + "constant": false, + "type": "function", + "inputs": [{ + "name": "a", + "type": "uint256" + }], + "outputs": [{ + "name": "d", + "type": "uint256" + }] + }] + ` + + desc := "Will multiply `a` by 7 and return `a * 7`." + + method := "multiply" + + ns, err := NewNATSpec() if err != nil { t.Errorf("NewNATSpec error %v", err) } - ns.SetABI(` - [{ - "name": "multiply", - "constant": false, - "type": "function", - "inputs": [{ - "name": "a", - "type": "uint256" - }], - "outputs": [{ - "name": "d", - "type": "uint256" - }] - }] - `) - ns.SetDescription("Will multiply `a` by 7 and return `a * 7`.") - ns.SetMethod("multiply") - - notice := ns.Parse() + notice, err := ns.Notice(tx, abi, method, desc) + + if err != nil { + t.Errorf("expected no error got %v", err) + } expected := "Will multiply 122 by 7 and return 854." if notice != expected { @@ -48,4 +55,42 @@ func TestNotice(t *testing.T) { } else { t.Logf("returned notice \"%v\"", notice) } + + notice, err = ns.Notice(tx, abi, method, "Will multiply 122 by \"7\" and return 854.") + + expected = "natspec.js error setting expression: (anonymous): Line 1:41 Unexpected number" + + if err == nil { + t.Errorf("expected error, got nothing (notice: '%v')", err, notice) + } else { + if err.Error() != expected { + t.Errorf("expected error '%s' got '%v' (notice: '%v')", expected, err, notice) + } + } + + // badDesc := "Will multiply `e` by 7 and return `a * 7`." + // notice, err = ns.Notice(tx, abi, method, badDesc) + + // expected = "natspec.js error evaluating expression: wrong input param in expression ''" + + // if err == nil { + // t.Errorf("expected error, got nothing (notice: '%v')", notice) + // } else { + // if err.Error() != expected { + // t.Errorf("expected error '%s' got '%v' (notice: '%v')", expected, err, notice) + // } + // } + + notice, err = ns.Notice(tx, abi, "missing_method", desc) + + expected = "natspec.js error evaluating expression: wrong input params in expression 'Will multiply `a` by 7 and return `a * 7`.'" + + if err == nil { + t.Errorf("expected error, got nothing (notice: '%v')", notice) + } else { + if err.Error() != expected { + t.Errorf("expected error '%s' got '%v' (notice: '%v')", expected, err, notice) + } + } + } From 0743d68d4c4938549dde881428edd4141fa1a5f9 Mon Sep 17 00:00:00 2001 From: zelig Date: Sun, 8 Mar 2015 20:17:20 +0700 Subject: [PATCH 2/4] rename constructor New --- ethutil/natspec/natspec.go | 2 +- ethutil/natspec/natspec_test.go | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/ethutil/natspec/natspec.go b/ethutil/natspec/natspec.go index ebc2658dab..793bf59ae3 100644 --- a/ethutil/natspec/natspec.go +++ b/ethutil/natspec/natspec.go @@ -10,7 +10,7 @@ type NatSpec struct { } // TODO: should initialise with abi and userdoc jsons -func NewNATSpec() (self *NatSpec, err error) { +func New() (self *NatSpec, err error) { self = new(NatSpec) self.jsvm = otto.New() diff --git a/ethutil/natspec/natspec_test.go b/ethutil/natspec/natspec_test.go index 3c70a08400..498f8d78ee 100644 --- a/ethutil/natspec/natspec_test.go +++ b/ethutil/natspec/natspec_test.go @@ -38,7 +38,7 @@ func TestNotice(t *testing.T) { method := "multiply" - ns, err := NewNATSpec() + ns, err := New() if err != nil { t.Errorf("NewNATSpec error %v", err) } @@ -68,6 +68,7 @@ func TestNotice(t *testing.T) { } } + // https://github.com/ethereum/natspec.js/issues/1 // badDesc := "Will multiply `e` by 7 and return `a * 7`." // notice, err = ns.Notice(tx, abi, method, badDesc) From 51eed7964ec35bbdc23dd8a4a8fffedad247e33d Mon Sep 17 00:00:00 2001 From: zelig Date: Sun, 8 Mar 2015 22:44:48 +0700 Subject: [PATCH 3/4] add export blockchain subcommand, remove BlockDo --- cmd/ethereum/main.go | 31 +++++++++++++++++++++++++------ cmd/utils/cmd.go | 41 +++++++++++++++++------------------------ 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/cmd/ethereum/main.go b/cmd/ethereum/main.go index 8b361f7ae8..f3f4281566 100644 --- a/cmd/ethereum/main.go +++ b/cmd/ethereum/main.go @@ -87,6 +87,11 @@ runtime will execute the file and exit. Name: "import", Usage: `import a blockchain file`, }, + { + Action: exportchain, + Name: "export", + Usage: `export blockchain into file`, + }, } app.Author = "" app.Email = "" @@ -171,25 +176,39 @@ func importchain(ctx *cli.Context) { if len(ctx.Args()) != 1 { utils.Fatalf("This command requires an argument.") } - chain, _, _ := utils.GetChain(ctx) + chainmgr, _, _ := utils.GetChain(ctx) start := time.Now() - err := utils.ImportChain(chain, ctx.Args().First()) + err := utils.ImportChain(chainmgr, ctx.Args().First()) if err != nil { utils.Fatalf("Import error: %v\n", err) } - fmt.Printf("Import done in", time.Since(start)) + fmt.Printf("Import done in %v", time.Since(start)) + return +} + +func exportchain(ctx *cli.Context) { + if len(ctx.Args()) != 1 { + utils.Fatalf("This command requires an argument.") + } + chainmgr, _, _ := utils.GetChain(ctx) + start := time.Now() + err := utils.ExportChain(chainmgr, ctx.Args().First()) + if err != nil { + utils.Fatalf("Export error: %v\n", err) + } + fmt.Printf("Export done in %v", time.Since(start)) return } func dump(ctx *cli.Context) { - chain, _, stateDb := utils.GetChain(ctx) + chainmgr, _, stateDb := utils.GetChain(ctx) for _, arg := range ctx.Args() { var block *types.Block if hashish(arg) { - block = chain.GetBlock(ethutil.Hex2Bytes(arg)) + block = chainmgr.GetBlock(ethutil.Hex2Bytes(arg)) } else { num, _ := strconv.Atoi(arg) - block = chain.GetBlockByNumber(uint64(num)) + block = chainmgr.GetBlockByNumber(uint64(num)) } if block == nil { fmt.Println("{}") diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index 99e60ff9e7..8e516748ef 100644 --- a/cmd/utils/cmd.go +++ b/cmd/utils/cmd.go @@ -35,7 +35,6 @@ import ( "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/rlp" rpchttp "github.com/ethereum/go-ethereum/rpc/http" - "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/xeth" ) @@ -188,27 +187,8 @@ func FormatTransactionData(data string) []byte { return d } -// Replay block -func BlockDo(ethereum *eth.Ethereum, hash []byte) error { - block := ethereum.ChainManager().GetBlock(hash) - if block == nil { - return fmt.Errorf("unknown block %x", hash) - } - - parent := ethereum.ChainManager().GetBlock(block.ParentHash()) - - statedb := state.New(parent.Root(), ethereum.StateDb()) - _, err := ethereum.BlockProcessor().TransitionState(statedb, parent, block, true) - if err != nil { - return err - } - - return nil - -} - -func ImportChain(chain *core.ChainManager, fn string) error { - fmt.Printf("importing chain '%s'\n", fn) +func ImportChain(chainmgr *core.ChainManager, fn string) error { + fmt.Printf("importing blockchain '%s'\n", fn) fh, err := os.OpenFile(fn, os.O_RDONLY, os.ModePerm) if err != nil { return err @@ -220,11 +200,24 @@ func ImportChain(chain *core.ChainManager, fn string) error { return err } - chain.Reset() - if err := chain.InsertChain(blocks); err != nil { + chainmgr.Reset() + if err := chainmgr.InsertChain(blocks); err != nil { return err } fmt.Printf("imported %d blocks\n", len(blocks)) return nil } + +func ExportChain(chainmgr *core.ChainManager, fn string) error { + fmt.Printf("exporting blockchain '%s'\n", fn) + + data := chainmgr.Export() + + if err := ethutil.WriteFile(fn, data); err != nil { + return err + } + fmt.Printf("exported blockchain\n") + + return nil +} From 41a89e18f68f4b12b8212544be01a4e0a6716976 Mon Sep 17 00:00:00 2001 From: zelig Date: Sun, 8 Mar 2015 22:50:27 +0700 Subject: [PATCH 4/4] version output - make version output more verbose - include NetworkId - go version and os not displayed like env vars (confusing) --- cmd/ethereum/main.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/cmd/ethereum/main.go b/cmd/ethereum/main.go index f3f4281566..12e205f375 100644 --- a/cmd/ethereum/main.go +++ b/cmd/ethereum/main.go @@ -228,11 +228,13 @@ func hashish(x string) bool { } func version(c *cli.Context) { - fmt.Printf(`%v %v -PV=%d -GOOS=%s -GO=%s + fmt.Printf(`%v +Version: %v +Protocol Version: %d +Network Id: %d +GO: %s +OS: %s GOPATH=%s GOROOT=%s -`, ClientIdentifier, Version, eth.ProtocolVersion, runtime.GOOS, runtime.Version(), os.Getenv("GOPATH"), runtime.GOROOT()) +`, ClientIdentifier, Version, eth.ProtocolVersion, eth.NetworkId, runtime.Version(), runtime.GOOS, os.Getenv("GOPATH"), runtime.GOROOT()) }