Skip to content

Commit 9fedf19

Browse files
authored
Merge pull request bnb-chain#53 from binance-chain/elegant_upgrade
[R4R]support fork id in header; elegant upgrade
2 parents 5a1c39d + 24dc208 commit 9fedf19

File tree

6 files changed

+100
-36
lines changed

6 files changed

+100
-36
lines changed

consensus/parlia/parlia.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package parlia
33
import (
44
"bytes"
55
"context"
6+
"encoding/hex"
67
"errors"
78
"fmt"
89
"io"
@@ -25,6 +26,7 @@ import (
2526
"github.com/ethereum/go-ethereum/consensus"
2627
"github.com/ethereum/go-ethereum/consensus/misc"
2728
"github.com/ethereum/go-ethereum/core"
29+
"github.com/ethereum/go-ethereum/core/forkid"
2830
"github.com/ethereum/go-ethereum/core/state"
2931
"github.com/ethereum/go-ethereum/core/systemcontracts"
3032
"github.com/ethereum/go-ethereum/core/types"
@@ -45,8 +47,9 @@ const (
4547
checkpointInterval = 1024 // Number of blocks after which to save the snapshot to the database
4648
defaultEpochLength = uint64(100) // Default number of blocks of checkpoint to update validatorSet from contract
4749

48-
extraVanity = 32 // Fixed number of extra-data prefix bytes reserved for signer vanity
49-
extraSeal = 65 // Fixed number of extra-data suffix bytes reserved for signer seal
50+
extraVanity = 32 // Fixed number of extra-data prefix bytes reserved for signer vanity
51+
extraSeal = 65 // Fixed number of extra-data suffix bytes reserved for signer seal
52+
nextForkHashSize = 4 // Fixed number of extra-data suffix bytes reserved for nextForkHash.
5053

5154
validatorBytesLength = common.AddressLength
5255
wiggleTime = uint64(1) // second, Random delay (per signer) to allow concurrent signers
@@ -188,7 +191,8 @@ func ParliaRLP(header *types.Header, chainId *big.Int) []byte {
188191
type Parlia struct {
189192
chainConfig *params.ChainConfig // Chain config
190193
config *params.ParliaConfig // Consensus engine configuration parameters for parlia consensus
191-
db ethdb.Database // Database to store and retrieve snapshot checkpoints
194+
genesisHash common.Hash
195+
db ethdb.Database // Database to store and retrieve snapshot checkpoints
192196

193197
recentSnaps *lru.ARCCache // Snapshots for recent block to speed up
194198
signatures *lru.ARCCache // Signatures of recent blocks to speed up mining
@@ -214,6 +218,7 @@ func New(
214218
chainConfig *params.ChainConfig,
215219
db ethdb.Database,
216220
ethAPI *ethapi.PublicBlockChainAPI,
221+
genesisHash common.Hash,
217222
) *Parlia {
218223
// get parlia config
219224
parliaConfig := chainConfig.Parlia
@@ -243,6 +248,7 @@ func New(
243248
c := &Parlia{
244249
chainConfig: chainConfig,
245250
config: parliaConfig,
251+
genesisHash: genesisHash,
246252
db: db,
247253
ethAPI: ethAPI,
248254
recentSnaps: recentSnaps,
@@ -599,10 +605,12 @@ func (p *Parlia) Prepare(chain consensus.ChainReader, header *types.Header) erro
599605
header.Difficulty = CalcDifficulty(snap, p.val)
600606

601607
// Ensure the extra data has all it's components
602-
if len(header.Extra) < extraVanity {
603-
header.Extra = append(header.Extra, bytes.Repeat([]byte{0x00}, extraVanity-len(header.Extra))...)
608+
if len(header.Extra) < extraVanity-nextForkHashSize {
609+
header.Extra = append(header.Extra, bytes.Repeat([]byte{0x00}, extraVanity-nextForkHashSize-len(header.Extra))...)
604610
}
605-
header.Extra = header.Extra[:extraVanity]
611+
header.Extra = header.Extra[:extraVanity-nextForkHashSize]
612+
nextForkHash := forkid.NextForkHash(p.chainConfig, p.genesisHash, number)
613+
header.Extra = append(header.Extra, nextForkHash[:]...)
606614

607615
if number%p.config.Epoch == 0 {
608616
newValidators, err := p.getCurrentValidators(header.ParentHash)
@@ -638,6 +646,16 @@ func (p *Parlia) Prepare(chain consensus.ChainReader, header *types.Header) erro
638646
// rewards given.
639647
func (p *Parlia) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs *[]*types.Transaction,
640648
uncles []*types.Header, receipts *[]*types.Receipt, systemTxs *[]*types.Transaction, usedGas *uint64) error {
649+
// warn if not in majority fork
650+
number := header.Number.Uint64()
651+
snap, err := p.snapshot(chain, number-1, header.ParentHash, nil)
652+
if err != nil {
653+
panic(err)
654+
}
655+
nextForkHash := forkid.NextForkHash(p.chainConfig, p.genesisHash, number)
656+
if !snap.isMajorityFork(hex.EncodeToString(nextForkHash[:])) {
657+
log.Warn("there is a possible fork, and your client is not the majority. Please check...", "nextForkHash", hex.EncodeToString(nextForkHash[:]))
658+
}
641659
// If the block is a epoch end block, verify the validator list
642660
// The verification can only be done when the state is ready, it can't be done in VerifyHeader.
643661
if header.Number.Uint64()%p.config.Epoch == 0 {
@@ -666,11 +684,6 @@ func (p *Parlia) Finalize(chain consensus.ChainReader, header *types.Header, sta
666684
}
667685
}
668686
if header.Difficulty.Cmp(diffInTurn) != 0 {
669-
number := header.Number.Uint64()
670-
snap, err := p.snapshot(chain, number-1, header.ParentHash, nil)
671-
if err != nil {
672-
panic(err)
673-
}
674687
spoiledVal := snap.supposeValidator()
675688
signedRecently := false
676689
for _, recent := range snap.Recents {
@@ -689,7 +702,7 @@ func (p *Parlia) Finalize(chain consensus.ChainReader, header *types.Header, sta
689702
}
690703
}
691704
val := header.Coinbase
692-
err := p.distributeIncoming(val, state, header, cx, txs, receipts, systemTxs, usedGas, false)
705+
err = p.distributeIncoming(val, state, header, cx, txs, receipts, systemTxs, usedGas, false)
693706
if err != nil {
694707
panic(err)
695708
}

consensus/parlia/snapshot.go

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package parlia
1818

1919
import (
2020
"bytes"
21+
"encoding/hex"
2122
"encoding/json"
2223
"errors"
2324
"math/big"
@@ -38,10 +39,11 @@ type Snapshot struct {
3839
ethAPI *ethapi.PublicBlockChainAPI
3940
sigCache *lru.ARCCache // Cache of recent block signatures to speed up ecrecover
4041

41-
Number uint64 `json:"number"` // Block number where the snapshot was created
42-
Hash common.Hash `json:"hash"` // Block hash where the snapshot was created
43-
Validators map[common.Address]struct{} `json:"validators"` // Set of authorized validators at this moment
44-
Recents map[uint64]common.Address `json:"recents"` // Set of recent validators for spam protections
42+
Number uint64 `json:"number"` // Block number where the snapshot was created
43+
Hash common.Hash `json:"hash"` // Block hash where the snapshot was created
44+
Validators map[common.Address]struct{} `json:"validators"` // Set of authorized validators at this moment
45+
Recents map[uint64]common.Address `json:"recents"` // Set of recent validators for spam protections
46+
RecentForkHashes map[uint64]string `json:"recent_fork_hashes"` // Set of recent forkHash
4547
}
4648

4749
// newSnapshot creates a new snapshot with the specified startup parameters. This
@@ -56,13 +58,14 @@ func newSnapshot(
5658
ethAPI *ethapi.PublicBlockChainAPI,
5759
) *Snapshot {
5860
snap := &Snapshot{
59-
config: config,
60-
ethAPI: ethAPI,
61-
sigCache: sigCache,
62-
Number: number,
63-
Hash: hash,
64-
Recents: make(map[uint64]common.Address),
65-
Validators: make(map[common.Address]struct{}),
61+
config: config,
62+
ethAPI: ethAPI,
63+
sigCache: sigCache,
64+
Number: number,
65+
Hash: hash,
66+
Recents: make(map[uint64]common.Address),
67+
RecentForkHashes: make(map[uint64]string),
68+
Validators: make(map[common.Address]struct{}),
6669
}
6770
for _, v := range validators {
6871
snap.Validators[v] = struct{}{}
@@ -106,13 +109,14 @@ func (s *Snapshot) store(db ethdb.Database) error {
106109
// copy creates a deep copy of the snapshot
107110
func (s *Snapshot) copy() *Snapshot {
108111
cpy := &Snapshot{
109-
config: s.config,
110-
ethAPI: s.ethAPI,
111-
sigCache: s.sigCache,
112-
Number: s.Number,
113-
Hash: s.Hash,
114-
Validators: make(map[common.Address]struct{}),
115-
Recents: make(map[uint64]common.Address),
112+
config: s.config,
113+
ethAPI: s.ethAPI,
114+
sigCache: s.sigCache,
115+
Number: s.Number,
116+
Hash: s.Hash,
117+
Validators: make(map[common.Address]struct{}),
118+
Recents: make(map[uint64]common.Address),
119+
RecentForkHashes: make(map[uint64]string),
116120
}
117121

118122
for v := range s.Validators {
@@ -121,9 +125,22 @@ func (s *Snapshot) copy() *Snapshot {
121125
for block, v := range s.Recents {
122126
cpy.Recents[block] = v
123127
}
128+
for block, id := range s.RecentForkHashes {
129+
cpy.RecentForkHashes[block] = id
130+
}
124131
return cpy
125132
}
126133

134+
func (s *Snapshot) isMajorityFork(forkHash string) bool {
135+
ally := 0
136+
for _, h := range s.RecentForkHashes {
137+
if h == forkHash {
138+
ally++
139+
}
140+
}
141+
return ally > len(s.RecentForkHashes)/2
142+
}
143+
127144
func (s *Snapshot) apply(headers []*types.Header, chain consensus.ChainReader, parents []*types.Header, chainId *big.Int) (*Snapshot, error) {
128145
// Allow passing in no headers for cleaner code
129146
if len(headers) == 0 {
@@ -153,6 +170,9 @@ func (s *Snapshot) apply(headers []*types.Header, chain consensus.ChainReader, p
153170
if limit := uint64(len(snap.Validators)/2 + 1); number >= limit {
154171
delete(snap.Recents, number-limit)
155172
}
173+
if limit := uint64(len(snap.Validators)); number >= limit {
174+
delete(snap.RecentForkHashes, number-limit)
175+
}
156176
// Resolve the authorization key and check against signers
157177
validator, err := ecrecover(header, s.sigCache, chainId)
158178
if err != nil {
@@ -191,8 +211,16 @@ func (s *Snapshot) apply(headers []*types.Header, chain consensus.ChainReader, p
191211
delete(snap.Recents, number-uint64(newLimit)-uint64(i))
192212
}
193213
}
214+
oldLimit = len(snap.Validators)
215+
newLimit = len(newVals)
216+
if newLimit < oldLimit {
217+
for i := 0; i < oldLimit-newLimit; i++ {
218+
delete(snap.RecentForkHashes, number-uint64(newLimit)-uint64(i))
219+
}
220+
}
194221
snap.Validators = newVals
195222
}
223+
snap.RecentForkHashes[number] = hex.EncodeToString(header.Extra[extraVanity-nextForkHashSize : extraVanity])
196224
}
197225
snap.Number += uint64(len(headers))
198226
snap.Hash = headers[len(headers)-1].Hash()

core/forkid/forkid.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,28 @@ func NewID(chain *core.BlockChain) ID {
6262
)
6363
}
6464

65+
func NextForkHash(config *params.ChainConfig, genesis common.Hash, head uint64) [4]byte {
66+
// Calculate the starting checksum from the genesis hash
67+
hash := crc32.ChecksumIEEE(genesis[:])
68+
69+
// Calculate the current fork checksum and the next fork block
70+
var next uint64
71+
for _, fork := range gatherForks(config) {
72+
if fork <= head {
73+
// Fork already passed, checksum the previous hash and the fork number
74+
hash = checksumUpdate(hash, fork)
75+
continue
76+
}
77+
next = fork
78+
break
79+
}
80+
if next == 0 {
81+
return checksumToBytes(hash)
82+
} else {
83+
return checksumToBytes(checksumUpdate(hash, next))
84+
}
85+
}
86+
6587
// newID is the internal version of NewID, which takes extracted values as its
6688
// arguments instead of a chain. The reason is to allow testing the IDs without
6789
// having to simulate an entire blockchain.

eth/backend.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
158158

159159
eth.APIBackend = &EthAPIBackend{ctx.ExtRPCEnabled(), eth, nil}
160160
ethAPI := ethapi.NewPublicBlockChainAPI(eth.APIBackend)
161-
eth.engine = CreateConsensusEngine(ctx, chainConfig, &config.Ethash, config.Miner.Notify, config.Miner.Noverify, chainDb, ethAPI)
161+
eth.engine = CreateConsensusEngine(ctx, chainConfig, &config.Ethash, config.Miner.Notify, config.Miner.Noverify, chainDb, ethAPI, genesisHash)
162162

163163
bcVersion := rawdb.ReadDatabaseVersion(chainDb)
164164
var dbVer = "<nil>"
@@ -243,21 +243,21 @@ func makeExtraData(extra []byte) []byte {
243243
runtime.GOOS,
244244
})
245245
}
246-
if uint64(len(extra)) > params.MaximumExtraDataSize {
247-
log.Warn("Miner extra data exceed limit", "extra", hexutil.Bytes(extra), "limit", params.MaximumExtraDataSize)
246+
if uint64(len(extra)) > params.MaximumExtraDataSize-params.ForkIDSize {
247+
log.Warn("Miner extra data exceed limit", "extra", hexutil.Bytes(extra), "limit", params.MaximumExtraDataSize-params.ForkIDSize)
248248
extra = nil
249249
}
250250
return extra
251251
}
252252

253253
// CreateConsensusEngine creates the required type of consensus engine instance for an Ethereum service
254-
func CreateConsensusEngine(ctx *node.ServiceContext, chainConfig *params.ChainConfig, config *ethash.Config, notify []string, noverify bool, db ethdb.Database, ee *ethapi.PublicBlockChainAPI) consensus.Engine {
254+
func CreateConsensusEngine(ctx *node.ServiceContext, chainConfig *params.ChainConfig, config *ethash.Config, notify []string, noverify bool, db ethdb.Database, ee *ethapi.PublicBlockChainAPI, genesisHash common.Hash) consensus.Engine {
255255
// If proof-of-authority is requested, set it up
256256
if chainConfig.Clique != nil {
257257
return clique.New(chainConfig.Clique, db)
258258
}
259259
if chainConfig.Parlia != nil {
260-
return parlia.New(chainConfig, db, ee)
260+
return parlia.New(chainConfig, db, ee, genesisHash)
261261
}
262262
// Otherwise assume proof-of-work
263263
switch config.PowMode {

les/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
102102
eventMux: ctx.EventMux,
103103
reqDist: newRequestDistributor(peers, &mclock.System{}),
104104
accountManager: ctx.AccountManager,
105-
engine: eth.CreateConsensusEngine(ctx, chainConfig, &config.Ethash, nil, false, chainDb, nil),
105+
engine: eth.CreateConsensusEngine(ctx, chainConfig, &config.Ethash, nil, false, chainDb, nil, genesisHash),
106106
bloomRequests: make(chan chan *bloombits.Retrieval),
107107
bloomIndexer: eth.NewBloomIndexer(chainDb, params.BloomBitsBlocksClient, params.HelperTrieConfirmations),
108108
serverPool: newServerPool(chainDb, config.UltraLightServers),

params/protocol_params.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const (
2424
GenesisGasLimit uint64 = 4712388 // Gas limit of the Genesis block.
2525

2626
MaximumExtraDataSize uint64 = 32 // Maximum size extra data may be after Genesis.
27+
ForkIDSize uint64 = 4 // The length of fork id
2728
ExpByteGas uint64 = 10 // Times ceil(log256(exponent)) for the EXP instruction.
2829
SloadGas uint64 = 50 // Multiplied by the number of 32-byte words that are copied (round up) for any *COPY operation and added.
2930
CallValueTransferGas uint64 = 9000 // Paid for CALL when the value transfer is non-zero.

0 commit comments

Comments
 (0)