Skip to content

Commit

Permalink
Merge pull request #497 from ava-labs/poa-e2e-test
Browse files Browse the repository at this point in the history
PoA e2e test
  • Loading branch information
minghinmatthewlam authored Aug 21, 2024
2 parents bf79590 + 598cef4 commit 92aa0e6
Show file tree
Hide file tree
Showing 26 changed files with 1,780 additions and 76 deletions.
1,358 changes: 1,358 additions & 0 deletions abi-bindings/go/staking/PoAValidatorManager/PoAValidatorManager.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion scripts/abi_bindings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export ARCH=$(uname -m)
[ $ARCH = x86_64 ] && ARCH=amd64
echo "ARCH set to $ARCH"

DEFAULT_CONTRACT_LIST="TeleporterMessenger TeleporterRegistry ExampleERC20 TestMessenger ValidatorSetSig NativeTokenStakingManager ERC20TokenStakingManager"
DEFAULT_CONTRACT_LIST="TeleporterMessenger TeleporterRegistry ExampleERC20 TestMessenger ValidatorSetSig NativeTokenStakingManager ERC20TokenStakingManager PoAValidatorManager"

CONTRACT_LIST=
HELP=
Expand Down
2 changes: 1 addition & 1 deletion tests/flows/errors.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package flows

const (
errTxReverted = "execution reverted"
ErrTxReverted = "execution reverted"
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flows
package governance

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flows
package staking

import (
"context"
Expand Down Expand Up @@ -82,7 +82,6 @@ func ERC20TokenStakingManager(network interfaces.LocalNetwork) {
// (Sending to the P-Chain will be skipped for now)
signedWarpMessage := network.ConstructSignedWarpMessage(context.Background(), receipt, subnetAInfo, pChainInfo)

// Validate the Warp message, (this will be done on the P-Chain in the future)
// Validate the Warp message, (this will be done on the P-Chain in the future)
utils.ValidateRegisterSubnetValidatorMessage(
signedWarpMessage,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flows
package staking

import (
"context"
Expand Down
206 changes: 206 additions & 0 deletions tests/flows/staking/poa_validator_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
package staking

import (
"context"
"math/big"
"time"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/subnet-evm/accounts/abi/bind"
"github.com/ava-labs/subnet-evm/core/types"
"github.com/ava-labs/teleporter/tests/interfaces"
"github.com/ava-labs/teleporter/tests/utils"
"github.com/ethereum/go-ethereum/crypto"
. "github.com/onsi/gomega"
)

/*
* Register a PoA validator manager on a L1. The steps are as follows:
* - Generate random address to be the owner address
* - Fund native assets to the owner address
* - Deploy the PoAValidatorManager contract
* - Attempt to initiate with non owner and check that it fails
* - Initiate validator registration
* - Deliver the Warp message to the P-Chain (not implemented)
* - Aggregate P-Chain signatures on the response Warp message
* - Deliver the Warp message to the L1
* - Verify that the validator is registered in the validator manager contract
*
* Delists the validator from the L1. The steps are as follows:
* - Attempt to initiate with non owner and check that it fails
* - Initiate validator delisting
* - Deliver the Warp message to the P-Chain (not implemented)
* - Aggregate P-Chain signatures on the response Warp message
* - Deliver the Warp message to the L1
* - Verify that the validator is delisted from the validator manager contract
*/
func PoAValidatorManager(network interfaces.LocalNetwork) {
cChainInfo := network.GetPrimaryNetworkInfo()
subnetAInfo, _ := utils.GetTwoSubnets(network)
_, fundedKey := network.GetFundedAccountInfo()
pChainInfo := utils.GetPChainInfo(cChainInfo)

signatureAggregator := utils.NewSignatureAggregator(
cChainInfo.NodeURIs[0],
[]ids.ID{
subnetAInfo.SubnetID,
ids.Empty, // Primary network subnet ID
},
)

// Generate random address to be the owner address
ownerKey, err := crypto.GenerateKey()
Expect(err).Should(BeNil())
ownerAddress := crypto.PubkeyToAddress(ownerKey.PublicKey)

// Transfer native assets to the owner account
ctx := context.Background()
fundAmount := big.NewInt(1e18) // 1avax
utils.SendNativeTransfer(
ctx,
subnetAInfo,
fundedKey,
ownerAddress,
fundAmount,
)

validatorManagerAddress, validatorManager := utils.DeployAndInitializePoAValidatorManager(
ctx,
fundedKey,
subnetAInfo,
pChainInfo,
ownerAddress,
)

var validationID ids.ID // To be used in the delisting step
nodeID := ids.GenerateTestID()
blsPublicKey := [bls.PublicKeyLen]byte{}
weight := uint64(1)

{
// Try to call with invalid owner
opts, err := bind.NewKeyedTransactorWithChainID(fundedKey, subnetAInfo.EVMChainID)
Expect(err).Should(BeNil())

_, err = validatorManager.InitializeValidatorRegistration(
opts,
weight,
nodeID,
uint64(time.Now().Add(24*time.Hour).Unix()),
blsPublicKey[:],
)
Expect(err).ShouldNot(BeNil())

// Initiate validator registration
var receipt *types.Receipt
receipt, validationID = utils.InitializePoAValidatorRegistration(
ownerKey,
subnetAInfo,
weight,
nodeID,
blsPublicKey,
validatorManager,
)

// Gather subnet-evm Warp signatures for the RegisterSubnetValidatorMessage & relay to the P-Chain
// (Sending to the P-Chain will be skipped for now)
signedWarpMessage := network.ConstructSignedWarpMessage(context.Background(), receipt, subnetAInfo, pChainInfo)

// Validate the Warp message, (this will be done on the P-Chain in the future)
utils.ValidateRegisterSubnetValidatorMessage(
signedWarpMessage,
nodeID,
weight,
subnetAInfo.SubnetID,
blsPublicKey,
)

// Construct a SubnetValidatorRegistrationMessage Warp message from the P-Chain
registrationSignedMessage := utils.ConstructSubnetValidatorRegistrationMessage(
validationID,
true,
subnetAInfo,
pChainInfo,
network,
signatureAggregator,
)

// Deliver the Warp message to the subnet
receipt = utils.CompletePoAValidatorRegistration(
fundedKey,
subnetAInfo,
validatorManagerAddress,
registrationSignedMessage,
)
// Check that the validator is registered in the staking contract
registrationEvent, err := utils.GetEventFromLogs(
receipt.Logs,
validatorManager.ParseValidationPeriodRegistered,
)
Expect(err).Should(BeNil())
Expect(registrationEvent.ValidationID[:]).Should(Equal(validationID[:]))
}

//
// Delist the validator
//
{
// Try with invalid validator owner
opts, err := bind.NewKeyedTransactorWithChainID(fundedKey, subnetAInfo.EVMChainID)
Expect(err).Should(BeNil())
_, err = validatorManager.InitializeEndValidation(
opts,
validationID,
)
Expect(err).ShouldNot(BeNil())

receipt := utils.InitializeEndPoAValidation(
ownerKey,
subnetAInfo,
validatorManager,
validationID,
)
validatorRemovalEvent, err := utils.GetEventFromLogs(
receipt.Logs,
validatorManager.ParseValidatorRemovalInitialized,
)
Expect(err).Should(BeNil())
Expect(validatorRemovalEvent.ValidationID[:]).Should(Equal(validationID[:]))
Expect(validatorRemovalEvent.Weight.Uint64()).Should(Equal(weight))

// Gather subnet-evm Warp signatures for the SetSubnetValidatorWeightMessage & relay to the P-Chain
// (Sending to the P-Chain will be skipped for now)
signedWarpMessage := network.ConstructSignedWarpMessage(context.Background(), receipt, subnetAInfo, pChainInfo)
Expect(err).Should(BeNil())

// Validate the Warp message, (this will be done on the P-Chain in the future)
utils.ValidateSetSubnetValidatorWeightMessage(signedWarpMessage, validationID, 0, 0)

// Construct a SubnetValidatorRegistrationMessage Warp message from the P-Chain
registrationSignedMessage := utils.ConstructSubnetValidatorRegistrationMessage(
validationID,
false,
subnetAInfo,
pChainInfo,
network,
signatureAggregator,
)

// Deliver the Warp message to the subnet
receipt = utils.CompleteEndPoAValidation(
ownerKey,
subnetAInfo,
validatorManagerAddress,
registrationSignedMessage,
)

// Check that the validator is has been delisted from the staking contract
registrationEvent, err := utils.GetEventFromLogs(
receipt.Logs,
validatorManager.ParseValidationPeriodEnded,
)
Expect(err).Should(BeNil())
Expect(registrationEvent.ValidationID[:]).Should(Equal(validationID[:]))
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flows
package teleporter

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flows
package teleporter

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flows
package teleporter

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flows
package teleporter

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flows
package teleporter

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package flows
package registry

import (
"context"
"math/big"

"github.com/ava-labs/subnet-evm/accounts/abi/bind"
"github.com/ava-labs/teleporter/tests/flows"
"github.com/ava-labs/teleporter/tests/interfaces"
"github.com/ava-labs/teleporter/tests/utils"
"github.com/ethereum/go-ethereum/crypto"

. "github.com/onsi/gomega"
)

Expand Down Expand Up @@ -53,7 +55,7 @@ func CheckUpgradeAccess(network interfaces.Network) {
Expect(err).Should(BeNil())
_, err = testMessenger.PauseTeleporterAddress(nonOwnerOpts, teleporterAddress)
Expect(err).ShouldNot(BeNil())
Expect(err.Error()).Should(ContainSubstring(errTxReverted))
Expect(err.Error()).Should(ContainSubstring(flows.ErrTxReverted))

// Check that the teleporter address is not paused, because previous call should have failed
isPaused, err := testMessenger.IsTeleporterAddressPaused(&bind.CallOpts{}, teleporterAddress)
Expand Down Expand Up @@ -84,7 +86,7 @@ func CheckUpgradeAccess(network interfaces.Network) {
// Try to call unpauseTeleporterAddress from the previous owner account
_, err = testMessenger.UnpauseTeleporterAddress(ownerOpts, teleporterAddress)
Expect(err).ShouldNot(BeNil())
Expect(err.Error()).Should(ContainSubstring(errTxReverted))
Expect(err.Error()).Should(ContainSubstring(flows.ErrTxReverted))

// Make sure the teleporter address is still paused
isPaused, err = testMessenger.IsTeleporterAddressPaused(&bind.CallOpts{}, teleporterAddress)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flows
package registry

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flows
package registry

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flows
package teleporter

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flows
package teleporter

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flows
package teleporter

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flows
package teleporter

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flows
package teleporter

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// (c) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package flows
package teleporter

import (
"math/big"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flows
package teleporter

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flows
package teleporter

import (
"context"
Expand Down
Loading

0 comments on commit 92aa0e6

Please sign in to comment.