Skip to content

Commit 02c9966

Browse files
committed
fix validator failed to sync a block produced by itself
1 parent b72be12 commit 02c9966

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

consensus/parlia/parlia.go

+19-19
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ func (p *Parlia) Finalize(chain consensus.ChainReader, header *types.Header, sta
653653
// No block rewards in PoA, so the state remains as is and uncles are dropped
654654
cx := chainContext{Chain: chain, parlia: p}
655655
if header.Number.Cmp(common.Big1) == 0 {
656-
err := p.initContract(state, header, cx, txs, receipts, systemTxs, usedGas)
656+
err := p.initContract(state, header, cx, txs, receipts, systemTxs, usedGas, false)
657657
if err != nil {
658658
log.Error("init contract failed")
659659
}
@@ -674,14 +674,14 @@ func (p *Parlia) Finalize(chain consensus.ChainReader, header *types.Header, sta
674674
}
675675
if !signedRecently {
676676
log.Info("slash validator", "block hash", header.Hash(), "address", spoiledVal)
677-
err = p.slash(spoiledVal, state, header, cx, txs, receipts, systemTxs, usedGas)
677+
err = p.slash(spoiledVal, state, header, cx, txs, receipts, systemTxs, usedGas, false)
678678
if err != nil {
679679
panic(err)
680680
}
681681
}
682682
}
683683
val := header.Coinbase
684-
err := p.distributeIncoming(val, state, header, cx, txs, receipts, systemTxs, usedGas)
684+
err := p.distributeIncoming(val, state, header, cx, txs, receipts, systemTxs, usedGas, false)
685685
if err != nil {
686686
panic(err)
687687
}
@@ -706,7 +706,7 @@ func (p *Parlia) FinalizeAndAssemble(chain consensus.ChainReader, header *types.
706706
receipts = make([]*types.Receipt, 0)
707707
}
708708
if header.Number.Cmp(common.Big1) == 0 {
709-
err := p.initContract(state, header, cx, &txs, &receipts, nil, &header.GasUsed)
709+
err := p.initContract(state, header, cx, &txs, &receipts, nil, &header.GasUsed, true)
710710
if err != nil {
711711
log.Error("init contract failed")
712712
}
@@ -726,13 +726,13 @@ func (p *Parlia) FinalizeAndAssemble(chain consensus.ChainReader, header *types.
726726
}
727727
}
728728
if !signedRecently {
729-
err = p.slash(spoiledVal, state, header, cx, &txs, &receipts, nil, &header.GasUsed)
729+
err = p.slash(spoiledVal, state, header, cx, &txs, &receipts, nil, &header.GasUsed, true)
730730
if err != nil {
731731
panic(err)
732732
}
733733
}
734734
}
735-
err := p.distributeIncoming(p.val, state, header, cx, &txs, &receipts, nil, &header.GasUsed)
735+
err := p.distributeIncoming(p.val, state, header, cx, &txs, &receipts, nil, &header.GasUsed, true)
736736
if err != nil {
737737
panic(err)
738738
}
@@ -923,7 +923,7 @@ func (p *Parlia) getCurrentValidators(blockHash common.Hash) ([]common.Address,
923923

924924
// slash spoiled validators
925925
func (p *Parlia) distributeIncoming(val common.Address, state *state.StateDB, header *types.Header, chain core.ChainContext,
926-
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64) error {
926+
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64, mine bool) error {
927927
coinbase := header.Coinbase
928928
balance := state.GetBalance(consensus.SystemAddress)
929929
if balance.Cmp(common.Big0) <= 0 {
@@ -937,7 +937,7 @@ func (p *Parlia) distributeIncoming(val common.Address, state *state.StateDB, he
937937
var rewards = new(big.Int)
938938
rewards = rewards.Rsh(balance, systemRewardPercent)
939939
if rewards.Cmp(common.Big0) > 0 {
940-
err := p.distributeToSystem(rewards, state, header, chain, txs, receipts, receivedTxs, usedGas)
940+
err := p.distributeToSystem(rewards, state, header, chain, txs, receipts, receivedTxs, usedGas, mine)
941941
if err != nil {
942942
return err
943943
}
@@ -946,12 +946,12 @@ func (p *Parlia) distributeIncoming(val common.Address, state *state.StateDB, he
946946
}
947947
}
948948
log.Info("distribute to validator contract", "block hash", header.Hash(), "amount", balance)
949-
return p.distributeToValidator(balance, val, state, header, chain, txs, receipts, receivedTxs, usedGas)
949+
return p.distributeToValidator(balance, val, state, header, chain, txs, receipts, receivedTxs, usedGas, mine)
950950
}
951951

952952
// slash spoiled validators
953953
func (p *Parlia) slash(spoiledVal common.Address, state *state.StateDB, header *types.Header, chain core.ChainContext,
954-
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64) error {
954+
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64, mine bool) error {
955955
// method
956956
method := "slash"
957957

@@ -966,12 +966,12 @@ func (p *Parlia) slash(spoiledVal common.Address, state *state.StateDB, header *
966966
// get system message
967967
msg := p.getSystemMessage(header.Coinbase, common.HexToAddress(SlashContract), data, common.Big0)
968968
// apply message
969-
return p.applyTransaction(msg, state, header, chain, txs, receipts, receivedTxs, usedGas)
969+
return p.applyTransaction(msg, state, header, chain, txs, receipts, receivedTxs, usedGas, mine)
970970
}
971971

972972
// init contract
973973
func (p *Parlia) initContract(state *state.StateDB, header *types.Header, chain core.ChainContext,
974-
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64) error {
974+
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64, mine bool) error {
975975
// method
976976
method := "init"
977977
// contracts
@@ -986,7 +986,7 @@ func (p *Parlia) initContract(state *state.StateDB, header *types.Header, chain
986986
msg := p.getSystemMessage(header.Coinbase, common.HexToAddress(c), data, common.Big0)
987987
// apply message
988988
log.Info("init contract", "block hash", header.Hash(), "contract", c)
989-
err = p.applyTransaction(msg, state, header, chain, txs, receipts, receivedTxs, usedGas)
989+
err = p.applyTransaction(msg, state, header, chain, txs, receipts, receivedTxs, usedGas, mine)
990990
if err != nil {
991991
return err
992992
}
@@ -995,17 +995,17 @@ func (p *Parlia) initContract(state *state.StateDB, header *types.Header, chain
995995
}
996996

997997
func (p *Parlia) distributeToSystem(amount *big.Int, state *state.StateDB, header *types.Header, chain core.ChainContext,
998-
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64) error {
998+
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64, mine bool) error {
999999
// get system message
10001000
msg := p.getSystemMessage(header.Coinbase, common.HexToAddress(SystemRewardContract), nil, amount)
10011001
// apply message
1002-
return p.applyTransaction(msg, state, header, chain, txs, receipts, receivedTxs, usedGas)
1002+
return p.applyTransaction(msg, state, header, chain, txs, receipts, receivedTxs, usedGas, mine)
10031003
}
10041004

10051005
// slash spoiled validators
10061006
func (p *Parlia) distributeToValidator(amount *big.Int, validator common.Address,
10071007
state *state.StateDB, header *types.Header, chain core.ChainContext,
1008-
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64) error {
1008+
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64, mine bool) error {
10091009
// method
10101010
method := "deposit"
10111011

@@ -1020,7 +1020,7 @@ func (p *Parlia) distributeToValidator(amount *big.Int, validator common.Address
10201020
// get system message
10211021
msg := p.getSystemMessage(header.Coinbase, common.HexToAddress(ValidatorContract), data, amount)
10221022
// apply message
1023-
return p.applyTransaction(msg, state, header, chain, txs, receipts, receivedTxs, usedGas)
1023+
return p.applyTransaction(msg, state, header, chain, txs, receipts, receivedTxs, usedGas, mine)
10241024
}
10251025

10261026
// get system message
@@ -1043,13 +1043,13 @@ func (p *Parlia) applyTransaction(
10431043
header *types.Header,
10441044
chainContext core.ChainContext,
10451045
txs *[]*types.Transaction, receipts *[]*types.Receipt,
1046-
receivedTxs *[]*types.Transaction, usedGas *uint64,
1046+
receivedTxs *[]*types.Transaction, usedGas *uint64, mine bool,
10471047
) (err error) {
10481048
nonce := state.GetNonce(msg.From())
10491049
expectedTx := types.NewTransaction(nonce, *msg.To(), msg.Value(), msg.Gas(), msg.GasPrice(), msg.Data())
10501050
expectedHash := p.signer.Hash(expectedTx)
10511051

1052-
if msg.From() == p.val {
1052+
if msg.From() == p.val && mine {
10531053
expectedTx, err = p.signTxFn(accounts.Account{Address: msg.From()}, expectedTx, p.chainConfig.ChainID)
10541054
if err != nil {
10551055
return err

0 commit comments

Comments
 (0)