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
5b5dfba7
Unverified
Commit
5b5dfba7
authored
Jul 14, 2022
by
Jens W
Committed by
GitHub
Jul 14, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
accounts/abi/bind/backends: return hash of new blocks (#25163)
Co-authored-by:
Jens
<
jmw.1906@gmx.de
>
parent
93f981bb
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
1 deletion
+44
-1
simulated.go
accounts/abi/bind/backends/simulated.go
+5
-1
simulated_test.go
accounts/abi/bind/backends/simulated_test.go
+39
-0
No files found.
accounts/abi/bind/backends/simulated.go
View file @
5b5dfba7
...
@@ -106,16 +106,20 @@ func (b *SimulatedBackend) Close() error {
...
@@ -106,16 +106,20 @@ func (b *SimulatedBackend) Close() error {
// Commit imports all the pending transactions as a single block and starts a
// Commit imports all the pending transactions as a single block and starts a
// fresh new state.
// fresh new state.
func
(
b
*
SimulatedBackend
)
Commit
()
{
func
(
b
*
SimulatedBackend
)
Commit
()
common
.
Hash
{
b
.
mu
.
Lock
()
b
.
mu
.
Lock
()
defer
b
.
mu
.
Unlock
()
defer
b
.
mu
.
Unlock
()
if
_
,
err
:=
b
.
blockchain
.
InsertChain
([]
*
types
.
Block
{
b
.
pendingBlock
});
err
!=
nil
{
if
_
,
err
:=
b
.
blockchain
.
InsertChain
([]
*
types
.
Block
{
b
.
pendingBlock
});
err
!=
nil
{
panic
(
err
)
// This cannot happen unless the simulator is wrong, fail in that case
panic
(
err
)
// This cannot happen unless the simulator is wrong, fail in that case
}
}
blockHash
:=
b
.
pendingBlock
.
Hash
()
// Using the last inserted block here makes it possible to build on a side
// Using the last inserted block here makes it possible to build on a side
// chain after a fork.
// chain after a fork.
b
.
rollback
(
b
.
pendingBlock
)
b
.
rollback
(
b
.
pendingBlock
)
return
blockHash
}
}
// Rollback aborts all pending transactions, reverting to the last committed state.
// Rollback aborts all pending transactions, reverting to the last committed state.
...
...
accounts/abi/bind/backends/simulated_test.go
View file @
5b5dfba7
...
@@ -1335,3 +1335,42 @@ func TestForkResendTx(t *testing.T) {
...
@@ -1335,3 +1335,42 @@ func TestForkResendTx(t *testing.T) {
t
.
Errorf
(
"TX included in wrong block: %d"
,
h
)
t
.
Errorf
(
"TX included in wrong block: %d"
,
h
)
}
}
}
}
func
TestCommitReturnValue
(
t
*
testing
.
T
)
{
testAddr
:=
crypto
.
PubkeyToAddress
(
testKey
.
PublicKey
)
sim
:=
simTestBackend
(
testAddr
)
defer
sim
.
Close
()
startBlockHeight
:=
sim
.
blockchain
.
CurrentBlock
()
.
NumberU64
()
// Test if Commit returns the correct block hash
h1
:=
sim
.
Commit
()
if
h1
!=
sim
.
blockchain
.
CurrentBlock
()
.
Hash
()
{
t
.
Error
(
"Commit did not return the hash of the last block."
)
}
// Create a block in the original chain (containing a transaction to force different block hashes)
head
,
_
:=
sim
.
HeaderByNumber
(
context
.
Background
(),
nil
)
// Should be child's, good enough
gasPrice
:=
new
(
big
.
Int
)
.
Add
(
head
.
BaseFee
,
big
.
NewInt
(
1
))
_tx
:=
types
.
NewTransaction
(
0
,
testAddr
,
big
.
NewInt
(
1000
),
params
.
TxGas
,
gasPrice
,
nil
)
tx
,
_
:=
types
.
SignTx
(
_tx
,
types
.
HomesteadSigner
{},
testKey
)
sim
.
SendTransaction
(
context
.
Background
(),
tx
)
h2
:=
sim
.
Commit
()
// Create another block in the original chain
sim
.
Commit
()
// Fork at the first bock
if
err
:=
sim
.
Fork
(
context
.
Background
(),
h1
);
err
!=
nil
{
t
.
Errorf
(
"forking: %v"
,
err
)
}
// Test if Commit returns the correct block hash after the reorg
h2fork
:=
sim
.
Commit
()
if
h2
==
h2fork
{
t
.
Error
(
"The block in the fork and the original block are the same block!"
)
}
if
sim
.
blockchain
.
GetHeader
(
h2fork
,
startBlockHeight
+
2
)
==
nil
{
t
.
Error
(
"Could not retrieve the just created block (side-chain)"
)
}
}
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