Skip to content

Commit

Permalink
Merge pull request #562 from ava-labs/p-chain-id
Browse files Browse the repository at this point in the history
Setting P-Chain blockchain id constant
  • Loading branch information
minghinmatthewlam authored Sep 19, 2024
2 parents 4b2e326 + f03a6c8 commit ef2425c
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 71 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions contracts/validator-manager/ValidatorManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ abstract contract ValidatorManager is Initializable, ContextUpgradeable, IValida
/// @custom:storage-location erc7201:avalanche-icm.storage.ValidatorManager

struct ValidatorManagerStorage {
/// @notice The blockchainID of the P-Chain.
bytes32 _pChainBlockchainID;
/// @notice The subnetID associated with this validator manager.
bytes32 _subnetID;
/// @notice The number of seconds after which to reset the churn tracker.
Expand Down Expand Up @@ -62,6 +60,7 @@ abstract contract ValidatorManager is Initializable, ContextUpgradeable, IValida
uint64 public constant MAXIMUM_REGISTRATION_EXPIRY_LENGTH = 2 days;
uint32 public constant ADDRESS_LENGTH = 20; // This is only used as a packed uint32
uint8 public constant BLS_PUBLIC_KEY_LENGTH = 48;
bytes32 public constant P_CHAIN_BLOCKCHAIN_ID = bytes32(0);

// solhint-disable ordering
function _getValidatorManagerStorage()
Expand Down Expand Up @@ -96,7 +95,6 @@ abstract contract ValidatorManager is Initializable, ContextUpgradeable, IValida
onlyInitializing
{
ValidatorManagerStorage storage $ = _getValidatorManagerStorage();
$._pChainBlockchainID = settings.pChainBlockchainID;
$._subnetID = settings.subnetID;

require(
Expand Down Expand Up @@ -457,12 +455,12 @@ abstract contract ValidatorManager is Initializable, ContextUpgradeable, IValida
view
returns (WarpMessage memory)
{
ValidatorManagerStorage storage $ = _getValidatorManagerStorage();
(WarpMessage memory warpMessage, bool valid) =
WARP_MESSENGER.getVerifiedWarpMessage(messageIndex);
require(valid, "ValidatorManager: invalid warp message");
// Must match to P-Chain blockchain id, which is 0.
require(
warpMessage.sourceChainID == $._pChainBlockchainID,
warpMessage.sourceChainID == P_CHAIN_BLOCKCHAIN_ID,
"ValidatorManager: invalid source chain ID"
);
require(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ event ValidatorWeightUpdate(
);

struct ValidatorManagerSettings {
bytes32 pChainBlockchainID;
bytes32 subnetID;
uint64 churnPeriodSeconds;
uint8 maximumChurnPercentage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ contract ERC20TokenStakingManagerTest is PoSValidatorManagerTest {
app.initialize(
PoSValidatorManagerSettings({
baseSettings: ValidatorManagerSettings({
pChainBlockchainID: P_CHAIN_BLOCKCHAIN_ID,
subnetID: DEFAULT_SUBNET_ID,
churnPeriodSeconds: DEFAULT_CHURN_PERIOD,
maximumChurnPercentage: DEFAULT_MAXIMUM_CHURN_PERCENTAGE
Expand All @@ -63,7 +62,6 @@ contract ERC20TokenStakingManagerTest is PoSValidatorManagerTest {
app.initialize(
PoSValidatorManagerSettings({
baseSettings: ValidatorManagerSettings({
pChainBlockchainID: P_CHAIN_BLOCKCHAIN_ID,
subnetID: DEFAULT_SUBNET_ID,
churnPeriodSeconds: DEFAULT_CHURN_PERIOD,
maximumChurnPercentage: DEFAULT_MAXIMUM_CHURN_PERCENTAGE
Expand All @@ -86,7 +84,6 @@ contract ERC20TokenStakingManagerTest is PoSValidatorManagerTest {
app.initialize(
PoSValidatorManagerSettings({
baseSettings: ValidatorManagerSettings({
pChainBlockchainID: P_CHAIN_BLOCKCHAIN_ID,
subnetID: DEFAULT_SUBNET_ID,
churnPeriodSeconds: DEFAULT_CHURN_PERIOD,
maximumChurnPercentage: DEFAULT_MAXIMUM_CHURN_PERCENTAGE
Expand All @@ -108,7 +105,6 @@ contract ERC20TokenStakingManagerTest is PoSValidatorManagerTest {
app.initialize(
PoSValidatorManagerSettings({
baseSettings: ValidatorManagerSettings({
pChainBlockchainID: P_CHAIN_BLOCKCHAIN_ID,
subnetID: DEFAULT_SUBNET_ID,
churnPeriodSeconds: DEFAULT_CHURN_PERIOD,
maximumChurnPercentage: DEFAULT_MAXIMUM_CHURN_PERCENTAGE
Expand All @@ -130,7 +126,6 @@ contract ERC20TokenStakingManagerTest is PoSValidatorManagerTest {
app.initialize(
PoSValidatorManagerSettings({
baseSettings: ValidatorManagerSettings({
pChainBlockchainID: P_CHAIN_BLOCKCHAIN_ID,
subnetID: DEFAULT_SUBNET_ID,
churnPeriodSeconds: DEFAULT_CHURN_PERIOD,
maximumChurnPercentage: DEFAULT_MAXIMUM_CHURN_PERCENTAGE
Expand All @@ -153,7 +148,6 @@ contract ERC20TokenStakingManagerTest is PoSValidatorManagerTest {
app.initialize(
PoSValidatorManagerSettings({
baseSettings: ValidatorManagerSettings({
pChainBlockchainID: P_CHAIN_BLOCKCHAIN_ID,
subnetID: DEFAULT_SUBNET_ID,
churnPeriodSeconds: DEFAULT_CHURN_PERIOD,
maximumChurnPercentage: DEFAULT_MAXIMUM_CHURN_PERCENTAGE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ contract NativeTokenStakingManagerTest is PoSValidatorManagerTest {
app.initialize(
PoSValidatorManagerSettings({
baseSettings: ValidatorManagerSettings({
pChainBlockchainID: P_CHAIN_BLOCKCHAIN_ID,
subnetID: DEFAULT_SUBNET_ID,
churnPeriodSeconds: DEFAULT_CHURN_PERIOD,
maximumChurnPercentage: DEFAULT_MAXIMUM_CHURN_PERCENTAGE
Expand All @@ -56,7 +55,6 @@ contract NativeTokenStakingManagerTest is PoSValidatorManagerTest {
app.initialize(
PoSValidatorManagerSettings({
baseSettings: ValidatorManagerSettings({
pChainBlockchainID: P_CHAIN_BLOCKCHAIN_ID,
subnetID: DEFAULT_SUBNET_ID,
churnPeriodSeconds: DEFAULT_CHURN_PERIOD,
maximumChurnPercentage: DEFAULT_MAXIMUM_CHURN_PERCENTAGE
Expand All @@ -78,7 +76,6 @@ contract NativeTokenStakingManagerTest is PoSValidatorManagerTest {
app.initialize(
PoSValidatorManagerSettings({
baseSettings: ValidatorManagerSettings({
pChainBlockchainID: P_CHAIN_BLOCKCHAIN_ID,
subnetID: DEFAULT_SUBNET_ID,
churnPeriodSeconds: DEFAULT_CHURN_PERIOD,
maximumChurnPercentage: DEFAULT_MAXIMUM_CHURN_PERCENTAGE
Expand All @@ -99,7 +96,6 @@ contract NativeTokenStakingManagerTest is PoSValidatorManagerTest {
app.initialize(
PoSValidatorManagerSettings({
baseSettings: ValidatorManagerSettings({
pChainBlockchainID: P_CHAIN_BLOCKCHAIN_ID,
subnetID: DEFAULT_SUBNET_ID,
churnPeriodSeconds: DEFAULT_CHURN_PERIOD,
maximumChurnPercentage: DEFAULT_MAXIMUM_CHURN_PERCENTAGE
Expand All @@ -120,7 +116,6 @@ contract NativeTokenStakingManagerTest is PoSValidatorManagerTest {
app.initialize(
PoSValidatorManagerSettings({
baseSettings: ValidatorManagerSettings({
pChainBlockchainID: P_CHAIN_BLOCKCHAIN_ID,
subnetID: DEFAULT_SUBNET_ID,
churnPeriodSeconds: DEFAULT_CHURN_PERIOD,
maximumChurnPercentage: DEFAULT_MAXIMUM_CHURN_PERCENTAGE
Expand All @@ -142,7 +137,6 @@ contract NativeTokenStakingManagerTest is PoSValidatorManagerTest {
app.initialize(
PoSValidatorManagerSettings({
baseSettings: ValidatorManagerSettings({
pChainBlockchainID: P_CHAIN_BLOCKCHAIN_ID,
subnetID: DEFAULT_SUBNET_ID,
churnPeriodSeconds: DEFAULT_CHURN_PERIOD,
maximumChurnPercentage: DEFAULT_MAXIMUM_CHURN_PERCENTAGE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ contract PoAValidatorManagerTest is ValidatorManagerTest {
app = new PoAValidatorManager(ICMInitializable.Allowed);
app.initialize(
ValidatorManagerSettings({
pChainBlockchainID: P_CHAIN_BLOCKCHAIN_ID,
subnetID: DEFAULT_SUBNET_ID,
churnPeriodSeconds: DEFAULT_CHURN_PERIOD,
maximumChurnPercentage: DEFAULT_MAXIMUM_CHURN_PERCENTAGE
Expand Down
20 changes: 10 additions & 10 deletions contracts/validator-manager/tests/PoSValidatorManagerTests.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
blsPublicKey: DEFAULT_BLS_PUBLIC_KEY,
registrationTimestamp: DEFAULT_REGISTRATION_TIMESTAMP
});
_mockGetVerifiedWarpMessage(new bytes(0), false);
_mockGetUptimeWarpMessage(new bytes(0), false);
vm.warp(DEFAULT_REGISTRATION_TIMESTAMP + DEFAULT_MINIMUM_STAKE_DURATION);
vm.expectRevert(_formatErrorMessage("invalid warp message"));
posValidatorManager.initializeEndValidation(validationID, true, 0);
Expand All @@ -190,8 +190,8 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
registrationTimestamp: DEFAULT_REGISTRATION_TIMESTAMP
});

_mockGetVerifiedWarpMessage(new bytes(0), true);
_mockGetBlockchainID();
_mockGetUptimeWarpMessage(new bytes(0), true);
_mockGetBlockchainID(posValidatorManager.P_CHAIN_BLOCKCHAIN_ID());
vm.warp(DEFAULT_REGISTRATION_TIMESTAMP + DEFAULT_MINIMUM_STAKE_DURATION);
vm.expectRevert(_formatErrorMessage("invalid source chain ID"));
posValidatorManager.initializeEndValidation(validationID, true, 0);
Expand Down Expand Up @@ -367,7 +367,7 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
.packSubnetValidatorWeightUpdateMessage(
validationID, 1, DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT
);
_mockGetVerifiedWarpMessage(setValidatorWeightPayload, true);
_mockGetPChainWarpMessage(setValidatorWeightPayload, true);

vm.warp(DEFAULT_DELEGATOR_COMPLETE_REGISTRATION_TIMESTAMP);
vm.expectRevert("PoSValidatorManager: nonce does not match");
Expand Down Expand Up @@ -650,7 +650,7 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
.packSubnetValidatorWeightUpdateMessage(
validationID, 3, DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT
);
_mockGetVerifiedWarpMessage(setValidatorWeightPayload, true);
_mockGetPChainWarpMessage(setValidatorWeightPayload, true);

vm.expectRevert("PoSValidatorManager: nonce does not match");
posValidatorManager.completeEndDelegation(0, delegationID2);
Expand Down Expand Up @@ -873,7 +873,7 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
.packSubnetValidatorWeightUpdateMessage(
validationID, expectedNonce, expectedValidatorWeight
);
_mockGetVerifiedWarpMessage(setValidatorWeightPayload, true);
_mockGetPChainWarpMessage(setValidatorWeightPayload, true);

vm.warp(completeRegistrationTimestamp);
vm.expectEmit(true, true, true, true, address(posValidatorManager));
Expand Down Expand Up @@ -903,8 +903,8 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
bytes memory uptimeMsg = ValidatorMessages.packValidationUptimeMessage(
validationID, endDelegationTimestamp - startDelegationTimestamp
);
_mockGetVerifiedWarpMessage(uptimeMsg, true);
_mockGetBlockchainID(P_CHAIN_BLOCKCHAIN_ID);
_mockGetUptimeWarpMessage(uptimeMsg, true);
_mockGetBlockchainID();

vm.expectEmit(true, true, true, true, address(posValidatorManager));
emit ValidatorWeightUpdate({
Expand Down Expand Up @@ -934,7 +934,7 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
bytes memory subnetValidatorRegistrationMessage =
ValidatorMessages.packSubnetValidatorRegistrationMessage(validationID, false);

_mockGetVerifiedWarpMessage(subnetValidatorRegistrationMessage, true);
_mockGetPChainWarpMessage(subnetValidatorRegistrationMessage, true);

vm.expectEmit(true, true, true, true, address(posValidatorManager));
emit ValidationPeriodEnded(validationID, ValidatorStatus.Completed);
Expand Down Expand Up @@ -965,7 +965,7 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
bytes memory weightUpdateMessage = ValidatorMessages.packSubnetValidatorWeightUpdateMessage(
validationID, expectedNonce, validatorWeight
);
_mockGetVerifiedWarpMessage(weightUpdateMessage, true);
_mockGetPChainWarpMessage(weightUpdateMessage, true);

vm.expectEmit(true, true, true, true, address(posValidatorManager));
emit DelegationEnded(
Expand Down
36 changes: 26 additions & 10 deletions contracts/validator-manager/tests/ValidatorManagerTests.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import {
// TODO: Remove this once all unit tests implemented
// solhint-disable no-empty-blocks
abstract contract ValidatorManagerTest is Test {
bytes32 public constant P_CHAIN_BLOCKCHAIN_ID =
bytes32(hex"0000000000000000000000000000000000000000000000000000000000000000");
bytes32 public constant DEFAULT_SUBNET_ID =
bytes32(hex"1234567812345678123456781234567812345678123456781234567812345678");
bytes32 public constant DEFAULT_NODE_ID =
Expand Down Expand Up @@ -178,7 +176,7 @@ abstract contract ValidatorManagerTest is Test {
bytes memory subnetValidatorRegistrationMessage =
ValidatorMessages.packSubnetValidatorRegistrationMessage(validationID, false);

_mockGetVerifiedWarpMessage(subnetValidatorRegistrationMessage, true);
_mockGetPChainWarpMessage(subnetValidatorRegistrationMessage, true);

vm.expectEmit(true, true, true, true, address(validatorManager));
emit ValidationPeriodEnded(validationID, ValidatorStatus.Completed);
Expand All @@ -197,7 +195,7 @@ abstract contract ValidatorManagerTest is Test {
bytes memory subnetValidatorRegistrationMessage =
ValidatorMessages.packSubnetValidatorRegistrationMessage(validationID, false);

_mockGetVerifiedWarpMessage(subnetValidatorRegistrationMessage, true);
_mockGetPChainWarpMessage(subnetValidatorRegistrationMessage, true);

vm.expectEmit(true, true, true, true, address(validatorManager));
emit ValidationPeriodEnded(validationID, ValidatorStatus.Invalidated);
Expand Down Expand Up @@ -326,7 +324,7 @@ abstract contract ValidatorManagerTest is Test {
bytes memory subnetValidatorRegistrationMessage =
ValidatorMessages.packSubnetValidatorRegistrationMessage(validationID, true);

_mockGetVerifiedWarpMessage(subnetValidatorRegistrationMessage, true);
_mockGetPChainWarpMessage(subnetValidatorRegistrationMessage, true);

vm.warp(registrationTimestamp);
vm.expectEmit(true, true, true, true, address(validatorManager));
Expand Down Expand Up @@ -383,8 +381,8 @@ abstract contract ValidatorManagerTest is Test {
bytes memory uptimeMsg = ValidatorMessages.packValidationUptimeMessage(
validationID, completionTimestamp - registrationTimestamp
);
_mockGetVerifiedWarpMessage(uptimeMsg, true);
_mockGetBlockchainID(P_CHAIN_BLOCKCHAIN_ID);
_mockGetUptimeWarpMessage(uptimeMsg, true);
_mockGetBlockchainID();
}
vm.expectEmit(true, true, true, true, address(validatorManager));
emit ValidatorRemovalInitialized(validationID, bytes32(0), weight, completionTimestamp);
Expand All @@ -403,13 +401,31 @@ abstract contract ValidatorManagerTest is Test {
);
}

function _mockGetVerifiedWarpMessage(bytes memory expectedPayload, bool valid) internal {
function _mockGetPChainWarpMessage(bytes memory expectedPayload, bool valid) internal {
vm.mockCall(
WARP_PRECOMPILE_ADDRESS,
abi.encodeWithSelector(IWarpMessenger.getVerifiedWarpMessage.selector, uint32(0)),
abi.encode(
WarpMessage({
sourceChainID: P_CHAIN_BLOCKCHAIN_ID,
sourceChainID: validatorManager.P_CHAIN_BLOCKCHAIN_ID(),
originSenderAddress: address(0),
payload: expectedPayload
}),
valid
)
);
vm.expectCall(
WARP_PRECOMPILE_ADDRESS, abi.encodeCall(IWarpMessenger.getVerifiedWarpMessage, 0)
);
}

function _mockGetUptimeWarpMessage(bytes memory expectedPayload, bool valid) internal {
vm.mockCall(
WARP_PRECOMPILE_ADDRESS,
abi.encodeWithSelector(IWarpMessenger.getVerifiedWarpMessage.selector, uint32(0)),
abi.encode(
WarpMessage({
sourceChainID: DEFAULT_SOURCE_BLOCKCHAIN_ID,
originSenderAddress: address(0),
payload: expectedPayload
}),
Expand Down Expand Up @@ -437,7 +453,7 @@ abstract contract ValidatorManagerTest is Test {
}

function _mockInitializeValidatorSet() internal {
_mockGetVerifiedWarpMessage(
_mockGetPChainWarpMessage(
ValidatorMessages.packSubnetConversionMessage(DEFAULT_SUBNET_CONVERSION_TX_ID), true
);
}
Expand Down
2 changes: 0 additions & 2 deletions tests/flows/validator-manager/poa_to_pos.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ func PoAMigrationToPoS(network interfaces.LocalNetwork) {
tx, err := poaValidatorManager.Initialize(
opts,
poavalidatormanager.ValidatorManagerSettings{
PChainBlockchainID: pChainInfo.BlockchainID,
SubnetID: subnetAInfo.SubnetID,
ChurnPeriodSeconds: uint64(0),
MaximumChurnPercentage: uint8(20),
Expand Down Expand Up @@ -165,7 +164,6 @@ func PoAMigrationToPoS(network interfaces.LocalNetwork) {
opts,
nativetokenstakingmanager.PoSValidatorManagerSettings{
BaseSettings: nativetokenstakingmanager.ValidatorManagerSettings{
PChainBlockchainID: pChainInfo.BlockchainID,
SubnetID: subnetAInfo.SubnetID,
ChurnPeriodSeconds: utils.DefaultChurnPeriodSeconds,
MaximumChurnPercentage: utils.DefaultMaxChurnPercentage,
Expand Down
3 changes: 0 additions & 3 deletions tests/utils/validator_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ func DeployAndInitializeNativeTokenStakingManager(
opts,
nativetokenstakingmanager.PoSValidatorManagerSettings{
BaseSettings: nativetokenstakingmanager.ValidatorManagerSettings{
PChainBlockchainID: pChainInfo.BlockchainID,
SubnetID: subnet.SubnetID,
ChurnPeriodSeconds: DefaultChurnPeriodSeconds,
MaximumChurnPercentage: DefaultMaxChurnPercentage,
Expand Down Expand Up @@ -159,7 +158,6 @@ func DeployAndInitializeERC20TokenStakingManager(
opts,
erc20tokenstakingmanager.PoSValidatorManagerSettings{
BaseSettings: erc20tokenstakingmanager.ValidatorManagerSettings{
PChainBlockchainID: pChainInfo.BlockchainID,
SubnetID: subnet.SubnetID,
ChurnPeriodSeconds: DefaultChurnPeriodSeconds,
MaximumChurnPercentage: DefaultMaxChurnPercentage,
Expand Down Expand Up @@ -216,7 +214,6 @@ func DeployAndInitializePoAValidatorManager(
tx, err := validatorManager.Initialize(
opts,
poavalidatormanager.ValidatorManagerSettings{
PChainBlockchainID: pChainInfo.BlockchainID,
SubnetID: subnet.SubnetID,
ChurnPeriodSeconds: uint64(0),
MaximumChurnPercentage: uint8(20),
Expand Down

0 comments on commit ef2425c

Please sign in to comment.