Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
e1571472
Commit
e1571472
authored
Mar 06, 2015
by
Fefe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Now using github.com/obscuren/otto, no need for an Ethereum object
(got rid of some messy code in test)
parent
b67ded9f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
53 additions
and
198 deletions
+53
-198
natspec.go
ethutil/natspec/natspec.go
+12
-16
natspec.js
ethutil/natspec/natspec.js
+40
-69
natspec_test.go
ethutil/natspec/natspec_test.go
+1
-113
No files found.
ethutil/natspec/natspec.go
View file @
e1571472
package
natspec
import
(
// "encoding/json"
// "fmt"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/javascript"
"github.com/obscuren/otto"
"io/ioutil"
)
type
NatSpec
struct
{
js
re
*
javascript
.
JSRE
js
vm
*
otto
.
Otto
}
func
NewNATSpec
(
ethereum
*
eth
.
Ethereum
,
transaction
string
)
(
self
*
NatSpec
,
err
error
)
{
func
NewNATSpec
(
transaction
string
)
(
self
*
NatSpec
,
err
error
)
{
self
=
new
(
NatSpec
)
self
.
jsre
=
javascript
.
NewJSRE
(
ethereum
)
//self.jsre.LoadExtFile("/home/fefe/go-ethereum/ethutil/natspec/natspec.js")
self
.
jsvm
=
otto
.
New
()
code
,
err
:=
ioutil
.
ReadFile
(
"natspec.js"
)
if
err
!=
nil
{
return
}
_
,
err
=
self
.
js
re
.
Run
(
string
(
code
))
_
,
err
=
self
.
js
vm
.
Run
(
string
(
code
))
if
err
!=
nil
{
return
}
_
,
err
=
self
.
js
re
.
Run
(
"var natspec = require('natspec');"
)
_
,
err
=
self
.
js
vm
.
Run
(
"var natspec = require('natspec');"
)
if
err
!=
nil
{
return
}
self
.
js
re
.
Run
(
"var transaction = "
+
transaction
+
";"
)
self
.
js
vm
.
Run
(
"var transaction = "
+
transaction
+
";"
)
return
}
func
(
self
*
NatSpec
)
SetDescription
(
desc
string
)
(
err
error
)
{
_
,
err
=
self
.
js
re
.
Run
(
"var expression =
\"
"
+
desc
+
"
\"
;"
)
_
,
err
=
self
.
js
vm
.
Run
(
"var expression =
\"
"
+
desc
+
"
\"
;"
)
return
}
func
(
self
*
NatSpec
)
SetABI
(
abi
string
)
(
err
error
)
{
_
,
err
=
self
.
js
re
.
Run
(
"var abi = "
+
abi
+
";"
)
_
,
err
=
self
.
js
vm
.
Run
(
"var abi = "
+
abi
+
";"
)
return
}
func
(
self
*
NatSpec
)
SetMethod
(
method
string
)
(
err
error
)
{
_
,
err
=
self
.
js
re
.
Run
(
"var method = '"
+
method
+
"';"
)
_
,
err
=
self
.
js
vm
.
Run
(
"var method = '"
+
method
+
"';"
)
return
}
func
(
self
*
NatSpec
)
Parse
()
string
{
self
.
js
re
.
Run
(
"var call = {method: method,abi: abi,transaction: transaction};"
)
value
,
err
:=
self
.
js
re
.
Run
(
"natspec.evaluateExpression(expression, call);"
)
self
.
js
vm
.
Run
(
"var call = {method: method,abi: abi,transaction: transaction};"
)
value
,
err
:=
self
.
js
vm
.
Run
(
"natspec.evaluateExpression(expression, call);"
)
if
err
!=
nil
{
return
err
.
Error
()
}
...
...
ethutil/natspec/natspec.js
View file @
e1571472
...
...
@@ -1824,10 +1824,6 @@ module.exports = {
// Handle values that fail the validity test in BigNumber.
// Zsolt Felfoldi 15/03/06
// modified regexps in order to compile with go JSRE
parseNumeric
=
(
function
()
{
// var basePrefix = /^(-?)0([xbo])(?=\w[\w.]*$)/i,
var
basePrefix
=
/^
(
-
?)
0
([
xbo
])
/i
,
...
...
@@ -1838,7 +1834,6 @@ module.exports = {
whitespaceOrPlus
=
/^
\s
*
\+[\w
.
]
|^
\s
+|
\s
+$/g
;
return
function
(
x
,
str
,
num
,
b
)
{
var
base
,
s
=
num
?
str
:
str
.
replace
(
whitespaceOrPlus
,
''
);
...
...
@@ -3387,7 +3382,6 @@ module.exports = {
})(
this
);
},{
"crypto"
:
1
}],
"natspec"
:[
function
(
require
,
module
,
exports
){
(
function
(
global
){
/*
This file is part of natspec.js.
...
...
@@ -3418,31 +3412,18 @@ var abi = require('./node_modules/ethereum.js/lib/abi.js');
*/
var
natspec
=
(
function
()
{
/// Helper method
/// Modifications by Zsolt Felfoldi, 15/03/06
/// eval() under go JSRE is unable to reach variables that
/// are added to the global context runtime, so now we
/// create a variable assignment code for each param
/// and run in an isolated function(context)
/// variable assignment code is returned by copyToContext
/// Should be called to copy values from object to global context
var
copyToContext
=
function
(
obj
,
context
)
{
var
code
=
""
;
var
keys
=
Object
.
keys
(
obj
);
keys
.
forEach
(
function
(
key
)
{
Object
.
keys
(
obj
).
forEach
(
function
(
key
)
{
context
[
key
]
=
obj
[
key
];
code
=
code
+
"var "
+
key
+
" = context['"
+
key
+
"'];
\n
"
;
});
return
code
;
}
/// this function will not be used in 'production' natspec evaluation
/// it's only used to enable tests in node environment
/// it copies all functions from current context to nodejs global context
var
copyToNodeGlobal
=
function
(
obj
)
{
if
(
typeof
global
===
'undefined'
)
{
return
;
}
copyToContext
(
obj
,
global
);
/// generate codes, which will be evaluated
var
generateCode
=
function
(
obj
)
{
return
Object
.
keys
(
obj
).
reduce
(
function
(
acc
,
key
)
{
return
acc
+
"var "
+
key
+
" = context['"
+
key
+
"'];
\n
"
;
},
""
);
};
/// Helper method
...
...
@@ -3455,81 +3436,72 @@ var natspec = (function () {
})[
0
];
};
/// Function called to get all contract's storage values
/// @returns hashmap with contract properties which are used
/// TODO: check if this function will be used
var
getContractProperties
=
function
(
address
,
abi
)
{
return
{};
};
/// Function called to get all contract method input variables
/// @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
));
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
var
match
;
var
lastIndex
=
0
;
while
((
match
=
pattern
.
exec
(
expression
))
!==
null
)
{
var
startIndex
=
pattern
.
lastIndex
-
match
[
0
].
length
;
var
toEval
=
match
[
0
].
slice
(
1
,
match
[
0
].
length
-
1
);
evaluatedExpression
+=
expression
.
slice
(
lastIndex
,
startIndex
);
var
evaluatedPart
=
cb
(
toEval
);
evaluatedExpression
+=
evaluatedPart
;
lastIndex
=
pattern
.
lastIndex
;
}
evaluatedExpression
+=
expression
.
slice
(
lastIndex
);
return
evaluatedExpression
;
};
/// Should be called to evaluate single expression
/// Is internally using javascript's 'eval' method
/// @param expression which should be evaluated
/// @param [call] object containing contract abi, transaction, called method
/// TODO: separate evaluation from getting input params, so as not to spoil 'evaluateExpression' function
var
evaluateExpression
=
function
(
expression
,
call
)
{
var
self
=
this
;
var
code
=
""
;
var
context
=
[];
//var self = this;
var
context
=
{};
if
(
!!
call
)
{
try
{
var
method
=
getMethodWithName
(
call
.
abi
,
call
.
method
);
var
params
=
getMethodInputParams
(
method
,
call
.
transaction
);
code
=
copyToContext
(
params
,
context
);
// see copyToContext comments
copyToContext
(
params
,
context
);
}
catch
(
err
)
{
return
"Natspec evaluation failed, wrong input params"
;
}
}
// used only for tests
copyToNodeGlobal
(
context
);
var
evaluatedExpression
=
""
;
// match everything in `` quotes
var
pattern
=
/
\`(?:\\
.|
[^
`
\\])
*
\`
/gim
var
match
;
var
lastIndex
=
0
;
while
((
match
=
pattern
.
exec
(
expression
))
!==
null
)
{
var
startIndex
=
pattern
.
lastIndex
-
match
[
0
].
length
;
var
code
=
generateCode
(
context
);
var
toEval
=
match
[
0
].
slice
(
1
,
match
[
0
].
length
-
1
);
evaluatedExpression
+=
expression
.
slice
(
lastIndex
,
startIndex
);
var
evaluatedPart
;
var
evaluatedExpression
=
mapExpressionsToEvaluate
(
expression
,
function
(
toEval
)
{
try
{
var
fn
=
new
Function
(
"context"
,
code
+
"return "
+
toEval
+
";"
);
evaluatedPart
=
fn
(
context
).
toString
();
// see copyToContext comments
// evaluatedPart = eval(toEval).toString();
var
fn
=
new
Function
(
"context"
,
code
+
"return "
+
toEval
+
";"
);
return
fn
(
context
).
toString
();
}
catch
(
err
)
{
evaluatedPart
=
'undefined'
;
return
'undefined'
;
}
});
evaluatedExpression
+=
evaluatedPart
;
lastIndex
=
pattern
.
lastIndex
;
}
evaluatedExpression
+=
expression
.
slice
(
lastIndex
);
return
evaluatedExpression
;
};
...
...
@@ -3542,5 +3514,4 @@ var natspec = (function () {
module
.
exports
=
natspec
;
}).
call
(
this
,
typeof
global
!==
"undefined"
?
global
:
typeof
self
!==
"undefined"
?
self
:
typeof
window
!==
"undefined"
?
window
:
{})
},{
"./node_modules/ethereum.js/lib/abi.js"
:
3
}]},{},[]);
ethutil/natspec/natspec_test.go
View file @
e1571472
package
natspec
import
(
"flag"
// "crypto/rand"
// "io/ioutil"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/eth"
"testing"
)
const
(
ClientIdentifier
=
"Ethereum(G)"
Version
=
"0.8.1"
)
var
(
Identifier
string
KeyRing
string
DiffTool
bool
DiffType
string
KeyStore
string
StartRpc
bool
StartWebSockets
bool
RpcPort
int
NatType
string
PMPGateway
string
OutboundPort
string
ShowGenesis
bool
AddPeer
string
MaxPeer
int
GenAddr
bool
UseSeed
bool
SecretFile
string
ExportDir
string
NonInteractive
bool
Datadir
string
LogFile
string
ConfigFile
string
DebugFile
string
LogLevel
int
Dump
bool
DumpHash
string
DumpNumber
int
VmType
int
ImportChain
string
SHH
bool
Dial
bool
PrintVersion
bool
)
func
Init
()
{
/* flag.Usage = func() {
fmt.Fprintf(os.Stderr, "%s [options] [filename]:\noptions precedence: default < config file < environment variables < command line\n", os.Args[0])
flag.PrintDefaults()
}*/
flag
.
IntVar
(
&
VmType
,
"vm"
,
0
,
"Virtual Machine type: 0-1: standard, debug"
)
flag
.
StringVar
(
&
Identifier
,
"id"
,
""
,
"Custom client identifier"
)
flag
.
StringVar
(
&
KeyRing
,
"keyring"
,
""
,
"identifier for keyring to use"
)
flag
.
StringVar
(
&
KeyStore
,
"keystore"
,
"db"
,
"system to store keyrings: db|file (db)"
)
flag
.
StringVar
(
&
OutboundPort
,
"port"
,
"30303"
,
"listening port"
)
flag
.
StringVar
(
&
NatType
,
"nat"
,
""
,
"NAT support (UPNP|PMP) (none)"
)
flag
.
StringVar
(
&
PMPGateway
,
"pmp"
,
""
,
"Gateway IP for PMP"
)
flag
.
IntVar
(
&
MaxPeer
,
"maxpeer"
,
30
,
"maximum desired peers"
)
flag
.
IntVar
(
&
RpcPort
,
"rpcport"
,
8080
,
"port to start json-rpc server on"
)
flag
.
BoolVar
(
&
StartRpc
,
"rpc"
,
false
,
"start rpc server"
)
flag
.
BoolVar
(
&
StartWebSockets
,
"ws"
,
false
,
"start websocket server"
)
flag
.
BoolVar
(
&
NonInteractive
,
"y"
,
false
,
"non-interactive mode (say yes to confirmations)"
)
flag
.
BoolVar
(
&
UseSeed
,
"seed"
,
true
,
"seed peers"
)
flag
.
BoolVar
(
&
SHH
,
"shh"
,
true
,
"whisper protocol (on)"
)
flag
.
BoolVar
(
&
Dial
,
"dial"
,
true
,
"dial out connections (on)"
)
flag
.
BoolVar
(
&
GenAddr
,
"genaddr"
,
false
,
"create a new priv/pub key"
)
flag
.
StringVar
(
&
SecretFile
,
"import"
,
""
,
"imports the file given (hex or mnemonic formats)"
)
flag
.
StringVar
(
&
ExportDir
,
"export"
,
""
,
"exports the session keyring to files in the directory given"
)
flag
.
StringVar
(
&
LogFile
,
"logfile"
,
""
,
"log file (defaults to standard output)"
)
flag
.
StringVar
(
&
Datadir
,
"datadir"
,
""
,
"specifies the datadir to use"
)
flag
.
StringVar
(
&
ConfigFile
,
"conf"
,
""
,
"config file"
)
flag
.
StringVar
(
&
DebugFile
,
"debug"
,
""
,
"debug file (no debugging if not set)"
)
flag
.
IntVar
(
&
LogLevel
,
"loglevel"
,
0
,
"loglevel: 0-5: silent,error,warn,info,debug,debug detail)"
)
flag
.
BoolVar
(
&
DiffTool
,
"difftool"
,
false
,
"creates output for diff'ing. Sets LogLevel=0"
)
flag
.
StringVar
(
&
DiffType
,
"diff"
,
"all"
,
"sets the level of diff output [vm, all]. Has no effect if difftool=false"
)
flag
.
BoolVar
(
&
ShowGenesis
,
"genesis"
,
false
,
"Dump the genesis block"
)
flag
.
StringVar
(
&
ImportChain
,
"chain"
,
""
,
"Imports given chain"
)
flag
.
BoolVar
(
&
Dump
,
"dump"
,
false
,
"output the ethereum state in JSON format. Sub args [number, hash]"
)
flag
.
StringVar
(
&
DumpHash
,
"hash"
,
""
,
"specify arg in hex"
)
flag
.
IntVar
(
&
DumpNumber
,
"number"
,
-
1
,
"specify arg in number"
)
/* flag.BoolVar(&StartMining, "mine", false, "start dagger mining")
flag.BoolVar(&StartJsConsole, "js", false, "launches javascript console")
flag.BoolVar(&PrintVersion, "version", false, "prints version number")*/
flag
.
Parse
()
}
func
TestNotice
(
t
*
testing
.
T
)
{
Init
()
utils
.
InitConfig
(
VmType
,
ConfigFile
,
Datadir
,
"ETH"
)
ethereum
,
_
:=
eth
.
New
(
&
eth
.
Config
{
Name
:
ClientIdentifier
,
Version
:
Version
,
KeyStore
:
KeyStore
,
DataDir
:
Datadir
,
LogFile
:
LogFile
,
LogLevel
:
LogLevel
,
Identifier
:
Identifier
,
MaxPeers
:
MaxPeer
,
Port
:
OutboundPort
,
NATType
:
PMPGateway
,
PMPGateway
:
PMPGateway
,
KeyRing
:
KeyRing
,
Shh
:
SHH
,
Dial
:
Dial
,
})
ns
,
err
:=
NewNATSpec
(
ethereum
,
`
ns
,
err
:=
NewNATSpec
(
`
{
"jsonrpc": "2.0",
"method": "eth_call",
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment