Skip to content

Commit 912f6f9

Browse files
Add partial-sync support to the wallet (#3567)
1 parent f9d4e39 commit 912f6f9

File tree

18 files changed

+153
-120
lines changed

18 files changed

+153
-120
lines changed

vms/example/xsvm/cmd/chain/create/cmd.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,13 @@ func createFunc(c *cobra.Command, args []string) error {
3838
ctx := c.Context()
3939
kc := secp256k1fx.NewKeychain(config.PrivateKey)
4040

41-
// NewWalletFromURI fetches the available UTXOs owned by [kc] on the network
42-
// that [uri] is hosting.
41+
// MakePWallet fetches the available UTXOs owned by [kc] on the P-chain that
42+
// [uri] is hosting.
4343
walletSyncStartTime := time.Now()
44-
wallet, err := primary.MakeWallet(
44+
wallet, err := primary.MakePWallet(
4545
ctx,
4646
config.URI,
4747
kc,
48-
kc,
4948
primary.WalletConfig{
5049
SubnetIDs: []ids.ID{config.SubnetID},
5150
},
@@ -55,9 +54,6 @@ func createFunc(c *cobra.Command, args []string) error {
5554
}
5655
log.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime))
5756

58-
// Get the P-chain wallet
59-
pWallet := wallet.P()
60-
6157
genesisBytes, err := genesis.Codec.Marshal(genesis.CodecVersion, &genesis.Genesis{
6258
Timestamp: 0,
6359
Allocations: []genesis.Allocation{
@@ -72,7 +68,7 @@ func createFunc(c *cobra.Command, args []string) error {
7268
}
7369

7470
createChainStartTime := time.Now()
75-
createChainTxID, err := pWallet.IssueCreateChainTx(
71+
createChainTxID, err := wallet.IssueCreateChainTx(
7672
config.SubnetID,
7773
genesisBytes,
7874
constants.XSVMID,

vms/platformvm/client.go

+26
Original file line numberDiff line numberDiff line change
@@ -687,3 +687,29 @@ func GetDeactivationOwners(
687687
}
688688
return deactivationOwners, nil
689689
}
690+
691+
// GetOwners returns the union of GetSubnetOwners and GetDeactivationOwners.
692+
func GetOwners(
693+
c Client,
694+
ctx context.Context,
695+
subnetIDs []ids.ID,
696+
validationIDs []ids.ID,
697+
) (map[ids.ID]fx.Owner, error) {
698+
subnetOwners, err := GetSubnetOwners(c, ctx, subnetIDs...)
699+
if err != nil {
700+
return nil, err
701+
}
702+
deactivationOwners, err := GetDeactivationOwners(c, ctx, validationIDs...)
703+
if err != nil {
704+
return nil, err
705+
}
706+
707+
owners := make(map[ids.ID]fx.Owner, len(subnetOwners)+len(deactivationOwners))
708+
for id, owner := range subnetOwners {
709+
owners[id] = owner
710+
}
711+
for id, owner := range deactivationOwners {
712+
owners[id] = owner
713+
}
714+
return owners, nil
715+
}

wallet/chain/p/context.go

+9-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"context"
88

99
"github.com/ava-labs/avalanchego/api/info"
10-
"github.com/ava-labs/avalanchego/vms/avm"
10+
"github.com/ava-labs/avalanchego/utils/constants"
1111
"github.com/ava-labs/avalanchego/vms/platformvm"
1212
"github.com/ava-labs/avalanchego/vms/platformvm/txs/fee"
1313
"github.com/ava-labs/avalanchego/wallet/chain/p/builder"
@@ -21,42 +21,40 @@ const gasPriceMultiplier = 2
2121

2222
func NewContextFromURI(ctx context.Context, uri string) (*builder.Context, error) {
2323
infoClient := info.NewClient(uri)
24-
xChainClient := avm.NewClient(uri, "X")
25-
pChainClient := platformvm.NewClient(uri)
26-
return NewContextFromClients(ctx, infoClient, xChainClient, pChainClient)
24+
chainClient := platformvm.NewClient(uri)
25+
return NewContextFromClients(ctx, infoClient, chainClient)
2726
}
2827

2928
func NewContextFromClients(
3029
ctx context.Context,
3130
infoClient info.Client,
32-
xChainClient avm.Client,
33-
pChainClient platformvm.Client,
31+
chainClient platformvm.Client,
3432
) (*builder.Context, error) {
3533
networkID, err := infoClient.GetNetworkID(ctx)
3634
if err != nil {
3735
return nil, err
3836
}
3937

40-
asset, err := xChainClient.GetAssetDescription(ctx, "AVAX")
38+
avaxAssetID, err := chainClient.GetStakingAssetID(ctx, constants.PrimaryNetworkID)
4139
if err != nil {
4240
return nil, err
4341
}
4442

45-
dynamicFeeConfig, err := pChainClient.GetFeeConfig(ctx)
43+
dynamicFeeConfig, err := chainClient.GetFeeConfig(ctx)
4644
if err != nil {
4745
return nil, err
4846
}
4947

5048
// TODO: After Etna is activated, assume the gas price is always non-zero.
5149
if dynamicFeeConfig.MinPrice != 0 {
52-
_, gasPrice, _, err := pChainClient.GetFeeState(ctx)
50+
_, gasPrice, _, err := chainClient.GetFeeState(ctx)
5351
if err != nil {
5452
return nil, err
5553
}
5654

5755
return &builder.Context{
5856
NetworkID: networkID,
59-
AVAXAssetID: asset.AssetID,
57+
AVAXAssetID: avaxAssetID,
6058
ComplexityWeights: dynamicFeeConfig.Weights,
6159
GasPrice: gasPriceMultiplier * gasPrice,
6260
}, nil
@@ -69,7 +67,7 @@ func NewContextFromClients(
6967

7068
return &builder.Context{
7169
NetworkID: networkID,
72-
AVAXAssetID: asset.AssetID,
70+
AVAXAssetID: avaxAssetID,
7371
StaticFeeConfig: fee.StaticConfig{
7472
TxFee: uint64(staticFeeConfig.TxFee),
7573
CreateSubnetTxFee: uint64(staticFeeConfig.CreateSubnetTxFee),

wallet/subnet/primary/api.go

+33-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func FetchState(
8080
xClient := avm.NewClient(uri, "X")
8181
cClient := evm.NewCChainClient(uri)
8282

83-
pCTX, err := p.NewContextFromClients(ctx, infoClient, xClient, pClient)
83+
pCTX, err := p.NewContextFromClients(ctx, infoClient, pClient)
8484
if err != nil {
8585
return nil, err
8686
}
@@ -145,6 +145,38 @@ func FetchState(
145145
}, nil
146146
}
147147

148+
func FetchPState(
149+
ctx context.Context,
150+
uri string,
151+
addrs set.Set[ids.ShortID],
152+
) (
153+
platformvm.Client,
154+
*pbuilder.Context,
155+
walletcommon.UTXOs,
156+
error,
157+
) {
158+
infoClient := info.NewClient(uri)
159+
chainClient := platformvm.NewClient(uri)
160+
161+
context, err := p.NewContextFromClients(ctx, infoClient, chainClient)
162+
if err != nil {
163+
return nil, nil, nil, err
164+
}
165+
166+
utxos := walletcommon.NewUTXOs()
167+
addrList := addrs.List()
168+
err = AddAllUTXOs(
169+
ctx,
170+
utxos,
171+
chainClient,
172+
txs.Codec,
173+
constants.PlatformChainID,
174+
constants.PlatformChainID,
175+
addrList,
176+
)
177+
return chainClient, context, utxos, err
178+
}
179+
148180
type EthState struct {
149181
Client ethclient.Client
150182
Accounts map[ethcommon.Address]*c.Account

wallet/subnet/primary/examples/add-permissioned-subnet-validator/main.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,13 @@ func main() {
4141
}
4242
log.Printf("fetched node ID %s in %s\n", nodeID, time.Since(nodeInfoStartTime))
4343

44-
// MakeWallet fetches the available UTXOs owned by [kc] on the network that
44+
// MakePWallet fetches the available UTXOs owned by [kc] on the P-chain that
4545
// [uri] is hosting and registers [subnetID].
4646
walletSyncStartTime := time.Now()
47-
wallet, err := primary.MakeWallet(
47+
wallet, err := primary.MakePWallet(
4848
ctx,
4949
uri,
5050
kc,
51-
kc,
5251
primary.WalletConfig{
5352
SubnetIDs: []ids.ID{subnetID},
5453
},
@@ -58,11 +57,8 @@ func main() {
5857
}
5958
log.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime))
6059

61-
// Get the P-chain wallet
62-
pWallet := wallet.P()
63-
6460
addValidatorStartTime := time.Now()
65-
addValidatorTx, err := pWallet.IssueAddSubnetValidatorTx(&txs.SubnetValidator{
61+
addValidatorTx, err := wallet.IssueAddSubnetValidatorTx(&txs.SubnetValidator{
6662
Validator: txs.Validator{
6763
NodeID: nodeID,
6864
Start: uint64(startTime.Unix()),

wallet/subnet/primary/examples/add-primary-validator/main.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -39,37 +39,33 @@ func main() {
3939
}
4040
log.Printf("fetched node ID %s in %s\n", nodeID, time.Since(nodeInfoStartTime))
4141

42-
// MakeWallet fetches the available UTXOs owned by [kc] on the network that
42+
// MakePWallet fetches the available UTXOs owned by [kc] on the P-chain that
4343
// [uri] is hosting.
4444
walletSyncStartTime := time.Now()
45-
wallet, err := primary.MakeWallet(
45+
wallet, err := primary.MakePWallet(
4646
ctx,
4747
uri,
4848
kc,
49-
kc,
5049
primary.WalletConfig{},
5150
)
5251
if err != nil {
5352
log.Fatalf("failed to initialize wallet: %s\n", err)
5453
}
5554
log.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime))
5655

57-
// Get the P-chain wallet
58-
pWallet := wallet.P()
59-
pBuilder := pWallet.Builder()
60-
pContext := pBuilder.Context()
61-
avaxAssetID := pContext.AVAXAssetID
56+
// Get the chain context
57+
context := wallet.Builder().Context()
6258

6359
addValidatorStartTime := time.Now()
64-
addValidatorTx, err := pWallet.IssueAddPermissionlessValidatorTx(
60+
addValidatorTx, err := wallet.IssueAddPermissionlessValidatorTx(
6561
&txs.SubnetValidator{Validator: txs.Validator{
6662
NodeID: nodeID,
6763
Start: uint64(startTime.Unix()),
6864
End: uint64(startTime.Add(duration).Unix()),
6965
Wght: weight,
7066
}},
7167
nodePOP,
72-
avaxAssetID,
68+
context.AVAXAssetID,
7369
&secp256k1fx.OutputOwners{
7470
Threshold: 1,
7571
Addrs: []ids.ShortID{validatorRewardAddr},

wallet/subnet/primary/examples/c-chain-export/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func main() {
3939
}
4040
log.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime))
4141

42-
// Get the P-chain wallet
42+
// Get the chain wallets
4343
pWallet := wallet.P()
4444
cWallet := wallet.C()
4545

wallet/subnet/primary/examples/c-chain-import/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func main() {
4343
}
4444
log.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime))
4545

46-
// Get the P-chain wallet
46+
// Get the chain wallets
4747
pWallet := wallet.P()
4848
cWallet := wallet.C()
4949

wallet/subnet/primary/examples/convert-subnet-to-l1/main.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,13 @@ func main() {
6060
log.Fatalf("failed to calculate conversionID: %s\n", err)
6161
}
6262

63-
// MakeWallet fetches the available UTXOs owned by [kc] on the network that
63+
// MakePWallet fetches the available UTXOs owned by [kc] on the P-chain that
6464
// [uri] is hosting and registers [subnetID].
6565
walletSyncStartTime := time.Now()
66-
wallet, err := primary.MakeWallet(
66+
wallet, err := primary.MakePWallet(
6767
ctx,
6868
uri,
6969
kc,
70-
kc,
7170
primary.WalletConfig{
7271
SubnetIDs: []ids.ID{subnetID},
7372
},
@@ -77,11 +76,8 @@ func main() {
7776
}
7877
log.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime))
7978

80-
// Get the P-chain wallet
81-
pWallet := wallet.P()
82-
8379
convertSubnetToL1StartTime := time.Now()
84-
convertSubnetToL1Tx, err := pWallet.IssueConvertSubnetToL1Tx(
80+
convertSubnetToL1Tx, err := wallet.IssueConvertSubnetToL1Tx(
8581
subnetID,
8682
chainID,
8783
address,

wallet/subnet/primary/examples/create-chain/main.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,13 @@ func main() {
4747

4848
ctx := context.Background()
4949

50-
// MakeWallet fetches the available UTXOs owned by [kc] on the network that
50+
// MakePWallet fetches the available UTXOs owned by [kc] on the P-chain that
5151
// [uri] is hosting and registers [subnetID].
5252
walletSyncStartTime := time.Now()
53-
wallet, err := primary.MakeWallet(
53+
wallet, err := primary.MakePWallet(
5454
ctx,
5555
uri,
5656
kc,
57-
kc,
5857
primary.WalletConfig{
5958
SubnetIDs: []ids.ID{subnetID},
6059
},
@@ -64,11 +63,8 @@ func main() {
6463
}
6564
log.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime))
6665

67-
// Get the P-chain wallet
68-
pWallet := wallet.P()
69-
7066
createChainStartTime := time.Now()
71-
createChainTx, err := pWallet.IssueCreateChainTx(
67+
createChainTx, err := wallet.IssueCreateChainTx(
7268
subnetID,
7369
genesisBytes,
7470
vmID,

wallet/subnet/primary/examples/create-locked-stakeable/main.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,28 @@ func main() {
3333

3434
ctx := context.Background()
3535

36-
// MakeWallet fetches the available UTXOs owned by [kc] on the network that
36+
// MakePWallet fetches the available UTXOs owned by [kc] on the P-chain that
3737
// [uri] is hosting.
3838
walletSyncStartTime := time.Now()
39-
wallet, err := primary.MakeWallet(
39+
wallet, err := primary.MakePWallet(
4040
ctx,
4141
uri,
4242
kc,
43-
kc,
4443
primary.WalletConfig{},
4544
)
4645
if err != nil {
4746
log.Fatalf("failed to initialize wallet: %s\n", err)
4847
}
4948
log.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime))
5049

51-
// Get the P-chain wallet
52-
pWallet := wallet.P()
53-
pBuilder := pWallet.Builder()
54-
pContext := pBuilder.Context()
55-
avaxAssetID := pContext.AVAXAssetID
50+
// Get the chain context
51+
context := wallet.Builder().Context()
5652

5753
issueTxStartTime := time.Now()
58-
tx, err := pWallet.IssueBaseTx([]*avax.TransferableOutput{
54+
tx, err := wallet.IssueBaseTx([]*avax.TransferableOutput{
5955
{
6056
Asset: avax.Asset{
61-
ID: avaxAssetID,
57+
ID: context.AVAXAssetID,
6258
},
6359
Out: &stakeable.LockOut{
6460
Locktime: locktime,

0 commit comments

Comments
 (0)