Skip to content

Commit e83397e

Browse files
authored
Merge pull request ethereum#4 from binance-chain/issue3
[R4R]fix validator failed to sync a block produced by itself, resolve ethereum#3
2 parents fd8c9c0 + ed9b28f commit e83397e

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

consensus/parlia/parlia.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ func (p *Parlia) Finalize(chain consensus.ChainReader, header *types.Header, sta
659659
// No block rewards in PoA, so the state remains as is and uncles are dropped
660660
cx := chainContext{Chain: chain, parlia: p}
661661
if header.Number.Cmp(common.Big1) == 0 {
662-
err := p.initContract(state, header, cx, txs, receipts, systemTxs, usedGas)
662+
err := p.initContract(state, header, cx, txs, receipts, systemTxs, usedGas, false)
663663
if err != nil {
664664
log.Error("init contract failed")
665665
}
@@ -680,14 +680,14 @@ func (p *Parlia) Finalize(chain consensus.ChainReader, header *types.Header, sta
680680
}
681681
if !signedRecently {
682682
log.Info("slash validator", "block hash", header.Hash(), "address", spoiledVal)
683-
err = p.slash(spoiledVal, state, header, cx, txs, receipts, systemTxs, usedGas)
683+
err = p.slash(spoiledVal, state, header, cx, txs, receipts, systemTxs, usedGas, false)
684684
if err != nil {
685685
panic(err)
686686
}
687687
}
688688
}
689689
val := header.Coinbase
690-
err := p.distributeIncoming(val, state, header, cx, txs, receipts, systemTxs, usedGas)
690+
err := p.distributeIncoming(val, state, header, cx, txs, receipts, systemTxs, usedGas, false)
691691
if err != nil {
692692
panic(err)
693693
}
@@ -712,7 +712,7 @@ func (p *Parlia) FinalizeAndAssemble(chain consensus.ChainReader, header *types.
712712
receipts = make([]*types.Receipt, 0)
713713
}
714714
if header.Number.Cmp(common.Big1) == 0 {
715-
err := p.initContract(state, header, cx, &txs, &receipts, nil, &header.GasUsed)
715+
err := p.initContract(state, header, cx, &txs, &receipts, nil, &header.GasUsed, true)
716716
if err != nil {
717717
log.Error("init contract failed")
718718
}
@@ -732,13 +732,13 @@ func (p *Parlia) FinalizeAndAssemble(chain consensus.ChainReader, header *types.
732732
}
733733
}
734734
if !signedRecently {
735-
err = p.slash(spoiledVal, state, header, cx, &txs, &receipts, nil, &header.GasUsed)
735+
err = p.slash(spoiledVal, state, header, cx, &txs, &receipts, nil, &header.GasUsed, true)
736736
if err != nil {
737737
panic(err)
738738
}
739739
}
740740
}
741-
err := p.distributeIncoming(p.val, state, header, cx, &txs, &receipts, nil, &header.GasUsed)
741+
err := p.distributeIncoming(p.val, state, header, cx, &txs, &receipts, nil, &header.GasUsed, true)
742742
if err != nil {
743743
panic(err)
744744
}
@@ -929,7 +929,7 @@ func (p *Parlia) getCurrentValidators(blockHash common.Hash) ([]common.Address,
929929

930930
// slash spoiled validators
931931
func (p *Parlia) distributeIncoming(val common.Address, state *state.StateDB, header *types.Header, chain core.ChainContext,
932-
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64) error {
932+
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64, mining bool) error {
933933
coinbase := header.Coinbase
934934
balance := state.GetBalance(consensus.SystemAddress)
935935
if balance.Cmp(common.Big0) <= 0 {
@@ -943,7 +943,7 @@ func (p *Parlia) distributeIncoming(val common.Address, state *state.StateDB, he
943943
var rewards = new(big.Int)
944944
rewards = rewards.Rsh(balance, systemRewardPercent)
945945
if rewards.Cmp(common.Big0) > 0 {
946-
err := p.distributeToSystem(rewards, state, header, chain, txs, receipts, receivedTxs, usedGas)
946+
err := p.distributeToSystem(rewards, state, header, chain, txs, receipts, receivedTxs, usedGas, mining)
947947
if err != nil {
948948
return err
949949
}
@@ -952,12 +952,12 @@ func (p *Parlia) distributeIncoming(val common.Address, state *state.StateDB, he
952952
}
953953
}
954954
log.Info("distribute to validator contract", "block hash", header.Hash(), "amount", balance)
955-
return p.distributeToValidator(balance, val, state, header, chain, txs, receipts, receivedTxs, usedGas)
955+
return p.distributeToValidator(balance, val, state, header, chain, txs, receipts, receivedTxs, usedGas, mining)
956956
}
957957

958958
// slash spoiled validators
959959
func (p *Parlia) slash(spoiledVal common.Address, state *state.StateDB, header *types.Header, chain core.ChainContext,
960-
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64) error {
960+
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64, mining bool) error {
961961
// method
962962
method := "slash"
963963

@@ -972,12 +972,12 @@ func (p *Parlia) slash(spoiledVal common.Address, state *state.StateDB, header *
972972
// get system message
973973
msg := p.getSystemMessage(header.Coinbase, common.HexToAddress(SlashContract), data, common.Big0)
974974
// apply message
975-
return p.applyTransaction(msg, state, header, chain, txs, receipts, receivedTxs, usedGas)
975+
return p.applyTransaction(msg, state, header, chain, txs, receipts, receivedTxs, usedGas, mining)
976976
}
977977

978978
// init contract
979979
func (p *Parlia) initContract(state *state.StateDB, header *types.Header, chain core.ChainContext,
980-
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64) error {
980+
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64, mining bool) error {
981981
// method
982982
method := "init"
983983
// contracts
@@ -992,7 +992,7 @@ func (p *Parlia) initContract(state *state.StateDB, header *types.Header, chain
992992
msg := p.getSystemMessage(header.Coinbase, common.HexToAddress(c), data, common.Big0)
993993
// apply message
994994
log.Info("init contract", "block hash", header.Hash(), "contract", c)
995-
err = p.applyTransaction(msg, state, header, chain, txs, receipts, receivedTxs, usedGas)
995+
err = p.applyTransaction(msg, state, header, chain, txs, receipts, receivedTxs, usedGas, mining)
996996
if err != nil {
997997
return err
998998
}
@@ -1001,17 +1001,17 @@ func (p *Parlia) initContract(state *state.StateDB, header *types.Header, chain
10011001
}
10021002

10031003
func (p *Parlia) distributeToSystem(amount *big.Int, state *state.StateDB, header *types.Header, chain core.ChainContext,
1004-
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64) error {
1004+
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64, mining bool) error {
10051005
// get system message
10061006
msg := p.getSystemMessage(header.Coinbase, common.HexToAddress(SystemRewardContract), nil, amount)
10071007
// apply message
1008-
return p.applyTransaction(msg, state, header, chain, txs, receipts, receivedTxs, usedGas)
1008+
return p.applyTransaction(msg, state, header, chain, txs, receipts, receivedTxs, usedGas, mining)
10091009
}
10101010

10111011
// slash spoiled validators
10121012
func (p *Parlia) distributeToValidator(amount *big.Int, validator common.Address,
10131013
state *state.StateDB, header *types.Header, chain core.ChainContext,
1014-
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64) error {
1014+
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64, mining bool) error {
10151015
// method
10161016
method := "deposit"
10171017

@@ -1026,7 +1026,7 @@ func (p *Parlia) distributeToValidator(amount *big.Int, validator common.Address
10261026
// get system message
10271027
msg := p.getSystemMessage(header.Coinbase, common.HexToAddress(ValidatorContract), data, amount)
10281028
// apply message
1029-
return p.applyTransaction(msg, state, header, chain, txs, receipts, receivedTxs, usedGas)
1029+
return p.applyTransaction(msg, state, header, chain, txs, receipts, receivedTxs, usedGas, mining)
10301030
}
10311031

10321032
// get system message
@@ -1049,13 +1049,13 @@ func (p *Parlia) applyTransaction(
10491049
header *types.Header,
10501050
chainContext core.ChainContext,
10511051
txs *[]*types.Transaction, receipts *[]*types.Receipt,
1052-
receivedTxs *[]*types.Transaction, usedGas *uint64,
1052+
receivedTxs *[]*types.Transaction, usedGas *uint64, mining bool,
10531053
) (err error) {
10541054
nonce := state.GetNonce(msg.From())
10551055
expectedTx := types.NewTransaction(nonce, *msg.To(), msg.Value(), msg.Gas(), msg.GasPrice(), msg.Data())
10561056
expectedHash := p.signer.Hash(expectedTx)
10571057

1058-
if msg.From() == p.val {
1058+
if msg.From() == p.val && mining {
10591059
expectedTx, err = p.signTxFn(accounts.Account{Address: msg.From()}, expectedTx, p.chainConfig.ChainID)
10601060
if err != nil {
10611061
return err

0 commit comments

Comments
 (0)