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
f55e39cf
Commit
f55e39cf
authored
Oct 14, 2014
by
obscuren
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tests for native function calling
parent
393a9281
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
80 additions
and
14 deletions
+80
-14
address.go
ethvm/address.go
+0
-2
execution.go
ethvm/execution.go
+3
-1
virtual_machine.go
ethvm/virtual_machine.go
+2
-0
vm.go
ethvm/vm.go
+3
-0
vm_debug.go
ethvm/vm_debug.go
+2
-2
vm_test.go
ethvm/vm_test.go
+70
-9
No files found.
ethvm/address.go
View file @
f55e39cf
...
...
@@ -26,8 +26,6 @@ var Precompiled = map[uint64]*PrecompiledAddress{
3
:
&
PrecompiledAddress
{
big
.
NewInt
(
100
),
ripemd160Func
},
}
var
NoAddr
=
PrecompiledAddress
{}
func
sha256Func
(
in
[]
byte
)
[]
byte
{
return
ethcrypto
.
Sha256
(
in
)
}
...
...
ethvm/execution.go
View file @
f55e39cf
...
...
@@ -50,9 +50,11 @@ func (self *Execution) Exec(codeAddr []byte, caller ClosureRef) (ret []byte, err
stateObject
.
AddAmount
(
self
.
value
)
// Precompiled contracts (address.go) 1, 2 & 3.
if
p
:=
Precompiled
[
ethutil
.
BigD
(
codeAddr
)
.
Uint64
()];
p
!=
nil
{
naddr
:=
ethutil
.
BigD
(
codeAddr
)
.
Uint64
()
if
p
:=
Precompiled
[
naddr
];
p
!=
nil
{
if
self
.
gas
.
Cmp
(
p
.
Gas
)
>=
0
{
ret
=
p
.
Call
(
self
.
input
)
self
.
vm
.
Printf
(
"NATIVE_FUNC(%x) => %x"
,
naddr
,
ret
)
}
}
else
{
if
self
.
vm
.
Depth
()
==
MaxCallDepth
{
...
...
ethvm/virtual_machine.go
View file @
f55e39cf
...
...
@@ -4,4 +4,6 @@ type VirtualMachine interface {
Env
()
Environment
RunClosure
(
*
Closure
)
([]
byte
,
error
)
Depth
()
int
Printf
(
string
,
...
interface
{})
VirtualMachine
Endl
()
VirtualMachine
}
ethvm/vm.go
View file @
f55e39cf
...
...
@@ -694,3 +694,6 @@ func (self *Vm) Env() Environment {
func
(
self
*
Vm
)
Depth
()
int
{
return
self
.
depth
}
func
(
self
*
Vm
)
Printf
(
format
string
,
v
...
interface
{})
VirtualMachine
{
return
self
}
func
(
self
*
Vm
)
Endl
()
VirtualMachine
{
return
self
}
ethvm/vm_debug.go
View file @
f55e39cf
...
...
@@ -846,7 +846,7 @@ func (self *DebugVm) RunClosure(closure *Closure) (ret []byte, err error) {
}
}
func
(
self
*
DebugVm
)
Printf
(
format
string
,
v
...
interface
{})
*
DebugVm
{
func
(
self
*
DebugVm
)
Printf
(
format
string
,
v
...
interface
{})
VirtualMachine
{
if
self
.
logTy
==
LogTyPretty
{
self
.
logStr
+=
fmt
.
Sprintf
(
format
,
v
...
)
}
...
...
@@ -854,7 +854,7 @@ func (self *DebugVm) Printf(format string, v ...interface{}) *DebugVm {
return
self
}
func
(
self
*
DebugVm
)
Endl
()
*
DebugVm
{
func
(
self
*
DebugVm
)
Endl
()
VirtualMachine
{
if
self
.
logTy
==
LogTyPretty
{
vmlogger
.
Debugln
(
self
.
logStr
)
self
.
logStr
=
""
...
...
ethvm/vm_test.go
View file @
f55e39cf
package
ethvm
import
(
"bytes"
"fmt"
"io/ioutil"
"log"
"math/big"
"os"
"testing"
"github.com/ethereum/eth-go/ethcrypto"
"github.com/ethereum/eth-go/ethlog"
"github.com/ethereum/eth-go/ethstate"
"github.com/ethereum/eth-go/ethtrie"
"github.com/ethereum/eth-go/ethutil"
)
type
TestEnv
struct
{
}
func
(
self
TestEnv
)
Origin
()
[]
byte
{
return
nil
}
func
(
self
TestEnv
)
BlockNumber
()
*
big
.
Int
{
return
nil
}
func
(
self
TestEnv
)
BlockHash
()
[]
byte
{
return
nil
}
func
(
self
TestEnv
)
PrevHash
()
[]
byte
{
return
nil
}
func
(
self
TestEnv
)
Coinbase
()
[]
byte
{
return
nil
}
func
(
self
TestEnv
)
Time
()
int64
{
return
0
}
func
(
self
TestEnv
)
Difficulty
()
*
big
.
Int
{
return
nil
}
func
(
self
TestEnv
)
Value
()
*
big
.
Int
{
return
nil
}
func
(
self
TestEnv
)
State
()
*
ethstate
.
State
{
return
nil
}
func
(
self
TestEnv
)
Origin
()
[]
byte
{
return
nil
}
func
(
self
TestEnv
)
BlockNumber
()
*
big
.
Int
{
return
nil
}
func
(
self
TestEnv
)
BlockHash
()
[]
byte
{
return
nil
}
func
(
self
TestEnv
)
PrevHash
()
[]
byte
{
return
nil
}
func
(
self
TestEnv
)
Coinbase
()
[]
byte
{
return
nil
}
func
(
self
TestEnv
)
Time
()
int64
{
return
0
}
func
(
self
TestEnv
)
Difficulty
()
*
big
.
Int
{
return
nil
}
func
(
self
TestEnv
)
Value
()
*
big
.
Int
{
return
nil
}
// This is likely to fail if anything ever gets looked up in the state trie :-)
func
(
self
TestEnv
)
State
()
*
ethstate
.
State
{
return
ethstate
.
New
(
ethtrie
.
New
(
nil
,
""
))
}
const
mutcode
=
`
var x = 0;
...
...
@@ -93,3 +99,58 @@ func BenchmarkVm(b *testing.B) {
closure
.
Call
(
vm
,
nil
)
}
}
func
RunCode
(
mutCode
string
,
typ
Type
)
[]
byte
{
code
,
err
:=
ethutil
.
Compile
(
mutCode
,
true
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
// Pipe output to /dev/null
ethlog
.
AddLogSystem
(
ethlog
.
NewStdLogSystem
(
os
.
Stdout
,
log
.
LstdFlags
,
ethlog
.
LogLevel
(
4
)))
ethutil
.
ReadConfig
(
".ethtest"
,
"/tmp/ethtest"
,
""
)
stateObject
:=
ethstate
.
NewStateObject
([]
byte
{
'j'
,
'e'
,
'f'
,
'f'
})
closure
:=
NewClosure
(
nil
,
stateObject
,
stateObject
,
code
,
big
.
NewInt
(
1000000
),
big
.
NewInt
(
0
))
vm
:=
New
(
TestEnv
{},
typ
)
ret
,
_
,
e
:=
closure
.
Call
(
vm
,
nil
)
if
e
!=
nil
{
fmt
.
Println
(
e
)
}
return
ret
}
func
TestBuildInSha256
(
t
*
testing
.
T
)
{
ret
:=
RunCode
(
`
var in = 42
var out = 0
call(0x2, 0, 10000, in, out)
return out
`
,
DebugVmTy
)
exp
:=
ethcrypto
.
Sha256
(
ethutil
.
LeftPadBytes
([]
byte
{
42
},
32
))
if
bytes
.
Compare
(
ret
,
exp
)
!=
0
{
t
.
Errorf
(
"Expected %x, got %x"
,
exp
,
ret
)
}
}
func
TestBuildInRipemd
(
t
*
testing
.
T
)
{
ret
:=
RunCode
(
`
var in = 42
var out = 0
call(0x3, 0, 10000, in, out)
return out
`
,
DebugVmTy
)
exp
:=
ethutil
.
RightPadBytes
(
ethcrypto
.
Ripemd160
(
ethutil
.
LeftPadBytes
([]
byte
{
42
},
32
)),
32
)
if
bytes
.
Compare
(
ret
,
exp
)
!=
0
{
t
.
Errorf
(
"Expected %x, got %x"
,
exp
,
ret
)
}
}
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