Skip to content

Commit 21d19e9

Browse files
authored
Merge pull request ethereum#32 from QuarkChain/tm_w3q_single_val
Enable single validator mode
2 parents 1f35a08 + 8056ed6 commit 21d19e9

File tree

9 files changed

+61
-25
lines changed

9 files changed

+61
-25
lines changed

cmd/geth/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ var (
147147
utils.RinkebyFlag,
148148
utils.GoerliFlag,
149149
utils.Web3QTestnetFlag,
150+
utils.Web3QGalileoFlag,
150151
utils.Web3QMainnetFlag,
151152
utils.VMEnableDebugFlag,
152153
utils.NetworkIdFlag,

consensus/tendermint/adapter/store.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ type Store struct {
1717
chain *core.BlockChain
1818
governance *gov.Governance
1919
verifyHeaderFunc func(chain consensus.ChainHeaderReader, header *types.Header, seal bool) error
20-
makeBlock func(parentHash common.Hash, timestamp uint64) (block *types.FullBlock)
20+
makeBlock func(parentHash common.Hash, coinbase common.Address, timestamp uint64) (block *types.FullBlock)
2121
}
2222

2323
func NewStore(
2424
chain *core.BlockChain,
2525
governance *gov.Governance,
2626
verifyHeaderFunc func(chain consensus.ChainHeaderReader, header *types.Header, seal bool) error,
27-
makeBlock func(parentHash common.Hash, timestamp uint64) (block *types.FullBlock)) *Store {
27+
makeBlock func(parentHash common.Hash, coinbase common.Address, timestamp uint64) (block *types.FullBlock)) *Store {
2828
return &Store{chain: chain, governance: governance, verifyHeaderFunc: verifyHeaderFunc, makeBlock: makeBlock}
2929
}
3030

@@ -161,6 +161,17 @@ func updateState(
161161
}, nil
162162
}
163163

164-
func (s *Store) MakeBlock(parentHash common.Hash, timestamp uint64) *types.FullBlock {
165-
return s.makeBlock(parentHash, timestamp)
164+
func (s *Store) MakeBlock(state *pbft.ChainState, height uint64,
165+
commit *pbft.Commit,
166+
proposerAddress common.Address) *types.FullBlock {
167+
168+
// Set time.
169+
var timestamp uint64
170+
if height == state.InitialHeight {
171+
timestamp = state.LastBlockTime + 1 // genesis time + 1
172+
} else {
173+
timestamp = pbft.MedianTime(commit, state.LastValidators)
174+
}
175+
176+
return s.makeBlock(state.LastBlockID, proposerAddress, timestamp)
166177
}

consensus/tendermint/gov/gov.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,19 @@ func (g *Governance) NextValidators(height uint64) []common.Address {
3636
return header.NextValidators
3737
}
3838
}
39+
40+
func (g *Governance) NextValidatorPowers(height uint64) []uint64 {
41+
if height%g.epoch != 0 {
42+
return nil
43+
}
44+
45+
switch {
46+
case height == 0:
47+
header := g.chain.GetHeaderByNumber(0)
48+
return header.NextValidatorPowers
49+
default:
50+
// TODO get real validators by calling contract
51+
header := g.chain.GetHeaderByNumber(height - g.epoch)
52+
return header.NextValidatorPowers
53+
}
54+
}

consensus/tendermint/tendermint.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"fmt"
2626
"io/ioutil"
2727
"math/big"
28-
"reflect"
2928
"time"
3029

3130
pbftconsensus "github.com/QuarkChain/go-minimal-pbft/consensus"
@@ -111,7 +110,7 @@ func (c *Tendermint) SetBlockChain(chain *core.BlockChain) {
111110
c.chain = chain
112111
}
113112

114-
func (c *Tendermint) Init(makeBlock func(parent common.Hash, timestamp uint64) (*types.Block, error)) (err error) {
113+
func (c *Tendermint) Init(makeBlock func(parent common.Hash, coinbase common.Address, timestamp uint64) (*types.Block, error)) (err error) {
115114
chain := c.chain
116115
// Outbound gossip message queue
117116
sendC := make(chan pbftconsensus.Message, 1000)
@@ -124,9 +123,9 @@ func (c *Tendermint) Init(makeBlock func(parent common.Hash, timestamp uint64) (
124123
c.rootCtxCancel = rootCtxCancel
125124
c.rootCtx = rootCtx
126125

127-
makeFullBlock := func(parentHash common.Hash, timestamp uint64) *types.FullBlock {
128-
129-
block, err := makeBlock(parentHash, timestamp)
126+
makeFullBlock := func(parentHash common.Hash, coinbase common.Address, timestamp uint64) *types.FullBlock {
127+
log.Info("Making a block", "parent", parentHash)
128+
block, err := makeBlock(parentHash, coinbase, timestamp)
130129
if err != nil {
131130
log.Warn("makeBlock", "err", err)
132131
return nil
@@ -181,11 +180,12 @@ func (c *Tendermint) Init(makeBlock func(parent common.Hash, timestamp uint64) (
181180
c.config.Epoch,
182181
int64(c.config.ProposerRepetition),
183182
)
183+
gcs.LastBlockID = genesis.Hash()
184184

185185
// consensus
186186
consensusState := pbftconsensus.NewConsensusState(
187187
rootCtx,
188-
pbftconsensus.NewDefaultConsesusConfig(),
188+
&c.config.ConsensusConfig,
189189
*gcs,
190190
store,
191191
store,
@@ -280,15 +280,12 @@ func (c *Tendermint) verifyHeader(chain consensus.ChainHeaderReader, header *typ
280280
if header.Time > uint64(time.Now().Unix()) {
281281
return consensus.ErrFutureBlock
282282
}
283-
// Checkpoint blocks need to enforce zero beneficiary
284-
checkpoint := (number % c.config.Epoch) == 0
285-
if checkpoint && header.Coinbase != (common.Address{}) {
286-
return errInvalidCheckpointBeneficiary
287-
}
288283

289-
nextValidators := c.governance.NextValidators(number)
290-
if !reflect.DeepEqual(nextValidators, header.NextValidators) {
291-
return errors.New("invalid NextValidators")
284+
if number%c.config.Epoch != 0 && len(header.NextValidators) != 0 {
285+
return errors.New("NextValidators must be empty for non-epoch block")
286+
}
287+
if len(header.NextValidatorPowers) != len(header.NextValidators) {
288+
return errors.New("NextValidators must have the same len as powers")
292289
}
293290
if !bytes.Equal(header.Nonce[:], nonceDefault) {
294291
return errors.New("invalid nonce")
@@ -303,7 +300,7 @@ func (c *Tendermint) verifyHeader(chain consensus.ChainHeaderReader, header *typ
303300
}
304301
// Ensure that the block's difficulty is meaningful (may not be correct at this point)
305302
if number > 0 {
306-
if header.Difficulty == nil || (header.Difficulty.Cmp(big.NewInt(0)) != 0) {
303+
if header.Difficulty == nil || (header.Difficulty.Cmp(big.NewInt(1)) != 0) {
307304
return errInvalidDifficulty
308305
}
309306
}
@@ -377,8 +374,15 @@ func (c *Tendermint) Prepare(chain consensus.ChainHeaderReader, header *types.He
377374

378375
header.TimeMs = timestamp
379376
header.Time = timestamp / 1000
377+
header.Difficulty = big.NewInt(1)
378+
379+
if (number % c.config.Epoch) != 0 {
380+
header.NextValidators = []common.Address{}
381+
header.NextValidatorPowers = []uint64{}
382+
} else {
383+
header.NextValidators = c.governance.NextValidators(number)
384+
}
380385

381-
header.NextValidators = c.governance.NextValidators(number)
382386
return nil
383387
}
384388

core/blockchain.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2306,6 +2306,8 @@ func (bc *BlockChain) PreExecuteBlock(block *types.Block) (err error) {
23062306
return
23072307
}
23082308

2309+
statedb.StartPrefetcher("pre_execute")
2310+
23092311
txHash := types.DeriveSha(block.Transactions(), trie.NewStackTrie(nil))
23102312
if block.TxHash() != txHash {
23112313
err = fmt.Errorf("txHash mismatch, got %s, expect %s", block.TxHash(), txHash)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.15
44

55
require (
66
github.com/Azure/azure-storage-blob-go v0.7.0
7-
github.com/QuarkChain/go-minimal-pbft v0.0.0-20220303075012-6084b729db36
7+
github.com/QuarkChain/go-minimal-pbft v0.0.0-20220306083522-1c8dc76afb94
88
github.com/VictoriaMetrics/fastcache v1.6.0
99
github.com/aws/aws-sdk-go-v2 v1.2.0
1010
github.com/aws/aws-sdk-go-v2/config v1.1.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ github.com/Kubuxu/go-os-helper v0.0.1/go.mod h1:N8B+I7vPCT80IcP58r50u4+gEEcsZETF
8585
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
8686
github.com/QuarkChain/go-minimal-pbft v0.0.0-20220303075012-6084b729db36 h1:2SJS7tfMZdmXSWpB1DsLIImaq/BKxfaYv9OvOJqY0hw=
8787
github.com/QuarkChain/go-minimal-pbft v0.0.0-20220303075012-6084b729db36/go.mod h1:nC/gDMz8G2h+K5KGDiEwEGJ//D70G8mwOBgHraRUab8=
88+
github.com/QuarkChain/go-minimal-pbft v0.0.0-20220306083522-1c8dc76afb94 h1:T2Ogn+k7G/0hybvozBlps68CWpfvczJ5MT+wgwqKJgQ=
89+
github.com/QuarkChain/go-minimal-pbft v0.0.0-20220306083522-1c8dc76afb94/go.mod h1:nC/gDMz8G2h+K5KGDiEwEGJ//D70G8mwOBgHraRUab8=
8890
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
8991
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
9092
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=

miner/worker.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,9 @@ func recalcRecommit(minRecommit, prev time.Duration, target float64, inc bool) t
409409
return time.Duration(int64(next))
410410
}
411411

412-
func (w *worker) makeBlock(parent common.Hash, timestamp uint64) (*types.Block, error) {
412+
func (w *worker) makeBlock(parent common.Hash, coinbase common.Address, timestamp uint64) (*types.Block, error) {
413413

414-
return w.getSealingBlock(parent, timestamp, w.coinbase, common.Hash{})
414+
return w.getSealingBlock(parent, timestamp, coinbase, common.Hash{})
415415
}
416416

417417
// newWorkLoop is a standalone goroutine to submit new sealing work upon received events.

params/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ var (
301301
P2pBootstrap: "/ip4/127.0.0.1/udp/33333/quic/p2p/12D3KooWRqZRJf6gYeLgeUnNCnKeRb29KiEVQcvRWk2tet9Q3Hmy",
302302
NodeKeyPath: "/Users/qizhou/.ssh/node_galileo.key",
303303
ValKeyPath: "/Users/qizhou/.ssh/val_galileo.key",
304-
consensusConfig: ConsensusConfig{
304+
ConsensusConfig: ConsensusConfig{
305305
// WalPath: filepath.Join(defaultDataDir, "cs.wal", "wal"),
306306
TimeoutPropose: 3000 * time.Millisecond,
307307
TimeoutProposeDelta: 500 * time.Millisecond,
@@ -471,7 +471,7 @@ type TendermintConfig struct {
471471
P2pBootstrap string
472472
NodeName string
473473
ProposerRepetition uint64
474-
consensusConfig ConsensusConfig
474+
ConsensusConfig ConsensusConfig
475475
}
476476

477477
// String implements the stringer interface, returning the consensus engine details.

0 commit comments

Comments
 (0)