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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open
bor
Commits
0efd6a88
Commit
0efd6a88
authored
10 years ago
by
Ethan Buchman
Browse files
Options
Downloads
Patches
Plain Diff
public functions for making chains on the fly
parent
5a827417
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
core/chain_makers.go
+131
-0
131 additions, 0 deletions
core/chain_makers.go
core/chain_manager_test.go
+0
-96
0 additions, 96 deletions
core/chain_manager_test.go
with
131 additions
and
96 deletions
core/chain_makers.go
0 → 100644
+
131
−
0
View file @
0efd6a88
package
core
import
(
"fmt"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/pow"
"github.com/ethereum/go-ethereum/state"
"math/big"
)
// So we can generate blocks easily
type
FakePow
struct
{}
func
(
f
FakePow
)
Search
(
block
pow
.
Block
,
stop
<-
chan
struct
{})
[]
byte
{
return
nil
}
func
(
f
FakePow
)
Verify
(
block
pow
.
Block
)
bool
{
return
true
}
func
(
f
FakePow
)
GetHashrate
()
int64
{
return
0
}
func
(
f
FakePow
)
Turbo
(
bool
)
{}
func
NewBlockFromParent
(
addr
[]
byte
,
parent
*
types
.
Block
)
*
types
.
Block
{
return
newBlockFromParent
(
addr
,
parent
)
}
func
MakeBlock
(
bman
*
BlockProcessor
,
parent
*
types
.
Block
,
i
int
,
db
ethutil
.
Database
)
*
types
.
Block
{
return
makeBlock
(
bman
,
parent
,
i
,
db
)
}
func
MakeChain
(
bman
*
BlockProcessor
,
parent
*
types
.
Block
,
max
int
,
db
ethutil
.
Database
)
types
.
Blocks
{
return
makeChain
(
bman
,
parent
,
max
,
db
)
}
func
NewChainMan
(
block
*
types
.
Block
,
eventMux
*
event
.
TypeMux
,
db
ethutil
.
Database
)
*
ChainManager
{
return
newChainManager
(
block
,
eventMux
,
db
)
}
func
NewBlockProc
(
db
ethutil
.
Database
,
txpool
*
TxPool
,
cman
*
ChainManager
,
eventMux
*
event
.
TypeMux
)
*
BlockProcessor
{
return
newBlockProcessor
(
db
,
txpool
,
cman
,
eventMux
)
}
func
NewCanonical
(
n
int
,
db
ethutil
.
Database
)
(
*
BlockProcessor
,
error
)
{
return
newCanonical
(
n
,
db
)
}
func
newBlockFromParent
(
addr
[]
byte
,
parent
*
types
.
Block
)
*
types
.
Block
{
block
:=
types
.
NewBlock
(
parent
.
Hash
(),
addr
,
parent
.
Root
(),
ethutil
.
BigPow
(
2
,
32
),
nil
,
""
)
block
.
SetUncles
(
nil
)
block
.
SetTransactions
(
nil
)
block
.
SetReceipts
(
nil
)
header
:=
block
.
Header
()
header
.
Difficulty
=
CalcDifficulty
(
block
,
parent
)
header
.
Number
=
new
(
big
.
Int
)
.
Add
(
parent
.
Header
()
.
Number
,
ethutil
.
Big1
)
header
.
GasLimit
=
CalcGasLimit
(
parent
,
block
)
block
.
Td
=
parent
.
Td
return
block
}
// Actually make a block by simulating what miner would do
func
makeBlock
(
bman
*
BlockProcessor
,
parent
*
types
.
Block
,
i
int
,
db
ethutil
.
Database
)
*
types
.
Block
{
addr
:=
ethutil
.
LeftPadBytes
([]
byte
{
byte
(
i
)},
20
)
block
:=
newBlockFromParent
(
addr
,
parent
)
state
:=
state
.
New
(
block
.
Root
(),
db
)
cbase
:=
state
.
GetOrNewStateObject
(
addr
)
cbase
.
SetGasPool
(
CalcGasLimit
(
parent
,
block
))
cbase
.
AddBalance
(
BlockReward
)
state
.
Update
(
ethutil
.
Big0
)
block
.
SetRoot
(
state
.
Root
())
return
block
}
// Make a chain with real blocks
// Runs ProcessWithParent to get proper state roots
func
makeChain
(
bman
*
BlockProcessor
,
parent
*
types
.
Block
,
max
int
,
db
ethutil
.
Database
)
types
.
Blocks
{
bman
.
bc
.
currentBlock
=
parent
blocks
:=
make
(
types
.
Blocks
,
max
)
for
i
:=
0
;
i
<
max
;
i
++
{
block
:=
makeBlock
(
bman
,
parent
,
i
,
db
)
td
,
err
:=
bman
.
processWithParent
(
block
,
parent
)
if
err
!=
nil
{
fmt
.
Println
(
"process with parent failed"
,
err
)
panic
(
err
)
}
block
.
Td
=
td
blocks
[
i
]
=
block
parent
=
block
fmt
.
Printf
(
"New Block: %x
\n
"
,
block
.
Hash
())
}
fmt
.
Println
(
"Done making chain"
)
return
blocks
}
// Create a new chain manager starting from given block
// Effectively a fork factory
func
newChainManager
(
block
*
types
.
Block
,
eventMux
*
event
.
TypeMux
,
db
ethutil
.
Database
)
*
ChainManager
{
bc
:=
&
ChainManager
{
db
:
db
,
genesisBlock
:
GenesisBlock
(
db
),
eventMux
:
eventMux
}
if
block
==
nil
{
bc
.
Reset
()
}
else
{
bc
.
currentBlock
=
block
bc
.
td
=
block
.
Td
}
return
bc
}
// block processor with fake pow
func
newBlockProcessor
(
db
ethutil
.
Database
,
txpool
*
TxPool
,
cman
*
ChainManager
,
eventMux
*
event
.
TypeMux
)
*
BlockProcessor
{
bman
:=
NewBlockProcessor
(
db
,
txpool
,
newChainManager
(
nil
,
eventMux
,
db
),
eventMux
)
bman
.
Pow
=
FakePow
{}
return
bman
}
// Make a new canonical chain by running InsertChain
// on result of makeChain
func
newCanonical
(
n
int
,
db
ethutil
.
Database
)
(
*
BlockProcessor
,
error
)
{
eventMux
:=
&
event
.
TypeMux
{}
txpool
:=
NewTxPool
(
eventMux
)
bman
:=
newBlockProcessor
(
db
,
txpool
,
newChainManager
(
nil
,
eventMux
,
db
),
eventMux
)
bman
.
bc
.
SetProcessor
(
bman
)
parent
:=
bman
.
bc
.
CurrentBlock
()
if
n
==
0
{
return
bman
,
nil
}
lchain
:=
makeChain
(
bman
,
parent
,
n
,
db
)
bman
.
bc
.
InsertChain
(
lchain
)
return
bman
,
nil
}
This diff is collapsed.
Click to expand it.
core/chain_manager_test.go
+
0
−
96
View file @
0efd6a88
...
@@ -19,107 +19,11 @@ import (
...
@@ -19,107 +19,11 @@ import (
"github.com/ethereum/go-ethereum/state"
"github.com/ethereum/go-ethereum/state"
)
)
// So we can generate blocks easily
type
fakePow
struct
{}
func
(
f
fakePow
)
Search
(
block
pow
.
Block
,
stop
<-
chan
struct
{})
[]
byte
{
return
nil
}
func
(
f
fakePow
)
Verify
(
block
pow
.
Block
)
bool
{
return
true
}
func
(
f
fakePow
)
GetHashrate
()
int64
{
return
0
}
func
(
f
fakePow
)
Turbo
(
bool
)
{}
func
init
()
{
func
init
()
{
runtime
.
GOMAXPROCS
(
runtime
.
NumCPU
())
runtime
.
GOMAXPROCS
(
runtime
.
NumCPU
())
ethutil
.
ReadConfig
(
"/tmp/ethtest"
,
"/tmp/ethtest"
,
"ETH"
)
ethutil
.
ReadConfig
(
"/tmp/ethtest"
,
"/tmp/ethtest"
,
"ETH"
)
}
}
func
newBlockFromParent
(
addr
[]
byte
,
parent
*
types
.
Block
)
*
types
.
Block
{
block
:=
types
.
NewBlock
(
parent
.
Hash
(),
addr
,
parent
.
Root
(),
ethutil
.
BigPow
(
2
,
32
),
nil
,
""
)
block
.
SetUncles
(
nil
)
block
.
SetTransactions
(
nil
)
block
.
SetReceipts
(
nil
)
header
:=
block
.
Header
()
header
.
Difficulty
=
CalcDifficulty
(
block
,
parent
)
header
.
Number
=
new
(
big
.
Int
)
.
Add
(
parent
.
Header
()
.
Number
,
ethutil
.
Big1
)
header
.
GasLimit
=
CalcGasLimit
(
parent
,
block
)
block
.
Td
=
parent
.
Td
return
block
}
// Actually make a block by simulating what miner would do
func
makeBlock
(
bman
*
BlockProcessor
,
parent
*
types
.
Block
,
i
int
,
db
ethutil
.
Database
)
*
types
.
Block
{
addr
:=
ethutil
.
LeftPadBytes
([]
byte
{
byte
(
i
)},
20
)
block
:=
newBlockFromParent
(
addr
,
parent
)
state
:=
state
.
New
(
block
.
Root
(),
db
)
cbase
:=
state
.
GetOrNewStateObject
(
addr
)
cbase
.
SetGasPool
(
CalcGasLimit
(
parent
,
block
))
cbase
.
AddAmount
(
BlockReward
)
state
.
Update
(
ethutil
.
Big0
)
block
.
SetRoot
(
state
.
Root
())
return
block
}
// Make a chain with real blocks
// Runs ProcessWithParent to get proper state roots
func
makeChain
(
bman
*
BlockProcessor
,
parent
*
types
.
Block
,
max
int
,
db
ethutil
.
Database
)
types
.
Blocks
{
bman
.
bc
.
currentBlock
=
parent
blocks
:=
make
(
types
.
Blocks
,
max
)
for
i
:=
0
;
i
<
max
;
i
++
{
block
:=
makeBlock
(
bman
,
parent
,
i
,
db
)
td
,
err
:=
bman
.
processWithParent
(
block
,
parent
)
if
err
!=
nil
{
fmt
.
Println
(
"process with parent failed"
,
err
)
panic
(
err
)
}
block
.
Td
=
td
blocks
[
i
]
=
block
parent
=
block
fmt
.
Printf
(
"New Block: %x
\n
"
,
block
.
Hash
())
}
fmt
.
Println
(
"Done making chain"
)
return
blocks
}
// Create a new chain manager starting from given block
// Effectively a fork factory
func
newChainManager
(
block
*
types
.
Block
,
eventMux
*
event
.
TypeMux
,
db
ethutil
.
Database
)
*
ChainManager
{
bc
:=
&
ChainManager
{
db
:
db
,
genesisBlock
:
GenesisBlock
(
db
),
eventMux
:
eventMux
}
if
block
==
nil
{
bc
.
Reset
()
}
else
{
bc
.
currentBlock
=
block
bc
.
td
=
block
.
Td
}
return
bc
}
// block processor with fake pow
func
newBlockProcessor
(
db
ethutil
.
Database
,
txpool
*
TxPool
,
cman
*
ChainManager
,
eventMux
*
event
.
TypeMux
)
*
BlockProcessor
{
bman
:=
NewBlockProcessor
(
db
,
txpool
,
newChainManager
(
nil
,
eventMux
,
db
),
eventMux
)
bman
.
Pow
=
fakePow
{}
return
bman
}
// Make a new canonical chain by running InsertChain
// on result of makeChain
func
newCanonical
(
n
int
,
db
ethutil
.
Database
)
(
*
BlockProcessor
,
error
)
{
eventMux
:=
&
event
.
TypeMux
{}
txpool
:=
NewTxPool
(
eventMux
)
bman
:=
newBlockProcessor
(
db
,
txpool
,
newChainManager
(
nil
,
eventMux
,
db
),
eventMux
)
bman
.
bc
.
SetProcessor
(
bman
)
parent
:=
bman
.
bc
.
CurrentBlock
()
if
n
==
0
{
return
bman
,
nil
}
lchain
:=
makeChain
(
bman
,
parent
,
n
,
db
)
bman
.
bc
.
InsertChain
(
lchain
)
return
bman
,
nil
}
// Test fork of length N starting from block i
// Test fork of length N starting from block i
func
testFork
(
t
*
testing
.
T
,
bman
*
BlockProcessor
,
i
,
N
int
,
f
func
(
td1
,
td2
*
big
.
Int
))
{
func
testFork
(
t
*
testing
.
T
,
bman
*
BlockProcessor
,
i
,
N
int
,
f
func
(
td1
,
td2
*
big
.
Int
))
{
fmt
.
Println
(
"Testing Fork!"
)
fmt
.
Println
(
"Testing Fork!"
)
...
...
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