From 57f25f0dd6ed8bec8cbadc5a3378547745fc93c5 Mon Sep 17 00:00:00 2001 From: minh-bq <97180373+minh-bq@users.noreply.github.com> Date: Wed, 6 Sep 2023 11:15:42 +0700 Subject: [PATCH] consortium/v2: distribute finality reward for the voters in parent block (#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 (https://github.com/axieinfinity/ronin-dpos-contracts/pull/279) to record the finality reward for voters. --- consensus/consortium/common/contract.go | 12 +- .../finality_tracking/finality_tracking.go | 202 ++++++++++++++++++ consensus/consortium/v2/consortium.go | 13 +- params/config.go | 9 +- 4 files changed, 230 insertions(+), 6 deletions(-) create mode 100644 consensus/consortium/generated_contracts/finality_tracking/finality_tracking.go diff --git a/consensus/consortium/common/contract.go b/consensus/consortium/common/contract.go index ed7a03beaa..8d5733aebe 100644 --- a/consensus/consortium/common/contract.go +++ b/consensus/consortium/common/contract.go @@ -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" @@ -69,6 +70,7 @@ type ContractIntegrator struct { roninValidatorSetSC *roninValidatorSet.RoninValidatorSet slashIndicatorSC *slashIndicator.SlashIndicator profileSC *profile.Profile + finalityTrackingSC *finalityTracking.FinalityTracking signTxFn SignerTxFn coinbase common.Address } @@ -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, @@ -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 } diff --git a/consensus/consortium/generated_contracts/finality_tracking/finality_tracking.go b/consensus/consortium/generated_contracts/finality_tracking/finality_tracking.go new file mode 100644 index 0000000000..4c9c54c133 --- /dev/null +++ b/consensus/consortium/generated_contracts/finality_tracking/finality_tracking.go @@ -0,0 +1,202 @@ +// Code generated - DO NOT EDIT. +// This file is a generated binding and any manual changes will be lost. + +package finalityTracking + +import ( + "errors" + "math/big" + "strings" + + ethereum "github.com/ethereum/go-ethereum" + "github.com/ethereum/go-ethereum/accounts/abi" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/event" +) + +// Reference imports to suppress errors if they are not otherwise used. +var ( + _ = errors.New + _ = big.NewInt + _ = strings.NewReader + _ = ethereum.NotFound + _ = bind.Bind + _ = common.Big1 + _ = types.BloomLookup + _ = event.NewSubscription + _ = abi.ConvertType +) + +// FinalityTrackingMetaData contains all meta data concerning the FinalityTracking contract. +var FinalityTrackingMetaData = &bind.MetaData{ + ABI: "[{\"inputs\":[{\"internalType\":\"address[]\",\"name\":\"voters\",\"type\":\"address[]\"}],\"name\":\"recordFinality\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]", +} + +// FinalityTrackingABI is the input ABI used to generate the binding from. +// Deprecated: Use FinalityTrackingMetaData.ABI instead. +var FinalityTrackingABI = FinalityTrackingMetaData.ABI + +// FinalityTracking is an auto generated Go binding around an Ethereum contract. +type FinalityTracking struct { + FinalityTrackingCaller // Read-only binding to the contract + FinalityTrackingTransactor // Write-only binding to the contract + FinalityTrackingFilterer // Log filterer for contract events +} + +// FinalityTrackingCaller is an auto generated read-only Go binding around an Ethereum contract. +type FinalityTrackingCaller struct { + contract *bind.BoundContract // Generic contract wrapper for the low level calls +} + +// FinalityTrackingTransactor is an auto generated write-only Go binding around an Ethereum contract. +type FinalityTrackingTransactor struct { + contract *bind.BoundContract // Generic contract wrapper for the low level calls +} + +// FinalityTrackingFilterer is an auto generated log filtering Go binding around an Ethereum contract events. +type FinalityTrackingFilterer struct { + contract *bind.BoundContract // Generic contract wrapper for the low level calls +} + +// FinalityTrackingSession is an auto generated Go binding around an Ethereum contract, +// with pre-set call and transact options. +type FinalityTrackingSession struct { + Contract *FinalityTracking // Generic contract binding to set the session for + CallOpts bind.CallOpts // Call options to use throughout this session + TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session +} + +// FinalityTrackingCallerSession is an auto generated read-only Go binding around an Ethereum contract, +// with pre-set call options. +type FinalityTrackingCallerSession struct { + Contract *FinalityTrackingCaller // Generic contract caller binding to set the session for + CallOpts bind.CallOpts // Call options to use throughout this session +} + +// FinalityTrackingTransactorSession is an auto generated write-only Go binding around an Ethereum contract, +// with pre-set transact options. +type FinalityTrackingTransactorSession struct { + Contract *FinalityTrackingTransactor // Generic contract transactor binding to set the session for + TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session +} + +// FinalityTrackingRaw is an auto generated low-level Go binding around an Ethereum contract. +type FinalityTrackingRaw struct { + Contract *FinalityTracking // Generic contract binding to access the raw methods on +} + +// FinalityTrackingCallerRaw is an auto generated low-level read-only Go binding around an Ethereum contract. +type FinalityTrackingCallerRaw struct { + Contract *FinalityTrackingCaller // Generic read-only contract binding to access the raw methods on +} + +// FinalityTrackingTransactorRaw is an auto generated low-level write-only Go binding around an Ethereum contract. +type FinalityTrackingTransactorRaw struct { + Contract *FinalityTrackingTransactor // Generic write-only contract binding to access the raw methods on +} + +// NewFinalityTracking creates a new instance of FinalityTracking, bound to a specific deployed contract. +func NewFinalityTracking(address common.Address, backend bind.ContractBackend) (*FinalityTracking, error) { + contract, err := bindFinalityTracking(address, backend, backend, backend) + if err != nil { + return nil, err + } + return &FinalityTracking{FinalityTrackingCaller: FinalityTrackingCaller{contract: contract}, FinalityTrackingTransactor: FinalityTrackingTransactor{contract: contract}, FinalityTrackingFilterer: FinalityTrackingFilterer{contract: contract}}, nil +} + +// NewFinalityTrackingCaller creates a new read-only instance of FinalityTracking, bound to a specific deployed contract. +func NewFinalityTrackingCaller(address common.Address, caller bind.ContractCaller) (*FinalityTrackingCaller, error) { + contract, err := bindFinalityTracking(address, caller, nil, nil) + if err != nil { + return nil, err + } + return &FinalityTrackingCaller{contract: contract}, nil +} + +// NewFinalityTrackingTransactor creates a new write-only instance of FinalityTracking, bound to a specific deployed contract. +func NewFinalityTrackingTransactor(address common.Address, transactor bind.ContractTransactor) (*FinalityTrackingTransactor, error) { + contract, err := bindFinalityTracking(address, nil, transactor, nil) + if err != nil { + return nil, err + } + return &FinalityTrackingTransactor{contract: contract}, nil +} + +// NewFinalityTrackingFilterer creates a new log filterer instance of FinalityTracking, bound to a specific deployed contract. +func NewFinalityTrackingFilterer(address common.Address, filterer bind.ContractFilterer) (*FinalityTrackingFilterer, error) { + contract, err := bindFinalityTracking(address, nil, nil, filterer) + if err != nil { + return nil, err + } + return &FinalityTrackingFilterer{contract: contract}, nil +} + +// bindFinalityTracking binds a generic wrapper to an already deployed contract. +func bindFinalityTracking(address common.Address, caller bind.ContractCaller, transactor bind.ContractTransactor, filterer bind.ContractFilterer) (*bind.BoundContract, error) { + parsed, err := FinalityTrackingMetaData.GetAbi() + if err != nil { + return nil, err + } + return bind.NewBoundContract(address, *parsed, caller, transactor, filterer), nil +} + +// Call invokes the (constant) contract method with params as input values and +// sets the output to result. The result type might be a single field for simple +// returns, a slice of interfaces for anonymous returns and a struct for named +// returns. +func (_FinalityTracking *FinalityTrackingRaw) Call(opts *bind.CallOpts, result *[]interface{}, method string, params ...interface{}) error { + return _FinalityTracking.Contract.FinalityTrackingCaller.contract.Call(opts, result, method, params...) +} + +// Transfer initiates a plain transaction to move funds to the contract, calling +// its default method if one is available. +func (_FinalityTracking *FinalityTrackingRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) { + return _FinalityTracking.Contract.FinalityTrackingTransactor.contract.Transfer(opts) +} + +// Transact invokes the (paid) contract method with params as input values. +func (_FinalityTracking *FinalityTrackingRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) { + return _FinalityTracking.Contract.FinalityTrackingTransactor.contract.Transact(opts, method, params...) +} + +// Call invokes the (constant) contract method with params as input values and +// sets the output to result. The result type might be a single field for simple +// returns, a slice of interfaces for anonymous returns and a struct for named +// returns. +func (_FinalityTracking *FinalityTrackingCallerRaw) Call(opts *bind.CallOpts, result *[]interface{}, method string, params ...interface{}) error { + return _FinalityTracking.Contract.contract.Call(opts, result, method, params...) +} + +// Transfer initiates a plain transaction to move funds to the contract, calling +// its default method if one is available. +func (_FinalityTracking *FinalityTrackingTransactorRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) { + return _FinalityTracking.Contract.contract.Transfer(opts) +} + +// Transact invokes the (paid) contract method with params as input values. +func (_FinalityTracking *FinalityTrackingTransactorRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) { + return _FinalityTracking.Contract.contract.Transact(opts, method, params...) +} + +// RecordFinality is a paid mutator transaction binding the contract method 0xc245db0f. +// +// Solidity: function recordFinality(address[] voters) returns() +func (_FinalityTracking *FinalityTrackingTransactor) RecordFinality(opts *bind.TransactOpts, voters []common.Address) (*types.Transaction, error) { + return _FinalityTracking.contract.Transact(opts, "recordFinality", voters) +} + +// RecordFinality is a paid mutator transaction binding the contract method 0xc245db0f. +// +// Solidity: function recordFinality(address[] voters) returns() +func (_FinalityTracking *FinalityTrackingSession) RecordFinality(voters []common.Address) (*types.Transaction, error) { + return _FinalityTracking.Contract.RecordFinality(&_FinalityTracking.TransactOpts, voters) +} + +// RecordFinality is a paid mutator transaction binding the contract method 0xc245db0f. +// +// Solidity: function recordFinality(address[] voters) returns() +func (_FinalityTracking *FinalityTrackingTransactorSession) RecordFinality(voters []common.Address) (*types.Transaction, error) { + return _FinalityTracking.Contract.RecordFinality(&_FinalityTracking.TransactOpts, voters) +} diff --git a/consensus/consortium/v2/consortium.go b/consensus/consortium/v2/consortium.go index 772e5dd263..5b0a8285a3 100644 --- a/consensus/consortium/v2/consortium.go +++ b/consensus/consortium/v2/consortium.go @@ -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 { diff --git a/params/config.go b/params/config.go index 4084857599..be51881b27 100644 --- a/params/config.go +++ b/params/config.go @@ -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 { @@ -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, @@ -655,6 +661,7 @@ func (c *ChainConfig) String() string { c.OlekBlock, c.ShillinBlock, profileContract.Hex(), + finalityTrackingContract.Hex(), ) }