Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(misc): examples: validator rewards with ERC20 minting #1200

Merged
merged 148 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
148 commits
Select commit Hold shift + click to select a range
19f93f5
complete flow
May 30, 2024
fcdc70f
convert fendermint to cometbft genesis
Jun 3, 2024
3d8f00f
remove deprecated fields
Jun 3, 2024
8a8efd6
fix cursor
Jun 3, 2024
3890de4
fix genesis app bytes
Jun 3, 2024
14e6dea
fmt
Jun 3, 2024
4dc0482
minor changes
Jun 4, 2024
02b183f
Fix cicd (#1023)
cryptoAtwill Jun 5, 2024
d88face
Fix cicd (#1025)
cryptoAtwill Jun 6, 2024
2a4d428
Fix cicd (#1027)
cryptoAtwill Jun 10, 2024
0e31699
Merge branch 'main' of protocol-github:consensus-shipyard/ipc into si…
Jun 10, 2024
0d1c55c
Merge branch 'simple-genesis' of protocol-github:consensus-shipyard/i…
Jun 10, 2024
d551aed
Merge branch 'main' into simple-genesis
cryptoAtwill Aug 5, 2024
01c8bf1
add block gas limit
Aug 5, 2024
dd2832d
check gas limit in deliver
Aug 6, 2024
2ea9203
review feedback
Aug 7, 2024
b686131
demote mod to file
Aug 7, 2024
22219f1
Update fendermint/vm/interpreter/src/genesis.rs
cryptoAtwill Aug 7, 2024
6a1b4dd
Update fendermint/vm/interpreter/src/genesis.rs
cryptoAtwill Aug 7, 2024
7c6276b
review feedbacks
Aug 7, 2024
c4b7e66
Merge branch 'simple-genesis' of protocol-github:consensus-shipyard/i…
Aug 7, 2024
fa0f330
add genesis app state schema
Aug 7, 2024
b0038cc
update infra and simplify genesis
Aug 8, 2024
22e5958
fmt
Aug 8, 2024
0319141
fix infra
Aug 8, 2024
672ed23
update gas usage
Aug 9, 2024
a57a7df
Merge branch 'simple-genesis' into block-gas-limit
cryptoAtwill Aug 12, 2024
05d79e1
review feedback
Aug 12, 2024
5438147
Update fendermint/vm/interpreter/src/fvm/gas/actor.rs
cryptoAtwill Aug 12, 2024
99926b3
review feedbacks
Aug 13, 2024
7a86832
Merge branch 'main' of protocol-github:consensus-shipyard/ipc into si…
Aug 13, 2024
79d1264
merge with main
Aug 13, 2024
6d15ea6
update based on review
Aug 13, 2024
e7b3e06
update base fee logic
Aug 13, 2024
0ae95c8
Update fevm-contract-tests.yaml
cryptoAtwill Aug 14, 2024
169bc36
fix tests
Aug 14, 2024
fa372d6
Update fevm-contract-tests.yaml
cryptoAtwill Aug 14, 2024
5332a31
Update testnode.toml
cryptoAtwill Aug 14, 2024
ed66780
skip fendermint build
Aug 14, 2024
272c29c
remove pull
Aug 14, 2024
29cda12
prepare and process
Aug 15, 2024
0db23d2
Merge branch 'main' into simple-genesis
raulk Aug 16, 2024
ef2740c
rename to genesis builder
Aug 19, 2024
4ba0219
Merge branch 'simple-genesis' of protocol-github:consensus-shipyard/i…
Aug 19, 2024
b074530
Update fendermint/vm/interpreter/src/fvm/state/exec.rs
cryptoAtwill Aug 20, 2024
5b4d061
update bail to logging
Aug 20, 2024
ca9ba85
add header
Aug 20, 2024
eabc77d
Merge branch 'simple-genesis' of protocol-github:consensus-shipyard/i…
Aug 20, 2024
f04e987
init gas market in genesis
Aug 20, 2024
cc36665
Merge branch 'simple-genesis' into block-gas-limit
cryptoAtwill Aug 20, 2024
a33c4b9
fmt
Aug 20, 2024
2b4318a
Merge branch 'block-gas-limit' into block-gas-limit-review
cryptoAtwill Aug 20, 2024
7a3b914
Merge branch 'block-gas-limit-review' into gas-market-genesis
cryptoAtwill Aug 20, 2024
9f96896
add gas_market to custom actor build
Aug 20, 2024
391d596
Merge branch 'gas-market-genesis' of protocol-github:consensus-shipya…
Aug 20, 2024
5b72dc5
format code
Aug 20, 2024
4de6eac
feat(node): block gas limit PR review changes (#1107)
cryptoAtwill Aug 20, 2024
a732872
Update fendermint/vm/interpreter/src/fvm/exec.rs
cryptoAtwill Aug 20, 2024
7e6e2ca
Update fendermint/vm/interpreter/src/fvm/exec.rs
cryptoAtwill Aug 20, 2024
cf0228a
Update fendermint/actors/gas_market/src/lib.rs
cryptoAtwill Aug 20, 2024
b00bf39
Update fendermint/vm/interpreter/src/fvm/gas/actor.rs
cryptoAtwill Aug 20, 2024
88090ac
Update fendermint/vm/interpreter/src/fvm/gas/actor.rs
cryptoAtwill Aug 20, 2024
ca9b0af
Update fendermint/vm/interpreter/src/fvm/gas/actor.rs
cryptoAtwill Aug 20, 2024
7ffaa0c
feedbacks
Aug 20, 2024
1a6fb21
more customization params
Aug 20, 2024
7b9c1de
resolve conflicts
Aug 20, 2024
cd90e98
up gas limit
Aug 21, 2024
47556e9
fix(node): remove genesis interpreter (#1118)
cryptoAtwill Aug 21, 2024
e5fb277
update gas market actor
Aug 22, 2024
cac495c
Merge branch 'simple-genesis' into block-gas-limit
cryptoAtwill Aug 22, 2024
8eae194
Merge branch 'block-gas-limit' into gas-market-genesis
cryptoAtwill Aug 22, 2024
53ca9f2
unit tests
Aug 22, 2024
d3bbb35
end block
Aug 22, 2024
65a6802
add validator tracking and gas premium distribution
Aug 26, 2024
05ec86b
distribute validator reward
Aug 27, 2024
7fc7df0
rename methods
Aug 27, 2024
ae6277b
use read only block store
Aug 27, 2024
46f8e2b
add more tests
Sep 11, 2024
f681494
fmt
Sep 11, 2024
706e44e
base fee rotation
Sep 12, 2024
4ff965b
more tests
Sep 12, 2024
22f8b51
prototype the validator rewards feature.
raulk Oct 2, 2024
7b3e127
Merge branch 'block-gas-limit' into integration/gas-market
raulk Oct 3, 2024
2265aed
Merge branch 'gas-market-genesis' into integration/gas-market
raulk Oct 3, 2024
d9f3cf1
Merge branch 'prepare_process_gas' into integration/gas-market
raulk Oct 3, 2024
1bfc60f
Merge branch 'rotate-base-fee' into integration/gas-market
raulk Oct 3, 2024
5b8c2da
fix Cargo.lock.
raulk Oct 4, 2024
37b6cd3
skeleton implementation
Oct 7, 2024
4ba4dbe
Merge branch 'main' into integration/gas-market
raulk Oct 8, 2024
80eabcb
merkle proof
Oct 8, 2024
2928bb7
integrate with solidity merkle proof
Oct 9, 2024
59085f5
call state from exec state
Oct 10, 2024
558bf19
fmt
Oct 10, 2024
ca136da
merge with caller
Oct 11, 2024
c78383e
integration tests for validator rewards
Oct 11, 2024
4f03a21
update deployment
Oct 11, 2024
425b395
Merge branch 'main' of protocol-github:consensus-shipyard/ipc into in…
Oct 15, 2024
c85761a
deploy map
Oct 15, 2024
385b4ab
init activity
Oct 15, 2024
cc1ebf2
human readble string
Oct 15, 2024
9d10df2
update build
Oct 15, 2024
983ca6d
update name
Oct 15, 2024
f8f379d
fix errors
Oct 16, 2024
9e6136b
add commit
Oct 16, 2024
cd27325
fmt code
Oct 16, 2024
32262a2
update contract
Oct 16, 2024
957baa2
shift reward facet to subnet actor
Oct 17, 2024
25252be
update comments
Oct 17, 2024
92ebc67
migrate to event
Oct 18, 2024
cf72dc7
Merge branch 'main' of protocol-github:consensus-shipyard/ipc into in…
Oct 18, 2024
e7edce1
update impl
Oct 18, 2024
96e9d77
Merge branch 'raulk/prototype-rewards' into integration/rewards
raulk Oct 18, 2024
4fb53b9
impl ipc cli
Oct 21, 2024
a10b6ae
Merge branch 'skeleton-validator-rewards' into integration/rewards
raulk Oct 21, 2024
08888c4
Merge branch 'integration-validator-reward' into integration/rewards
raulk Oct 21, 2024
0fc593f
fix clippy.
raulk Oct 22, 2024
e239a05
update deployment related
Oct 22, 2024
125e6ec
update batch claim params
Oct 22, 2024
de66b71
batch claim tests
Oct 22, 2024
c74a21c
add events
Oct 23, 2024
91298ca
remove checkpoitn height
Oct 23, 2024
06538e2
filter end block events
Oct 23, 2024
547da76
fix genesis
Oct 23, 2024
6f91bb6
patch end block events
Oct 23, 2024
ec8c0eb
patch deployment
Oct 23, 2024
95690dd
Merge branch 'main' into integration/rewards
raulk Nov 14, 2024
55f79e0
validator rewards with erc20
Nov 14, 2024
5803102
remove unused todo and comments
Nov 15, 2024
6883910
Merge branch 'integration/rewards' of protocol-github:consensus-shipy…
Nov 15, 2024
568a734
merge with main
Nov 15, 2024
86538d5
Merge branch 'main' into integration/rewards
raulk Nov 22, 2024
69e04a9
feat(node): integration rewards, review comments. (#1205)
raulk Nov 25, 2024
ff3baf7
remove stale code
Nov 25, 2024
0c3d067
unroll nested implementation.
raulk Nov 25, 2024
b223101
lint Cargo.toml.
raulk Nov 25, 2024
0496330
InvalidProof => InvalidActivityProof.
raulk Nov 25, 2024
b0a9c04
updateRewarder => setRewarder. more idiomatic.
raulk Nov 25, 2024
2b9c3b4
rm unimplemented relayer logic; use NotAuthorized error.
raulk Nov 25, 2024
3ad2c0f
chore: cleanup/refactor activity and rewards contracts. (#1214)
raulk Nov 26, 2024
970229e
Merge branch 'main' into integration/rewards
raulk Nov 26, 2024
03e3ebd
clean up
Nov 27, 2024
1d281bb
clippy
Nov 27, 2024
01c0d21
add checkpoint height to IValidatorRewarder#notifyValidClaim.
raulk Nov 27, 2024
06116c4
temporarily remove docs from this branch to work on them.
raulk Nov 27, 2024
dd95a47
fix lint.
raulk Nov 27, 2024
1c7c8de
Merge branch 'integration/rewards' into erc20-rewards
raulk Nov 27, 2024
6ca4a7e
Merge branch 'main' into erc20-rewards
raulk Nov 27, 2024
182ad61
adjustments.
raulk Nov 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions contracts/contracts/examples/MintingValidatorRewarder.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.23;

import {IValidatorRewarder} from "../interfaces/IValidatorRewarder.sol";
import {Consensus} from "../structs/Activity.sol";
import {SubnetID} from "../structs/Subnet.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";

contract MintableERC20 is ERC20, Ownable {
constructor(string memory name, string memory symbol) ERC20(name, symbol) Ownable(msg.sender) {}

function mint(address recipient, uint256 amount) external onlyOwner {
_mint(recipient, amount);
}
}

/// An example validator rewarder implementation that mint ERC20 token for valiator
contract MintingValidatorRewarder is IValidatorRewarder, Ownable {
SubnetID public subnetId;
MintableERC20 public token;

constructor() Ownable(msg.sender) {
// We can also pass this address as a constructor parameter or update
// using a setter as well.
token = new MintableERC20("test", "TST");
}

function setSubnet(SubnetID calldata id) external onlyOwner {
require(id.route.length > 0, "root not allowed");

subnetId = id;
}

function notifyValidClaim(
SubnetID calldata id,
uint64 checkpointHeight,
Consensus.ValidatorData calldata data
) external override {
require(keccak256(abi.encode(id)) == keccak256(abi.encode(subnetId)), "not my subnet");

address actor = id.route[id.route.length - 1];
require(actor == msg.sender, "not from subnet");

uint256 reward = calculateReward(data, checkpointHeight);

token.mint(data.validator, reward);
}

/// @notice The internal method to derive the amount of reward that each validator should receive
/// based on their subnet activities
function calculateReward(Consensus.ValidatorData calldata data, uint64) internal pure returns (uint256) {
// Reward is the same as blocks mined for convenience.
return data.blocksCommitted;
}
}
99 changes: 99 additions & 0 deletions contracts/test/integration/SubnetActorDiamond.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {SubnetValidatorGater} from "../../contracts/examples/SubnetValidatorGate

import {FullActivityRollup, Consensus} from "../../contracts/structs/Activity.sol";
import {ValidatorRewarderMap} from "../../contracts/examples/ValidatorRewarderMap.sol";
import {MintingValidatorRewarder} from "../../contracts/examples/MintingValidatorRewarder.sol";
import {MerkleTreeHelper} from "../helpers/MerkleTreeHelper.sol";
import {ActivityHelper} from "../helpers/ActivityHelper.sol";

Expand Down Expand Up @@ -2578,6 +2579,104 @@ contract SubnetActorDiamondTest is Test, IntegrationTestBase {
saDiamond.activity().batchSubnetClaim(subnetId, heights, claimProofs);
}

function testGatewayDiamond_ValidatorBatchClaimERC20Reward_Works() public {
MintingValidatorRewarder m = new MintingValidatorRewarder();
{
gatewayAddress = address(gatewayDiamond);

Asset memory source = Asset({kind: AssetKind.Native, tokenAddress: address(0)});

SubnetActorDiamond.ConstructorParams memory params = defaultSubnetActorParamsWith(
gatewayAddress,
SubnetID(ROOTNET_CHAINID, new address[](0)),
source,
AssetHelper.native()
);
params.validatorRewarder = address(m);
params.minValidators = 2;
params.permissionMode = PermissionMode.Federated;

saDiamond = createSubnetActor(params);
}

SubnetID memory subnetId = SubnetID(ROOTNET_CHAINID, new address[](1));
subnetId.route[0] = address(saDiamond);
m.setSubnet(subnetId);

(address[] memory addrs, uint256[] memory privKeys, bytes[] memory pubkeys) = TestUtils.newValidators(4);

{
uint256[] memory powers = new uint256[](4);
powers[0] = 10000;
powers[1] = 10000;
powers[2] = 10000;
powers[3] = 10000;
saDiamond.manager().setFederatedPower(addrs, pubkeys, powers);
}

bytes[] memory metadata = new bytes[](addrs.length);
uint64[] memory blocksMined = new uint64[](addrs.length);

// assign extra metadata to validator 0
// hardcode storageReward and uptimeReward to avoid stack too deep issues
metadata[0] = abi.encode(uint256(100), uint256(10));

blocksMined[0] = 11; // the first validator mined 11 blocks per checkpoint
blocksMined[1] = 2; // the second validator mined 2 blocks per checkpoint

(bytes32 activityRoot1, bytes32[][] memory proofs1) = MerkleTreeHelper.createMerkleProofsForConsensusActivity(
addrs,
blocksMined
);

(bytes32 activityRoot2, bytes32[][] memory proofs2) = MerkleTreeHelper.createMerkleProofsForConsensusActivity(
addrs,
blocksMined
);

// two checkpoints
confirmChange(addrs, privKeys, ActivityHelper.newCompressedActivityRollup(2, 3, activityRoot1));
confirmChange(addrs, privKeys, ActivityHelper.newCompressedActivityRollup(2, 3, activityRoot2));

Consensus.ValidatorClaim[] memory claimProofs = new Consensus.ValidatorClaim[](2);
uint64[] memory heights = new uint64[](2);

heights[0] = uint64(gatewayDiamond.getter().bottomUpCheckPeriod());
heights[1] = uint64(gatewayDiamond.getter().bottomUpCheckPeriod() * 2);

// Validator 0 claims 11 blocks per checkpoint = 22 tokens
claimProofs[0] = Consensus.ValidatorClaim({
data: Consensus.ValidatorData({validator: addrs[0], blocksCommitted: blocksMined[0]}),
proof: ActivityHelper.wrapBytes32Array(proofs1[0])
});
claimProofs[1] = Consensus.ValidatorClaim({
data: Consensus.ValidatorData({validator: addrs[0], blocksCommitted: blocksMined[0]}),
proof: ActivityHelper.wrapBytes32Array(proofs2[0])
});

vm.startPrank(addrs[0]);
vm.deal(addrs[0], 1 ether);
saDiamond.activity().batchSubnetClaim(subnetId, heights, claimProofs);

// Validator 1 claims 2 blocks per checkpoint = 4 tokens
claimProofs[0] = Consensus.ValidatorClaim({
data: Consensus.ValidatorData({validator: addrs[1], blocksCommitted: blocksMined[1]}),
proof: ActivityHelper.wrapBytes32Array(proofs1[1])
});
claimProofs[1] = Consensus.ValidatorClaim({
data: Consensus.ValidatorData({validator: addrs[1], blocksCommitted: blocksMined[1]}),
proof: ActivityHelper.wrapBytes32Array(proofs2[1])
});

vm.startPrank(addrs[1]);
vm.deal(addrs[1], 1 ether);
saDiamond.activity().batchSubnetClaim(subnetId, heights, claimProofs);

// Assert
assert(m.token().balanceOf(addrs[0]) == 22);
assert(m.token().balanceOf(addrs[1]) == 4);
}

// -----------------------------------------------------------------------------------------------------------------
// Tests for validator gater
// -----------------------------------------------------------------------------------------------------------------
Expand Down
Loading