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
5dea0f2a
Unverified
Commit
5dea0f2a
authored
Nov 24, 2017
by
Péter Szilágyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core/state: copy trie too, not just content
parent
9ff9d04a
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
1 deletion
+52
-1
statedb.go
core/state/statedb.go
+1
-1
statedb_test.go
core/state/statedb_test.go
+51
-0
No files found.
core/state/statedb.go
View file @
5dea0f2a
...
@@ -453,7 +453,7 @@ func (self *StateDB) Copy() *StateDB {
...
@@ -453,7 +453,7 @@ func (self *StateDB) Copy() *StateDB {
// Copy all the basic fields, initialize the memory ones
// Copy all the basic fields, initialize the memory ones
state
:=
&
StateDB
{
state
:=
&
StateDB
{
db
:
self
.
db
,
db
:
self
.
db
,
trie
:
self
.
trie
,
trie
:
self
.
db
.
CopyTrie
(
self
.
trie
)
,
stateObjects
:
make
(
map
[
common
.
Address
]
*
stateObject
,
len
(
self
.
stateObjectsDirty
)),
stateObjects
:
make
(
map
[
common
.
Address
]
*
stateObject
,
len
(
self
.
stateObjectsDirty
)),
stateObjectsDirty
:
make
(
map
[
common
.
Address
]
struct
{},
len
(
self
.
stateObjectsDirty
)),
stateObjectsDirty
:
make
(
map
[
common
.
Address
]
struct
{},
len
(
self
.
stateObjectsDirty
)),
refund
:
new
(
big
.
Int
)
.
Set
(
self
.
refund
),
refund
:
new
(
big
.
Int
)
.
Set
(
self
.
refund
),
...
...
core/state/statedb_test.go
View file @
5dea0f2a
...
@@ -117,6 +117,57 @@ func TestIntermediateLeaks(t *testing.T) {
...
@@ -117,6 +117,57 @@ func TestIntermediateLeaks(t *testing.T) {
}
}
}
}
// TestCopy tests that copying a statedb object indeed makes the original and
// the copy independent of each other. This test is a regression test against
// https://github.com/ethereum/go-ethereum/pull/15549.
func
TestCopy
(
t
*
testing
.
T
)
{
// Create a random state test to copy and modify "independently"
mem
,
_
:=
ethdb
.
NewMemDatabase
()
orig
,
_
:=
New
(
common
.
Hash
{},
NewDatabase
(
mem
))
for
i
:=
byte
(
0
);
i
<
255
;
i
++
{
obj
:=
orig
.
GetOrNewStateObject
(
common
.
BytesToAddress
([]
byte
{
i
}))
obj
.
AddBalance
(
big
.
NewInt
(
int64
(
i
)))
orig
.
updateStateObject
(
obj
)
}
orig
.
Finalise
(
false
)
// Copy the state, modify both in-memory
copy
:=
orig
.
Copy
()
for
i
:=
byte
(
0
);
i
<
255
;
i
++
{
origObj
:=
orig
.
GetOrNewStateObject
(
common
.
BytesToAddress
([]
byte
{
i
}))
copyObj
:=
copy
.
GetOrNewStateObject
(
common
.
BytesToAddress
([]
byte
{
i
}))
origObj
.
AddBalance
(
big
.
NewInt
(
2
*
int64
(
i
)))
copyObj
.
AddBalance
(
big
.
NewInt
(
3
*
int64
(
i
)))
orig
.
updateStateObject
(
origObj
)
copy
.
updateStateObject
(
copyObj
)
}
// Finalise the changes on both concurrently
done
:=
make
(
chan
struct
{})
go
func
()
{
orig
.
Finalise
(
true
)
close
(
done
)
}()
copy
.
Finalise
(
true
)
<-
done
// Verify that the two states have been updated independently
for
i
:=
byte
(
0
);
i
<
255
;
i
++
{
origObj
:=
orig
.
GetOrNewStateObject
(
common
.
BytesToAddress
([]
byte
{
i
}))
copyObj
:=
copy
.
GetOrNewStateObject
(
common
.
BytesToAddress
([]
byte
{
i
}))
if
want
:=
big
.
NewInt
(
3
*
int64
(
i
));
origObj
.
Balance
()
.
Cmp
(
want
)
!=
0
{
t
.
Errorf
(
"orig obj %d: balance mismatch: have %v, want %v"
,
i
,
origObj
.
Balance
(),
want
)
}
if
want
:=
big
.
NewInt
(
4
*
int64
(
i
));
copyObj
.
Balance
()
.
Cmp
(
want
)
!=
0
{
t
.
Errorf
(
"copy obj %d: balance mismatch: have %v, want %v"
,
i
,
copyObj
.
Balance
(),
want
)
}
}
}
func
TestSnapshotRandom
(
t
*
testing
.
T
)
{
func
TestSnapshotRandom
(
t
*
testing
.
T
)
{
config
:=
&
quick
.
Config
{
MaxCount
:
1000
}
config
:=
&
quick
.
Config
{
MaxCount
:
1000
}
err
:=
quick
.
Check
((
*
snapshotTest
)
.
run
,
config
)
err
:=
quick
.
Check
((
*
snapshotTest
)
.
run
,
config
)
...
...
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