Skip to content

Commit

Permalink
NTN Inflation mechanism (autonity#979)
Browse files Browse the repository at this point in the history
* WIP Inflation controller contract

* Implement reward mechanism for inflation

* New transitory inflation formula

Signed-off-by: yazzaoui <[email protected]>

* Added inflation reserve check

Signed-off-by: yazzaoui <[email protected]>

* WIP: Genesis sequence

Signed-off-by: yazzaoui <[email protected]>

* Genesis Deployment sequence

Building and seems working OK

Signed-off-by: yazzaoui <[email protected]>

* Code review fixes and test fixes

Signed-off-by: yazzaoui <[email protected]>

* Correction for time based calculation

Signed-off-by: yazzaoui <[email protected]>

* Include inflation NTN for slashing rewards

Signed-off-by: yazzaoui <[email protected]>

* Progress test fixes

Signed-off-by: yazzaoui <[email protected]>

* Progress test fixes

Signed-off-by: yazzaoui <[email protected]>

---------

Signed-off-by: yazzaoui <[email protected]>
  • Loading branch information
yazzaoui committed Jun 2, 2024
1 parent e798d0e commit 30a3282
Show file tree
Hide file tree
Showing 41 changed files with 3,077 additions and 659 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ contracts: $(SOLC_BINARY) $(GOBINDATA_BINARY) $(CONTRACTS_DIR)/*.sol $(ABIGEN_BI
@$(call gen-contract,,AutonityTest)
@$(call gen-contract,,AccountabilityTest)
@$(call gen-contract,,UpgradeManager)
@$(call gen-contract,,InflationController)
@$(call gen-contract,asm/,ACU)
@$(call gen-contract,asm/,SupplyControl)
@$(call gen-contract,asm/,Stabilization)
Expand Down Expand Up @@ -171,9 +172,9 @@ APE_VERSION := 0.6.26
HARDHAT_VERSION := 2.19.1
test-contracts-asm: test-contracts-asm-pre
@echo "run tests for the asm contracts"
@cd $(CONTRACTS_BASE_DIR) && ape --verbosity WARNING test --network ::hardhat ./test/asm/acu
@cd $(CONTRACTS_BASE_DIR) && ape --verbosity WARNING test --network ::hardhat ./test/asm/stabilization
@cd $(CONTRACTS_BASE_DIR) && ape --verbosity WARNING test --network ::hardhat ./test/asm/supply_control
@cd $(CONTRACTS_BASE_DIR) && ape --verbosity DEBUG test --network ::hardhat ./test/asm/acu
@cd $(CONTRACTS_BASE_DIR) && ape --verbosity DEBUG test --network ::hardhat ./test/asm/stabilization
@cd $(CONTRACTS_BASE_DIR) && ape --verbosity DEBUG test --network ::hardhat ./test/asm/supply_control

.PHONY: test-contracts-asm-pre
test-contracts-asm-pre:
Expand Down
17 changes: 17 additions & 0 deletions autonity/autonity.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@ type UpgradeManagerContract struct {
EVMContract
}

type InflationControllerContract struct {
EVMContract
}

func NewGenesisEVMContract(genesisEvmProvider GenesisEVMProvider, statedb vm.StateDB, db ethdb.Database, chainConfig *params.ChainConfig) *GenesisEVMContracts {
evmProvider := func(header *types.Header, origin common.Address, statedb vm.StateDB) *vm.EVM {
if header != nil {
Expand Down Expand Up @@ -531,6 +535,14 @@ func NewGenesisEVMContract(genesisEvmProvider GenesisEVMProvider, statedb vm.Sta
chainConfig: chainConfig,
},
},
InflationControllerContract: InflationControllerContract{
EVMContract{
evmProvider: evmProvider,
contractABI: &generated.InflationControllerAbi,
db: db,
chainConfig: chainConfig,
},
},
statedb: statedb,
}
}
Expand All @@ -543,6 +555,7 @@ type GenesisEVMContracts struct {
SupplyControlContract
StabilizationContract
UpgradeManagerContract
InflationControllerContract

statedb vm.StateDB
}
Expand Down Expand Up @@ -613,3 +626,7 @@ func (c *GenesisEVMContracts) DeployStabilizationContract(
func (c *GenesisEVMContracts) DeployUpgradeManagerContract(autonityAddress common.Address, operatorAddress common.Address, bytecode []byte) error {
return c.UpgradeManagerContract.DeployContract(nil, params.DeployerAddress, c.statedb, bytecode, autonityAddress, operatorAddress)
}

func (c *GenesisEVMContracts) DeployInflationControllerContract(bytecode []byte, param InflationControllerParams) error {
return c.InflationControllerContract.DeployContract(nil, params.DeployerAddress, c.statedb, bytecode, param)
}
24 changes: 13 additions & 11 deletions autonity/autonity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,19 +454,21 @@ func randomValidators(count int, randomPercentage int) ([]params.Validator, erro
func autonityTestConfig() AutonityConfig {
config := AutonityConfig{
Policy: AutonityPolicy{
TreasuryFee: new(big.Int).SetUint64(params.TestAutonityContractConfig.TreasuryFee),
MinBaseFee: new(big.Int).SetUint64(params.TestAutonityContractConfig.MinBaseFee),
DelegationRate: new(big.Int).SetUint64(params.TestAutonityContractConfig.DelegationRate),
UnbondingPeriod: new(big.Int).SetUint64(params.TestAutonityContractConfig.UnbondingPeriod),
TreasuryAccount: params.TestAutonityContractConfig.Operator,
TreasuryFee: new(big.Int).SetUint64(params.TestAutonityContractConfig.TreasuryFee),
MinBaseFee: new(big.Int).SetUint64(params.TestAutonityContractConfig.MinBaseFee),
DelegationRate: new(big.Int).SetUint64(params.TestAutonityContractConfig.DelegationRate),
UnbondingPeriod: new(big.Int).SetUint64(params.TestAutonityContractConfig.UnbondingPeriod),
InitialInflationReserve: (*big.Int)(params.TestAutonityContractConfig.InitialInflationReserve),
TreasuryAccount: params.TestAutonityContractConfig.Operator,
},
Contracts: AutonityContracts{
AccountabilityContract: params.AccountabilityContractAddress,
OracleContract: params.OracleContractAddress,
AcuContract: params.ACUContractAddress,
SupplyControlContract: params.SupplyControlContractAddress,
StabilizationContract: params.StabilizationContractAddress,
UpgradeManagerContract: params.UpgradeManagerContractAddress,
AccountabilityContract: params.AccountabilityContractAddress,
OracleContract: params.OracleContractAddress,
AcuContract: params.ACUContractAddress,
SupplyControlContract: params.SupplyControlContractAddress,
StabilizationContract: params.StabilizationContractAddress,
UpgradeManagerContract: params.UpgradeManagerContractAddress,
InflationControllerContract: params.InflationControllerContractAddress,
},
Protocol: AutonityProtocol{
OperatorAccount: params.TestAutonityContractConfig.Operator,
Expand Down
1,048 changes: 854 additions & 194 deletions autonity/bindings.go

Large diffs are not rendered by default.

49 changes: 38 additions & 11 deletions autonity/calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func DeployContracts(genesisConfig *params.ChainConfig, genesisBonds GenesisBond
if err := DeployUpgradeManagerContract(genesisConfig, evmContracts); err != nil {
return fmt.Errorf("error when deploying the upgrade manager contract: %w", err)
}
if err := DeployInflationControllerContract(genesisConfig, evmContracts); err != nil {
return fmt.Errorf("error when deploying the upgrade manager contract: %w", err)
}
return nil
}

Expand Down Expand Up @@ -125,6 +128,28 @@ func DeploySupplyControlContract(config *params.ChainConfig, evmContracts *Genes
return nil
}

func DeployInflationControllerContract(config *params.ChainConfig, evmContracts *GenesisEVMContracts) error {
if config.InflationContractConfig == nil {
log.Info("Config missing, using default parameters for the Inflation Controller contract")
config.InflationContractConfig = params.DefaultInflationControllerGenesis
} else {
config.InflationContractConfig.SetDefaults()
}
param := InflationControllerParams{
IInit: (*big.Int)(config.InflationContractConfig.IInit),
ITrans: (*big.Int)(config.InflationContractConfig.ITrans),
AE: (*big.Int)(config.InflationContractConfig.Ae),
T: (*big.Int)(config.InflationContractConfig.T),
IPerm: (*big.Int)(config.InflationContractConfig.IPerm),
}
if err := evmContracts.DeployInflationControllerContract(generated.InflationControllerBytecode, param); err != nil {
log.Error("DeployInflationControllerContract failed", "err", err)
return fmt.Errorf("failed to deploy inflation controller contract: %w", err)
}
log.Info("Deployed inflation controller contract", "address", params.InflationControllerContractAddress)
return nil
}

func DeployACUContract(config *params.ChainConfig, evmContracts *GenesisEVMContracts) error {
if config.ASM.ACUContractConfig == nil {
log.Info("Config missing, using default parameters for the ACU contract")
Expand Down Expand Up @@ -182,19 +207,21 @@ func DeployAccountabilityContract(config *params.AccountabilityGenesis, evmContr
func DeployAutonityContract(genesisConfig *params.AutonityContractGenesis, genesisBonds GenesisBonds, evmContracts *GenesisEVMContracts) error {
contractConfig := AutonityConfig{
Policy: AutonityPolicy{
TreasuryFee: new(big.Int).SetUint64(genesisConfig.TreasuryFee),
MinBaseFee: new(big.Int).SetUint64(genesisConfig.MinBaseFee),
DelegationRate: new(big.Int).SetUint64(genesisConfig.DelegationRate),
UnbondingPeriod: new(big.Int).SetUint64(genesisConfig.UnbondingPeriod),
TreasuryAccount: genesisConfig.Treasury,
TreasuryFee: new(big.Int).SetUint64(genesisConfig.TreasuryFee),
MinBaseFee: new(big.Int).SetUint64(genesisConfig.MinBaseFee),
DelegationRate: new(big.Int).SetUint64(genesisConfig.DelegationRate),
UnbondingPeriod: new(big.Int).SetUint64(genesisConfig.UnbondingPeriod),
InitialInflationReserve: (*big.Int)(genesisConfig.InitialInflationReserve),
TreasuryAccount: genesisConfig.Treasury,
},
Contracts: AutonityContracts{
AccountabilityContract: params.AccountabilityContractAddress,
OracleContract: params.OracleContractAddress,
AcuContract: params.ACUContractAddress,
SupplyControlContract: params.SupplyControlContractAddress,
StabilizationContract: params.StabilizationContractAddress,
UpgradeManagerContract: params.UpgradeManagerContractAddress,
AccountabilityContract: params.AccountabilityContractAddress,
OracleContract: params.OracleContractAddress,
AcuContract: params.ACUContractAddress,
SupplyControlContract: params.SupplyControlContractAddress,
StabilizationContract: params.StabilizationContractAddress,
UpgradeManagerContract: params.UpgradeManagerContractAddress,
InflationControllerContract: params.InflationControllerContractAddress,
},
Protocol: AutonityProtocol{
OperatorAccount: genesisConfig.Operator,
Expand Down
1 change: 1 addition & 0 deletions autonity/solidity/ape-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ compiler:
- "*package-lock.json"
- "*tsconfig.json"
- "bindings.sol"
- "./*.sol"
13 changes: 12 additions & 1 deletion autonity/solidity/contracts/Accountability.sol
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,22 @@ contract Accountability is IAccountability {
}
}

function distributeRewards(address _validator) payable external onlyAutonity {
/**
* @notice called by the Autonity Contract at block finalization, to reward the reporter of
* a valid proof.
* @param _validator validator account which got slashed.
* @param _ntnReward total amount of ntn to be transferred to the repoter. MUST BE AVAILABLE
* in the accountability contract balance.
*/
function distributeRewards(address _validator, uint256 _ntnReward) payable external onlyAutonity {
// There is an edge-case scenario where slashing events for the
// same accused validator are created during the same epoch.
// In this case we only reward the last reporter.
address _reporterTreasury = autonity.getValidator(beneficiaries[_validator]).treasury;

try autonity.transfer(_reporterTreasury, _ntnReward) {}
catch {}

// if for some reasons, funds can't be transferred to the reporter treasury (sneaky contract)
(bool ok, ) = _reporterTreasury.call{value:msg.value, gas: 2300}("");
// well, too bad, it goes to the autonity global treasury.
Expand Down
Loading

0 comments on commit 30a3282

Please sign in to comment.