good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
bor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open
bor
Commits
4d3209ad
Commit
4d3209ad
authored
Jun 11, 2014
by
Jeffrey Wilcke
Browse files
Options
Downloads
Patches
Plain Diff
Moved process transaction to state manager
* Buy gas of the coinbase address
parent
9ff97a98
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
ethchain/state_manager.go
+81
-12
81 additions, 12 deletions
ethchain/state_manager.go
ethchain/transaction_pool.go
+19
-10
19 additions, 10 deletions
ethchain/transaction_pool.go
with
100 additions
and
22 deletions
ethchain/state_manager.go
+
81
−
12
View file @
4d3209ad
...
@@ -108,15 +108,90 @@ func (sm *StateManager) MakeStateObject(state *State, tx *Transaction) *StateObj
...
@@ -108,15 +108,90 @@ func (sm *StateManager) MakeStateObject(state *State, tx *Transaction) *StateObj
return
nil
return
nil
}
}
func
(
self
*
StateManager
)
ProcessTransaction
(
tx
*
Transaction
,
coinbase
*
StateObject
,
state
*
State
,
toContract
bool
)
(
gas
*
big
.
Int
,
err
error
)
{
fmt
.
Printf
(
"state root before update %x
\n
"
,
state
.
Root
())
defer
func
()
{
if
r
:=
recover
();
r
!=
nil
{
ethutil
.
Config
.
Log
.
Infoln
(
r
)
err
=
fmt
.
Errorf
(
"%v"
,
r
)
}
}()
gas
=
new
(
big
.
Int
)
addGas
:=
func
(
g
*
big
.
Int
)
{
gas
.
Add
(
gas
,
g
)
}
addGas
(
GasTx
)
// Get the sender
sender
:=
state
.
GetAccount
(
tx
.
Sender
())
if
sender
.
Nonce
!=
tx
.
Nonce
{
err
=
NonceError
(
tx
.
Nonce
,
sender
.
Nonce
)
return
}
sender
.
Nonce
+=
1
defer
func
()
{
//state.UpdateStateObject(sender)
// Notify all subscribers
self
.
Ethereum
.
Reactor
()
.
Post
(
"newTx:post"
,
tx
)
}()
txTotalBytes
:=
big
.
NewInt
(
int64
(
len
(
tx
.
Data
)))
txTotalBytes
.
Div
(
txTotalBytes
,
ethutil
.
Big32
)
addGas
(
new
(
big
.
Int
)
.
Mul
(
txTotalBytes
,
GasSStore
))
rGas
:=
new
(
big
.
Int
)
.
Set
(
gas
)
rGas
.
Mul
(
gas
,
tx
.
GasPrice
)
// Make sure there's enough in the sender's account. Having insufficient
// funds won't invalidate this transaction but simple ignores it.
totAmount
:=
new
(
big
.
Int
)
.
Add
(
tx
.
Value
,
rGas
)
if
sender
.
Amount
.
Cmp
(
totAmount
)
<
0
{
state
.
UpdateStateObject
(
sender
)
err
=
fmt
.
Errorf
(
"[TXPL] Insufficient amount in sender's (%x) account"
,
tx
.
Sender
())
return
}
coinbase
.
BuyGas
(
gas
,
tx
.
GasPrice
)
state
.
UpdateStateObject
(
coinbase
)
// Get the receiver
receiver
:=
state
.
GetAccount
(
tx
.
Recipient
)
// Send Tx to self
if
bytes
.
Compare
(
tx
.
Recipient
,
tx
.
Sender
())
==
0
{
// Subtract the fee
sender
.
SubAmount
(
rGas
)
}
else
{
// Subtract the amount from the senders account
sender
.
SubAmount
(
totAmount
)
fmt
.
Printf
(
"state root after sender update %x
\n
"
,
state
.
Root
())
// Add the amount to receivers account which should conclude this transaction
receiver
.
AddAmount
(
tx
.
Value
)
state
.
UpdateStateObject
(
receiver
)
fmt
.
Printf
(
"state root after receiver update %x
\n
"
,
state
.
Root
())
}
state
.
UpdateStateObject
(
sender
)
ethutil
.
Config
.
Log
.
Infof
(
"[TXPL] Processed Tx %x
\n
"
,
tx
.
Hash
())
return
}
// Apply transactions uses the transaction passed to it and applies them onto
// Apply transactions uses the transaction passed to it and applies them onto
// the current processing state.
// the current processing state.
func
(
sm
*
StateManager
)
ApplyTransactions
(
state
*
State
,
block
*
Block
,
txs
[]
*
Transaction
)
([]
*
Receipt
,
[]
*
Transaction
)
{
func
(
sm
*
StateManager
)
ApplyTransactions
(
coinbase
[]
byte
,
state
*
State
,
block
*
Block
,
txs
[]
*
Transaction
)
([]
*
Receipt
,
[]
*
Transaction
)
{
// Process each transaction/contract
// Process each transaction/contract
var
receipts
[]
*
Receipt
var
receipts
[]
*
Receipt
var
validTxs
[]
*
Transaction
var
validTxs
[]
*
Transaction
totalUsedGas
:=
big
.
NewInt
(
0
)
totalUsedGas
:=
big
.
NewInt
(
0
)
for
_
,
tx
:=
range
txs
{
for
_
,
tx
:=
range
txs
{
usedGas
,
err
:=
sm
.
ApplyTransaction
(
state
,
block
,
tx
)
usedGas
,
err
:=
sm
.
ApplyTransaction
(
coinbase
,
state
,
block
,
tx
)
if
err
!=
nil
{
if
err
!=
nil
{
if
IsNonceErr
(
err
)
{
if
IsNonceErr
(
err
)
{
continue
continue
...
@@ -135,7 +210,7 @@ func (sm *StateManager) ApplyTransactions(state *State, block *Block, txs []*Tra
...
@@ -135,7 +210,7 @@ func (sm *StateManager) ApplyTransactions(state *State, block *Block, txs []*Tra
return
receipts
,
validTxs
return
receipts
,
validTxs
}
}
func
(
sm
*
StateManager
)
ApplyTransaction
(
state
*
State
,
block
*
Block
,
tx
*
Transaction
)
(
totalGasUsed
*
big
.
Int
,
err
error
)
{
func
(
sm
*
StateManager
)
ApplyTransaction
(
coinbase
[]
byte
,
state
*
State
,
block
*
Block
,
tx
*
Transaction
)
(
totalGasUsed
*
big
.
Int
,
err
error
)
{
/*
/*
Applies transactions to the given state and creates new
Applies transactions to the given state and creates new
state objects where needed.
state objects where needed.
...
@@ -152,8 +227,9 @@ func (sm *StateManager) ApplyTransaction(state *State, block *Block, tx *Transac
...
@@ -152,8 +227,9 @@ func (sm *StateManager) ApplyTransaction(state *State, block *Block, tx *Transac
)
)
totalGasUsed
=
big
.
NewInt
(
0
)
totalGasUsed
=
big
.
NewInt
(
0
)
ca
:=
state
.
GetAccount
(
coinbase
)
// Apply the transaction to the current state
// Apply the transaction to the current state
gas
,
err
=
sm
.
Ethereum
.
TxPool
()
.
ProcessTransaction
(
tx
,
state
,
false
)
gas
,
err
=
sm
.
ProcessTransaction
(
tx
,
ca
,
state
,
false
)
addTotalGas
(
gas
)
addTotalGas
(
gas
)
if
tx
.
CreatesContract
()
{
if
tx
.
CreatesContract
()
{
...
@@ -229,7 +305,7 @@ func (sm *StateManager) ProcessBlock(state *State, parent, block *Block, dontRea
...
@@ -229,7 +305,7 @@ func (sm *StateManager) ProcessBlock(state *State, parent, block *Block, dontRea
}
}
// Process the transactions on to current block
// Process the transactions on to current block
sm
.
ApplyTransactions
(
state
,
parent
,
block
.
Transactions
())
sm
.
ApplyTransactions
(
block
.
Coinbase
,
state
,
parent
,
block
.
Transactions
())
// Block validation
// Block validation
if
err
:=
sm
.
ValidateBlock
(
block
);
err
!=
nil
{
if
err
:=
sm
.
ValidateBlock
(
block
);
err
!=
nil
{
...
@@ -337,13 +413,6 @@ func CalculateBlockReward(block *Block, uncleLength int) *big.Int {
...
@@ -337,13 +413,6 @@ func CalculateBlockReward(block *Block, uncleLength int) *big.Int {
base
.
Add
(
base
,
UncleInclusionReward
)
base
.
Add
(
base
,
UncleInclusionReward
)
}
}
lastCumulGasUsed
:=
big
.
NewInt
(
0
)
for
_
,
r
:=
range
block
.
Receipts
()
{
usedGas
:=
new
(
big
.
Int
)
.
Sub
(
r
.
CumulativeGasUsed
,
lastCumulGasUsed
)
usedGas
.
Add
(
usedGas
,
r
.
Tx
.
GasPrice
)
base
.
Add
(
base
,
usedGas
)
}
return
base
.
Add
(
base
,
BlockReward
)
return
base
.
Add
(
base
,
BlockReward
)
}
}
...
...
This diff is collapsed.
Click to expand it.
ethchain/transaction_pool.go
+
19
−
10
View file @
4d3209ad
...
@@ -89,9 +89,11 @@ func (pool *TxPool) addTransaction(tx *Transaction) {
...
@@ -89,9 +89,11 @@ func (pool *TxPool) addTransaction(tx *Transaction) {
pool
.
Ethereum
.
Broadcast
(
ethwire
.
MsgTxTy
,
[]
interface
{}{
tx
.
RlpData
()})
pool
.
Ethereum
.
Broadcast
(
ethwire
.
MsgTxTy
,
[]
interface
{}{
tx
.
RlpData
()})
}
}
/*
// Process transaction validates the Tx and processes funds from the
// Process transaction validates the Tx and processes funds from the
// sender to the recipient.
// sender to the recipient.
func (pool *TxPool) ProcessTransaction(tx *Transaction, state *State, toContract bool) (gas *big.Int, err error) {
func (pool *TxPool) ProcessTransaction(tx *Transaction, state *State, toContract bool) (gas *big.Int, err error) {
fmt.Printf("state root before update %x\n", state.Root())
defer func() {
defer func() {
if r := recover(); r != nil {
if r := recover(); r != nil {
ethutil.Config.Log.Infoln(r)
ethutil.Config.Log.Infoln(r)
...
@@ -101,6 +103,7 @@ func (pool *TxPool) ProcessTransaction(tx *Transaction, state *State, toContract
...
@@ -101,6 +103,7 @@ func (pool *TxPool) ProcessTransaction(tx *Transaction, state *State, toContract
gas = new(big.Int)
gas = new(big.Int)
addGas := func(g *big.Int) { gas.Add(gas, g) }
addGas := func(g *big.Int) { gas.Add(gas, g) }
addGas(GasTx)
// Get the sender
// Get the sender
sender := state.GetAccount(tx.Sender())
sender := state.GetAccount(tx.Sender())
...
@@ -110,28 +113,37 @@ func (pool *TxPool) ProcessTransaction(tx *Transaction, state *State, toContract
...
@@ -110,28 +113,37 @@ func (pool *TxPool) ProcessTransaction(tx *Transaction, state *State, toContract
return
return
}
}
sender.Nonce += 1
defer func() {
//state.UpdateStateObject(sender)
// Notify all subscribers
pool.Ethereum.Reactor().Post("newTx:post", tx)
}()
txTotalBytes := big.NewInt(int64(len(tx.Data)))
txTotalBytes := big.NewInt(int64(len(tx.Data)))
txTotalBytes.Div(txTotalBytes, ethutil.Big32)
txTotalBytes.Div(txTotalBytes, ethutil.Big32)
addGas(new(big.Int).Mul(txTotalBytes, GasSStore))
addGas(new(big.Int).Mul(txTotalBytes, GasSStore))
rGas := new(big.Int).Set(gas)
rGas.Mul(gas, tx.GasPrice)
// Make sure there's enough in the sender's account. Having insufficient
// Make sure there's enough in the sender's account. Having insufficient
// funds won't invalidate this transaction but simple ignores it.
// funds won't invalidate this transaction but simple ignores it.
//totAmount := new(big.Int).Add(tx.Value, new(big.Int).Mul(TxFee, TxFeeRat))
totAmount := new(big.Int).Add(tx.Value, rGas)
totAmount
:=
new
(
big
.
Int
)
.
Add
(
tx
.
Value
,
new
(
big
.
Int
)
.
Mul
(
tx
.
Gas
,
tx
.
GasPrice
))
if sender.Amount.Cmp(totAmount) < 0 {
if sender.Amount.Cmp(totAmount) < 0 {
err = fmt.Errorf("[TXPL] Insufficient amount in sender's (%x) account", tx.Sender())
err = fmt.Errorf("[TXPL] Insufficient amount in sender's (%x) account", tx.Sender())
return
return
}
}
state.UpdateStateObject(sender)
fmt.Printf("state root after sender update %x\n", state.Root())
// Get the receiver
// Get the receiver
receiver := state.GetAccount(tx.Recipient)
receiver := state.GetAccount(tx.Recipient)
sender
.
Nonce
+=
1
// Send Tx to self
// Send Tx to self
if bytes.Compare(tx.Recipient, tx.Sender()) == 0 {
if bytes.Compare(tx.Recipient, tx.Sender()) == 0 {
addGas
(
GasTx
)
// Subtract the fee
// Subtract the fee
sender
.
SubAmount
(
new
(
big
.
Int
)
.
Mul
(
GasTx
,
tx
.
GasPrice
)
)
sender.SubAmount(
rGas
)
} else {
} else {
// Subtract the amount from the senders account
// Subtract the amount from the senders account
sender.SubAmount(totAmount)
sender.SubAmount(totAmount)
...
@@ -140,17 +152,14 @@ func (pool *TxPool) ProcessTransaction(tx *Transaction, state *State, toContract
...
@@ -140,17 +152,14 @@ func (pool *TxPool) ProcessTransaction(tx *Transaction, state *State, toContract
receiver.AddAmount(tx.Value)
receiver.AddAmount(tx.Value)
state.UpdateStateObject(receiver)
state.UpdateStateObject(receiver)
fmt.Printf("state root after receiver update %x\n", state.Root())
}
}
state
.
UpdateStateObject
(
sender
)
ethutil.Config.Log.Infof("[TXPL] Processed Tx %x\n", tx.Hash())
ethutil.Config.Log.Infof("[TXPL] Processed Tx %x\n", tx.Hash())
// Notify all subscribers
pool
.
Ethereum
.
Reactor
()
.
Post
(
"newTx:post"
,
tx
)
return
return
}
}
*/
func
(
pool
*
TxPool
)
ValidateTransaction
(
tx
*
Transaction
)
error
{
func
(
pool
*
TxPool
)
ValidateTransaction
(
tx
*
Transaction
)
error
{
// Get the last block so we can retrieve the sender and receiver from
// Get the last block so we can retrieve the sender and receiver from
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment