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

WIP: Validator Manager contracts #571

Draft
wants to merge 657 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 250 commits
Commits
Show all changes
657 commits
Select commit Hold shift + click to select a range
2109a68
Add comments
geoff-vball Sep 18, 2024
0eb93c6
rename newWeight and oldWeight
geoff-vball Sep 18, 2024
69242fd
Merge branch 'validation-ends-first' into staking-rewards
geoff-vball Sep 18, 2024
8d2969a
Update contracts/staking/PoSValidatorManager.sol
geoff-vball Sep 18, 2024
b465fba
Update contracts/staking/PoSValidatorManager.sol
geoff-vball Sep 18, 2024
c215b3c
Update contracts/staking/PoSValidatorManager.sol
geoff-vball Sep 18, 2024
d9ae361
fix comment
geoff-vball Sep 18, 2024
fbeb9c5
Update contracts/staking/ValidatorMessages.sol
cam-schultz Sep 18, 2024
fb872a7
fix comment
geoff-vball Sep 18, 2024
b943edc
Update contracts/staking/interfaces/IValidatorManager.sol
cam-schultz Sep 18, 2024
8e25aa2
review fixes
geoff-vball Sep 18, 2024
9bbfde2
review fixes
geoff-vball Sep 18, 2024
7d96776
Merge branch 'validation-ends-first' into staking-rewards
geoff-vball Sep 18, 2024
be8d434
lint
geoff-vball Sep 18, 2024
bda2341
Merge branch 'validation-ends-first' into staking-rewards
geoff-vball Sep 18, 2024
ebe122e
lint
geoff-vball Sep 18, 2024
32d2b9e
minor feedback
cam-schultz Sep 18, 2024
260b8a1
remove subnetConversionData validity checks
cam-schultz Sep 18, 2024
75b4b38
Merge branch 'initial-validators-ending' of github.com:ava-labs/telep…
cam-schultz Sep 18, 2024
90e33ad
build fix
cam-schultz Sep 18, 2024
6623df0
fix packing
cam-schultz Sep 18, 2024
0896ba3
Merge pull request #547 from ava-labs/initial-validators-ending
cam-schultz Sep 18, 2024
ff7163e
Merge branch 'staking-contract' into validation-ends-first
geoff-vball Sep 18, 2024
8a791c1
Merge branch 'staking-contract' into staking-rewards
geoff-vball Sep 18, 2024
56fae8b
rename staking folder to validatorManager
iansuvak Sep 18, 2024
fcaf32c
remove staking abi-bindings
iansuvak Sep 18, 2024
44807c9
update imports
iansuvak Sep 18, 2024
13e210b
Review fixes
geoff-vball Sep 18, 2024
061c125
Review fixes
geoff-vball Sep 18, 2024
3716819
rename more files
iansuvak Sep 18, 2024
19e28b3
Fix unit tests
geoff-vball Sep 18, 2024
d5e5e26
Function for withdrawing delegation fees
geoff-vball Sep 18, 2024
25e8de5
require uptime; split safe and unsafe functions
cam-schultz Sep 18, 2024
622e6a8
validatorManager -> validator-manager
iansuvak Sep 18, 2024
a7998b8
Merge remote-tracking branch 'origin/staking-rewards' into staking-re…
iansuvak Sep 18, 2024
c9b1556
Hook up native minter precompile to PoS tests
geoff-vball Sep 18, 2024
bc7ddc7
wait for success in AddNativeMinterAdmin
iansuvak Sep 18, 2024
9e80079
lint
iansuvak Sep 18, 2024
491ae79
Merge branch 'staking-rewards' into staking-rename
iansuvak Sep 18, 2024
4733df1
store latest uptime
cam-schultz Sep 19, 2024
00504cb
return stored uptime if greater than arg
cam-schultz Sep 19, 2024
e17bb7f
uptime unit tests
cam-schultz Sep 19, 2024
5a288d5
lint
cam-schultz Sep 19, 2024
4087297
update abi bindings
cam-schultz Sep 19, 2024
2eda1da
fix e2e
cam-schultz Sep 19, 2024
25aade4
Merge branch 'staking-rename' into require-uptime
cam-schultz Sep 19, 2024
015d92e
Review fixes
geoff-vball Sep 19, 2024
6e46195
e2e build fix
cam-schultz Sep 19, 2024
daad525
Check for owner when initializing validator completion
geoff-vball Sep 19, 2024
3286116
Small fix
geoff-vball Sep 19, 2024
6b1e9c0
Small fix
geoff-vball Sep 19, 2024
db44dab
Emit rewards and fees on delegator exit
geoff-vball Sep 19, 2024
49636c1
manual packing:
iansuvak Sep 19, 2024
b4ea1b3
rename more references
iansuvak Sep 19, 2024
71230b9
Merge remote-tracking branch 'origin/staking-rewards' into staking-re…
iansuvak Sep 19, 2024
8f25e07
inline memory vars
cam-schultz Sep 19, 2024
099bf13
add test
geoff-vball Sep 19, 2024
2b9ff69
use constant for pchain blockchain id
Sep 19, 2024
c1de7d3
Merge pull request #534 from ava-labs/staking-rewards
geoff-vball Sep 19, 2024
22bd94a
Merge remote-tracking branch 'origin/staking-contract' into p-chain-id
Sep 19, 2024
b8d58ee
bindings
Sep 19, 2024
f01daad
Merge remote-tracking branch 'origin/staking-contract' into staking-r…
iansuvak Sep 19, 2024
4b2e326
Merge pull request #563 from ava-labs/staking-rename
iansuvak Sep 19, 2024
6c4b0f7
fix uptime get blockchain ID
Sep 19, 2024
0c7b3eb
Merge branch 'staking-contract' into iv-packing
iansuvak Sep 19, 2024
f03a6c8
Merge remote-tracking branch 'origin/staking-contract' into p-chain-id
Sep 19, 2024
ef2425c
Merge pull request #562 from ava-labs/p-chain-id
Sep 19, 2024
fd20c84
pass unit tests but exceed max contract size
iansuvak Sep 19, 2024
bc4de86
make all other pack functions use abi.encodePacked
iansuvak Sep 19, 2024
627f47b
Merge remote-tracking branch 'origin/staking-contract' into iv-packing
iansuvak Sep 19, 2024
1aa83fe
Small fixes
geoff-vball Sep 19, 2024
59a98b1
Fix e2e
geoff-vball Sep 19, 2024
ed15d98
Fix unit tests
geoff-vball Sep 19, 2024
5048225
review feedback
iansuvak Sep 19, 2024
9683e9c
Fix bug where validator exits while delegation is still pending added
geoff-vball Sep 20, 2024
4149155
Merge pull request #564 from ava-labs/iv-packing
iansuvak Sep 20, 2024
a2e817a
Merge branch 'staking-contract' into gstuart/fixups
geoff-vball Sep 20, 2024
e6e1629
validator manager custom errors
cam-schultz Sep 20, 2024
c8ee395
remove error decls from interface
cam-schultz Sep 20, 2024
bafc103
pos manager custom errors
cam-schultz Sep 20, 2024
e7ac131
update abi
cam-schultz Sep 20, 2024
329d1e7
Merge branch 'staking-contract' into custom-errors
cam-schultz Sep 20, 2024
059aee3
Update contracts/validator-manager/PoSValidatorManager.sol
cam-schultz Sep 20, 2024
6a80d63
comment type
cam-schultz Sep 20, 2024
556c147
Fix bug where delegator can't exit after validation has ended
geoff-vball Sep 20, 2024
4b00073
cleanup
geoff-vball Sep 20, 2024
396bf4c
Fix delegator end time
geoff-vball Sep 20, 2024
b61841d
consistent types
cam-schultz Sep 20, 2024
315ee62
update bindings
cam-schultz Sep 20, 2024
69cf948
reference custom errors directly
cam-schultz Sep 20, 2024
1793727
validator messages custom errors
cam-schultz Sep 20, 2024
64700da
Fixups
geoff-vball Sep 20, 2024
d0b3724
fix comment
geoff-vball Sep 20, 2024
5d1be58
Add check
geoff-vball Sep 20, 2024
d9b18da
pre-increment
michaelkaplan13 Sep 20, 2024
0eaeea8
Fix abi bindings
michaelkaplan13 Sep 20, 2024
d7367d6
Remove Delegator.endedAt
geoff-vball Sep 20, 2024
f73d2d0
format + lint
cam-schultz Sep 20, 2024
c54321c
Merge pull request #566 from ava-labs/custom-errors
cam-schultz Sep 20, 2024
9a17f4c
Merge branch 'staking-contract' into gstuart/fixups
geoff-vball Sep 20, 2024
8c27c5a
update vdr manager readme
cam-schultz Sep 20, 2024
f3a6194
clarification
cam-schultz Sep 20, 2024
09e69d7
Merge pull request #565 from ava-labs/gstuart/fixups
geoff-vball Sep 20, 2024
855eb00
Merge pull request #567 from ava-labs/update-readme
cam-schultz Sep 20, 2024
6c38fdd
Merge branch 'validator-manager' into require-uptime
cam-schultz Sep 24, 2024
a23bedb
delegation readme + state diagram
cam-schultz Sep 24, 2024
be119a0
Clean up state transitions
geoff-vball Sep 24, 2024
8aa030e
Remove unneeded line
geoff-vball Sep 24, 2024
1c5d546
address feedback
cam-schultz Sep 24, 2024
bb4d482
fix e2e
cam-schultz Sep 24, 2024
a384357
store uptime seconds in vdr info
cam-schultz Sep 24, 2024
7077a9a
separate helpers
cam-schultz Sep 24, 2024
4dbb52c
Merge pull request #572 from ava-labs/delegation-state-diagram
cam-schultz Sep 24, 2024
2f8e980
lint
cam-schultz Sep 24, 2024
b5ee7f4
Review fixes
geoff-vball Sep 24, 2024
9dbb1fa
Rewards refactor
geoff-vball Sep 25, 2024
d8635f3
Fix
geoff-vball Sep 25, 2024
c25a48a
Remove event
geoff-vball Sep 25, 2024
81edf43
Refactor tests
geoff-vball Sep 25, 2024
0b5e0e2
Merge pull request #574 from ava-labs/unit-test-cleanup
geoff-vball Sep 25, 2024
56fb125
Merge branch 'validator-manager' into delegator-refactor
geoff-vball Sep 25, 2024
98ff484
errors with params and storage slot tests
Sep 25, 2024
fd7cf49
separate invalid input error
Sep 25, 2024
d5771d0
adding params to errors and passing unit tests
Sep 25, 2024
2b7acc6
add comments
cam-schultz Sep 25, 2024
8532f14
Update contracts/validator-manager/PoSValidatorManager.sol
geoff-vball Sep 25, 2024
ff9e06e
remove former poa validator unit test
cam-schultz Sep 25, 2024
c1e1b95
refactor force helpers
cam-schultz Sep 25, 2024
698d4a4
Merge branch 'validator-manager' into require-uptime
cam-schultz Sep 25, 2024
14003d2
Make _calculateDelegationReward view
geoff-vball Sep 25, 2024
fadf58f
Fix out of gas error
geoff-vball Sep 26, 2024
a3ba881
Bindings
geoff-vball Sep 26, 2024
890ae47
lint
geoff-vball Sep 26, 2024
d7924fc
Merge branch 'require-uptime' into delegator-refactor
geoff-vball Sep 26, 2024
d1304e9
Fix out of gas error
geoff-vball Sep 26, 2024
18b0a11
Fix out of gas error
geoff-vball Sep 26, 2024
d69a789
Add context to validator manager functions
geoff-vball Sep 26, 2024
3a61c5a
Rename sendingKey to senderKey
geoff-vball Sep 26, 2024
32c5f81
Merge pull request #576 from ava-labs/test-cleanup
geoff-vball Sep 26, 2024
c82792f
Merge branch 'validator-manager' into delegator-refactor
geoff-vball Sep 26, 2024
3798c23
revamp validator manager docs
cam-schultz Sep 26, 2024
080824e
minor rewordings for clarify
cam-schultz Sep 26, 2024
0de3381
Add tests
geoff-vball Sep 26, 2024
454157e
Update contracts/validator-manager/README.md
cam-schultz Sep 27, 2024
0fda279
Merge branch 'validator-manager' into require-uptime
cam-schultz Sep 27, 2024
f119180
revert to using abi.encodePacked for packSubnetConversionData
iansuvak Sep 27, 2024
6492e9d
Add comment line
iansuvak Sep 27, 2024
c4a34bc
forceInitEndDelegation
cam-schultz Sep 27, 2024
e1c3a48
Merge remote-tracking branch 'origin/validator-manager' into error-pa…
Sep 27, 2024
1420c70
Merge branch 'require-uptime' into delegator-refactor
cam-schultz Sep 27, 2024
3f15578
add comment
iansuvak Sep 27, 2024
46acf6b
remove temp files
cam-schultz Sep 27, 2024
c44ae2b
Merge branch 'require-uptime' into delegator-refactor
cam-schultz Sep 27, 2024
b64f307
Merge pull request #561 from ava-labs/require-uptime
cam-schultz Sep 27, 2024
44a40e3
Merge branch 'validator-manager' into delegator-refactor
cam-schultz Sep 27, 2024
3adb900
Merge pull request #577 from ava-labs/validator-manager-doc-improvements
cam-schultz Sep 27, 2024
8d95406
Merge pull request #573 from ava-labs/delegator-refactor
cam-schultz Sep 27, 2024
df755ce
Merge branch 'validator-manager' into error-params
cam-schultz Sep 27, 2024
9a8a7c6
Merge remote-tracking branch 'origin/validator-manager' into error-pa…
Sep 27, 2024
414f9a9
fix unit tests
Sep 27, 2024
fd9663f
Merge pull request #579 from ava-labs/error-params
Sep 27, 2024
b9b7d04
Merge remote-tracking branch 'origin/validator-manager' into scd-packing
iansuvak Sep 27, 2024
0d6cd3d
Merge pull request #578 from ava-labs/scd-packing
iansuvak Sep 27, 2024
fed2329
remove todos
cam-schultz Sep 27, 2024
14be1a8
move event inside interface def
cam-schultz Sep 27, 2024
6c729e7
remove more todos
cam-schultz Sep 27, 2024
7e5a023
add doccomments
cam-schultz Sep 27, 2024
8b01977
completeEndValidation nonReentrant
cam-schultz Sep 27, 2024
debe234
update bindings
cam-schultz Sep 27, 2024
9229121
format
cam-schultz Sep 27, 2024
81754e5
Merge branch 'validator-manager' into cleanup
cam-schultz Sep 27, 2024
80c4230
Merge pull request #580 from ava-labs/cleanup
cam-schultz Sep 27, 2024
6121347
cleanup message spec
cam-schultz Oct 1, 2024
6e8fa6b
use correct type ids
cam-schultz Oct 1, 2024
f1a5147
code block formatting
cam-schultz Oct 1, 2024
b412e48
update to latest spec
cam-schultz Oct 1, 2024
9515841
pack lengths as uint32
cam-schultz Oct 2, 2024
3b06b23
scope local vars to bypass stack limit
cam-schultz Oct 2, 2024
80902f0
fix init vdr validationID
cam-schultz Oct 2, 2024
b015856
reorder fields
cam-schultz Oct 2, 2024
54e0c76
unit tests working
cam-schultz Oct 2, 2024
e6ffd3b
update bindings
cam-schultz Oct 2, 2024
90967aa
remove unused file
cam-schultz Oct 2, 2024
907e41d
use updated bindings
cam-schultz Oct 3, 2024
506c603
bump avago
cam-schultz Oct 3, 2024
29214d2
version extracts rc tags
cam-schultz Oct 3, 2024
85daef0
increase timeout to 120s
cam-schultz Oct 3, 2024
d618a68
Merge branch 'main' into validator-manager
cam-schultz Oct 3, 2024
5f26868
Merge branch 'validator-manager' into acp-77-updates
cam-schultz Oct 3, 2024
3f0b287
wip: convertsubnet working
cam-schultz Oct 4, 2024
f74c575
wip: flaky p-chain warp verification
cam-schultz Oct 5, 2024
0ca3a53
wip
cam-schultz Oct 7, 2024
2207e65
use tagged subnet-evm
cam-schultz Oct 8, 2024
b553d96
lengthen timeout
cam-schultz Oct 8, 2024
f90c70d
new justification format
cam-schultz Oct 8, 2024
f4bb683
bump avago
cam-schultz Oct 9, 2024
f281179
add delegation to erc20 staking flow
cam-schultz Oct 9, 2024
06a244e
resolve todos
cam-schultz Oct 9, 2024
d6e0632
separate init vdr helpers
cam-schultz Oct 9, 2024
ead93bc
reduce to 67 quorum
cam-schultz Oct 9, 2024
7c1e007
separate e2e suites
cam-schultz Oct 9, 2024
72ea62d
init chainid before tests
cam-schultz Oct 9, 2024
63bad1a
init chainid before tests
cam-schultz Oct 9, 2024
406caf7
simplify local network
cam-schultz Oct 9, 2024
2d5bbb3
cleanup
cam-schultz Oct 9, 2024
3b62b9b
build fix
cam-schultz Oct 9, 2024
c8ebfd2
add back in teleporter setup
cam-schultz Oct 9, 2024
5a76c07
recover subnet helper
cam-schultz Oct 9, 2024
0fa2ae8
native staking e2e
cam-schultz Oct 10, 2024
c3a0640
remove delegation flows
cam-schultz Oct 10, 2024
70b85e9
tear down network between validator manager tests
cam-schultz Oct 10, 2024
b4710e5
create new default registerer at runtime
cam-schultz Oct 10, 2024
c9320a9
bump awm-relayer
cam-schultz Oct 10, 2024
7136669
poa->pos migration e2e
cam-schultz Oct 10, 2024
3ecdb46
remove debug logs
cam-schultz Oct 10, 2024
2f3390a
add todo
cam-schultz Oct 10, 2024
e033da0
component-wise e2e test jobs
cam-schultz Oct 10, 2024
e0e108b
update job names
cam-schultz Oct 10, 2024
f57c860
remove upload artifact
cam-schultz Oct 10, 2024
11e8a39
lint fixes
cam-schultz Oct 10, 2024
0cb408f
Merge branch 'acp-77-e2e' into e2e-refactor-separate-suites
cam-schultz Oct 10, 2024
380fc9a
resolve todos
cam-schultz Oct 10, 2024
a3b77b0
fix typo
cam-schultz Oct 10, 2024
9123f15
remove unneeded input validation
cam-schultz Oct 10, 2024
ec11cd1
update bindings
cam-schultz Oct 10, 2024
9a962ed
clarify stack too deep workaround
cam-schultz Oct 10, 2024
f49fe5f
clarify stack too deep workaround - round 2
cam-schultz Oct 10, 2024
6ec27cc
lint
cam-schultz Oct 10, 2024
6f73651
remove teleporter from local network interface
cam-schultz Oct 11, 2024
f6c5e23
optional teleporter genesis spec
cam-schultz Oct 11, 2024
944591a
Remove todo
cam-schultz Oct 14, 2024
33d624e
remove todos
cam-schultz Oct 14, 2024
ba5cd03
Merge branch 'main' into validator-manager
cam-schultz Oct 14, 2024
02ce9d0
Merge branch 'validator-manager' into acp-77-updates
cam-schultz Oct 14, 2024
43c2279
Merge branch 'acp-77-updates' into acp-77-e2e
cam-schultz Oct 14, 2024
391b03c
Merge branch 'acp-77-e2e' into e2e-refactor-separate-suites
cam-schultz Oct 14, 2024
cbe7a7e
validate p chain owner
cam-schultz Oct 14, 2024
4bd2158
simplify component check
cam-schultz Oct 14, 2024
52524ea
delete testnet test code
cam-schultz Oct 14, 2024
c12941b
teleporter info getters
cam-schultz Oct 14, 2024
75ecd7c
lint
cam-schultz Oct 14, 2024
098442e
remove ref to unused var
cam-schultz Oct 15, 2024
646e4dd
test all suites by default
cam-schultz Oct 15, 2024
4cbde54
correct arg
cam-schultz Oct 15, 2024
dac3220
Merge pull request #592 from ava-labs/e2e-refactor-separate-suites
cam-schultz Oct 15, 2024
6174cef
Merge pull request #585 from ava-labs/acp-77-updates
cam-schultz Oct 15, 2024
a6eb5b1
Merge pull request #600 from ava-labs/e2e-refactor-cleanup
cam-schultz Oct 15, 2024
7bcb3e3
validators can remove their delegations
cam-schultz Oct 15, 2024
2c756cd
describe validator/delegator owner roles
cam-schultz Oct 15, 2024
8af29c8
Merge pull request #602 from ava-labs/vdrs-can-remove-dlgrs
cam-schultz Oct 16, 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
634 changes: 634 additions & 0 deletions abi-bindings/go/INativeMinter/INativeMinter.go

Large diffs are not rendered by default.

481 changes: 481 additions & 0 deletions abi-bindings/go/ProxyAdmin/ProxyAdmin.go

Large diffs are not rendered by default.

Large diffs are not rendered by default.

43 changes: 32 additions & 11 deletions abi-bindings/go/mocks/ExampleERC20/ExampleERC20.go

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1,977 changes: 1,977 additions & 0 deletions abi-bindings/go/validator-manager/PoAValidatorManager/PoAValidatorManager.go

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions contracts/mocks/ExampleERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import {
ERC20Burnable,
ERC20
} from "@openzeppelin/[email protected]/token/ERC20/extensions/ERC20Burnable.sol";
import {IERC20Mintable} from "../validator-manager/interfaces/IERC20Mintable.sol";

contract ExampleERC20 is ERC20Burnable {
contract ExampleERC20 is ERC20Burnable, IERC20Mintable {
string private constant _TOKEN_NAME = "Mock Token";
string private constant _TOKEN_SYMBOL = "EXMP";

uint256 private constant _MAX_MINT = 1e16;
uint256 private constant _MAX_MINT = 1e19;

constructor() ERC20(_TOKEN_NAME, _TOKEN_SYMBOL) {
_mint(msg.sender, 1e28);
Expand All @@ -30,4 +31,11 @@ contract ExampleERC20 is ERC20Burnable {

_mint(msg.sender, amount);
}

function mint(address account, uint256 amount) external {
// Can only mint 10 at a time.
require(amount <= _MAX_MINT, "ExampleERC20: max mint exceeded");

_mint(account, amount);
}
}
2 changes: 1 addition & 1 deletion contracts/teleporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Once sent on chain, Warp messages cannot be re-signed by a new validator set in

**Do not deploy the `TeleporterMessenger` contract using `forge create`**. The `TeleporterMessenger` contract must be deployed to the same contract address on every chain. To achieve this, the contract can be deployed using a static transaction that uses Nick's method as documented in [this guide](../..//utils/contract-deployment/README.md). Alternatively, if creating a new Subnet, the contract can be pre-allocated with the proper address and state in the new chain's [genesis file](https://docs.avax.network/build/subnet/upgrade/customize-a-subnet#setting-the-genesis-allocation).

As an example, to include `TeleporterMessenger` `v1.0.0` in the genesis file, include the following values in the `alloc` settings, as documented at the link above. The `storage` values included below correspond to the two contract values that are initialized as part of the default constructor of `TeleporterMessenger`. These are the `ReentrancyGuard` values set in this [abstract contract](../utilities/ReentrancyGuards.sol). Future versions of `TeleporterMessenger` may require different storage value intializations.
As an example, to include `TeleporterMessenger` `v1.0.0` in the genesis file, include the following values in the `alloc` settings, as documented at the link above. The `storage` values included below correspond to the two contract values that are initialized as part of the default constructor of `TeleporterMessenger`. These are the `ReentrancyGuard` values set in this [abstract contract](../utilities/ReentrancyGuards.sol). Future versions of `TeleporterMessenger` may require different storage value initializations.
```json
"alloc": {
"0x253b2784c75e510dD0fF1da844684a1aC0aa5fcf": {
Expand Down
10 changes: 10 additions & 0 deletions contracts/utilities/ICMInitializable.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// (c) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

// SPDX-License-Identifier: Ecosystem
pragma solidity 0.8.25;

enum ICMInitializable {
Allowed,
Disallowed
}
37 changes: 37 additions & 0 deletions contracts/validator-manager/Delegation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## Delegation

`PoSValidatorManager` supports Delegation to an active Validator as a way for users to earn staking rewards without having to validate the chain. Delegators pay a configurable percentage fee on any earned staking rewards to the host Validator. Delegations are reflected on the P-Chain by adjusting the Validator's registered weight via a [`SetSubnetValidatorWeightTx`](https://github.com/avalanche-foundation/ACPs/tree/main/ACPs/77-reinventing-subnets#setsubnetvalidatorweighttx). The weight change acknowledgement is delivered to the `PoSValidatorManager` via a [`SubnetValidatorWeightUpdateMessage`](./README.md#subnetvalidatorweightupdatemessage).

> [!NOTE]
> The P-Chain is only willing to sign a `SubnetValidatorWeightUpdateMessage` for an active Validator. Once Validator exit has been initiated (via a call to `initializeEndValidation`), the `PoSValidatorManager` must assume that the Validator has been deactivated on the P-Chain, and will therefore not sign any further weight updates. Therefore, it is invalid to _initiate_ adding or removing a Delegator when the Validator is in this state, though it _may be_ valid to _complete_ an already initiated Delegator action, depending on the order of delivery to the P-Chain. If the Delegator weight change was submitted (and a Warp signature on the acknowledgement retrieved) before the Validator was removed, then the Delegator action may be completed. Otherwise, the acknowledgement of the Validation end must first be delivered before completing the Delegator action.

The following state transition diagram illustrates the relationship between Validator and Delegator state. `Validator` is abbreviated as `V`, `Delegator` is abbreviated as `D`, and function names are shortened to improve readability. `Delegator.Completed` is omitted to show equivalance between the Validator's initial state and the state after a Delegation completes.
```mermaid
stateDiagram-v2
% Happy path
[*] --> V.PendingAdded : initAddVdr
V.PendingAdded --> V.Active : completeVdrReg
V.Active --> V.Active,D.PendingAdded : initAddDel
V.Active,D.PendingAdded --> V.Active,D.Active : completeAddDel
V.Active,D.Active --> V.Active,D.PendingRemoved : initEndDel
V.Active,D.PendingRemoved --> V.Active : completeEndDel
V.Active --> V.PendingRemoved : initEndVdr
V.PendingRemoved --> V.Completed : completeEndVdr

% Validator/Delegator state changes do not affect the Delegator/Validator state
V.Active,D.PendingRemoved --> V.PendingRemoved,D.PendingRemoved : initEndVdr
V.Active,D.PendingAdded --> V.PendingRemoved,D.PendingAdded : initEndVdr
V.Active,D.Active --> V.PendingRemoved,D.Active: initEndVdr

% When the Validator is in PendingRemoved or Completed, in general Delegator actions
% may be completed, but not initialized.
V.PendingRemoved,D.PendingAdded --> V.Completed,D.PendingAdded : completeEndVdr
V.PendingRemoved,D.PendingRemoved --> V.Completed,D.PendingRemoved : completeEndVdr
V.PendingRemoved,D.PendingRemoved --> V.PendingRemoved : completeEndDel
V.PendingRemoved,D.Active --> V.Completed,D.Active : completeEndVdr
% This is a no-op
V.PendingRemoved,D.PendingAdded --> V.PendingRemoved,D.Active : completeAddDel
V.Completed,D.PendingRemoved --> V.Completed : endDelegationCompletedValidator
V.Completed,D.Active --> V.Completed : endDelegationCompletedValidator
V.Completed,D.PendingAdded --> V.Completed : endDelegationCompletedValidator
```
132 changes: 132 additions & 0 deletions contracts/validator-manager/ERC20TokenStakingManager.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// (c) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

// SPDX-License-Identifier: Ecosystem

pragma solidity 0.8.25;

import {IERC20TokenStakingManager} from "./interfaces/IERC20TokenStakingManager.sol";
import {Initializable} from
"@openzeppelin/[email protected]/proxy/utils/Initializable.sol";
import {IERC20Mintable} from "./interfaces/IERC20Mintable.sol";
import {SafeERC20TransferFrom} from "@utilities/SafeERC20TransferFrom.sol";
import {SafeERC20} from "@openzeppelin/[email protected]/token/ERC20/utils/SafeERC20.sol";
import {ICMInitializable} from "../utilities/ICMInitializable.sol";
import {PoSValidatorManager} from "./PoSValidatorManager.sol";
import {PoSValidatorManagerSettings} from "./interfaces/IPoSValidatorManager.sol";
import {ValidatorRegistrationInput} from "./interfaces/IValidatorManager.sol";

contract ERC20TokenStakingManager is

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should add natspec contract docs here including security contact

Initializable,
PoSValidatorManager,
IERC20TokenStakingManager
{
using SafeERC20 for IERC20Mintable;
using SafeERC20TransferFrom for IERC20Mintable;

// solhint-disable private-vars-leading-underscore
/// @custom:storage-location erc7201:avalanche-icm.storage.ERC20TokenStakingManager
struct ERC20TokenStakingManagerStorage {
IERC20Mintable _token;
uint8 _tokenDecimals;
}
// solhint-enable private-vars-leading-underscore

// keccak256(abi.encode(uint256(keccak256("avalanche-icm.storage.ERC20TokenStakingManager")) - 1)) & ~bytes32(uint256(0xff));
// TODO: Update to correct storage slot

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this after verified

bytes32 private constant _ERC20_STAKING_MANAGER_STORAGE_LOCATION =
0x6e5bdfcce15e53c3406ea67bfce37dcd26f5152d5492824e43fd5e3c8ac5ab00;

// solhint-disable ordering
function _getERC20StakingManagerStorage()
private
pure
returns (ERC20TokenStakingManagerStorage storage $)
{
// solhint-disable-next-line no-inline-assembly
assembly {
$.slot := _ERC20_STAKING_MANAGER_STORAGE_LOCATION
}
}

constructor(ICMInitializable init) {
if (init == ICMInitializable.Disallowed) {
_disableInitializers();
}
}

/**
* @notice Initialize the ERC20 token staking manager
* @dev Uses reinitializer(2) on the PoS staking contracts to make sure after migration from PoA, the PoS contracts can reinitialize with its needed values.
* @param settings Initial settings for the PoS validator manager
* @param token The ERC20 token to be staked
*/
function initialize(
PoSValidatorManagerSettings calldata settings,
IERC20Mintable token
) external reinitializer(2) {
__ERC20TokenStakingManager_init(settings, token);
}

// solhint-disable func-name-mixedcase

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should use solhint-disable-next-line here

function __ERC20TokenStakingManager_init(
PoSValidatorManagerSettings calldata settings,
IERC20Mintable token
) internal onlyInitializing {
__POS_Validator_Manager_init(settings);
__ERC20TokenStakingManager_init_unchained(token);
}

// solhint-disable func-name-mixedcase
function __ERC20TokenStakingManager_init_unchained(IERC20Mintable token)
internal
onlyInitializing
{
ERC20TokenStakingManagerStorage storage $ = _getERC20StakingManagerStorage();
if (address(token) == address(0)) {
revert InvalidAddress();
}
$._token = token;
}

/**
* @notice See {IERC20TokenStakingManager-initializeValidatorRegistration}
* Begins the validator registration process. Locks the configured ERC20 in the contract as the stake.
*/
function initializeValidatorRegistration(
ValidatorRegistrationInput calldata registrationInput,
uint16 delegationFeeBips,
uint64 minStakeDuration,
uint256 stakeAmount
) external nonReentrant returns (bytes32 validationID) {
return _initializeValidatorRegistration(
registrationInput, delegationFeeBips, minStakeDuration, stakeAmount
);
}

/**
* @notice Begins the delegator registration process. Locks the configured ERC20 in the contract as the delegated stake.
* @param validationID The ID of the validation period being delegated to.
* @param delegationAmount The amount to be delegated.
*/
function initializeDelegatorRegistration(
bytes32 validationID,
uint256 delegationAmount
) external nonReentrant returns (bytes32) {
return _initializeDelegatorRegistration(validationID, _msgSender(), delegationAmount);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't think this internal funciton needs the msgSender passed in as a parameter, and instead can directly access it

}

// Must be guarded with reentrancy guard for safe transfer from
function _lock(uint256 value) internal virtual override returns (uint256) {
return _getERC20StakingManagerStorage()._token.safeTransferFrom(value);
}

function _unlock(address to, uint256 value) internal virtual override {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can also add a note about reentrancy guard like the one above for lock

_getERC20StakingManagerStorage()._token.safeTransfer(to, value);
}

function _reward(address account, uint256 amount) internal virtual override {
ERC20TokenStakingManagerStorage storage $ = _getERC20StakingManagerStorage();
$._token.mint(account, amount);
}
}
45 changes: 45 additions & 0 deletions contracts/validator-manager/ExampleRewardCalculator.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// (c) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

// SPDX-License-Identifier: Ecosystem

pragma solidity 0.8.25;

import {IRewardCalculator} from "./interfaces/IRewardCalculator.sol";

contract ExampleRewardCalculator is IRewardCalculator {
uint256 public constant SECONDS_IN_YEAR = 31536000;

uint8 public constant UPTIME_REWARDS_THRESHOLD_PERCENTAGE = 80;

uint64 public immutable rewardBasisPoints;

constructor(uint64 rewardBasisPoints_) {
rewardBasisPoints = rewardBasisPoints_;
}

/**
* @notice A linear, non-compounding reward calculation that rewards a set percentage of tokens per year.
*/
function calculateReward(
uint256 stakeAmount,
uint64 validatorStartTime,
uint64 stakingStartTime,
uint64 stakingEndTime,
uint64 uptimeSeconds,
uint256, // initialSupply
uint256 // endSupply
) external view returns (uint256) {
// Equivalent to uptimeSeconds/(validator.endedAt - validator.startedAt) < UPTIME_REWARDS_THRESHOLD_PERCENTAGE/100
// Rearranged to prevent integer division truncation.
if (
uptimeSeconds * 100
< (stakingEndTime - validatorStartTime) * UPTIME_REWARDS_THRESHOLD_PERCENTAGE
) {
return 0;
}

return (stakeAmount * rewardBasisPoints * (stakingEndTime - stakingStartTime))
/ SECONDS_IN_YEAR / 10000;
}
}
95 changes: 95 additions & 0 deletions contracts/validator-manager/NativeTokenStakingManager.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// (c) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

// SPDX-License-Identifier: Ecosystem

pragma solidity 0.8.25;

import {INativeTokenStakingManager} from "./interfaces/INativeTokenStakingManager.sol";
import {INativeMinter} from
"@avalabs/[email protected]/contracts/interfaces/INativeMinter.sol";
import {Address} from "@openzeppelin/[email protected]/utils/Address.sol";
import {Initializable} from
"@openzeppelin/[email protected]/proxy/utils/Initializable.sol";
import {ICMInitializable} from "../utilities/ICMInitializable.sol";
import {PoSValidatorManager} from "./PoSValidatorManager.sol";
import {PoSValidatorManagerSettings} from "./interfaces/IPoSValidatorManager.sol";
import {ValidatorRegistrationInput} from "./interfaces/IValidatorManager.sol";

contract NativeTokenStakingManager is
Initializable,
PoSValidatorManager,
INativeTokenStakingManager
{
using Address for address payable;

INativeMinter public constant NATIVE_MINTER =
INativeMinter(0x0200000000000000000000000000000000000001);

constructor(ICMInitializable init) {
if (init == ICMInitializable.Disallowed) {
_disableInitializers();
}
}

/**
* @notice Initialize the ERC20 token staking manager
* @dev Uses reinitializer(2) on the PoS staking contracts to make sure after migration from PoA, the PoS contracts can reinitialize with its needed values.
* @param settings Initial settings for the PoS validator manager
*/
// solhint-disable ordering
function initialize(PoSValidatorManagerSettings calldata settings) external reinitializer(2) {
__NativeTokenStakingManager_init(settings);
}

// solhint-disable-next-line func-name-mixedcase
function __NativeTokenStakingManager_init(PoSValidatorManagerSettings calldata settings)
internal
onlyInitializing
{
__POS_Validator_Manager_init(settings);
}

// solhint-disable-next-line func-name-mixedcase, no-empty-blocks
function __NativeTokenStakingManager_init_unchained() internal onlyInitializing {}

/**
* @notice See {INativeTokenStakingManager-initializeValidatorRegistration}.
* Begins the validator registration process. Locks the provided native asset in the contract as the stake.
*/
function initializeValidatorRegistration(
ValidatorRegistrationInput calldata registrationInput,
uint16 delegationFeeBips,
uint64 minStakeDuration
) external payable nonReentrant returns (bytes32) {
return _initializeValidatorRegistration(
registrationInput, delegationFeeBips, minStakeDuration, msg.value
);
}

/**
* @notice Begins the delegator registration process. Locks the provided native asset in the contract as the delegated stake.
* @param validationID The ID of the validation period being delegated to.
*/
function initializeDelegatorRegistration(bytes32 validationID)
external
payable
nonReentrant
returns (bytes32)
{
return _initializeDelegatorRegistration(validationID, _msgSender(), msg.value);
}

// solhint-enable ordering
function _lock(uint256 value) internal virtual override returns (uint256) {
return value;
}

function _unlock(address to, uint256 value) internal virtual override {
payable(to).sendValue(value);
}

function _reward(address account, uint256 amount) internal virtual override {
NATIVE_MINTER.mintNativeCoin(account, amount);
}
}
Loading