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
b5beb1aa
Commit
b5beb1aa
authored
Oct 22, 2014
by
obscuren
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added a transfer method to vm env
parent
27cb0750
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
59 additions
and
36 deletions
+59
-36
dump.go
ethstate/dump.go
+1
-1
state.go
ethstate/state.go
+1
-1
state_object.go
ethstate/state_object.go
+22
-18
peer.go
peer.go
+1
-1
vm.go
tests/helper/vm.go
+4
-1
gh_test.go
tests/vm/gh_test.go
+1
-5
environment.go
vm/environment.go
+20
-0
execution.go
vm/execution.go
+8
-8
vm.go
vm/vm.go
+1
-1
No files found.
ethstate/dump.go
View file @
b5beb1aa
...
...
@@ -28,7 +28,7 @@ func (self *State) Dump() []byte {
self
.
Trie
.
NewIterator
()
.
Each
(
func
(
key
string
,
value
*
ethutil
.
Value
)
{
stateObject
:=
NewStateObjectFromBytes
([]
byte
(
key
),
value
.
Bytes
())
account
:=
Account
{
Balance
:
stateObject
.
B
alance
.
String
(),
Nonce
:
stateObject
.
Nonce
,
CodeHash
:
ethutil
.
Bytes2Hex
(
stateObject
.
codeHash
)}
account
:=
Account
{
Balance
:
stateObject
.
b
alance
.
String
(),
Nonce
:
stateObject
.
Nonce
,
CodeHash
:
ethutil
.
Bytes2Hex
(
stateObject
.
codeHash
)}
account
.
Storage
=
make
(
map
[
string
]
string
)
stateObject
.
EachStorage
(
func
(
key
string
,
value
*
ethutil
.
Value
)
{
...
...
ethstate/state.go
View file @
b5beb1aa
...
...
@@ -33,7 +33,7 @@ func New(trie *ethtrie.Trie) *State {
func
(
self
*
State
)
GetBalance
(
addr
[]
byte
)
*
big
.
Int
{
stateObject
:=
self
.
GetStateObject
(
addr
)
if
stateObject
!=
nil
{
return
stateObject
.
B
alance
return
stateObject
.
b
alance
}
return
ethutil
.
Big0
...
...
ethstate/state_object.go
View file @
b5beb1aa
...
...
@@ -31,7 +31,7 @@ type StateObject struct {
// Address of the object
address
[]
byte
// Shared attributes
B
alance
*
big
.
Int
b
alance
*
big
.
Int
codeHash
[]
byte
Nonce
uint64
// Contract related attributes
...
...
@@ -61,7 +61,7 @@ func NewStateObject(addr []byte) *StateObject {
// This to ensure that it has 20 bytes (and not 0 bytes), thus left or right pad doesn't matter.
address
:=
ethutil
.
Address
(
addr
)
object
:=
&
StateObject
{
address
:
address
,
B
alance
:
new
(
big
.
Int
),
gasPool
:
new
(
big
.
Int
)}
object
:=
&
StateObject
{
address
:
address
,
b
alance
:
new
(
big
.
Int
),
gasPool
:
new
(
big
.
Int
)}
object
.
State
=
New
(
ethtrie
.
New
(
ethutil
.
Config
.
Db
,
""
))
object
.
storage
=
make
(
Storage
)
object
.
gasPool
=
new
(
big
.
Int
)
...
...
@@ -71,7 +71,7 @@ func NewStateObject(addr []byte) *StateObject {
func
NewContract
(
address
[]
byte
,
balance
*
big
.
Int
,
root
[]
byte
)
*
StateObject
{
contract
:=
NewStateObject
(
address
)
contract
.
B
alance
=
balance
contract
.
b
alance
=
balance
contract
.
State
=
New
(
ethtrie
.
New
(
ethutil
.
Config
.
Db
,
string
(
root
)))
return
contract
...
...
@@ -86,7 +86,7 @@ func NewStateObjectFromBytes(address, data []byte) *StateObject {
func
(
self
*
StateObject
)
MarkForDeletion
()
{
self
.
remove
=
true
statelogger
.
DebugDetailf
(
"%x: #%d %v (deletion)
\n
"
,
self
.
Address
(),
self
.
Nonce
,
self
.
B
alance
)
statelogger
.
DebugDetailf
(
"%x: #%d %v (deletion)
\n
"
,
self
.
Address
(),
self
.
Nonce
,
self
.
b
alance
)
}
func
(
c
*
StateObject
)
GetAddr
(
addr
[]
byte
)
*
ethutil
.
Value
{
...
...
@@ -174,22 +174,26 @@ func (c *StateObject) GetInstr(pc *big.Int) *ethutil.Value {
return
ethutil
.
NewValueFromBytes
([]
byte
{
c
.
Code
[
pc
.
Int64
()]})
}
func
(
c
*
StateObject
)
Add
Amount
(
amount
*
big
.
Int
)
{
c
.
SetBalance
(
new
(
big
.
Int
)
.
Add
(
c
.
B
alance
,
amount
))
func
(
c
*
StateObject
)
Add
Balance
(
amount
*
big
.
Int
)
{
c
.
SetBalance
(
new
(
big
.
Int
)
.
Add
(
c
.
b
alance
,
amount
))
statelogger
.
Debugf
(
"%x: #%d %v (+ %v)
\n
"
,
c
.
Address
(),
c
.
Nonce
,
c
.
B
alance
,
amount
)
statelogger
.
Debugf
(
"%x: #%d %v (+ %v)
\n
"
,
c
.
Address
(),
c
.
Nonce
,
c
.
b
alance
,
amount
)
}
func
(
c
*
StateObject
)
AddAmount
(
amount
*
big
.
Int
)
{
c
.
AddBalance
(
amount
)
}
func
(
c
*
StateObject
)
Sub
Amount
(
amount
*
big
.
Int
)
{
c
.
SetBalance
(
new
(
big
.
Int
)
.
Sub
(
c
.
B
alance
,
amount
))
func
(
c
*
StateObject
)
Sub
Balance
(
amount
*
big
.
Int
)
{
c
.
SetBalance
(
new
(
big
.
Int
)
.
Sub
(
c
.
b
alance
,
amount
))
statelogger
.
Debugf
(
"%x: #%d %v (- %v)
\n
"
,
c
.
Address
(),
c
.
Nonce
,
c
.
B
alance
,
amount
)
statelogger
.
Debugf
(
"%x: #%d %v (- %v)
\n
"
,
c
.
Address
(),
c
.
Nonce
,
c
.
b
alance
,
amount
)
}
func
(
c
*
StateObject
)
SubAmount
(
amount
*
big
.
Int
)
{
c
.
SubBalance
(
amount
)
}
func
(
c
*
StateObject
)
SetBalance
(
amount
*
big
.
Int
)
{
c
.
B
alance
=
amount
c
.
b
alance
=
amount
}
func
(
self
*
StateObject
)
Balance
()
*
big
.
Int
{
return
self
.
balance
}
//
// Gas setters and getters
//
...
...
@@ -198,8 +202,8 @@ func (c *StateObject) SetBalance(amount *big.Int) {
func
(
c
*
StateObject
)
ReturnGas
(
gas
,
price
*
big
.
Int
)
{}
func
(
c
*
StateObject
)
ConvertGas
(
gas
,
price
*
big
.
Int
)
error
{
total
:=
new
(
big
.
Int
)
.
Mul
(
gas
,
price
)
if
total
.
Cmp
(
c
.
B
alance
)
>
0
{
return
fmt
.
Errorf
(
"insufficient amount: %v, %v"
,
c
.
B
alance
,
total
)
if
total
.
Cmp
(
c
.
b
alance
)
>
0
{
return
fmt
.
Errorf
(
"insufficient amount: %v, %v"
,
c
.
b
alance
,
total
)
}
c
.
SubAmount
(
total
)
...
...
@@ -232,12 +236,12 @@ func (self *StateObject) RefundGas(gas, price *big.Int) {
rGas
:=
new
(
big
.
Int
)
.
Set
(
gas
)
rGas
.
Mul
(
rGas
,
price
)
self
.
Balance
.
Sub
(
self
.
B
alance
,
rGas
)
self
.
balance
.
Sub
(
self
.
b
alance
,
rGas
)
}
func
(
self
*
StateObject
)
Copy
()
*
StateObject
{
stateObject
:=
NewStateObject
(
self
.
Address
())
stateObject
.
Balance
.
Set
(
self
.
B
alance
)
stateObject
.
balance
.
Set
(
self
.
b
alance
)
stateObject
.
codeHash
=
ethutil
.
CopyBytes
(
self
.
codeHash
)
stateObject
.
Nonce
=
self
.
Nonce
if
self
.
State
!=
nil
{
...
...
@@ -281,7 +285,7 @@ func (self *StateObject) Object() *StateObject {
// Debug stuff
func
(
self
*
StateObject
)
CreateOutputForDiff
()
{
fmt
.
Printf
(
"%x %x %x %x
\n
"
,
self
.
Address
(),
self
.
State
.
Root
(),
self
.
B
alance
.
Bytes
(),
self
.
Nonce
)
fmt
.
Printf
(
"%x %x %x %x
\n
"
,
self
.
Address
(),
self
.
State
.
Root
(),
self
.
b
alance
.
Bytes
(),
self
.
Nonce
)
self
.
EachStorage
(
func
(
addr
string
,
value
*
ethutil
.
Value
)
{
fmt
.
Printf
(
"%x %x
\n
"
,
addr
,
value
.
Bytes
())
})
...
...
@@ -300,7 +304,7 @@ func (c *StateObject) RlpEncode() []byte {
root
=
""
}
return
ethutil
.
Encode
([]
interface
{}{
c
.
Nonce
,
c
.
B
alance
,
root
,
c
.
CodeHash
()})
return
ethutil
.
Encode
([]
interface
{}{
c
.
Nonce
,
c
.
b
alance
,
root
,
c
.
CodeHash
()})
}
func
(
c
*
StateObject
)
CodeHash
()
ethutil
.
Bytes
{
...
...
@@ -316,7 +320,7 @@ func (c *StateObject) RlpDecode(data []byte) {
decoder
:=
ethutil
.
NewValueFromBytes
(
data
)
c
.
Nonce
=
decoder
.
Get
(
0
)
.
Uint
()
c
.
B
alance
=
decoder
.
Get
(
1
)
.
BigInt
()
c
.
b
alance
=
decoder
.
Get
(
1
)
.
BigInt
()
c
.
State
=
New
(
ethtrie
.
New
(
ethutil
.
Config
.
Db
,
decoder
.
Get
(
2
)
.
Interface
()))
c
.
storage
=
make
(
map
[
string
]
*
ethutil
.
Value
)
c
.
gasPool
=
new
(
big
.
Int
)
...
...
peer.go
View file @
b5beb1aa
...
...
@@ -674,7 +674,7 @@ func (p *Peer) pushPeers() {
func
(
self
*
Peer
)
pushStatus
()
{
msg
:=
ethwire
.
NewMessage
(
ethwire
.
MsgStatusTy
,
[]
interface
{}{
//
uint32(ProtocolVersion),
uint32
(
ProtocolVersion
),
uint32
(
NetVersion
),
self
.
ethereum
.
ChainManager
()
.
TD
,
self
.
ethereum
.
ChainManager
()
.
CurrentBlock
.
Hash
(),
...
...
tests/helper/vm.go
View file @
b5beb1aa
...
...
@@ -50,11 +50,14 @@ func (self *Env) Difficulty() *big.Int { return self.difficulty }
func
(
self
*
Env
)
BlockHash
()
[]
byte
{
return
nil
}
func
(
self
*
Env
)
State
()
*
ethstate
.
State
{
return
self
.
state
}
func
(
self
*
Env
)
GasLimit
()
*
big
.
Int
{
return
self
.
gasLimit
}
func
(
self
*
Env
)
Transfer
(
from
,
to
vm
.
Account
,
amount
*
big
.
Int
)
error
{
return
nil
}
func
RunVm
(
state
*
ethstate
.
State
,
env
,
exec
map
[
string
]
string
)
([]
byte
,
*
big
.
Int
,
error
)
{
address
:=
FromHex
(
exec
[
"address"
])
caller
:=
state
.
GetOrNewStateObject
(
FromHex
(
exec
[
"caller"
]))
caller
.
Balance
=
ethutil
.
Big
(
exec
[
"value"
]
)
caller
.
SetBalance
(
ethutil
.
Big
(
exec
[
"value"
])
)
evm
:=
vm
.
New
(
NewEnvFromMap
(
state
,
env
,
exec
),
vm
.
DebugVmTy
)
...
...
tests/vm/gh_test.go
View file @
b5beb1aa
...
...
@@ -18,7 +18,7 @@ type Account struct {
func
StateObjectFromAccount
(
addr
string
,
account
Account
)
*
ethstate
.
StateObject
{
obj
:=
ethstate
.
NewStateObject
(
ethutil
.
Hex2Bytes
(
addr
))
obj
.
Balance
=
ethutil
.
Big
(
account
.
Balance
)
obj
.
SetBalance
(
ethutil
.
Big
(
account
.
Balance
)
)
if
ethutil
.
IsHex
(
account
.
Code
)
{
account
.
Code
=
account
.
Code
[
2
:
]
...
...
@@ -44,9 +44,6 @@ func RunVmTest(p string, t *testing.T) {
helper
.
CreateFileTests
(
t
,
p
,
&
tests
)
for
name
,
test
:=
range
tests
{
if
name
!=
"CallRecursiveBomb"
{
continue
}
state
:=
ethstate
.
New
(
helper
.
NewTrie
())
for
addr
,
account
:=
range
test
.
Pre
{
obj
:=
StateObjectFromAccount
(
addr
,
account
)
...
...
@@ -92,7 +89,6 @@ func TestVMArithmetic(t *testing.T) {
}
func
TestVMSystemOperation
(
t
*
testing
.
T
)
{
helper
.
Logger
.
SetLogLevel
(
5
)
const
fn
=
"../files/vmtests/vmSystemOperationsTest.json"
RunVmTest
(
fn
,
t
)
}
...
...
vm/environment.go
View file @
b5beb1aa
package
vm
import
(
"errors"
"math/big"
"github.com/ethereum/eth-go/ethstate"
...
...
@@ -18,9 +19,28 @@ type Environment interface {
Difficulty
()
*
big
.
Int
BlockHash
()
[]
byte
GasLimit
()
*
big
.
Int
Transfer
(
from
,
to
Account
,
amount
*
big
.
Int
)
error
}
type
Object
interface
{
GetStorage
(
key
*
big
.
Int
)
*
ethutil
.
Value
SetStorage
(
key
*
big
.
Int
,
value
*
ethutil
.
Value
)
}
type
Account
interface
{
SubBalance
(
amount
*
big
.
Int
)
AddBalance
(
amount
*
big
.
Int
)
Balance
()
*
big
.
Int
}
// generic transfer method
func
Transfer
(
from
,
to
Account
,
amount
*
big
.
Int
)
error
{
if
from
.
Balance
()
.
Cmp
(
amount
)
<
0
{
return
errors
.
New
(
"Insufficient balance in account"
)
}
from
.
SubBalance
(
amount
)
to
.
AddBalance
(
amount
)
return
nil
}
vm/execution.go
View file @
b5beb1aa
...
...
@@ -48,17 +48,17 @@ func (self *Execution) exec(code, caddr []byte, caller ClosureRef) (ret []byte,
Value
:
self
.
value
,
})
object
:=
caller
.
Object
()
if
object
.
Balance
.
Cmp
(
self
.
value
)
<
0
{
from
,
to
:=
caller
.
Object
(),
env
.
State
()
.
GetOrNewStateObject
(
self
.
address
)
err
=
env
.
Transfer
(
from
,
to
,
self
.
value
)
if
err
!=
nil
{
caller
.
ReturnGas
(
self
.
Gas
,
self
.
price
)
err
=
fmt
.
Errorf
(
"Insufficient funds to transfer value. Req %v, has %v"
,
self
.
value
,
object
.
Balance
)
err
=
fmt
.
Errorf
(
"Insufficient funds to transfer value. Req %v, has %v"
,
self
.
value
,
from
.
Balance
)
}
else
{
stateObject
:=
env
.
State
()
.
GetOrNewStateObject
(
self
.
address
)
self
.
object
=
stateObject
self
.
object
=
to
caller
.
Object
()
.
SubAmount
(
self
.
value
)
stateObject
.
AddAmount
(
self
.
value
)
//
caller.Object().SubAmount(self.value)
//
stateObject.AddAmount(self.value)
// Pre-compiled contracts (address.go) 1, 2 & 3.
naddr
:=
ethutil
.
BigD
(
caddr
)
.
Uint64
()
...
...
@@ -69,7 +69,7 @@ func (self *Execution) exec(code, caddr []byte, caller ClosureRef) (ret []byte,
}
}
else
{
// Create a new callable closure
c
:=
NewClosure
(
msg
,
caller
,
stateObject
,
code
,
self
.
Gas
,
self
.
price
)
c
:=
NewClosure
(
msg
,
caller
,
to
,
code
,
self
.
Gas
,
self
.
price
)
c
.
exe
=
self
if
self
.
vm
.
Depth
()
==
MaxCallDepth
{
...
...
vm/vm.go
View file @
b5beb1aa
...
...
@@ -692,7 +692,7 @@ func (self *Vm) RunClosure(closure *Closure) (ret []byte, err error) {
receiver
:=
self
.
env
.
State
()
.
GetOrNewStateObject
(
stack
.
Pop
()
.
Bytes
())
receiver
.
AddAmount
(
closure
.
object
.
Balance
)
receiver
.
AddAmount
(
closure
.
object
.
Balance
()
)
closure
.
object
.
MarkForDeletion
()
...
...
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