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
787a61bb
Commit
787a61bb
authored
Jun 17, 2015
by
obscuren
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core/state, core/vm: reworked storage get / set to use common.Hash
parent
5721fcf6
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
45 additions
and
41 deletions
+45
-41
types.go
common/types.go
+4
-0
state_object.go
core/state/state_object.go
+20
-27
statedb.go
core/state/statedb.go
+5
-5
vm.go
core/vm/vm.go
+11
-4
gh_test.go
tests/vm/gh_test.go
+5
-5
No files found.
common/types.go
View file @
787a61bb
...
...
@@ -62,6 +62,10 @@ func (h Hash) Generate(rand *rand.Rand, size int) reflect.Value {
return
reflect
.
ValueOf
(
h
)
}
func
EmptyHash
(
h
Hash
)
bool
{
return
h
==
Hash
{}
}
/////////// Address
func
BytesToAddress
(
b
[]
byte
)
Address
{
var
a
Address
...
...
core/state/state_object.go
View file @
787a61bb
...
...
@@ -19,11 +19,11 @@ func (self Code) String() string {
return
string
(
self
)
//strings.Join(Disassemble(self), " ")
}
type
Storage
map
[
string
]
*
common
.
Value
type
Storage
map
[
string
]
common
.
Hash
func
(
self
Storage
)
String
()
(
str
string
)
{
for
key
,
value
:=
range
self
{
str
+=
fmt
.
Sprintf
(
"%X : %X
\n
"
,
key
,
value
.
Bytes
()
)
str
+=
fmt
.
Sprintf
(
"%X : %X
\n
"
,
key
,
value
)
}
return
...
...
@@ -32,7 +32,6 @@ func (self Storage) String() (str string) {
func
(
self
Storage
)
Copy
()
Storage
{
cpy
:=
make
(
Storage
)
for
key
,
value
:=
range
self
{
// XXX Do we need a 'value' copy or is this sufficient?
cpy
[
key
]
=
value
}
...
...
@@ -112,7 +111,7 @@ func NewStateObjectFromBytes(address common.Address, data []byte, db common.Data
object
.
balance
=
extobject
.
Balance
object
.
codeHash
=
extobject
.
CodeHash
object
.
State
=
New
(
extobject
.
Root
,
db
)
object
.
storage
=
make
(
map
[
string
]
*
common
.
Value
)
object
.
storage
=
make
(
map
[
string
]
common
.
Hash
)
object
.
gasPool
=
new
(
big
.
Int
)
object
.
prepaid
=
new
(
big
.
Int
)
object
.
code
,
_
=
db
.
Get
(
extobject
.
CodeHash
)
...
...
@@ -129,35 +128,29 @@ func (self *StateObject) MarkForDeletion() {
}
}
func
(
c
*
StateObject
)
getAddr
(
addr
common
.
Hash
)
*
common
.
Value
{
return
common
.
NewValueFromBytes
([]
byte
(
c
.
State
.
trie
.
Get
(
addr
[
:
])
))
func
(
c
*
StateObject
)
getAddr
(
addr
common
.
Hash
)
(
ret
common
.
Hash
)
{
return
common
.
BytesToHash
(
common
.
NewValueFromBytes
([]
byte
(
c
.
State
.
trie
.
Get
(
addr
[
:
])))
.
Bytes
(
))
}
func
(
c
*
StateObject
)
setAddr
(
addr
[]
byte
,
value
interface
{})
{
c
.
State
.
trie
.
Update
(
addr
,
common
.
Encode
(
value
))
}
func
(
self
*
StateObject
)
GetStorage
(
key
*
big
.
Int
)
*
common
.
Value
{
fmt
.
Printf
(
"%v: get %v %v"
,
self
.
address
.
Hex
(),
key
)
return
self
.
GetState
(
common
.
BytesToHash
(
key
.
Bytes
()))
}
func
(
self
*
StateObject
)
SetStorage
(
key
*
big
.
Int
,
value
*
common
.
Value
)
{
fmt
.
Printf
(
"%v: set %v -> %v"
,
self
.
address
.
Hex
(),
key
,
value
)
self
.
SetState
(
common
.
BytesToHash
(
key
.
Bytes
()),
value
)
func
(
c
*
StateObject
)
setAddr
(
addr
[]
byte
,
value
common
.
Hash
)
{
v
,
err
:=
rlp
.
EncodeToBytes
(
bytes
.
TrimLeft
(
value
[
:
],
"
\x00
"
))
if
err
!=
nil
{
// if RLPing failed we better panic and not fail silently. This would be considered a consensus issue
panic
(
err
)
}
c
.
State
.
trie
.
Update
(
addr
,
v
)
}
func
(
self
*
StateObject
)
Storage
()
Storage
{
return
self
.
storage
}
func
(
self
*
StateObject
)
GetState
(
key
common
.
Hash
)
*
common
.
Value
{
func
(
self
*
StateObject
)
GetState
(
key
common
.
Hash
)
common
.
Hash
{
strkey
:=
key
.
Str
()
value
:=
self
.
storage
[
strkey
]
if
value
==
nil
{
value
,
exists
:=
self
.
storage
[
strkey
]
if
!
exists
{
value
=
self
.
getAddr
(
key
)
if
!
value
.
IsNil
()
{
if
(
value
!=
common
.
Hash
{})
{
self
.
storage
[
strkey
]
=
value
}
}
...
...
@@ -165,14 +158,14 @@ func (self *StateObject) GetState(key common.Hash) *common.Value {
return
value
}
func
(
self
*
StateObject
)
SetState
(
k
common
.
Hash
,
value
*
common
.
Value
)
{
self
.
storage
[
k
.
Str
()]
=
value
.
Copy
()
func
(
self
*
StateObject
)
SetState
(
k
,
value
common
.
Hash
)
{
self
.
storage
[
k
.
Str
()]
=
value
self
.
dirty
=
true
}
func
(
self
*
StateObject
)
Sync
()
{
for
key
,
value
:=
range
self
.
storage
{
if
value
.
Len
()
==
0
{
if
(
value
==
common
.
Hash
{})
{
self
.
State
.
trie
.
Delete
([]
byte
(
key
))
continue
}
...
...
@@ -370,7 +363,7 @@ func (c *StateObject) RlpDecode(data []byte) {
c
.
nonce
=
decoder
.
Get
(
0
)
.
Uint
()
c
.
balance
=
decoder
.
Get
(
1
)
.
BigInt
()
c
.
State
=
New
(
common
.
BytesToHash
(
decoder
.
Get
(
2
)
.
Bytes
()),
c
.
db
)
//New(trie.New(common.Config.Db, decoder.Get(2).Interface()))
c
.
storage
=
make
(
map
[
string
]
*
common
.
Value
)
c
.
storage
=
make
(
map
[
string
]
common
.
Hash
)
c
.
gasPool
=
new
(
big
.
Int
)
c
.
codeHash
=
decoder
.
Get
(
3
)
.
Bytes
()
...
...
core/state/statedb.go
View file @
787a61bb
...
...
@@ -103,13 +103,13 @@ func (self *StateDB) GetCode(addr common.Address) []byte {
return
nil
}
func
(
self
*
StateDB
)
GetState
(
a
common
.
Address
,
b
common
.
Hash
)
[]
byte
{
func
(
self
*
StateDB
)
GetState
(
a
common
.
Address
,
b
common
.
Hash
)
common
.
Hash
{
stateObject
:=
self
.
GetStateObject
(
a
)
if
stateObject
!=
nil
{
return
stateObject
.
GetState
(
b
)
.
Bytes
()
return
stateObject
.
GetState
(
b
)
}
return
nil
return
common
.
Hash
{}
}
func
(
self
*
StateDB
)
IsDeleted
(
addr
common
.
Address
)
bool
{
...
...
@@ -145,10 +145,10 @@ func (self *StateDB) SetCode(addr common.Address, code []byte) {
}
}
func
(
self
*
StateDB
)
SetState
(
addr
common
.
Address
,
key
common
.
Hash
,
value
interface
{}
)
{
func
(
self
*
StateDB
)
SetState
(
addr
common
.
Address
,
key
common
.
Hash
,
value
common
.
Hash
)
{
stateObject
:=
self
.
GetOrNewStateObject
(
addr
)
if
stateObject
!=
nil
{
stateObject
.
SetState
(
key
,
common
.
NewValue
(
value
)
)
stateObject
.
SetState
(
key
,
value
)
}
}
...
...
core/vm/vm.go
View file @
787a61bb
...
...
@@ -506,14 +506,14 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) {
case
SLOAD
:
loc
:=
common
.
BigToHash
(
stack
.
pop
())
val
:=
common
.
Bytes2Big
(
statedb
.
GetState
(
context
.
Address
(),
loc
)
)
val
:=
statedb
.
GetState
(
context
.
Address
(),
loc
)
.
Big
(
)
stack
.
push
(
val
)
case
SSTORE
:
loc
:=
common
.
BigToHash
(
stack
.
pop
())
val
:=
stack
.
pop
()
statedb
.
SetState
(
context
.
Address
(),
loc
,
val
)
statedb
.
SetState
(
context
.
Address
(),
loc
,
common
.
BigToHash
(
val
)
)
case
JUMP
:
if
err
:=
jump
(
pc
,
stack
.
pop
());
err
!=
nil
{
...
...
@@ -686,10 +686,10 @@ func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCo
var
g
*
big
.
Int
y
,
x
:=
stack
.
data
[
stack
.
len
()
-
2
],
stack
.
data
[
stack
.
len
()
-
1
]
val
:=
statedb
.
GetState
(
context
.
Address
(),
common
.
BigToHash
(
x
))
if
len
(
val
)
==
0
&&
len
(
y
.
Bytes
())
>
0
{
if
common
.
EmptyHash
(
val
)
&&
!
common
.
EmptyHash
(
common
.
BigToHash
(
y
))
{
// 0 => non 0
g
=
params
.
SstoreSetGas
}
else
if
len
(
val
)
>
0
&&
len
(
y
.
Bytes
())
==
0
{
}
else
if
!
common
.
EmptyHash
(
val
)
&&
common
.
EmptyHash
(
common
.
BigToHash
(
y
))
{
statedb
.
Refund
(
params
.
SstoreRefundGas
)
g
=
params
.
SstoreClearGas
...
...
@@ -697,6 +697,13 @@ func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCo
// non 0 => non 0 (or 0 => 0)
g
=
params
.
SstoreClearGas
}
/*
if len(val) == 0 && len(y.Bytes()) > 0 {
} else if len(val) > 0 && len(y.Bytes()) == 0 {
} else {
}
*/
gas
.
Set
(
g
)
case
SUICIDE
:
if
!
statedb
.
IsDeleted
(
context
.
Address
())
{
...
...
tests/vm/gh_test.go
View file @
787a61bb
...
...
@@ -97,7 +97,7 @@ func RunVmTest(p string, t *testing.T) {
obj
:=
StateObjectFromAccount
(
db
,
addr
,
account
)
statedb
.
SetStateObject
(
obj
)
for
a
,
v
:=
range
account
.
Storage
{
obj
.
SetState
(
common
.
HexToHash
(
a
),
common
.
NewValue
(
helper
.
FromHex
(
v
)
))
obj
.
SetState
(
common
.
HexToHash
(
a
),
common
.
HexToHash
(
v
))
}
}
...
...
@@ -168,11 +168,11 @@ func RunVmTest(p string, t *testing.T) {
}
for
addr
,
value
:=
range
account
.
Storage
{
v
:=
obj
.
GetState
(
common
.
HexToHash
(
addr
))
.
Bytes
()
vexp
:=
helper
.
FromHex
(
value
)
v
:=
obj
.
GetState
(
common
.
HexToHash
(
addr
))
vexp
:=
common
.
HexToHash
(
value
)
if
bytes
.
Compare
(
v
,
vexp
)
!=
0
{
t
.
Errorf
(
"%s's : (%x: %s) storage failed. Expected %x, got %x (%v %v)
\n
"
,
name
,
obj
.
Address
()
.
Bytes
()[
0
:
4
],
addr
,
vexp
,
v
,
common
.
BigD
(
vexp
),
common
.
BigD
(
v
))
if
v
!=
vexp
{
t
.
Errorf
(
"%s's : (%x: %s) storage failed. Expected %x, got %x (%v %v)
\n
"
,
name
,
obj
.
Address
()
.
Bytes
()[
0
:
4
],
addr
,
vexp
,
v
,
vexp
.
Big
(),
v
.
Big
(
))
}
}
}
...
...
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