Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/ava-labs/teleporter
go 1.22.8

require (
github.com/ava-labs/avalanchego v1.12.0-initial-poc.3
github.com/ava-labs/avalanchego v1.12.0-initial-poc.5
github.com/supranational/blst v0.3.11 // indirect
)

Expand All @@ -19,6 +19,7 @@ require (
github.com/stretchr/testify v1.9.0
go.uber.org/zap v1.27.0
golang.org/x/tools v0.26.0
google.golang.org/protobuf v1.34.2
)

require (
Expand Down Expand Up @@ -145,7 +146,6 @@ require (
google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
google.golang.org/grpc v1.67.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY
github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah4HI848JfFxHt+iPb26b4zyfspmqY0/8=
github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/ava-labs/avalanchego v1.12.0-initial-poc.3 h1:JfVooBCdMzpeGUT9/phJNl2GHflkGehlMJokXeWKa2A=
github.com/ava-labs/avalanchego v1.12.0-initial-poc.3/go.mod h1:qSHmog3wMVjo/ruIAQo0ppXAilyni07NIu5K88RyhWE=
github.com/ava-labs/avalanchego v1.12.0-initial-poc.5 h1:gW4xAqZNvkA4gP8M9yDyd7YUzuwfQbbCR+hgd1ztOto=
github.com/ava-labs/avalanchego v1.12.0-initial-poc.5/go.mod h1:qSHmog3wMVjo/ruIAQo0ppXAilyni07NIu5K88RyhWE=
github.com/ava-labs/awm-relayer v1.4.1-0.20241003183820-366b0dc8cea6 h1:v7k5wQxcvnYcMaz+zoO1OLAcU8REDD1EhfPZdl4Q+aI=
github.com/ava-labs/awm-relayer v1.4.1-0.20241003183820-366b0dc8cea6/go.mod h1:sSXpQtgiduMdZw5IVTSa1v418VCjb8GDLyrcGwJZ/HE=
github.com/ava-labs/coreth v0.13.8 h1:f14X3KgwHl9LwzfxlN6S4bbn5VA2rhEsNnHaRLSTo/8=
Expand Down
229 changes: 172 additions & 57 deletions tests/flows/validator-manager/erc20_token_staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package staking

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

Expand All @@ -12,7 +14,6 @@ import (
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/platformvm/warp/message"
"github.com/ava-labs/subnet-evm/accounts/abi/bind"
subnetEvmUtils "github.com/ava-labs/subnet-evm/tests/utils"
"github.com/ava-labs/teleporter/tests/interfaces"
"github.com/ava-labs/teleporter/tests/utils"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -59,6 +60,7 @@ func ERC20TokenStakingManager(network interfaces.LocalNetwork) {

var nodes []utils.Node

// Remove the current validators before converting the subnet
for _, uri := range subnetAInfo.NodeURIs {
infoClient := info.NewClient(uri)
nodeID, nodePoP, err := infoClient.GetNodeID(ctx)
Expand All @@ -68,7 +70,6 @@ func ERC20TokenStakingManager(network interfaces.LocalNetwork) {
NodePoP: nodePoP,
})

// Remove the current validators before converting the subnet
_, err = network.GetPChainWallet().IssueRemoveSubnetValidatorTx(
nodeID,
subnetAInfo.SubnetID,
Expand All @@ -90,35 +91,16 @@ func ERC20TokenStakingManager(network interfaces.LocalNetwork) {
// Set the last node's weight such that removing any other node will not violate the churn limit
weights[len(nodes)-1] = 4 * totalWeight

// TODONOW: issue a P-Chain ConvertSubnetTx
destAddrStr := "P-local18jma8ppw3nhx5r4ap8clazz0dps7rv5u00z96u"
destAddr, err := address.ParseToID(destAddrStr)
// Construct the convert subnet info
destAddr, err := address.ParseToID(utils.DefaultPChainAddress)
Expect(err).Should(BeNil())
// vdrs := make([]*txs.ConvertSubnetValidator, len(nodes))
// for i, node := range nodes {
// vdrs[i] = &txs.ConvertSubnetValidator{
// NodeID: node.NodeID.Bytes(),
// Weight: weights[i],
// Balance: units.Avax * 100,
// Signer: *node.NodePoP,
// RemainingBalanceOwner: message.PChainOwner{
// Threshold: 1,
// Addresses: []ids.ShortID{destAddr},
// },
// DeactivationOwner: message.PChainOwner{
// Threshold: 1,
// Addresses: []ids.ShortID{destAddr},
// },
// }
// }

// Set just the last node (with a high weight) to the initial validator list
vdrs := []*txs.ConvertSubnetValidator{
{
NodeID: nodes[len(nodes)-1].NodeID.Bytes(),
Weight: weights[len(nodes)-1],
vdrs := make([]*txs.ConvertSubnetValidator, len(nodes))
for i, node := range nodes {
vdrs[i] = &txs.ConvertSubnetValidator{
NodeID: node.NodeID.Bytes(),
Weight: weights[i],
Balance: units.Avax * 100,
Signer: *nodes[len(nodes)-1].NodePoP,
Signer: *node.NodePoP,
RemainingBalanceOwner: message.PChainOwner{
Threshold: 1,
Addresses: []ids.ShortID{destAddr},
Expand All @@ -127,9 +109,10 @@ func ERC20TokenStakingManager(network interfaces.LocalNetwork) {
Threshold: 1,
Addresses: []ids.ShortID{destAddr},
},
},
}
}

log.Println("Issuing ConvertSubnetTx")
_, err = network.GetPChainWallet().IssueConvertSubnetTx(
subnetAInfo.SubnetID,
subnetAInfo.BlockchainID,
Expand All @@ -139,17 +122,11 @@ func ERC20TokenStakingManager(network interfaces.LocalNetwork) {
Expect(err).Should(BeNil())

utils.PChainProposerVMWorkaround(network)

// Issue txs on the subnet to advance the proposer vm
for i := 0; i < 5; i++ {
err = subnetEvmUtils.IssueTxsToActivateProposerVMFork(
ctx, subnetAInfo.EVMChainID, fundedKey, subnetAInfo.WSClient,
)
Expect(err).Should(BeNil())
}
utils.AdvanceProposerVM(ctx, subnetAInfo, fundedKey, 5)

// Initialize the validator set on the subnet
_ = utils.InitializeERC20TokenValidatorSet(
log.Println("Initializing validator set")
initialValidationIDs := utils.InitializeERC20TokenValidatorSet(
ctx,
fundedKey,
subnetAInfo,
Expand All @@ -158,37 +135,35 @@ func ERC20TokenStakingManager(network interfaces.LocalNetwork) {
stakingManagerAddress,
network,
signatureAggregator,
nodes[len(nodes)-1:],
weights[len(nodes)-1:],
nodes,
weights,
)

// Delisting an initial validator does not work due to the way Warp signatures are requested for initial validators
//
// Delist one initial validators
//
// utils.InitializeAndCompleteEndERC20Validation(
// ctx,
// network,
// signatureAggregator,
// fundedKey,
// subnetAInfo,
// pChainInfo,
// stakingManager,
// stakingManagerAddress,
// initialValidationIDs[0],
// weights[0],
// 1,
// )
utils.InitializeAndCompleteEndInitialERC20Validation(
ctx,
network,
signatureAggregator,
fundedKey,
subnetAInfo,
pChainInfo,
stakingManager,
stakingManagerAddress,
initialValidationIDs[0],
0,
weights[0],
)

//
// Register a validator
// Register the validator as PoS
//
stakeAmount, err := stakingManager.WeightToValue(
&bind.CallOpts{},
weights[0],
)
Expect(err).Should(BeNil())
// TODONOW: pass the validatorID pre image to the signature aggregator
expiry := uint64(time.Now().Add(24 * time.Hour).Unix())
validationID := utils.InitializeAndCompleteERC20ValidatorRegistration(
ctx,
Expand All @@ -206,6 +181,146 @@ func ERC20TokenStakingManager(network interfaces.LocalNetwork) {
nodes[0],
)

//
// Register a delegator
//
var delegationID ids.ID
{
log.Println("Registering delegator")
delegatorStake, err := stakingManager.WeightToValue(
&bind.CallOpts{},
weights[0],
)
Expect(err).Should(BeNil())
delegatorStake.Div(delegatorStake, big.NewInt(10))
delegatorWeight, err := stakingManager.ValueToWeight(
&bind.CallOpts{},
delegatorStake,
)
Expect(err).Should(BeNil())
newValidatorWeight := weights[0] + delegatorWeight

nonce := uint64(1)

receipt := utils.InitializeERC20DelegatorRegistration(
ctx,
fundedKey,
subnetAInfo,
validationID,
delegatorStake,
erc20,
stakingManagerAddress,
stakingManager,
)
initRegistrationEvent, err := utils.GetEventFromLogs(
receipt.Logs,
stakingManager.ParseDelegatorAdded,
)
Expect(err).Should(BeNil())
delegationID = initRegistrationEvent.DelegationID

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

// Issue a tx to update the validator's weight on the P-Chain
network.GetPChainWallet().IssueSetSubnetValidatorWeightTx(signedWarpMessage.Bytes())
utils.PChainProposerVMWorkaround(network)
utils.AdvanceProposerVM(ctx, subnetAInfo, fundedKey, 5)

// Construct a SubnetValidatorWeightUpdateMessage Warp message from the P-Chain
registrationSignedMessage := utils.ConstructSubnetValidatorWeightUpdateMessage(
validationID,
nonce,
newValidatorWeight,
subnetAInfo,
pChainInfo,
network,
signatureAggregator,
)

// Deliver the Warp message to the subnet
receipt = utils.CompleteERC20DelegatorRegistration(
ctx,
fundedKey,
delegationID,
subnetAInfo,
stakingManagerAddress,
registrationSignedMessage,
)
// Check that the validator is registered in the staking contract
registrationEvent, err := utils.GetEventFromLogs(
receipt.Logs,
stakingManager.ParseDelegatorRegistered,
)
Expect(err).Should(BeNil())
Expect(registrationEvent.ValidationID[:]).Should(Equal(validationID[:]))
Expect(registrationEvent.DelegationID[:]).Should(Equal(delegationID[:]))
}

//
// Delist the delegator
//
{
log.Println("Delisting delegator")
nonce := uint64(2)
receipt := utils.InitializeEndERC20Delegation(
ctx,
fundedKey,
subnetAInfo,
stakingManager,
delegationID,
)
delegatorRemovalEvent, err := utils.GetEventFromLogs(
receipt.Logs,
stakingManager.ParseDelegatorRemovalInitialized,
)
Expect(err).Should(BeNil())
Expect(delegatorRemovalEvent.ValidationID[:]).Should(Equal(validationID[:]))
Expect(delegatorRemovalEvent.DelegationID[:]).Should(Equal(delegationID[:]))

// 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())

// Issue a tx to update the validator's weight on the P-Chain
network.GetPChainWallet().IssueSetSubnetValidatorWeightTx(signedWarpMessage.Bytes())

utils.PChainProposerVMWorkaround(network)
utils.AdvanceProposerVM(ctx, subnetAInfo, fundedKey, 5)

// Construct a SubnetValidatorWeightUpdateMessage Warp message from the P-Chain
signedMessage := utils.ConstructSubnetValidatorWeightUpdateMessage(
validationID,
nonce,
weights[0],
subnetAInfo,
pChainInfo,
network,
signatureAggregator,
)

// Deliver the Warp message to the subnet
receipt = utils.CompleteEndERC20Delegation(
ctx,
fundedKey,
delegationID,
subnetAInfo,
stakingManagerAddress,
signedMessage,
)

// Check that the delegator has been delisted from the staking contract
registrationEvent, err := utils.GetEventFromLogs(
receipt.Logs,
stakingManager.ParseDelegationEnded,
)
Expect(err).Should(BeNil())
Expect(registrationEvent.ValidationID[:]).Should(Equal(validationID[:]))
Expect(registrationEvent.DelegationID[:]).Should(Equal(delegationID[:]))
}

//
// Delist the validator
//
Expand Down
10 changes: 0 additions & 10 deletions tests/local/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,6 @@ func NewLocalNetwork(
globalFundedECDSAKey := globalFundedKey.ToECDSA()
Expect(err).Should(BeNil())

g, err := crypto.HexToECDSA(fundedKeyStr)
Expect(err).Should(BeNil())
// TODONOW: remove these once txs are verified
Expect(g.PublicKey.X).Should(Equal(globalFundedECDSAKey.PublicKey.X))
Expect(g.PublicKey.Y).Should(Equal(globalFundedECDSAKey.PublicKey.Y))
Expect(g.X).Should(Equal(globalFundedECDSAKey.X))
Expect(g.Y).Should(Equal(globalFundedECDSAKey.Y))
Expect(g.D).Should(Equal(globalFundedECDSAKey.D))

var subnets []*tmpnet.Subnet
for _, subnetSpec := range subnetSpecs {
nodes := subnetEvmTestUtils.NewTmpnetNodes(subnetSpec.NodeCount)
Expand Down Expand Up @@ -166,7 +157,6 @@ func NewLocalNetwork(
setupProposerVM(ctx, globalFundedECDSAKey, network, subnet.SubnetID)
}

// TODONOW: add Wallet LocalNetwork struct
localNetwork := &LocalNetwork{
primaryNetworkInfo: &interfaces.SubnetTestInfo{},
subnetsInfo: make(map[ids.ID]*interfaces.SubnetTestInfo),
Expand Down
Loading