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
6c6e8b0f
Commit
6c6e8b0f
authored
Mar 05, 2014
by
obscuren
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Renamed block manager to state manager
parent
79320e28
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
69 additions
and
69 deletions
+69
-69
state_manager.go
ethchain/state_manager.go
+63
-63
ethereum.go
ethereum.go
+6
-6
No files found.
ethchain/
block
_manager.go
→
ethchain/
state
_manager.go
View file @
6c6e8b0f
...
...
@@ -15,14 +15,14 @@ type BlockProcessor interface {
}
type
EthManager
interface
{
StateManager
()
*
Block
Manager
StateManager
()
*
State
Manager
BlockChain
()
*
BlockChain
TxPool
()
*
TxPool
Broadcast
(
msgType
ethwire
.
MsgType
,
data
[]
interface
{})
}
// TODO rename to state manager
type
Block
Manager
struct
{
type
State
Manager
struct
{
// Mutex for locking the block processor. Blocks can only be handled one at a time
mutex
sync
.
Mutex
...
...
@@ -52,8 +52,8 @@ type BlockManager struct {
compState
*
State
}
func
New
BlockManager
(
ethereum
EthManager
)
*
Block
Manager
{
bm
:=
&
Block
Manager
{
func
New
StateManager
(
ethereum
EthManager
)
*
State
Manager
{
sm
:=
&
State
Manager
{
stack
:
NewStack
(),
mem
:
make
(
map
[
string
]
*
big
.
Int
),
Pow
:
&
EasyPow
{},
...
...
@@ -62,57 +62,57 @@ func NewBlockManager(ethereum EthManager) *BlockManager {
bc
:
ethereum
.
BlockChain
(),
}
return
b
m
return
s
m
}
func
(
bm
*
Block
Manager
)
ProcState
()
*
State
{
return
b
m
.
procState
func
(
sm
*
State
Manager
)
ProcState
()
*
State
{
return
s
m
.
procState
}
// Watches any given address and puts it in the address state store
func
(
bm
*
Block
Manager
)
WatchAddr
(
addr
[]
byte
)
*
AccountState
{
//FIXME account :=
b
m.procState.GetAccount(addr)
account
:=
b
m
.
bc
.
CurrentBlock
.
state
.
GetAccount
(
addr
)
func
(
sm
*
State
Manager
)
WatchAddr
(
addr
[]
byte
)
*
AccountState
{
//FIXME account :=
s
m.procState.GetAccount(addr)
account
:=
s
m
.
bc
.
CurrentBlock
.
state
.
GetAccount
(
addr
)
return
b
m
.
addrStateStore
.
Add
(
addr
,
account
)
return
s
m
.
addrStateStore
.
Add
(
addr
,
account
)
}
func
(
bm
*
Block
Manager
)
GetAddrState
(
addr
[]
byte
)
*
AccountState
{
account
:=
b
m
.
addrStateStore
.
Get
(
addr
)
func
(
sm
*
State
Manager
)
GetAddrState
(
addr
[]
byte
)
*
AccountState
{
account
:=
s
m
.
addrStateStore
.
Get
(
addr
)
if
account
==
nil
{
a
:=
b
m
.
bc
.
CurrentBlock
.
state
.
GetAccount
(
addr
)
a
:=
s
m
.
bc
.
CurrentBlock
.
state
.
GetAccount
(
addr
)
account
=
&
AccountState
{
Nonce
:
a
.
Nonce
,
Account
:
a
}
}
return
account
}
func
(
bm
*
Block
Manager
)
BlockChain
()
*
BlockChain
{
return
b
m
.
bc
func
(
sm
*
State
Manager
)
BlockChain
()
*
BlockChain
{
return
s
m
.
bc
}
func
(
bm
*
Block
Manager
)
MakeContract
(
tx
*
Transaction
)
{
contract
:=
MakeContract
(
tx
,
b
m
.
procState
)
func
(
sm
*
State
Manager
)
MakeContract
(
tx
*
Transaction
)
{
contract
:=
MakeContract
(
tx
,
s
m
.
procState
)
if
contract
!=
nil
{
b
m
.
procState
.
states
[
string
(
tx
.
Hash
()[
12
:
])]
=
contract
.
state
s
m
.
procState
.
states
[
string
(
tx
.
Hash
()[
12
:
])]
=
contract
.
state
}
}
func
(
bm
*
Block
Manager
)
ApplyTransactions
(
block
*
Block
,
txs
[]
*
Transaction
)
{
func
(
sm
*
State
Manager
)
ApplyTransactions
(
block
*
Block
,
txs
[]
*
Transaction
)
{
// Process each transaction/contract
for
_
,
tx
:=
range
txs
{
// If there's no recipient, it's a contract
if
tx
.
IsContract
()
{
//FIXME
b
m.MakeContract(tx)
//FIXME
s
m.MakeContract(tx)
block
.
MakeContract
(
tx
)
}
else
{
//FIXME if contract := procState.GetContract(tx.Recipient); contract != nil {
if
contract
:=
block
.
state
.
GetContract
(
tx
.
Recipient
);
contract
!=
nil
{
b
m
.
ProcessContract
(
contract
,
tx
,
block
)
s
m
.
ProcessContract
(
contract
,
tx
,
block
)
}
else
{
err
:=
b
m
.
Ethereum
.
TxPool
()
.
ProcessTransaction
(
tx
,
block
)
err
:=
s
m
.
Ethereum
.
TxPool
()
.
ProcessTransaction
(
tx
,
block
)
if
err
!=
nil
{
ethutil
.
Config
.
Log
.
Infoln
(
"[
BM
GR]"
,
err
)
ethutil
.
Config
.
Log
.
Infoln
(
"[
sm
GR]"
,
err
)
}
}
}
...
...
@@ -121,78 +121,78 @@ func (bm *BlockManager) ApplyTransactions(block *Block, txs []*Transaction) {
// The prepare function, prepares the state manager for the next
// "ProcessBlock" action.
func
(
bm
*
Block
Manager
)
Prepare
(
processer
*
State
,
comparative
*
State
)
{
b
m
.
compState
=
comparative
b
m
.
procState
=
processer
func
(
sm
*
State
Manager
)
Prepare
(
processer
*
State
,
comparative
*
State
)
{
s
m
.
compState
=
comparative
s
m
.
procState
=
processer
}
// Default prepare function
func
(
bm
*
Block
Manager
)
PrepareDefault
(
block
*
Block
)
{
bm
.
Prepare
(
b
m
.
BlockChain
()
.
CurrentBlock
.
State
(),
block
.
State
())
func
(
sm
*
State
Manager
)
PrepareDefault
(
block
*
Block
)
{
sm
.
Prepare
(
s
m
.
BlockChain
()
.
CurrentBlock
.
State
(),
block
.
State
())
}
// Block processing and validating with a given (temporarily) state
func
(
bm
*
Block
Manager
)
ProcessBlock
(
block
*
Block
)
error
{
func
(
sm
*
State
Manager
)
ProcessBlock
(
block
*
Block
)
error
{
// Processing a blocks may never happen simultaneously
b
m
.
mutex
.
Lock
()
defer
b
m
.
mutex
.
Unlock
()
s
m
.
mutex
.
Lock
()
defer
s
m
.
mutex
.
Unlock
()
// Defer the Undo on the Trie. If the block processing happened
// we don't want to undo but since undo only happens on dirty
// nodes this won't happen because Commit would have been called
// before that.
defer
b
m
.
bc
.
CurrentBlock
.
Undo
()
defer
s
m
.
bc
.
CurrentBlock
.
Undo
()
hash
:=
block
.
Hash
()
if
b
m
.
bc
.
HasBlock
(
hash
)
{
if
s
m
.
bc
.
HasBlock
(
hash
)
{
return
nil
}
// Check if we have the parent hash, if it isn't known we discard it
// Reasons might be catching up or simply an invalid block
if
!
bm
.
bc
.
HasBlock
(
block
.
PrevHash
)
&&
b
m
.
bc
.
CurrentBlock
!=
nil
{
if
!
sm
.
bc
.
HasBlock
(
block
.
PrevHash
)
&&
s
m
.
bc
.
CurrentBlock
!=
nil
{
return
ParentError
(
block
.
PrevHash
)
}
// Process the transactions on to current block
bm
.
ApplyTransactions
(
b
m
.
bc
.
CurrentBlock
,
block
.
Transactions
())
sm
.
ApplyTransactions
(
s
m
.
bc
.
CurrentBlock
,
block
.
Transactions
())
// Block validation
if
err
:=
b
m
.
ValidateBlock
(
block
);
err
!=
nil
{
if
err
:=
s
m
.
ValidateBlock
(
block
);
err
!=
nil
{
return
err
}
// I'm not sure, but I don't know if there should be thrown
// any errors at this time.
if
err
:=
bm
.
AccumelateRewards
(
b
m
.
bc
.
CurrentBlock
,
block
);
err
!=
nil
{
if
err
:=
sm
.
AccumelateRewards
(
s
m
.
bc
.
CurrentBlock
,
block
);
err
!=
nil
{
return
err
}
// if !
bm.compState.Cmp(b
m.procState)
if
!
block
.
state
.
Cmp
(
b
m
.
bc
.
CurrentBlock
.
state
)
{
return
fmt
.
Errorf
(
"Invalid merkle root. Expected %x, got %x"
,
block
.
State
()
.
trie
.
Root
,
b
m
.
bc
.
CurrentBlock
.
State
()
.
trie
.
Root
)
//FIXME return fmt.Errorf("Invalid merkle root. Expected %x, got %x",
bm.compState.trie.Root, b
m.procState.trie.Root)
// if !
sm.compState.Cmp(s
m.procState)
if
!
block
.
state
.
Cmp
(
s
m
.
bc
.
CurrentBlock
.
state
)
{
return
fmt
.
Errorf
(
"Invalid merkle root. Expected %x, got %x"
,
block
.
State
()
.
trie
.
Root
,
s
m
.
bc
.
CurrentBlock
.
State
()
.
trie
.
Root
)
//FIXME return fmt.Errorf("Invalid merkle root. Expected %x, got %x",
sm.compState.trie.Root, s
m.procState.trie.Root)
}
// Calculate the new total difficulty and sync back to the db
if
b
m
.
CalculateTD
(
block
)
{
if
s
m
.
CalculateTD
(
block
)
{
// Sync the current block's state to the database and cancelling out the deferred Undo
b
m
.
bc
.
CurrentBlock
.
Sync
()
//FIXME
b
m.procState.Sync()
s
m
.
bc
.
CurrentBlock
.
Sync
()
//FIXME
s
m.procState.Sync()
// Broadcast the valid block back to the wire
//
b
m.Ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{block.Value().Val})
//
s
m.Ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{block.Value().Val})
// Add the block to the chain
b
m
.
bc
.
Add
(
block
)
s
m
.
bc
.
Add
(
block
)
// If there's a block processor present, pass in the block for further
// processing
if
b
m
.
SecondaryBlockProcessor
!=
nil
{
b
m
.
SecondaryBlockProcessor
.
ProcessBlock
(
block
)
if
s
m
.
SecondaryBlockProcessor
!=
nil
{
s
m
.
SecondaryBlockProcessor
.
ProcessBlock
(
block
)
}
ethutil
.
Config
.
Log
.
Infof
(
"[
BM
GR] Added block #%d (%x)
\n
"
,
block
.
BlockInfo
()
.
Number
,
block
.
Hash
())
ethutil
.
Config
.
Log
.
Infof
(
"[
sm
GR] Added block #%d (%x)
\n
"
,
block
.
BlockInfo
()
.
Number
,
block
.
Hash
())
}
else
{
fmt
.
Println
(
"total diff failed"
)
}
...
...
@@ -200,7 +200,7 @@ func (bm *BlockManager) ProcessBlock(block *Block) error {
return
nil
}
func
(
bm
*
Block
Manager
)
CalculateTD
(
block
*
Block
)
bool
{
func
(
sm
*
State
Manager
)
CalculateTD
(
block
*
Block
)
bool
{
uncleDiff
:=
new
(
big
.
Int
)
for
_
,
uncle
:=
range
block
.
Uncles
{
uncleDiff
=
uncleDiff
.
Add
(
uncleDiff
,
uncle
.
Difficulty
)
...
...
@@ -208,14 +208,14 @@ func (bm *BlockManager) CalculateTD(block *Block) bool {
// TD(genesis_block) = 0 and TD(B) = TD(B.parent) + sum(u.difficulty for u in B.uncles) + B.difficulty
td
:=
new
(
big
.
Int
)
td
=
td
.
Add
(
b
m
.
bc
.
TD
,
uncleDiff
)
td
=
td
.
Add
(
s
m
.
bc
.
TD
,
uncleDiff
)
td
=
td
.
Add
(
td
,
block
.
Difficulty
)
// The new TD will only be accepted if the new difficulty is
// is greater than the previous.
if
td
.
Cmp
(
b
m
.
bc
.
TD
)
>
0
{
if
td
.
Cmp
(
s
m
.
bc
.
TD
)
>
0
{
// Set the new total difficulty back to the block chain
b
m
.
bc
.
SetTotalDifficulty
(
td
)
s
m
.
bc
.
SetTotalDifficulty
(
td
)
return
true
}
...
...
@@ -226,20 +226,20 @@ func (bm *BlockManager) CalculateTD(block *Block) bool {
// Validates the current block. Returns an error if the block was invalid,
// an uncle or anything that isn't on the current block chain.
// Validation validates easy over difficult (dagger takes longer time = difficult)
func
(
bm
*
Block
Manager
)
ValidateBlock
(
block
*
Block
)
error
{
func
(
sm
*
State
Manager
)
ValidateBlock
(
block
*
Block
)
error
{
// TODO
// 2. Check if the difficulty is correct
// Check each uncle's previous hash. In order for it to be valid
// is if it has the same block hash as the current
previousBlock
:=
b
m
.
bc
.
GetBlock
(
block
.
PrevHash
)
previousBlock
:=
s
m
.
bc
.
GetBlock
(
block
.
PrevHash
)
for
_
,
uncle
:=
range
block
.
Uncles
{
if
bytes
.
Compare
(
uncle
.
PrevHash
,
previousBlock
.
PrevHash
)
!=
0
{
return
ValidationError
(
"Mismatch uncle's previous hash. Expected %x, got %x"
,
previousBlock
.
PrevHash
,
uncle
.
PrevHash
)
}
}
diff
:=
block
.
Time
-
b
m
.
bc
.
CurrentBlock
.
Time
diff
:=
block
.
Time
-
s
m
.
bc
.
CurrentBlock
.
Time
if
diff
<
0
{
return
ValidationError
(
"Block timestamp less then prev block %v"
,
diff
)
}
...
...
@@ -250,7 +250,7 @@ func (bm *BlockManager) ValidateBlock(block *Block) error {
}
// Verify the nonce of the block. Return an error if it's not valid
if
!
b
m
.
Pow
.
Verify
(
block
.
HashNoNonce
(),
block
.
Difficulty
,
block
.
Nonce
)
{
if
!
s
m
.
Pow
.
Verify
(
block
.
HashNoNonce
(),
block
.
Difficulty
,
block
.
Nonce
)
{
return
ValidationError
(
"Block's nonce is invalid (= %v)"
,
block
.
Nonce
)
}
...
...
@@ -269,7 +269,7 @@ func CalculateUncleReward(block *Block) *big.Int {
return
UncleReward
}
func
(
bm
*
Block
Manager
)
AccumelateRewards
(
processor
*
Block
,
block
*
Block
)
error
{
func
(
sm
*
State
Manager
)
AccumelateRewards
(
processor
*
Block
,
block
*
Block
)
error
{
// Get the coinbase rlp data
addr
:=
processor
.
state
.
GetAccount
(
block
.
Coinbase
)
//FIXME addr := proc.GetAccount(block.Coinbase)
...
...
@@ -290,11 +290,11 @@ func (bm *BlockManager) AccumelateRewards(processor *Block, block *Block) error
return
nil
}
func
(
bm
*
Block
Manager
)
Stop
()
{
b
m
.
bc
.
Stop
()
func
(
sm
*
State
Manager
)
Stop
()
{
s
m
.
bc
.
Stop
()
}
func
(
bm
*
Block
Manager
)
ProcessContract
(
contract
*
Contract
,
tx
*
Transaction
,
block
*
Block
)
{
func
(
sm
*
State
Manager
)
ProcessContract
(
contract
*
Contract
,
tx
*
Transaction
,
block
*
Block
)
{
// Recovering function in case the VM had any errors
/*
defer func() {
...
...
@@ -305,7 +305,7 @@ func (bm *BlockManager) ProcessContract(contract *Contract, tx *Transaction, blo
*/
vm
:=
&
Vm
{}
//vm.Process(contract,
b
m.procState, RuntimeVars{
//vm.Process(contract,
s
m.procState, RuntimeVars{
vm
.
Process
(
contract
,
block
.
state
,
RuntimeVars
{
address
:
tx
.
Hash
()[
12
:
],
blockNumber
:
block
.
BlockInfo
()
.
Number
,
...
...
ethereum.go
View file @
6c6e8b0f
...
...
@@ -36,8 +36,8 @@ type Ethereum struct {
// DB interface
//db *ethdb.LDBDatabase
db
ethutil
.
Database
//
Block manager for processing new blocks and managing the block chain
blockManager
*
ethchain
.
Block
Manager
//
State manager for processing new blocks and managing the over all states
stateManager
*
ethchain
.
State
Manager
// The transaction pool. Transaction can be pushed on this pool
// for later including in the blocks
txPool
*
ethchain
.
TxPool
...
...
@@ -91,7 +91,7 @@ func New(caps Caps, usePnp bool) (*Ethereum, error) {
}
ethereum
.
txPool
=
ethchain
.
NewTxPool
(
ethereum
)
ethereum
.
blockChain
=
ethchain
.
NewBlockChain
(
ethereum
)
ethereum
.
blockManager
=
ethchain
.
NewBlock
Manager
(
ethereum
)
ethereum
.
stateManager
=
ethchain
.
NewState
Manager
(
ethereum
)
// Start the tx pool
ethereum
.
txPool
.
Start
()
...
...
@@ -103,8 +103,8 @@ func (s *Ethereum) BlockChain() *ethchain.BlockChain {
return
s
.
blockChain
}
func
(
s
*
Ethereum
)
StateManager
()
*
ethchain
.
Block
Manager
{
return
s
.
block
Manager
func
(
s
*
Ethereum
)
StateManager
()
*
ethchain
.
State
Manager
{
return
s
.
state
Manager
}
func
(
s
*
Ethereum
)
TxPool
()
*
ethchain
.
TxPool
{
...
...
@@ -304,7 +304,7 @@ func (s *Ethereum) Stop() {
close
(
s
.
quit
)
s
.
txPool
.
Stop
()
s
.
block
Manager
.
Stop
()
s
.
state
Manager
.
Stop
()
close
(
s
.
shutdownChan
)
}
...
...
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