Skip to content

Commit

Permalink
consortium/v2: distribute finality reward for the voters in parent bl…
Browse files Browse the repository at this point in the history
…ock (#337)

* consortium/v2: distribute finality reward for the voters in parent block

Because the finality votes are assembled after we FinalizeAndAssemble the block
and the finality reward distribution transaction is in FinalizeAndAssemble, we
cannot reward the finality voters in the current block right away. Instead, the
reward distribution is delayed until next block. So in the FinalizeAndAssemble,
we look at the finality voters in the parent header and distribute reward to
them.

* consortium/v2: integrate with the finality tracking contract

This commit integrates with the finality tracking contract
(axieinfinity/ronin-dpos-contracts#279) to record the
finality reward for voters.
  • Loading branch information
minh-bq authored Sep 6, 2023
1 parent 4a794d8 commit 57f25f0
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 6 deletions.
12 changes: 10 additions & 2 deletions consensus/consortium/common/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/consensus"
finalityTracking "github.com/ethereum/go-ethereum/consensus/consortium/generated_contracts/finality_tracking"
"github.com/ethereum/go-ethereum/consensus/consortium/generated_contracts/profile"
roninValidatorSet "github.com/ethereum/go-ethereum/consensus/consortium/generated_contracts/ronin_validator_set"
slashIndicator "github.com/ethereum/go-ethereum/consensus/consortium/generated_contracts/slash_indicator"
Expand Down Expand Up @@ -69,6 +70,7 @@ type ContractIntegrator struct {
roninValidatorSetSC *roninValidatorSet.RoninValidatorSet
slashIndicatorSC *slashIndicator.SlashIndicator
profileSC *profile.Profile
finalityTrackingSC *finalityTracking.FinalityTracking
signTxFn SignerTxFn
coinbase common.Address
}
Expand All @@ -93,11 +95,18 @@ func NewContractIntegrator(config *chainParams.ChainConfig, backend bind.Contrac
return nil, err
}

// Create Finality Tracking contract instance
finalityTrackingSC, err := finalityTracking.NewFinalityTracking(config.ConsortiumV2Contracts.FinalityTracking, backend)
if err != nil {
return nil, err
}

return &ContractIntegrator{
chainId: config.ChainID,
roninValidatorSetSC: roninValidatorSetSC,
slashIndicatorSC: slashIndicatorSC,
profileSC: profileSC,
finalityTrackingSC: finalityTrackingSC,
signTxFn: signTxFn,
signer: types.LatestSignerForChainID(config.ChainID),
coinbase: coinbase,
Expand Down Expand Up @@ -214,8 +223,7 @@ func (c *ContractIntegrator) Slash(opts *ApplyTransactOpts, spoiledValidator com

func (c *ContractIntegrator) FinalityReward(opts *ApplyTransactOpts, votedValidators []common.Address) error {
nonce := opts.State.GetNonce(c.coinbase)
// FIXME: Change this
tx, err := c.slashIndicatorSC.SlashUnavailability(getTransactionOpts(c.coinbase, nonce, c.chainId, c.signTxFn), common.Address{})
tx, err := c.finalityTrackingSC.RecordFinality(getTransactionOpts(c.coinbase, nonce, c.chainId, c.signTxFn), votedValidators)
if err != nil {
return err
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions consensus/consortium/v2/consortium.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,17 +729,24 @@ func (c *Consortium) processSystemTransactions(chain consensus.ChainHeaderReader

_, _, _, contract := c.readSignerAndContract()

if c.chainConfig.IsShillin(header.Number) {
extraData, err := finality.DecodeExtra(header.Extra, c.chainConfig.IsShillin(header.Number))
// If the parent's block includes the finality votes, distribute reward for the voters
if c.chainConfig.IsShillin(new(big.Int).Sub(header.Number, common.Big1)) {
parentHeader := chain.GetHeaderByHash(header.ParentHash)
extraData, err := finality.DecodeExtra(parentHeader.Extra, true)
if err != nil {
return err
}
if extraData.HasFinalityVote == 1 {
parentSnap, err := c.snapshot(chain, parentHeader.Number.Uint64()-1, parentHeader.ParentHash, nil)
if err != nil {
return err
}

votedValidatorPositions := extraData.FinalityVotedValidators.Indices()
var votedValidators []common.Address
for _, position := range votedValidatorPositions {
// The header has been verified so there must be no out of bound here
votedValidators = append(votedValidators, snap.ValidatorsWithBlsPub[position].Address)
votedValidators = append(votedValidators, parentSnap.ValidatorsWithBlsPub[position].Address)
}

if err := contract.FinalityReward(transactOpts, votedValidators); err != nil {
Expand Down
9 changes: 8 additions & 1 deletion params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ type ConsortiumV2Contracts struct {
RoninValidatorSet common.Address `json:"roninValidatorSet"`
SlashIndicator common.Address `json:"slashIndicator"`
ProfileContract common.Address `json:"profileContract"`
FinalityTracking common.Address `json:"finalityTracking"`
}

func (c *ConsortiumV2Contracts) IsSystemContract(address common.Address) bool {
Expand Down Expand Up @@ -619,11 +620,16 @@ func (c *ChainConfig) String() string {
profileContract = c.ConsortiumV2Contracts.ProfileContract
}

finalityTrackingContract := common.HexToAddress("")
if c.ConsortiumV2Contracts != nil {
finalityTrackingContract = c.ConsortiumV2Contracts.FinalityTracking
}

chainConfigFmt := "{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v Byzantium: %v Constantinople: %v "
chainConfigFmt += "Petersburg: %v Istanbul: %v, Odysseus: %v, Fenix: %v, Muir Glacier: %v, Berlin: %v, London: %v, Arrow Glacier: %v, "
chainConfigFmt += "Engine: %v, Blacklist Contract: %v, Fenix Validator Contract: %v, ConsortiumV2: %v, ConsortiumV2.RoninValidatorSet: %v, "
chainConfigFmt += "ConsortiumV2.SlashIndicator: %v, ConsortiumV2.StakingContract: %v, Puffy: %v, Buba: %v, Olek: %v, Shillin: %v, "
chainConfigFmt += "ConsortiumV2.ProfileContract: %v}"
chainConfigFmt += "ConsortiumV2.ProfileContract: %v, ConsortiumV2.FinalityTracking: %v}"

return fmt.Sprintf(chainConfigFmt,
c.ChainID,
Expand Down Expand Up @@ -655,6 +661,7 @@ func (c *ChainConfig) String() string {
c.OlekBlock,
c.ShillinBlock,
profileContract.Hex(),
finalityTrackingContract.Hex(),
)
}

Expand Down

0 comments on commit 57f25f0

Please sign in to comment.