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
c33cc382
Commit
c33cc382
authored
Sep 21, 2015
by
Péter Szilágyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core: support inserting pure header chains
parent
92f9a3e5
Changes
9
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
651 additions
and
346 deletions
+651
-346
block_processor.go
core/block_processor.go
+14
-1
blockchain.go
core/blockchain.go
+233
-65
blockchain_test.go
core/blockchain_test.go
+352
-235
chain_makers.go
core/chain_makers.go
+37
-13
block.go
core/types/block.go
+9
-7
backend.go
eth/backend.go
+1
-1
handler.go
eth/handler.go
+2
-16
sync.go
eth/sync.go
+2
-1
debug.go
rpc/api/debug.go
+1
-7
No files found.
core/block_processor.go
View file @
c33cc382
...
...
@@ -369,13 +369,26 @@ func (sm *BlockProcessor) GetLogs(block *types.Block) (logs vm.Logs, err error)
return
logs
,
nil
}
// ValidateHeader verifies the validity of a header, relying on the database and
// POW behind the block processor.
func
(
sm
*
BlockProcessor
)
ValidateHeader
(
header
*
types
.
Header
,
checkPow
,
uncle
bool
)
error
{
// Short circuit if the header's already known or its parent missing
if
sm
.
bc
.
HasHeader
(
header
.
Hash
())
{
return
nil
}
if
parent
:=
sm
.
bc
.
GetHeader
(
header
.
ParentHash
);
parent
==
nil
{
return
ParentError
(
header
.
ParentHash
)
}
else
{
return
ValidateHeader
(
sm
.
Pow
,
header
,
parent
,
checkPow
,
uncle
)
}
}
// See YP section 4.3.4. "Block Header Validity"
// Validates a header. Returns an error if the header is invalid.
func
ValidateHeader
(
pow
pow
.
PoW
,
header
*
types
.
Header
,
parent
*
types
.
Header
,
checkPow
,
uncle
bool
)
error
{
if
big
.
NewInt
(
int64
(
len
(
header
.
Extra
)))
.
Cmp
(
params
.
MaximumExtraDataSize
)
==
1
{
return
fmt
.
Errorf
(
"Header extra data too long (%d)"
,
len
(
header
.
Extra
))
}
if
uncle
{
if
header
.
Time
.
Cmp
(
common
.
MaxBig
)
==
1
{
return
BlockTSTooBigErr
...
...
core/blockchain.go
View file @
c33cc382
This diff is collapsed.
Click to expand it.
core/blockchain_test.go
View file @
c33cc382
This diff is collapsed.
Click to expand it.
core/chain_makers.go
View file @
c33cc382
...
...
@@ -211,25 +211,49 @@ func makeHeader(parent *types.Block, state *state.StateDB) *types.Header {
}
}
// newCanonical creates a new deterministic canonical chain by running
// InsertChain on the result of makeChain.
func
newCanonical
(
n
int
,
db
ethdb
.
Database
)
(
*
BlockProcessor
,
error
)
{
// newCanonical creates a chain database, and injects a deterministic canonical
// chain. Depending on the full flag, if creates either a full block chain or a
// header only chain.
func
newCanonical
(
n
int
,
full
bool
)
(
ethdb
.
Database
,
*
BlockProcessor
,
error
)
{
// Create te new chain database
db
,
_
:=
ethdb
.
NewMemDatabase
()
evmux
:=
&
event
.
TypeMux
{}
WriteTestNetGenesisBlock
(
db
,
0
)
chainman
,
_
:=
NewBlockChain
(
db
,
FakePow
{},
evmux
)
bman
:=
NewBlockProcessor
(
db
,
FakePow
{},
chainman
,
evmux
)
bman
.
bc
.
SetProcessor
(
bman
)
parent
:=
bman
.
bc
.
CurrentBlock
()
// Initialize a fresh chain with only a genesis block
genesis
,
_
:=
WriteTestNetGenesisBlock
(
db
,
0
)
blockchain
,
_
:=
NewBlockChain
(
db
,
FakePow
{},
evmux
)
processor
:=
NewBlockProcessor
(
db
,
FakePow
{},
blockchain
,
evmux
)
processor
.
bc
.
SetProcessor
(
processor
)
// Create and inject the requested chain
if
n
==
0
{
return
bman
,
nil
return
db
,
processor
,
nil
}
if
full
{
// Full block-chain requested
blocks
:=
makeBlockChain
(
genesis
,
n
,
db
,
canonicalSeed
)
_
,
err
:=
blockchain
.
InsertChain
(
blocks
)
return
db
,
processor
,
err
}
// Header-only chain requested
headers
:=
makeHeaderChain
(
genesis
.
Header
(),
n
,
db
,
canonicalSeed
)
_
,
err
:=
blockchain
.
InsertHeaderChain
(
headers
,
true
)
return
db
,
processor
,
err
}
// makeHeaderChain creates a deterministic chain of headers rooted at parent.
func
makeHeaderChain
(
parent
*
types
.
Header
,
n
int
,
db
ethdb
.
Database
,
seed
int
)
[]
*
types
.
Header
{
blocks
:=
makeBlockChain
(
types
.
NewBlockWithHeader
(
parent
),
n
,
db
,
seed
)
headers
:=
make
([]
*
types
.
Header
,
len
(
blocks
))
for
i
,
block
:=
range
blocks
{
headers
[
i
]
=
block
.
Header
()
}
lchain
:=
makeChain
(
parent
,
n
,
db
,
canonicalSeed
)
_
,
err
:=
bman
.
bc
.
InsertChain
(
lchain
)
return
bman
,
err
return
headers
}
func
makeChain
(
parent
*
types
.
Block
,
n
int
,
db
ethdb
.
Database
,
seed
int
)
[]
*
types
.
Block
{
// makeBlockChain creates a deterministic chain of blocks rooted at parent.
func
makeBlockChain
(
parent
*
types
.
Block
,
n
int
,
db
ethdb
.
Database
,
seed
int
)
[]
*
types
.
Block
{
return
GenerateChain
(
parent
,
db
,
n
,
func
(
i
int
,
b
*
BlockGen
)
{
b
.
SetCoinbase
(
common
.
Address
{
0
:
byte
(
seed
),
19
:
byte
(
i
)})
})
...
...
core/types/block.go
View file @
c33cc382
...
...
@@ -184,7 +184,7 @@ var (
// are ignored and set to values derived from the given txs, uncles
// and receipts.
func
NewBlock
(
header
*
Header
,
txs
[]
*
Transaction
,
uncles
[]
*
Header
,
receipts
[]
*
Receipt
)
*
Block
{
b
:=
&
Block
{
header
:
c
opyHeader
(
header
),
td
:
new
(
big
.
Int
)}
b
:=
&
Block
{
header
:
C
opyHeader
(
header
),
td
:
new
(
big
.
Int
)}
// TODO: panic if len(txs) != len(receipts)
if
len
(
txs
)
==
0
{
...
...
@@ -210,7 +210,7 @@ func NewBlock(header *Header, txs []*Transaction, uncles []*Header, receipts []*
b
.
header
.
UncleHash
=
CalcUncleHash
(
uncles
)
b
.
uncles
=
make
([]
*
Header
,
len
(
uncles
))
for
i
:=
range
uncles
{
b
.
uncles
[
i
]
=
c
opyHeader
(
uncles
[
i
])
b
.
uncles
[
i
]
=
C
opyHeader
(
uncles
[
i
])
}
}
...
...
@@ -221,10 +221,12 @@ func NewBlock(header *Header, txs []*Transaction, uncles []*Header, receipts []*
// header data is copied, changes to header and to the field values
// will not affect the block.
func
NewBlockWithHeader
(
header
*
Header
)
*
Block
{
return
&
Block
{
header
:
c
opyHeader
(
header
)}
return
&
Block
{
header
:
C
opyHeader
(
header
)}
}
func
copyHeader
(
h
*
Header
)
*
Header
{
// CopyHeader creates a deep copy of a block header to prevent side effects from
// modifying a header variable.
func
CopyHeader
(
h
*
Header
)
*
Header
{
cpy
:=
*
h
if
cpy
.
Time
=
new
(
big
.
Int
);
h
.
Time
!=
nil
{
cpy
.
Time
.
Set
(
h
.
Time
)
...
...
@@ -326,7 +328,7 @@ func (b *Block) ReceiptHash() common.Hash { return b.header.ReceiptHash }
func
(
b
*
Block
)
UncleHash
()
common
.
Hash
{
return
b
.
header
.
UncleHash
}
func
(
b
*
Block
)
Extra
()
[]
byte
{
return
common
.
CopyBytes
(
b
.
header
.
Extra
)
}
func
(
b
*
Block
)
Header
()
*
Header
{
return
c
opyHeader
(
b
.
header
)
}
func
(
b
*
Block
)
Header
()
*
Header
{
return
C
opyHeader
(
b
.
header
)
}
func
(
b
*
Block
)
HashNoNonce
()
common
.
Hash
{
return
b
.
header
.
HashNoNonce
()
...
...
@@ -370,13 +372,13 @@ func (b *Block) WithMiningResult(nonce uint64, mixDigest common.Hash) *Block {
// WithBody returns a new block with the given transaction and uncle contents.
func
(
b
*
Block
)
WithBody
(
transactions
[]
*
Transaction
,
uncles
[]
*
Header
)
*
Block
{
block
:=
&
Block
{
header
:
c
opyHeader
(
b
.
header
),
header
:
C
opyHeader
(
b
.
header
),
transactions
:
make
([]
*
Transaction
,
len
(
transactions
)),
uncles
:
make
([]
*
Header
,
len
(
uncles
)),
}
copy
(
block
.
transactions
,
transactions
)
for
i
:=
range
uncles
{
block
.
uncles
[
i
]
=
c
opyHeader
(
uncles
[
i
])
block
.
uncles
[
i
]
=
C
opyHeader
(
uncles
[
i
])
}
return
block
}
...
...
eth/backend.go
View file @
c33cc382
...
...
@@ -464,7 +464,7 @@ func (s *Ethereum) NodeInfo() *NodeInfo {
DiscPort
:
int
(
node
.
UDP
),
TCPPort
:
int
(
node
.
TCP
),
ListenAddr
:
s
.
net
.
ListenAddr
,
Td
:
s
.
BlockChain
()
.
Td
(
)
.
String
(),
Td
:
s
.
BlockChain
()
.
GetTd
(
s
.
BlockChain
()
.
CurrentBlock
()
.
Hash
()
)
.
String
(),
}
}
...
...
eth/handler.go
View file @
c33cc382
...
...
@@ -589,15 +589,6 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
}
request
.
Block
.
ReceivedAt
=
msg
.
ReceivedAt
// Mark the block's arrival for whatever reason
_
,
chainHead
,
_
:=
pm
.
blockchain
.
Status
()
jsonlogger
.
LogJson
(
&
logger
.
EthChainReceivedNewBlock
{
BlockHash
:
request
.
Block
.
Hash
()
.
Hex
(),
BlockNumber
:
request
.
Block
.
Number
(),
ChainHeadHash
:
chainHead
.
Hex
(),
BlockPrevHash
:
request
.
Block
.
ParentHash
()
.
Hex
(),
RemoteId
:
p
.
ID
()
.
String
(),
})
// Mark the peer as owning the block and schedule it for import
p
.
MarkBlock
(
request
.
Block
.
Hash
())
p
.
SetHead
(
request
.
Block
.
Hash
())
...
...
@@ -607,7 +598,8 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
// Update the peers total difficulty if needed, schedule a download if gapped
if
request
.
TD
.
Cmp
(
p
.
Td
())
>
0
{
p
.
SetTd
(
request
.
TD
)
if
request
.
TD
.
Cmp
(
new
(
big
.
Int
)
.
Add
(
pm
.
blockchain
.
Td
(),
request
.
Block
.
Difficulty
()))
>
0
{
td
:=
pm
.
blockchain
.
GetTd
(
pm
.
blockchain
.
CurrentBlock
()
.
Hash
())
if
request
.
TD
.
Cmp
(
new
(
big
.
Int
)
.
Add
(
td
,
request
.
Block
.
Difficulty
()))
>
0
{
go
pm
.
synchronise
(
p
)
}
}
...
...
@@ -624,12 +616,6 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
return
errResp
(
ErrDecode
,
"transaction %d is nil"
,
i
)
}
p
.
MarkTransaction
(
tx
.
Hash
())
// Log it's arrival for later analysis
jsonlogger
.
LogJson
(
&
logger
.
EthTxReceived
{
TxHash
:
tx
.
Hash
()
.
Hex
(),
RemoteId
:
p
.
ID
()
.
String
(),
})
}
pm
.
txpool
.
AddTransactions
(
txs
)
...
...
eth/sync.go
View file @
c33cc382
...
...
@@ -160,7 +160,8 @@ func (pm *ProtocolManager) synchronise(peer *peer) {
return
}
// Make sure the peer's TD is higher than our own. If not drop.
if
peer
.
Td
()
.
Cmp
(
pm
.
blockchain
.
Td
())
<=
0
{
td
:=
pm
.
blockchain
.
GetTd
(
pm
.
blockchain
.
CurrentBlock
()
.
Hash
())
if
peer
.
Td
()
.
Cmp
(
td
)
<=
0
{
return
}
// Otherwise try to sync with the downloader
...
...
rpc/api/debug.go
View file @
c33cc382
...
...
@@ -146,13 +146,7 @@ func (self *debugApi) SetHead(req *shared.Request) (interface{}, error) {
if
err
:=
self
.
codec
.
Decode
(
req
.
Params
,
&
args
);
err
!=
nil
{
return
nil
,
shared
.
NewDecodeParamError
(
err
.
Error
())
}
block
:=
self
.
xeth
.
EthBlockByNumber
(
args
.
BlockNumber
)
if
block
==
nil
{
return
nil
,
fmt
.
Errorf
(
"block #%d not found"
,
args
.
BlockNumber
)
}
self
.
ethereum
.
BlockChain
()
.
SetHead
(
block
)
self
.
ethereum
.
BlockChain
()
.
SetHead
(
uint64
(
args
.
BlockNumber
))
return
nil
,
nil
}
...
...
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