Skip to content

Commit

Permalink
eth,les,light,params: fix CHT handling for forked chains (ethereum#95)
Browse files Browse the repository at this point in the history
eth,les,light,params: fix CHT handling for forked chains
  • Loading branch information
whilei authored Jun 10, 2019
2 parents 3d03b04 + 02d629a commit 0bb7fc9
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 53 deletions.
4 changes: 3 additions & 1 deletion eth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,13 @@ func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, ne
if mode == downloader.FastSync && blockchain.CurrentBlock().NumberU64() == 0 {
manager.fastSync = uint32(1)
}

// If we have trusted checkpoints, enforce them on the chain
if checkpoint, ok := params.TrustedCheckpoints[blockchain.Genesis().Hash()]; ok {
if checkpoint := config.TrustedCheckpoint; checkpoint != nil {
manager.checkpointNumber = (checkpoint.SectionIndex+1)*params.CHTFrequency - 1
manager.checkpointHash = checkpoint.SectionHead
}

// Initiate a sub-protocol for every implemented version we can handle
manager.SubProtocols = make([]p2p.Protocol, 0, len(ProtocolVersions))
for i, version := range ProtocolVersions {
Expand Down
11 changes: 6 additions & 5 deletions eth/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,10 +504,12 @@ func testCheckpointChallenge(t *testing.T, syncmode downloader.SyncMode, checkpo

// Initialize a chain and generate a fake CHT if checkpointing is enabled
var (
db = rawdb.NewMemoryDatabase()
config = new(params.ChainConfig)
genesis = (&core.Genesis{Config: config}).MustCommit(db)
db = rawdb.NewMemoryDatabase()
config = new(params.ChainConfig)
)

(&core.Genesis{Config: config}).MustCommit(db)

// If checkpointing is enabled, create and inject a fake CHT and the corresponding
// chllenge response.
var response *types.Header
Expand All @@ -520,8 +522,7 @@ func testCheckpointChallenge(t *testing.T, syncmode downloader.SyncMode, checkpo
SectionIndex: index,
SectionHead: response.Hash(),
}
params.TrustedCheckpoints[genesis.Hash()] = cht
defer delete(params.TrustedCheckpoints, genesis.Hash())
config.TrustedCheckpoint = cht
}
// Create a checkpoint aware protocol manager
blockchain, err := core.NewBlockChain(db, nil, config, ethash.NewFaker(), vm.Config{}, nil)
Expand Down
2 changes: 1 addition & 1 deletion les/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func NewProtocolManager(
}
if lightSync {
var checkpoint uint64
if cht, ok := params.TrustedCheckpoints[blockchain.Genesis().Hash()]; ok {
if cht := chainConfig.TrustedCheckpoint; cht != nil {
checkpoint = (cht.SectionIndex+1)*params.CHTFrequency - 1
}
manager.downloader = downloader.New(checkpoint, chainDb, nil, manager.eventMux, nil, blockchain, removePeer)
Expand Down
Empty file added les/transactions.rlp
Empty file.
2 changes: 1 addition & 1 deletion light/lightchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func NewLightChain(odr OdrBackend, config *params.ChainConfig, engine consensus.
if bc.genesisBlock == nil {
return nil, core.ErrNoGenesis
}
if cp, ok := params.TrustedCheckpoints[bc.genesisBlock.Hash()]; ok {
if cp := config.TrustedCheckpoint; cp != nil {
bc.addTrustedCheckpoint(cp)
}
if err := bc.loadLastState(); err != nil {
Expand Down
78 changes: 33 additions & 45 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,6 @@ var (
GoerliGenesisHash = common.HexToHash("0xbf7e331f7f7c1dd2e05159666b3bf8bc7a8a3a9eb1d518969eab529dd9b88c1a")
)

// TrustedCheckpoints associates each known checkpoint with the genesis hash of
// the chain it belongs to.
var TrustedCheckpoints = map[common.Hash]*TrustedCheckpoint{
MainnetGenesisHash: MainnetTrustedCheckpoint,
TestnetGenesisHash: TestnetTrustedCheckpoint,
RinkebyGenesisHash: RinkebyTrustedCheckpoint,
GoerliGenesisHash: GoerliTrustedCheckpoint,
}

var (
// MainnetChainConfig is the chain parameters to run a node on the main network.
MainnetChainConfig = &ChainConfig{
Expand All @@ -58,15 +49,13 @@ var (
ConstantinopleBlock: big.NewInt(7280000),
PetersburgBlock: big.NewInt(7280000),
Ethash: new(EthashConfig),
}

// MainnetTrustedCheckpoint contains the light client trusted checkpoint for the main network.
MainnetTrustedCheckpoint = &TrustedCheckpoint{
Name: "mainnet",
SectionIndex: 227,
SectionHead: common.HexToHash("0xa2e0b25d72c2fc6e35a7f853cdacb193b4b4f95c606accf7f8fa8415283582c7"),
CHTRoot: common.HexToHash("0xf69bdd4053b95b61a27b106a0e86103d791edd8574950dc96aa351ab9b9f1aa0"),
BloomRoot: common.HexToHash("0xec1b454d4c6322c78ccedf76ac922a8698c3cac4d98748a84af4995b7bd3d744"),
TrustedCheckpoint: &TrustedCheckpoint{
Name: "mainnet",
SectionIndex: 227,
SectionHead: common.HexToHash("0xa2e0b25d72c2fc6e35a7f853cdacb193b4b4f95c606accf7f8fa8415283582c7"),
CHTRoot: common.HexToHash("0xf69bdd4053b95b61a27b106a0e86103d791edd8574950dc96aa351ab9b9f1aa0"),
BloomRoot: common.HexToHash("0xec1b454d4c6322c78ccedf76ac922a8698c3cac4d98748a84af4995b7bd3d744"),
},
}

// TestnetChainConfig contains the chain parameters to run a node on the Ropsten test network.
Expand All @@ -86,15 +75,13 @@ var (
ConstantinopleBlock: big.NewInt(4230000),
PetersburgBlock: big.NewInt(4939394),
Ethash: new(EthashConfig),
}

// TestnetTrustedCheckpoint contains the light client trusted checkpoint for the Ropsten test network.
TestnetTrustedCheckpoint = &TrustedCheckpoint{
Name: "testnet",
SectionIndex: 161,
SectionHead: common.HexToHash("0x5378afa734e1feafb34bcca1534c4d96952b754579b96a4afb23d5301ecececc"),
CHTRoot: common.HexToHash("0x1cf2b071e7443a62914362486b613ff30f60cea0d9c268ed8c545f876a3ee60c"),
BloomRoot: common.HexToHash("0x5ac25c84bd18a9cbe878d4609a80220f57f85037a112644532412ba0d498a31b"),
TrustedCheckpoint: &TrustedCheckpoint{
Name: "testnet",
SectionIndex: 161,
SectionHead: common.HexToHash("0x5378afa734e1feafb34bcca1534c4d96952b754579b96a4afb23d5301ecececc"),
CHTRoot: common.HexToHash("0x1cf2b071e7443a62914362486b613ff30f60cea0d9c268ed8c545f876a3ee60c"),
BloomRoot: common.HexToHash("0x5ac25c84bd18a9cbe878d4609a80220f57f85037a112644532412ba0d498a31b"),
},
}

// RinkebyChainConfig contains the chain parameters to run a node on the Rinkeby test network.
Expand All @@ -117,15 +104,13 @@ var (
Period: 15,
Epoch: 30000,
},
}

// RinkebyTrustedCheckpoint contains the light client trusted checkpoint for the Rinkeby test network.
RinkebyTrustedCheckpoint = &TrustedCheckpoint{
Name: "rinkeby",
SectionIndex: 125,
SectionHead: common.HexToHash("0x8a738386f6bb34add15846f8f49c4c519a2f32519096e792b9f43bcb407c831c"),
CHTRoot: common.HexToHash("0xa1e5720a9bad4dce794f129e4ac6744398197b652868011486a6f89c8ec84a75"),
BloomRoot: common.HexToHash("0xa3048fe8b7e30f77f11bc755a88478363d7d3e71c2bdfe4e8ab9e269cd804ba2"),
TrustedCheckpoint: &TrustedCheckpoint{
Name: "rinkeby",
SectionIndex: 125,
SectionHead: common.HexToHash("0x8a738386f6bb34add15846f8f49c4c519a2f32519096e792b9f43bcb407c831c"),
CHTRoot: common.HexToHash("0xa1e5720a9bad4dce794f129e4ac6744398197b652868011486a6f89c8ec84a75"),
BloomRoot: common.HexToHash("0xa3048fe8b7e30f77f11bc755a88478363d7d3e71c2bdfe4e8ab9e269cd804ba2"),
},
}

// GoerliChainConfig contains the chain parameters to run a node on the Görli test network.
Expand All @@ -144,15 +129,13 @@ var (
Period: 15,
Epoch: 30000,
},
}

// GoerliTrustedCheckpoint contains the light client trusted checkpoint for the Görli test network.
GoerliTrustedCheckpoint = &TrustedCheckpoint{
Name: "goerli",
SectionIndex: 9,
SectionHead: common.HexToHash("0x8e223d827391eee53b07cb8ee057dbfa11c93e0b45352188c783affd7840a921"),
CHTRoot: common.HexToHash("0xe0a817ac69b36c1e437c5b0cff9e764853f5115702b5f66d451b665d6afb7e78"),
BloomRoot: common.HexToHash("0x50d672aeb655b723284969c7c1201fb6ca003c23ed144bcb9f2d1b30e2971c1b"),
TrustedCheckpoint: &TrustedCheckpoint{
Name: "goerli",
SectionIndex: 9,
SectionHead: common.HexToHash("0x8e223d827391eee53b07cb8ee057dbfa11c93e0b45352188c783affd7840a921"),
CHTRoot: common.HexToHash("0xe0a817ac69b36c1e437c5b0cff9e764853f5115702b5f66d451b665d6afb7e78"),
BloomRoot: common.HexToHash("0x50d672aeb655b723284969c7c1201fb6ca003c23ed144bcb9f2d1b30e2971c1b"),
},
}

// AllEthashProtocolChanges contains every protocol change (EIPs) introduced
Expand Down Expand Up @@ -212,6 +195,7 @@ var (

new(EthashConfig), // Ethash
nil, // Clique
nil,
}

// AllCliqueProtocolChanges contains every protocol change (EIPs) introduced
Expand Down Expand Up @@ -274,6 +258,7 @@ var (
Period: 0,
Epoch: 30000,
},
nil,
}

// TestChainConfig is used for tests.
Expand Down Expand Up @@ -329,6 +314,7 @@ var (

new(EthashConfig), // Ethash
nil, // Clique
nil,
}

// TestRules are all rules from TestChainConfig initialized at 0.
Expand Down Expand Up @@ -465,6 +451,8 @@ type ChainConfig struct {
// Various consensus engines
Ethash *EthashConfig `json:"ethash,omitempty"`
Clique *CliqueConfig `json:"clique,omitempty"`

TrustedCheckpoint *TrustedCheckpoint `json:"trustedCheckpoint"`
}

// EthashConfig is the consensus engine configs for proof-of-work based sealing.
Expand Down

0 comments on commit 0bb7fc9

Please sign in to comment.