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
11651615
Commit
11651615
authored
Apr 11, 2014
by
obscuren
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Renamed
parent
ca747f26
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
124 additions
and
37 deletions
+124
-37
closure.go
ethchain/closure.go
+2
-2
contract.go
ethchain/contract.go
+8
-0
stack.go
ethchain/stack.go
+8
-0
state_manager.go
ethchain/state_manager.go
+7
-7
vm.go
ethchain/vm.go
+77
-21
vm_test.go
ethchain/vm_test.go
+22
-7
No files found.
ethchain/closure.go
View file @
11651615
...
@@ -52,7 +52,7 @@ func (c *Closure) Get(x *big.Int) *ethutil.Value {
...
@@ -52,7 +52,7 @@ func (c *Closure) Get(x *big.Int) *ethutil.Value {
}
}
func
(
c
*
Closure
)
Gets
(
x
,
y
*
big
.
Int
)
*
ethutil
.
Value
{
func
(
c
*
Closure
)
Gets
(
x
,
y
*
big
.
Int
)
*
ethutil
.
Value
{
if
x
.
Int64
()
>
int64
(
len
(
c
.
Script
))
||
y
.
Int64
()
>
int64
(
len
(
c
.
Script
))
{
if
x
.
Int64
()
>
=
int64
(
len
(
c
.
Script
))
||
y
.
Int64
()
>=
int64
(
len
(
c
.
Script
))
{
return
ethutil
.
NewValue
(
0
)
return
ethutil
.
NewValue
(
0
)
}
}
...
@@ -69,7 +69,7 @@ func (c *Closure) Address() []byte {
...
@@ -69,7 +69,7 @@ func (c *Closure) Address() []byte {
return
c
.
object
.
Address
()
return
c
.
object
.
Address
()
}
}
type
DebugHook
func
(
op
OpCode
)
type
DebugHook
func
(
op
OpCode
,
mem
*
Memory
,
stack
*
Stack
)
func
(
c
*
Closure
)
Call
(
vm
*
Vm
,
args
[]
byte
,
hook
DebugHook
)
[]
byte
{
func
(
c
*
Closure
)
Call
(
vm
*
Vm
,
args
[]
byte
,
hook
DebugHook
)
[]
byte
{
c
.
Args
=
args
c
.
Args
=
args
...
...
ethchain/contract.go
View file @
11651615
...
@@ -69,6 +69,14 @@ func (c *Contract) Address() []byte {
...
@@ -69,6 +69,14 @@ func (c *Contract) Address() []byte {
return
c
.
address
return
c
.
address
}
}
func
(
c
*
Contract
)
Script
()
[]
byte
{
return
c
.
script
}
func
(
c
*
Contract
)
Init
()
[]
byte
{
return
c
.
initScript
}
func
(
c
*
Contract
)
RlpEncode
()
[]
byte
{
func
(
c
*
Contract
)
RlpEncode
()
[]
byte
{
return
ethutil
.
Encode
([]
interface
{}{
c
.
Amount
,
c
.
Nonce
,
c
.
state
.
trie
.
Root
,
c
.
script
,
c
.
initScript
})
return
ethutil
.
Encode
([]
interface
{}{
c
.
Amount
,
c
.
Nonce
,
c
.
state
.
trie
.
Root
,
c
.
script
,
c
.
initScript
})
}
}
...
...
ethchain/stack.go
View file @
11651615
...
@@ -173,6 +173,10 @@ func NewStack() *Stack {
...
@@ -173,6 +173,10 @@ func NewStack() *Stack {
return
&
Stack
{}
return
&
Stack
{}
}
}
func
(
st
*
Stack
)
Data
()
[]
*
big
.
Int
{
return
st
.
data
}
func
(
st
*
Stack
)
Pop
()
*
big
.
Int
{
func
(
st
*
Stack
)
Pop
()
*
big
.
Int
{
str
:=
st
.
data
[
len
(
st
.
data
)
-
1
]
str
:=
st
.
data
[
len
(
st
.
data
)
-
1
]
...
@@ -246,6 +250,10 @@ func (m *Memory) Len() int {
...
@@ -246,6 +250,10 @@ func (m *Memory) Len() int {
return
len
(
m
.
store
)
return
len
(
m
.
store
)
}
}
func
(
m
*
Memory
)
Data
()
[]
byte
{
return
m
.
store
}
func
(
m
*
Memory
)
Print
()
{
func
(
m
*
Memory
)
Print
()
{
fmt
.
Printf
(
"### mem %d bytes ###
\n
"
,
len
(
m
.
store
))
fmt
.
Printf
(
"### mem %d bytes ###
\n
"
,
len
(
m
.
store
))
if
len
(
m
.
store
)
>
0
{
if
len
(
m
.
store
)
>
0
{
...
...
ethchain/state_manager.go
View file @
11651615
...
@@ -318,14 +318,14 @@ func (sm *StateManager) ProcessContract(contract *Contract, tx *Transaction, blo
...
@@ -318,14 +318,14 @@ func (sm *StateManager) ProcessContract(contract *Contract, tx *Transaction, blo
caller
:=
sm
.
procState
.
GetAccount
(
tx
.
Sender
())
caller
:=
sm
.
procState
.
GetAccount
(
tx
.
Sender
())
closure
:=
NewClosure
(
caller
,
contract
,
contract
.
script
,
sm
.
procState
,
tx
.
Gas
,
tx
.
Value
)
closure
:=
NewClosure
(
caller
,
contract
,
contract
.
script
,
sm
.
procState
,
tx
.
Gas
,
tx
.
Value
)
vm
:=
NewVm
(
sm
.
procState
,
RuntimeVars
{
vm
:=
NewVm
(
sm
.
procState
,
RuntimeVars
{
o
rigin
:
caller
.
Address
(),
O
rigin
:
caller
.
Address
(),
b
lockNumber
:
block
.
BlockInfo
()
.
Number
,
B
lockNumber
:
block
.
BlockInfo
()
.
Number
,
p
revHash
:
block
.
PrevHash
,
P
revHash
:
block
.
PrevHash
,
c
oinbase
:
block
.
Coinbase
,
C
oinbase
:
block
.
Coinbase
,
t
ime
:
block
.
Time
,
T
ime
:
block
.
Time
,
d
iff
:
block
.
Difficulty
,
D
iff
:
block
.
Difficulty
,
// XXX Tx data? Could be just an argument to the closure instead
// XXX Tx data? Could be just an argument to the closure instead
t
xData
:
nil
,
T
xData
:
nil
,
})
})
closure
.
Call
(
vm
,
nil
,
nil
)
closure
.
Call
(
vm
,
nil
,
nil
)
...
...
ethchain/vm.go
View file @
11651615
...
@@ -2,7 +2,7 @@ package ethchain
...
@@ -2,7 +2,7 @@ package ethchain
import
(
import
(
_
"bytes"
_
"bytes"
_
"fmt"
"fmt"
"github.com/ethereum/eth-go/ethutil"
"github.com/ethereum/eth-go/ethutil"
_
"github.com/obscuren/secp256k1-go"
_
"github.com/obscuren/secp256k1-go"
_
"math"
_
"math"
...
@@ -33,13 +33,13 @@ type Vm struct {
...
@@ -33,13 +33,13 @@ type Vm struct {
}
}
type
RuntimeVars
struct
{
type
RuntimeVars
struct
{
o
rigin
[]
byte
O
rigin
[]
byte
b
lockNumber
uint64
B
lockNumber
uint64
p
revHash
[]
byte
P
revHash
[]
byte
c
oinbase
[]
byte
C
oinbase
[]
byte
t
ime
int64
T
ime
int64
d
iff
*
big
.
Int
D
iff
*
big
.
Int
t
xData
[]
string
T
xData
[]
string
}
}
func
NewVm
(
state
*
State
,
vars
RuntimeVars
)
*
Vm
{
func
NewVm
(
state
*
State
,
vars
RuntimeVars
)
*
Vm
{
...
@@ -65,9 +65,11 @@ func (vm *Vm) RunClosure(closure *Closure, hook DebugHook) []byte {
...
@@ -65,9 +65,11 @@ func (vm *Vm) RunClosure(closure *Closure, hook DebugHook) []byte {
// The base for all big integer arithmetic
// The base for all big integer arithmetic
base
:=
new
(
big
.
Int
)
base
:=
new
(
big
.
Int
)
if
ethutil
.
Config
.
Debug
{
/*
ethutil
.
Config
.
Log
.
Debugf
(
"# op
\n
"
)
if ethutil.Config.Debug {
}
ethutil.Config.Log.Debugf("# op\n")
}
*/
for
{
for
{
step
++
step
++
...
@@ -75,9 +77,11 @@ func (vm *Vm) RunClosure(closure *Closure, hook DebugHook) []byte {
...
@@ -75,9 +77,11 @@ func (vm *Vm) RunClosure(closure *Closure, hook DebugHook) []byte {
val
:=
closure
.
Get
(
pc
)
val
:=
closure
.
Get
(
pc
)
// Get the opcode (it must be an opcode!)
// Get the opcode (it must be an opcode!)
op
:=
OpCode
(
val
.
Uint
())
op
:=
OpCode
(
val
.
Uint
())
if
ethutil
.
Config
.
Debug
{
/*
ethutil
.
Config
.
Log
.
Debugf
(
"%-3d %-4s"
,
pc
,
op
.
String
())
if ethutil.Config.Debug {
}
ethutil.Config.Log.Debugf("%-3d %-4s", pc, op.String())
}
*/
// TODO Get each instruction cost properly
// TODO Get each instruction cost properly
gas
:=
new
(
big
.
Int
)
gas
:=
new
(
big
.
Int
)
...
@@ -270,7 +274,7 @@ func (vm *Vm) RunClosure(closure *Closure, hook DebugHook) []byte {
...
@@ -270,7 +274,7 @@ func (vm *Vm) RunClosure(closure *Closure, hook DebugHook) []byte {
case
oBALANCE
:
case
oBALANCE
:
stack
.
Push
(
closure
.
Value
)
stack
.
Push
(
closure
.
Value
)
case
oORIGIN
:
case
oORIGIN
:
stack
.
Push
(
ethutil
.
BigD
(
vm
.
vars
.
o
rigin
))
stack
.
Push
(
ethutil
.
BigD
(
vm
.
vars
.
O
rigin
))
case
oCALLER
:
case
oCALLER
:
stack
.
Push
(
ethutil
.
BigD
(
closure
.
Callee
()
.
Address
()))
stack
.
Push
(
ethutil
.
BigD
(
closure
.
Callee
()
.
Address
()))
case
oCALLVALUE
:
case
oCALLVALUE
:
...
@@ -286,15 +290,15 @@ func (vm *Vm) RunClosure(closure *Closure, hook DebugHook) []byte {
...
@@ -286,15 +290,15 @@ func (vm *Vm) RunClosure(closure *Closure, hook DebugHook) []byte {
// 0x40 range
// 0x40 range
case
oPREVHASH
:
case
oPREVHASH
:
stack
.
Push
(
ethutil
.
BigD
(
vm
.
vars
.
p
revHash
))
stack
.
Push
(
ethutil
.
BigD
(
vm
.
vars
.
P
revHash
))
case
oCOINBASE
:
case
oCOINBASE
:
stack
.
Push
(
ethutil
.
BigD
(
vm
.
vars
.
c
oinbase
))
stack
.
Push
(
ethutil
.
BigD
(
vm
.
vars
.
C
oinbase
))
case
oTIMESTAMP
:
case
oTIMESTAMP
:
stack
.
Push
(
big
.
NewInt
(
vm
.
vars
.
t
ime
))
stack
.
Push
(
big
.
NewInt
(
vm
.
vars
.
T
ime
))
case
oNUMBER
:
case
oNUMBER
:
stack
.
Push
(
big
.
NewInt
(
int64
(
vm
.
vars
.
b
lockNumber
)))
stack
.
Push
(
big
.
NewInt
(
int64
(
vm
.
vars
.
B
lockNumber
)))
case
oDIFFICULTY
:
case
oDIFFICULTY
:
stack
.
Push
(
vm
.
vars
.
d
iff
)
stack
.
Push
(
vm
.
vars
.
D
iff
)
case
oGASLIMIT
:
case
oGASLIMIT
:
// TODO
// TODO
...
@@ -406,7 +410,59 @@ func (vm *Vm) RunClosure(closure *Closure, hook DebugHook) []byte {
...
@@ -406,7 +410,59 @@ func (vm *Vm) RunClosure(closure *Closure, hook DebugHook) []byte {
pc
.
Add
(
pc
,
ethutil
.
Big1
)
pc
.
Add
(
pc
,
ethutil
.
Big1
)
if
hook
!=
nil
{
if
hook
!=
nil
{
hook
(
op
)
hook
(
op
,
mem
,
stack
)
}
}
}
func
Disassemble
(
script
[]
byte
)
(
asm
[]
string
)
{
pc
:=
new
(
big
.
Int
)
for
{
if
pc
.
Cmp
(
big
.
NewInt
(
int64
(
len
(
script
))))
>=
0
{
return
}
// Get the memory location of pc
val
:=
script
[
pc
.
Int64
()]
// Get the opcode (it must be an opcode!)
op
:=
OpCode
(
val
)
asm
=
append
(
asm
,
fmt
.
Sprintf
(
"%v"
,
op
))
switch
op
{
case
oPUSH
:
// Push PC+1 on to the stack
pc
.
Add
(
pc
,
ethutil
.
Big1
)
data
:=
script
[
pc
.
Int64
()
:
pc
.
Int64
()
+
32
]
val
:=
ethutil
.
BigD
(
data
)
var
b
[]
byte
if
val
.
Int64
()
==
0
{
b
=
[]
byte
{
0
}
}
else
{
b
=
val
.
Bytes
()
}
asm
=
append
(
asm
,
fmt
.
Sprintf
(
"0x%x"
,
b
))
pc
.
Add
(
pc
,
big
.
NewInt
(
31
))
case
oPUSH20
:
pc
.
Add
(
pc
,
ethutil
.
Big1
)
data
:=
script
[
pc
.
Int64
()
:
pc
.
Int64
()
+
20
]
val
:=
ethutil
.
BigD
(
data
)
var
b
[]
byte
if
val
.
Int64
()
==
0
{
b
=
[]
byte
{
0
}
}
else
{
b
=
val
.
Bytes
()
}
asm
=
append
(
asm
,
fmt
.
Sprintf
(
"0x%x"
,
b
))
pc
.
Add
(
pc
,
big
.
NewInt
(
19
))
}
}
pc
.
Add
(
pc
,
ethutil
.
Big1
)
}
}
return
}
}
ethchain/vm_test.go
View file @
11651615
...
@@ -130,14 +130,29 @@ func TestRun4(t *testing.T) {
...
@@ -130,14 +130,29 @@ func TestRun4(t *testing.T) {
callerClosure
:=
NewClosure
(
account
,
c
,
c
.
script
,
state
,
big
.
NewInt
(
1000000000
),
new
(
big
.
Int
))
callerClosure
:=
NewClosure
(
account
,
c
,
c
.
script
,
state
,
big
.
NewInt
(
1000000000
),
new
(
big
.
Int
))
vm
:=
NewVm
(
state
,
RuntimeVars
{
vm
:=
NewVm
(
state
,
RuntimeVars
{
o
rigin
:
account
.
Address
(),
O
rigin
:
account
.
Address
(),
b
lockNumber
:
1
,
B
lockNumber
:
1
,
p
revHash
:
ethutil
.
FromHex
(
"5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6"
),
P
revHash
:
ethutil
.
FromHex
(
"5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6"
),
c
oinbase
:
ethutil
.
FromHex
(
"2adc25665018aa1fe0e6bc666dac8fc2697ff9ba"
),
C
oinbase
:
ethutil
.
FromHex
(
"2adc25665018aa1fe0e6bc666dac8fc2697ff9ba"
),
t
ime
:
1
,
T
ime
:
1
,
d
iff
:
big
.
NewInt
(
256
),
D
iff
:
big
.
NewInt
(
256
),
// XXX Tx data? Could be just an argument to the closure instead
// XXX Tx data? Could be just an argument to the closure instead
t
xData
:
nil
,
T
xData
:
nil
,
})
})
callerClosure
.
Call
(
vm
,
nil
,
nil
)
callerClosure
.
Call
(
vm
,
nil
,
nil
)
}
}
func
TestRun5
(
t
*
testing
.
T
)
{
ethutil
.
ReadConfig
(
""
)
asm
,
_
:=
mutan
.
Compile
(
strings
.
NewReader
(
`
int32 a = 10
int32 b = 20
if a > b {
int32 c = this.caller()
}
exit()
`
),
false
)
script
:=
ethutil
.
Assemble
(
asm
...
)
fmt
.
Println
(
Disassemble
(
script
))
}
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