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
bc6031e7
Commit
bc6031e7
authored
Jun 09, 2015
by
obscuren
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core, xeth: moved nonce management burden from xeth to txpool
parent
93f48528
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
44 additions
and
23 deletions
+44
-23
managed_state.go
core/state/managed_state.go
+1
-1
transaction_pool.go
core/transaction_pool.go
+15
-10
transaction_pool_test.go
core/transaction_pool_test.go
+27
-11
xeth.go
xeth/xeth.go
+1
-1
No files found.
core/state/managed_state.go
View file @
bc6031e7
...
@@ -23,7 +23,7 @@ type ManagedState struct {
...
@@ -23,7 +23,7 @@ type ManagedState struct {
// ManagedState returns a new managed state with the statedb as it's backing layer
// ManagedState returns a new managed state with the statedb as it's backing layer
func
ManageState
(
statedb
*
StateDB
)
*
ManagedState
{
func
ManageState
(
statedb
*
StateDB
)
*
ManagedState
{
return
&
ManagedState
{
return
&
ManagedState
{
StateDB
:
statedb
,
StateDB
:
statedb
.
Copy
()
,
accounts
:
make
(
map
[
string
]
*
account
),
accounts
:
make
(
map
[
string
]
*
account
),
}
}
}
}
...
...
core/transaction_pool.go
View file @
bc6031e7
...
@@ -39,7 +39,7 @@ type stateFn func() *state.StateDB
...
@@ -39,7 +39,7 @@ type stateFn func() *state.StateDB
type
TxPool
struct
{
type
TxPool
struct
{
quit
chan
bool
// Quiting channel
quit
chan
bool
// Quiting channel
currentState
stateFn
// The state function which will allow us to do some pre checkes
currentState
stateFn
// The state function which will allow us to do some pre checkes
state
*
state
.
ManagedState
pendingState
*
state
.
ManagedState
gasLimit
func
()
*
big
.
Int
// The current gas limit function callback
gasLimit
func
()
*
big
.
Int
// The current gas limit function callback
eventMux
*
event
.
TypeMux
eventMux
*
event
.
TypeMux
events
event
.
Subscription
events
event
.
Subscription
...
@@ -57,7 +57,7 @@ func NewTxPool(eventMux *event.TypeMux, currentStateFn stateFn, gasLimitFn func(
...
@@ -57,7 +57,7 @@ func NewTxPool(eventMux *event.TypeMux, currentStateFn stateFn, gasLimitFn func(
eventMux
:
eventMux
,
eventMux
:
eventMux
,
currentState
:
currentStateFn
,
currentState
:
currentStateFn
,
gasLimit
:
gasLimitFn
,
gasLimit
:
gasLimitFn
,
state
:
state
.
ManageState
(
currentStateFn
()),
pendingState
:
state
.
ManageState
(
currentStateFn
()),
}
}
}
}
...
@@ -76,7 +76,7 @@ func (pool *TxPool) Start() {
...
@@ -76,7 +76,7 @@ func (pool *TxPool) Start() {
}
}
func
(
pool
*
TxPool
)
resetState
()
{
func
(
pool
*
TxPool
)
resetState
()
{
pool
.
s
tate
=
state
.
ManageState
(
pool
.
currentState
())
pool
.
pendingS
tate
=
state
.
ManageState
(
pool
.
currentState
())
// validate the pool of pending transactions, this will remove
// validate the pool of pending transactions, this will remove
// any transactions that have been included in the block or
// any transactions that have been included in the block or
...
@@ -90,7 +90,7 @@ func (pool *TxPool) resetState() {
...
@@ -90,7 +90,7 @@ func (pool *TxPool) resetState() {
if
addr
,
err
:=
tx
.
From
();
err
==
nil
{
if
addr
,
err
:=
tx
.
From
();
err
==
nil
{
// Set the nonce. Transaction nonce can never be lower
// Set the nonce. Transaction nonce can never be lower
// than the state nonce; validatePool took care of that.
// than the state nonce; validatePool took care of that.
pool
.
s
tate
.
SetNonce
(
addr
,
tx
.
Nonce
())
pool
.
pendingS
tate
.
SetNonce
(
addr
,
tx
.
Nonce
())
}
}
}
}
...
@@ -110,7 +110,7 @@ func (pool *TxPool) State() *state.ManagedState {
...
@@ -110,7 +110,7 @@ func (pool *TxPool) State() *state.ManagedState {
pool
.
mu
.
RLock
()
pool
.
mu
.
RLock
()
defer
pool
.
mu
.
RUnlock
()
defer
pool
.
mu
.
RUnlock
()
return
pool
.
s
tate
return
pool
.
pendingS
tate
}
}
// validateTx checks whether a transaction is valid according
// validateTx checks whether a transaction is valid according
...
@@ -302,7 +302,9 @@ func (pool *TxPool) addTx(hash common.Hash, addr common.Address, tx *types.Trans
...
@@ -302,7 +302,9 @@ func (pool *TxPool) addTx(hash common.Hash, addr common.Address, tx *types.Trans
if
_
,
ok
:=
pool
.
pending
[
hash
];
!
ok
{
if
_
,
ok
:=
pool
.
pending
[
hash
];
!
ok
{
pool
.
pending
[
hash
]
=
tx
pool
.
pending
[
hash
]
=
tx
pool
.
state
.
SetNonce
(
addr
,
tx
.
AccountNonce
)
// Increment the nonce on the pending state. This can only happen if
// the nonce is +1 to the previous one.
pool
.
pendingState
.
SetNonce
(
addr
,
tx
.
AccountNonce
+
1
)
// Notify the subscribers. This event is posted in a goroutine
// Notify the subscribers. This event is posted in a goroutine
// because it's possible that somewhere during the post "Remove transaction"
// because it's possible that somewhere during the post "Remove transaction"
// gets called which will then wait for the global tx pool lock and deadlock.
// gets called which will then wait for the global tx pool lock and deadlock.
...
@@ -312,14 +314,17 @@ func (pool *TxPool) addTx(hash common.Hash, addr common.Address, tx *types.Trans
...
@@ -312,14 +314,17 @@ func (pool *TxPool) addTx(hash common.Hash, addr common.Address, tx *types.Trans
// checkQueue moves transactions that have become processable to main pool.
// checkQueue moves transactions that have become processable to main pool.
func
(
pool
*
TxPool
)
checkQueue
()
{
func
(
pool
*
TxPool
)
checkQueue
()
{
state
:=
pool
.
s
tate
state
:=
pool
.
pendingS
tate
var
addq
txQueue
var
addq
txQueue
for
address
,
txs
:=
range
pool
.
queue
{
for
address
,
txs
:=
range
pool
.
queue
{
curnonce
:=
state
.
GetNonce
(
address
)
// guessed nonce is the nonce currently kept by the tx pool (pending state)
guessedNonce
:=
state
.
GetNonce
(
address
)
// true nonce is the nonce known by the last state
trueNonce
:=
pool
.
currentState
()
.
GetNonce
(
address
)
addq
:=
addq
[
:
0
]
addq
:=
addq
[
:
0
]
for
hash
,
tx
:=
range
txs
{
for
hash
,
tx
:=
range
txs
{
if
tx
.
AccountNonce
<
curn
once
{
if
tx
.
AccountNonce
<
trueN
once
{
// Drop queued transactions whose nonce is lower than
// Drop queued transactions whose nonce is lower than
// the account nonce because they have been processed.
// the account nonce because they have been processed.
delete
(
txs
,
hash
)
delete
(
txs
,
hash
)
...
@@ -332,7 +337,7 @@ func (pool *TxPool) checkQueue() {
...
@@ -332,7 +337,7 @@ func (pool *TxPool) checkQueue() {
// current account nonce.
// current account nonce.
sort
.
Sort
(
addq
)
sort
.
Sort
(
addq
)
for
_
,
e
:=
range
addq
{
for
_
,
e
:=
range
addq
{
if
e
.
AccountNonce
>
curn
once
{
if
e
.
AccountNonce
>
guessedN
once
{
break
break
}
}
delete
(
txs
,
e
.
hash
)
delete
(
txs
,
e
.
hash
)
...
...
core/transaction_pool_test.go
View file @
bc6031e7
...
@@ -37,21 +37,21 @@ func TestInvalidTransactions(t *testing.T) {
...
@@ -37,21 +37,21 @@ func TestInvalidTransactions(t *testing.T) {
}
}
from
,
_
:=
tx
.
From
()
from
,
_
:=
tx
.
From
()
pool
.
state
.
AddBalance
(
from
,
big
.
NewInt
(
1
))
pool
.
currentState
()
.
AddBalance
(
from
,
big
.
NewInt
(
1
))
err
=
pool
.
Add
(
tx
)
err
=
pool
.
Add
(
tx
)
if
err
!=
ErrInsufficientFunds
{
if
err
!=
ErrInsufficientFunds
{
t
.
Error
(
"expected"
,
ErrInsufficientFunds
)
t
.
Error
(
"expected"
,
ErrInsufficientFunds
)
}
}
balance
:=
new
(
big
.
Int
)
.
Add
(
tx
.
Value
(),
new
(
big
.
Int
)
.
Mul
(
tx
.
Gas
(),
tx
.
GasPrice
()))
balance
:=
new
(
big
.
Int
)
.
Add
(
tx
.
Value
(),
new
(
big
.
Int
)
.
Mul
(
tx
.
Gas
(),
tx
.
GasPrice
()))
pool
.
state
.
AddBalance
(
from
,
balance
)
pool
.
currentState
()
.
AddBalance
(
from
,
balance
)
err
=
pool
.
Add
(
tx
)
err
=
pool
.
Add
(
tx
)
if
err
!=
ErrIntrinsicGas
{
if
err
!=
ErrIntrinsicGas
{
t
.
Error
(
"expected"
,
ErrIntrinsicGas
,
"got"
,
err
)
t
.
Error
(
"expected"
,
ErrIntrinsicGas
,
"got"
,
err
)
}
}
pool
.
state
.
SetNonce
(
from
,
1
)
pool
.
currentState
()
.
SetNonce
(
from
,
1
)
pool
.
state
.
AddBalance
(
from
,
big
.
NewInt
(
0xffffffffffffff
))
pool
.
currentState
()
.
AddBalance
(
from
,
big
.
NewInt
(
0xffffffffffffff
))
tx
.
GasLimit
=
big
.
NewInt
(
100000
)
tx
.
GasLimit
=
big
.
NewInt
(
100000
)
tx
.
Price
=
big
.
NewInt
(
1
)
tx
.
Price
=
big
.
NewInt
(
1
)
tx
.
SignECDSA
(
key
)
tx
.
SignECDSA
(
key
)
...
@@ -67,7 +67,7 @@ func TestTransactionQueue(t *testing.T) {
...
@@ -67,7 +67,7 @@ func TestTransactionQueue(t *testing.T) {
tx
:=
transaction
()
tx
:=
transaction
()
tx
.
SignECDSA
(
key
)
tx
.
SignECDSA
(
key
)
from
,
_
:=
tx
.
From
()
from
,
_
:=
tx
.
From
()
pool
.
state
.
AddBalance
(
from
,
big
.
NewInt
(
1
))
pool
.
currentState
()
.
AddBalance
(
from
,
big
.
NewInt
(
1
))
pool
.
queueTx
(
tx
.
Hash
(),
tx
)
pool
.
queueTx
(
tx
.
Hash
(),
tx
)
pool
.
checkQueue
()
pool
.
checkQueue
()
...
@@ -79,7 +79,7 @@ func TestTransactionQueue(t *testing.T) {
...
@@ -79,7 +79,7 @@ func TestTransactionQueue(t *testing.T) {
tx
.
SetNonce
(
1
)
tx
.
SetNonce
(
1
)
tx
.
SignECDSA
(
key
)
tx
.
SignECDSA
(
key
)
from
,
_
=
tx
.
From
()
from
,
_
=
tx
.
From
()
pool
.
state
.
SetNonce
(
from
,
2
)
pool
.
currentState
()
.
SetNonce
(
from
,
2
)
pool
.
queueTx
(
tx
.
Hash
(),
tx
)
pool
.
queueTx
(
tx
.
Hash
(),
tx
)
pool
.
checkQueue
()
pool
.
checkQueue
()
if
_
,
ok
:=
pool
.
pending
[
tx
.
Hash
()];
ok
{
if
_
,
ok
:=
pool
.
pending
[
tx
.
Hash
()];
ok
{
...
@@ -117,7 +117,7 @@ func TestRemoveTx(t *testing.T) {
...
@@ -117,7 +117,7 @@ func TestRemoveTx(t *testing.T) {
tx
:=
transaction
()
tx
:=
transaction
()
tx
.
SignECDSA
(
key
)
tx
.
SignECDSA
(
key
)
from
,
_
:=
tx
.
From
()
from
,
_
:=
tx
.
From
()
pool
.
state
.
AddBalance
(
from
,
big
.
NewInt
(
1
))
pool
.
currentState
()
.
AddBalance
(
from
,
big
.
NewInt
(
1
))
pool
.
queueTx
(
tx
.
Hash
(),
tx
)
pool
.
queueTx
(
tx
.
Hash
(),
tx
)
pool
.
addTx
(
tx
.
Hash
(),
from
,
tx
)
pool
.
addTx
(
tx
.
Hash
(),
from
,
tx
)
if
len
(
pool
.
queue
)
!=
1
{
if
len
(
pool
.
queue
)
!=
1
{
...
@@ -146,7 +146,7 @@ func TestNegativeValue(t *testing.T) {
...
@@ -146,7 +146,7 @@ func TestNegativeValue(t *testing.T) {
tx
.
Value
()
.
Set
(
big
.
NewInt
(
-
1
))
tx
.
Value
()
.
Set
(
big
.
NewInt
(
-
1
))
tx
.
SignECDSA
(
key
)
tx
.
SignECDSA
(
key
)
from
,
_
:=
tx
.
From
()
from
,
_
:=
tx
.
From
()
pool
.
state
.
AddBalance
(
from
,
big
.
NewInt
(
1
))
pool
.
currentState
()
.
AddBalance
(
from
,
big
.
NewInt
(
1
))
err
:=
pool
.
Add
(
tx
)
err
:=
pool
.
Add
(
tx
)
if
err
!=
ErrNegativeValue
{
if
err
!=
ErrNegativeValue
{
t
.
Error
(
"expected"
,
ErrNegativeValue
,
"got"
,
err
)
t
.
Error
(
"expected"
,
ErrNegativeValue
,
"got"
,
err
)
...
@@ -156,7 +156,15 @@ func TestNegativeValue(t *testing.T) {
...
@@ -156,7 +156,15 @@ func TestNegativeValue(t *testing.T) {
func
TestTransactionChainFork
(
t
*
testing
.
T
)
{
func
TestTransactionChainFork
(
t
*
testing
.
T
)
{
pool
,
key
:=
setupTxPool
()
pool
,
key
:=
setupTxPool
()
addr
:=
crypto
.
PubkeyToAddress
(
key
.
PublicKey
)
addr
:=
crypto
.
PubkeyToAddress
(
key
.
PublicKey
)
pool
.
currentState
()
.
AddBalance
(
addr
,
big
.
NewInt
(
100000000000000
))
resetState
:=
func
()
{
db
,
_
:=
ethdb
.
NewMemDatabase
()
statedb
:=
state
.
New
(
common
.
Hash
{},
db
)
pool
.
currentState
=
func
()
*
state
.
StateDB
{
return
statedb
}
pool
.
currentState
()
.
AddBalance
(
addr
,
big
.
NewInt
(
100000000000000
))
pool
.
resetState
()
}
resetState
()
tx
:=
transaction
()
tx
:=
transaction
()
tx
.
GasLimit
=
big
.
NewInt
(
100000
)
tx
.
GasLimit
=
big
.
NewInt
(
100000
)
tx
.
SignECDSA
(
key
)
tx
.
SignECDSA
(
key
)
...
@@ -168,7 +176,7 @@ func TestTransactionChainFork(t *testing.T) {
...
@@ -168,7 +176,7 @@ func TestTransactionChainFork(t *testing.T) {
pool
.
RemoveTransactions
([]
*
types
.
Transaction
{
tx
})
pool
.
RemoveTransactions
([]
*
types
.
Transaction
{
tx
})
// reset the pool's internal state
// reset the pool's internal state
pool
.
resetState
()
resetState
()
err
=
pool
.
add
(
tx
)
err
=
pool
.
add
(
tx
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Error
(
"didn't expect error"
,
err
)
t
.
Error
(
"didn't expect error"
,
err
)
...
@@ -178,7 +186,15 @@ func TestTransactionChainFork(t *testing.T) {
...
@@ -178,7 +186,15 @@ func TestTransactionChainFork(t *testing.T) {
func
TestTransactionDoubleNonce
(
t
*
testing
.
T
)
{
func
TestTransactionDoubleNonce
(
t
*
testing
.
T
)
{
pool
,
key
:=
setupTxPool
()
pool
,
key
:=
setupTxPool
()
addr
:=
crypto
.
PubkeyToAddress
(
key
.
PublicKey
)
addr
:=
crypto
.
PubkeyToAddress
(
key
.
PublicKey
)
pool
.
currentState
()
.
AddBalance
(
addr
,
big
.
NewInt
(
100000000000000
))
resetState
:=
func
()
{
db
,
_
:=
ethdb
.
NewMemDatabase
()
statedb
:=
state
.
New
(
common
.
Hash
{},
db
)
pool
.
currentState
=
func
()
*
state
.
StateDB
{
return
statedb
}
pool
.
currentState
()
.
AddBalance
(
addr
,
big
.
NewInt
(
100000000000000
))
pool
.
resetState
()
}
resetState
()
tx
:=
transaction
()
tx
:=
transaction
()
tx
.
GasLimit
=
big
.
NewInt
(
100000
)
tx
.
GasLimit
=
big
.
NewInt
(
100000
)
tx
.
SignECDSA
(
key
)
tx
.
SignECDSA
(
key
)
...
...
xeth/xeth.go
View file @
bc6031e7
...
@@ -957,7 +957,7 @@ func (self *XEth) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceS
...
@@ -957,7 +957,7 @@ func (self *XEth) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceS
if
err
:=
self
.
backend
.
TxPool
()
.
Add
(
tx
);
err
!=
nil
{
if
err
:=
self
.
backend
.
TxPool
()
.
Add
(
tx
);
err
!=
nil
{
return
""
,
err
return
""
,
err
}
}
state
.
SetNonce
(
from
,
nonce
+
1
)
//
state.SetNonce(from, nonce+1)
if
contractCreation
{
if
contractCreation
{
addr
:=
core
.
AddressFromMessage
(
tx
)
addr
:=
core
.
AddressFromMessage
(
tx
)
...
...
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