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
2ea4c632
Commit
2ea4c632
authored
Mar 21, 2014
by
obscuren
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Closure return, arguments fixed. Added proper tests
parent
fa1db8d2
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
92 additions
and
60 deletions
+92
-60
address.go
ethchain/address.go
+7
-3
closure.go
ethchain/closure.go
+16
-8
stack.go
ethchain/stack.go
+26
-26
state_manager.go
ethchain/state_manager.go
+1
-1
vm.go
ethchain/vm.go
+28
-5
vm_test.go
ethchain/vm_test.go
+1
-2
parsing.go
ethutil/parsing.go
+13
-15
No files found.
ethchain/address.go
View file @
2ea4c632
...
@@ -6,7 +6,7 @@ import (
...
@@ -6,7 +6,7 @@ import (
)
)
type
Account
struct
{
type
Account
struct
{
A
ddress
[]
byte
a
ddress
[]
byte
Amount
*
big
.
Int
Amount
*
big
.
Int
Nonce
uint64
Nonce
uint64
}
}
...
@@ -16,7 +16,7 @@ func NewAccount(address []byte, amount *big.Int) *Account {
...
@@ -16,7 +16,7 @@ func NewAccount(address []byte, amount *big.Int) *Account {
}
}
func
NewAccountFromData
(
address
,
data
[]
byte
)
*
Account
{
func
NewAccountFromData
(
address
,
data
[]
byte
)
*
Account
{
account
:=
&
Account
{
A
ddress
:
address
}
account
:=
&
Account
{
a
ddress
:
address
}
account
.
RlpDecode
(
data
)
account
.
RlpDecode
(
data
)
return
account
return
account
...
@@ -30,11 +30,15 @@ func (a *Account) AddFunds(funds *big.Int) {
...
@@ -30,11 +30,15 @@ func (a *Account) AddFunds(funds *big.Int) {
a
.
Amount
.
Add
(
a
.
Amount
,
funds
)
a
.
Amount
.
Add
(
a
.
Amount
,
funds
)
}
}
func
(
a
*
Account
)
Address
()
[]
byte
{
return
a
.
address
}
// Implements Callee
// Implements Callee
func
(
a
*
Account
)
ReturnGas
(
value
*
big
.
Int
,
state
*
State
)
{
func
(
a
*
Account
)
ReturnGas
(
value
*
big
.
Int
,
state
*
State
)
{
// Return the value back to the sender
// Return the value back to the sender
a
.
AddFunds
(
value
)
a
.
AddFunds
(
value
)
state
.
UpdateAccount
(
a
.
A
ddress
,
a
)
state
.
UpdateAccount
(
a
.
a
ddress
,
a
)
}
}
func
(
a
*
Account
)
RlpEncode
()
[]
byte
{
func
(
a
*
Account
)
RlpEncode
()
[]
byte
{
...
...
ethchain/closure.go
View file @
2ea4c632
...
@@ -9,13 +9,13 @@ import (
...
@@ -9,13 +9,13 @@ import (
type
Callee
interface
{
type
Callee
interface
{
ReturnGas
(
*
big
.
Int
,
*
State
)
ReturnGas
(
*
big
.
Int
,
*
State
)
Address
()
[]
byte
}
}
type
ClosureBody
interface
{
type
ClosureBody
interface
{
Callee
Callee
ethutil
.
RlpEncodable
ethutil
.
RlpEncodable
GetMem
(
int64
)
*
ethutil
.
Value
GetMem
(
int64
)
*
ethutil
.
Value
Address
()
[]
byte
}
}
// Basic inline closure object which implement the 'closure' interface
// Basic inline closure object which implement the 'closure' interface
...
@@ -24,8 +24,8 @@ type Closure struct {
...
@@ -24,8 +24,8 @@ type Closure struct {
object
ClosureBody
object
ClosureBody
State
*
State
State
*
State
gas
*
big
.
Int
Gas
*
big
.
Int
val
*
big
.
Int
Value
*
big
.
Int
Args
[]
byte
Args
[]
byte
}
}
...
@@ -45,6 +45,10 @@ func (c *Closure) GetMem(x int64) *ethutil.Value {
...
@@ -45,6 +45,10 @@ func (c *Closure) GetMem(x int64) *ethutil.Value {
return
m
return
m
}
}
func
(
c
*
Closure
)
Address
()
[]
byte
{
return
c
.
object
.
Address
()
}
func
(
c
*
Closure
)
Call
(
vm
*
Vm
,
args
[]
byte
)
[]
byte
{
func
(
c
*
Closure
)
Call
(
vm
*
Vm
,
args
[]
byte
)
[]
byte
{
c
.
Args
=
args
c
.
Args
=
args
...
@@ -56,9 +60,9 @@ func (c *Closure) Return(ret []byte) []byte {
...
@@ -56,9 +60,9 @@ func (c *Closure) Return(ret []byte) []byte {
// If no callee is present return it to
// If no callee is present return it to
// the origin (i.e. contract or tx)
// the origin (i.e. contract or tx)
if
c
.
callee
!=
nil
{
if
c
.
callee
!=
nil
{
c
.
callee
.
ReturnGas
(
c
.
g
as
,
c
.
State
)
c
.
callee
.
ReturnGas
(
c
.
G
as
,
c
.
State
)
}
else
{
}
else
{
c
.
object
.
ReturnGas
(
c
.
g
as
,
c
.
State
)
c
.
object
.
ReturnGas
(
c
.
G
as
,
c
.
State
)
// TODO incase it's a POST contract we gotta serialise the contract again.
// TODO incase it's a POST contract we gotta serialise the contract again.
// But it's not yet defined
// But it's not yet defined
}
}
...
@@ -69,9 +73,13 @@ func (c *Closure) Return(ret []byte) []byte {
...
@@ -69,9 +73,13 @@ func (c *Closure) Return(ret []byte) []byte {
// Implement the Callee interface
// Implement the Callee interface
func
(
c
*
Closure
)
ReturnGas
(
gas
*
big
.
Int
,
state
*
State
)
{
func
(
c
*
Closure
)
ReturnGas
(
gas
*
big
.
Int
,
state
*
State
)
{
// Return the gas to the closure
// Return the gas to the closure
c
.
gas
.
Add
(
c
.
gas
,
gas
)
c
.
Gas
.
Add
(
c
.
Gas
,
gas
)
}
func
(
c
*
Closure
)
Object
()
ClosureBody
{
return
c
.
object
}
}
func
(
c
*
Closure
)
GetGas
()
*
big
.
Int
{
func
(
c
*
Closure
)
Callee
()
Callee
{
return
c
.
gas
return
c
.
callee
}
}
ethchain/stack.go
View file @
2ea4c632
...
@@ -43,17 +43,15 @@ const (
...
@@ -43,17 +43,15 @@ const (
oCALLVALUE
=
0x34
oCALLVALUE
=
0x34
oCALLDATA
=
0x35
oCALLDATA
=
0x35
oCALLDATASIZE
=
0x36
oCALLDATASIZE
=
0x36
oRETURNDATASIZE
=
0x37
oGASPRICE
=
0x37
oTXGASPRICE
=
0x38
// 0x40 range - block operations
// 0x40 range - block operations
oPREVHASH
=
0x40
oPREVHASH
=
0x40
oPREVNONCE
=
0x41
oCOINBASE
=
0x41
oCOINBASE
=
0x42
oTIMESTAMP
=
0x42
oTIMESTAMP
=
0x43
oNUMBER
=
0x43
oNUMBER
=
0x44
oDIFFICULTY
=
0x44
oDIFFICULTY
=
0x45
oGASLIMIT
=
0x45
oGASLIMIT
=
0x46
// 0x50 range - 'storage' and execution
// 0x50 range - 'storage' and execution
oPUSH
=
0x50
oPUSH
=
0x50
...
@@ -115,12 +113,10 @@ var opCodeToString = map[OpCode]string{
...
@@ -115,12 +113,10 @@ var opCodeToString = map[OpCode]string{
oCALLVALUE
:
"CALLVALUE"
,
oCALLVALUE
:
"CALLVALUE"
,
oCALLDATA
:
"CALLDATA"
,
oCALLDATA
:
"CALLDATA"
,
oCALLDATASIZE
:
"CALLDATASIZE"
,
oCALLDATASIZE
:
"CALLDATASIZE"
,
oRETURNDATASIZE
:
"RETURNDATASIZE"
,
oGASPRICE
:
"TXGASPRICE"
,
oTXGASPRICE
:
"TXGASPRICE"
,
// 0x40 range - block operations
// 0x40 range - block operations
oPREVHASH
:
"PREVHASH"
,
oPREVHASH
:
"PREVHASH"
,
oPREVNONCE
:
"PREVNONCE"
,
oCOINBASE
:
"COINBASE"
,
oCOINBASE
:
"COINBASE"
,
oTIMESTAMP
:
"TIMESTAMP"
,
oTIMESTAMP
:
"TIMESTAMP"
,
oNUMBER
:
"NUMBER"
,
oNUMBER
:
"NUMBER"
,
...
@@ -244,7 +240,11 @@ func (m *Memory) Get(offset, size int64) []byte {
...
@@ -244,7 +240,11 @@ func (m *Memory) Get(offset, size int64) []byte {
func
(
m
*
Memory
)
Print
()
{
func
(
m
*
Memory
)
Print
()
{
fmt
.
Println
(
"### MEM ###"
)
fmt
.
Println
(
"### MEM ###"
)
if
len
(
m
.
store
)
>
0
{
if
len
(
m
.
store
)
>
0
{
fmt
.
Println
(
m
.
store
)
addr
:=
0
for
i
:=
0
;
i
+
32
<
len
(
m
.
store
);
i
+=
32
{
fmt
.
Printf
(
"%03d %v
\n
"
,
addr
,
m
.
store
[
i
:
i
+
32
])
addr
++
}
}
else
{
}
else
{
fmt
.
Println
(
"-- empty --"
)
fmt
.
Println
(
"-- empty --"
)
}
}
...
...
ethchain/state_manager.go
View file @
2ea4c632
...
@@ -308,7 +308,7 @@ func (sm *StateManager) ProcessContract(contract *Contract, tx *Transaction, blo
...
@@ -308,7 +308,7 @@ 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
,
sm
.
procState
,
tx
.
Gas
,
tx
.
Value
)
closure
:=
NewClosure
(
caller
,
contract
,
sm
.
procState
,
tx
.
Gas
,
tx
.
Value
)
vm
:=
NewVm
(
sm
.
procState
,
RuntimeVars
{
vm
:=
NewVm
(
sm
.
procState
,
RuntimeVars
{
origin
:
caller
.
Address
,
origin
:
caller
.
Address
()
,
blockNumber
:
block
.
BlockInfo
()
.
Number
,
blockNumber
:
block
.
BlockInfo
()
.
Number
,
prevHash
:
block
.
PrevHash
,
prevHash
:
block
.
PrevHash
,
coinbase
:
block
.
Coinbase
,
coinbase
:
block
.
Coinbase
,
...
...
ethchain/vm.go
View file @
2ea4c632
...
@@ -40,7 +40,7 @@ var Pow256 = ethutil.BigPow(2, 256)
...
@@ -40,7 +40,7 @@ var Pow256 = ethutil.BigPow(2, 256)
func
(
vm
*
Vm
)
RunClosure
(
closure
*
Closure
)
[]
byte
{
func
(
vm
*
Vm
)
RunClosure
(
closure
*
Closure
)
[]
byte
{
// If the amount of gas supplied is less equal to 0
// If the amount of gas supplied is less equal to 0
if
closure
.
G
etGas
()
.
Cmp
(
big
.
NewInt
(
0
))
<=
0
{
if
closure
.
G
as
.
Cmp
(
big
.
NewInt
(
0
))
<=
0
{
// TODO Do something
// TODO Do something
}
}
...
@@ -73,7 +73,7 @@ func (vm *Vm) RunClosure(closure *Closure) []byte {
...
@@ -73,7 +73,7 @@ func (vm *Vm) RunClosure(closure *Closure) []byte {
fee
:=
new
(
big
.
Int
)
fee
:=
new
(
big
.
Int
)
fee
.
Add
(
fee
,
big
.
NewInt
(
1000
))
fee
.
Add
(
fee
,
big
.
NewInt
(
1000
))
if
closure
.
G
etGas
()
.
Cmp
(
fee
)
<
0
{
if
closure
.
G
as
.
Cmp
(
fee
)
<
0
{
return
closure
.
Return
(
nil
)
return
closure
.
Return
(
nil
)
}
}
...
@@ -192,25 +192,37 @@ func (vm *Vm) RunClosure(closure *Closure) []byte {
...
@@ -192,25 +192,37 @@ func (vm *Vm) RunClosure(closure *Closure) []byte {
// 0x30 range
// 0x30 range
case
oADDRESS
:
case
oADDRESS
:
stack
.
Push
(
ethutil
.
BigD
(
closure
.
Object
()
.
Address
()))
case
oBALANCE
:
case
oBALANCE
:
stack
.
Push
(
closure
.
Value
)
case
oORIGIN
:
case
oORIGIN
:
stack
.
Push
(
ethutil
.
BigD
(
vm
.
vars
.
origin
))
case
oCALLER
:
case
oCALLER
:
stack
.
Push
(
ethutil
.
BigD
(
closure
.
Callee
()
.
Address
()))
case
oCALLVALUE
:
case
oCALLVALUE
:
// FIXME: Original value of the call, not the current value
stack
.
Push
(
closure
.
Value
)
case
oCALLDATA
:
case
oCALLDATA
:
offset
:=
stack
.
Pop
()
offset
:=
stack
.
Pop
()
mem
.
Set
(
offset
.
Int64
(),
int64
(
len
(
closure
.
Args
)),
closure
.
Args
)
mem
.
Set
(
offset
.
Int64
(),
int64
(
len
(
closure
.
Args
)),
closure
.
Args
)
case
oCALLDATASIZE
:
case
oCALLDATASIZE
:
case
oRETURNDATASIZE
:
stack
.
Push
(
big
.
NewInt
(
int64
(
len
(
closure
.
Args
))))
case
oTXGASPRICE
:
case
oGASPRICE
:
// TODO
// 0x40 range
// 0x40 range
case
oPREVHASH
:
case
oPREVHASH
:
case
oPREVNONCE
:
stack
.
Push
(
ethutil
.
BigD
(
vm
.
vars
.
prevHash
))
case
oCOINBASE
:
case
oCOINBASE
:
stack
.
Push
(
ethutil
.
BigD
(
vm
.
vars
.
coinbase
))
case
oTIMESTAMP
:
case
oTIMESTAMP
:
stack
.
Push
(
big
.
NewInt
(
vm
.
vars
.
time
))
case
oNUMBER
:
case
oNUMBER
:
stack
.
Push
(
big
.
NewInt
(
int64
(
vm
.
vars
.
blockNumber
)))
case
oDIFFICULTY
:
case
oDIFFICULTY
:
stack
.
Push
(
vm
.
vars
.
diff
)
case
oGASLIMIT
:
case
oGASLIMIT
:
// TODO
// 0x50 range
// 0x50 range
case
oPUSH
:
// Push PC+1 on to the stack
case
oPUSH
:
// Push PC+1 on to the stack
...
@@ -218,8 +230,13 @@ func (vm *Vm) RunClosure(closure *Closure) []byte {
...
@@ -218,8 +230,13 @@ func (vm *Vm) RunClosure(closure *Closure) []byte {
val
:=
closure
.
GetMem
(
pc
)
.
BigInt
()
val
:=
closure
.
GetMem
(
pc
)
.
BigInt
()
stack
.
Push
(
val
)
stack
.
Push
(
val
)
case
oPOP
:
case
oPOP
:
stack
.
Pop
()
case
oDUP
:
case
oDUP
:
stack
.
Push
(
stack
.
Peek
())
case
oSWAP
:
case
oSWAP
:
x
,
y
:=
stack
.
Popn
()
stack
.
Push
(
y
)
stack
.
Push
(
x
)
case
oMLOAD
:
case
oMLOAD
:
offset
:=
stack
.
Pop
()
offset
:=
stack
.
Pop
()
stack
.
Push
(
ethutil
.
BigD
(
mem
.
Get
(
offset
.
Int64
(),
32
)))
stack
.
Push
(
ethutil
.
BigD
(
mem
.
Get
(
offset
.
Int64
(),
32
)))
...
@@ -228,7 +245,13 @@ func (vm *Vm) RunClosure(closure *Closure) []byte {
...
@@ -228,7 +245,13 @@ func (vm *Vm) RunClosure(closure *Closure) []byte {
val
,
mStart
:=
stack
.
Popn
()
val
,
mStart
:=
stack
.
Popn
()
mem
.
Set
(
mStart
.
Int64
(),
32
,
ethutil
.
BigToBytes
(
val
,
256
))
mem
.
Set
(
mStart
.
Int64
(),
32
,
ethutil
.
BigToBytes
(
val
,
256
))
case
oMSTORE8
:
case
oMSTORE8
:
val
,
mStart
:=
stack
.
Popn
()
base
.
And
(
val
,
new
(
big
.
Int
)
.
SetInt64
(
0xff
))
mem
.
Set
(
mStart
.
Int64
(),
32
,
ethutil
.
BigToBytes
(
base
,
256
))
case
oSLOAD
:
case
oSLOAD
:
loc
:=
stack
.
Pop
()
val
:=
closure
.
GetMem
(
loc
.
Int64
())
stack
.
Push
(
val
.
BigInt
())
case
oSSTORE
:
case
oSSTORE
:
case
oJUMP
:
case
oJUMP
:
case
oJUMPI
:
case
oJUMPI
:
...
...
ethchain/vm_test.go
View file @
2ea4c632
...
@@ -126,7 +126,6 @@ func TestRun3(t *testing.T) {
...
@@ -126,7 +126,6 @@ func TestRun3(t *testing.T) {
"PUSH"
,
"64"
,
"PUSH"
,
"64"
,
"PUSH"
,
"0"
,
"PUSH"
,
"0"
,
"LOG"
,
"RETURN"
,
"RETURN"
,
})
})
tx
:=
NewTransaction
(
ContractAddr
,
ethutil
.
Big
(
"100000000000000000000000000000000000000000000000000"
),
script
)
tx
:=
NewTransaction
(
ContractAddr
,
ethutil
.
Big
(
"100000000000000000000000000000000000000000000000000"
),
script
)
...
@@ -159,7 +158,7 @@ func TestRun3(t *testing.T) {
...
@@ -159,7 +158,7 @@ func TestRun3(t *testing.T) {
callerClosure
:=
NewClosure
(
account
,
MakeContract
(
callerTx
,
state
),
state
,
big
.
NewInt
(
1000000000
),
new
(
big
.
Int
))
callerClosure
:=
NewClosure
(
account
,
MakeContract
(
callerTx
,
state
),
state
,
big
.
NewInt
(
1000000000
),
new
(
big
.
Int
))
vm
:=
NewVm
(
state
,
RuntimeVars
{
vm
:=
NewVm
(
state
,
RuntimeVars
{
origin
:
account
.
Address
,
origin
:
account
.
Address
()
,
blockNumber
:
1
,
blockNumber
:
1
,
prevHash
:
ethutil
.
FromHex
(
"5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6"
),
prevHash
:
ethutil
.
FromHex
(
"5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6"
),
coinbase
:
ethutil
.
FromHex
(
"2adc25665018aa1fe0e6bc666dac8fc2697ff9ba"
),
coinbase
:
ethutil
.
FromHex
(
"2adc25665018aa1fe0e6bc666dac8fc2697ff9ba"
),
...
...
ethutil/parsing.go
View file @
2ea4c632
...
@@ -40,17 +40,15 @@ var OpCodes = map[string]byte{
...
@@ -40,17 +40,15 @@ var OpCodes = map[string]byte{
"CALLVALUE"
:
0x34
,
"CALLVALUE"
:
0x34
,
"CALLDATA"
:
0x35
,
"CALLDATA"
:
0x35
,
"CALLDATASIZE"
:
0x36
,
"CALLDATASIZE"
:
0x36
,
"RETURNDATASIZE"
:
0x37
,
"GASPRICE"
:
0x38
,
"TXGASPRICE"
:
0x38
,
// 0x40 range - block operations
// 0x40 range - block operations
"PREVHASH"
:
0x40
,
"PREVHASH"
:
0x40
,
"PREVNONCE"
:
0x41
,
"COINBASE"
:
0x41
,
"COINBASE"
:
0x42
,
"TIMESTAMP"
:
0x42
,
"TIMESTAMP"
:
0x43
,
"NUMBER"
:
0x43
,
"NUMBER"
:
0x44
,
"DIFFICULTY"
:
0x44
,
"DIFFICULTY"
:
0x45
,
"GASLIMIT"
:
0x45
,
"GASLIMIT"
:
0x46
,
// 0x50 range - 'storage' and execution
// 0x50 range - 'storage' and execution
"PUSH"
:
0x50
,
"PUSH"
:
0x50
,
...
...
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