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

Support json marshalling txs returned from the wallet #2494

Merged
merged 1 commit into from
Dec 16, 2023
Merged
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
10 changes: 10 additions & 0 deletions wallet/chain/c/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func (b *builder) NewImportTx(
importedInputs = append(importedInputs, &avax.TransferableInput{
UTXOID: utxo.UTXOID,
Asset: utxo.Asset,
FxID: secp256k1fx.ID,
In: &secp256k1fx.TransferInput{
Amt: amount,
Input: secp256k1fx.Input{
Expand Down Expand Up @@ -267,6 +268,7 @@ func (b *builder) NewExportTx(
for i, output := range outputs {
exportedOutputs[i] = &avax.TransferableOutput{
Asset: avax.Asset{ID: avaxAssetID},
FxID: secp256k1fx.ID,
Out: output,
}

Expand Down Expand Up @@ -376,6 +378,14 @@ func (b *builder) NewExportTx(

utils.Sort(inputs)
tx.Ins = inputs

snowCtx, err := newSnowContext(b.backend)
if err != nil {
return nil, err
}
for _, out := range tx.ExportedOutputs {
out.InitCtx(snowCtx)
}
return tx, nil
}

Expand Down
21 changes: 20 additions & 1 deletion wallet/chain/c/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ import (

"github.com/ava-labs/avalanchego/api/info"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/vms/avm"
)

const Alias = "C"

var _ Context = (*context)(nil)

type Context interface {
Expand Down Expand Up @@ -41,7 +46,7 @@ func NewContextFromClients(
return nil, err
}

chainID, err := infoClient.GetBlockchainID(ctx, "C")
chainID, err := infoClient.GetBlockchainID(ctx, Alias)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -81,3 +86,17 @@ func (c *context) BlockchainID() ids.ID {
func (c *context) AVAXAssetID() ids.ID {
return c.avaxAssetID
}

func newSnowContext(c Context) (*snow.Context, error) {
chainID := c.BlockchainID()
lookup := ids.NewAliaser()
return &snow.Context{
NetworkID: c.NetworkID(),
SubnetID: constants.PrimaryNetworkID,
ChainID: chainID,
CChainID: chainID,
AVAXAssetID: c.AVAXAssetID(),
Log: logging.NoLog{},
BCLookup: lookup,
}, lookup.Alias(chainID, Alias)
}
70 changes: 46 additions & 24 deletions wallet/chain/p/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ func (b *builder) NewBaseTx(
outputs = append(outputs, changeOutputs...)
avax.SortTransferableOutputs(outputs, txs.Codec) // sort the outputs

return &txs.CreateSubnetTx{
tx := &txs.CreateSubnetTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: constants.PlatformChainID,
Expand All @@ -320,7 +320,8 @@ func (b *builder) NewBaseTx(
Memo: ops.Memo(),
}},
Owner: &secp256k1fx.OutputOwners{},
}, nil
}
return tx, b.initCtx(tx)
}

func (b *builder) NewAddValidatorTx(
Expand All @@ -343,7 +344,7 @@ func (b *builder) NewAddValidatorTx(
}

utils.Sort(rewardsOwner.Addrs)
return &txs.AddValidatorTx{
tx := &txs.AddValidatorTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: constants.PlatformChainID,
Expand All @@ -355,7 +356,8 @@ func (b *builder) NewAddValidatorTx(
StakeOuts: stakeOutputs,
RewardsOwner: rewardsOwner,
DelegationShares: shares,
}, nil
}
return tx, b.initCtx(tx)
}

func (b *builder) NewAddSubnetValidatorTx(
Expand All @@ -377,7 +379,7 @@ func (b *builder) NewAddSubnetValidatorTx(
return nil, err
}

return &txs.AddSubnetValidatorTx{
tx := &txs.AddSubnetValidatorTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: constants.PlatformChainID,
Expand All @@ -387,7 +389,8 @@ func (b *builder) NewAddSubnetValidatorTx(
}},
SubnetValidator: *vdr,
SubnetAuth: subnetAuth,
}, nil
}
return tx, b.initCtx(tx)
}

func (b *builder) NewRemoveSubnetValidatorTx(
Expand All @@ -410,7 +413,7 @@ func (b *builder) NewRemoveSubnetValidatorTx(
return nil, err
}

return &txs.RemoveSubnetValidatorTx{
tx := &txs.RemoveSubnetValidatorTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: constants.PlatformChainID,
Expand All @@ -421,7 +424,8 @@ func (b *builder) NewRemoveSubnetValidatorTx(
Subnet: subnetID,
NodeID: nodeID,
SubnetAuth: subnetAuth,
}, nil
}
return tx, b.initCtx(tx)
}

func (b *builder) NewAddDelegatorTx(
Expand All @@ -443,7 +447,7 @@ func (b *builder) NewAddDelegatorTx(
}

utils.Sort(rewardsOwner.Addrs)
return &txs.AddDelegatorTx{
tx := &txs.AddDelegatorTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: constants.PlatformChainID,
Expand All @@ -454,7 +458,8 @@ func (b *builder) NewAddDelegatorTx(
Validator: *vdr,
StakeOuts: stakeOutputs,
DelegationRewardsOwner: rewardsOwner,
}, nil
}
return tx, b.initCtx(tx)
}

func (b *builder) NewCreateChainTx(
Expand All @@ -481,7 +486,7 @@ func (b *builder) NewCreateChainTx(
}

utils.Sort(fxIDs)
return &txs.CreateChainTx{
tx := &txs.CreateChainTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: constants.PlatformChainID,
Expand All @@ -495,7 +500,8 @@ func (b *builder) NewCreateChainTx(
FxIDs: fxIDs,
GenesisData: genesis,
SubnetAuth: subnetAuth,
}, nil
}
return tx, b.initCtx(tx)
}

func (b *builder) NewCreateSubnetTx(
Expand All @@ -513,7 +519,7 @@ func (b *builder) NewCreateSubnetTx(
}

utils.Sort(owner.Addrs)
return &txs.CreateSubnetTx{
tx := &txs.CreateSubnetTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: constants.PlatformChainID,
Expand All @@ -522,7 +528,8 @@ func (b *builder) NewCreateSubnetTx(
Memo: ops.Memo(),
}},
Owner: owner,
}, nil
}
return tx, b.initCtx(tx)
}

func (b *builder) NewImportTx(
Expand Down Expand Up @@ -618,7 +625,7 @@ func (b *builder) NewImportTx(
}

avax.SortTransferableOutputs(outputs, txs.Codec) // sort imported outputs
return &txs.ImportTx{
tx := &txs.ImportTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: constants.PlatformChainID,
Expand All @@ -628,7 +635,8 @@ func (b *builder) NewImportTx(
}},
SourceChain: sourceChainID,
ImportedInputs: importedInputs,
}, nil
}
return tx, b.initCtx(tx)
}

func (b *builder) NewExportTx(
Expand Down Expand Up @@ -656,7 +664,7 @@ func (b *builder) NewExportTx(
}

avax.SortTransferableOutputs(outputs, txs.Codec) // sort exported outputs
return &txs.ExportTx{
tx := &txs.ExportTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: constants.PlatformChainID,
Expand All @@ -666,7 +674,8 @@ func (b *builder) NewExportTx(
}},
DestinationChain: chainID,
ExportedOutputs: outputs,
}, nil
}
return tx, b.initCtx(tx)
}

func (b *builder) NewTransformSubnetTx(
Expand Down Expand Up @@ -702,7 +711,7 @@ func (b *builder) NewTransformSubnetTx(
return nil, err
}

return &txs.TransformSubnetTx{
tx := &txs.TransformSubnetTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: constants.PlatformChainID,
Expand All @@ -725,7 +734,8 @@ func (b *builder) NewTransformSubnetTx(
MaxValidatorWeightFactor: maxValidatorWeightFactor,
UptimeRequirement: uptimeRequirement,
SubnetAuth: subnetAuth,
}, nil
}
return tx, b.initCtx(tx)
}

func (b *builder) NewAddPermissionlessValidatorTx(
Expand Down Expand Up @@ -755,7 +765,7 @@ func (b *builder) NewAddPermissionlessValidatorTx(

utils.Sort(validationRewardsOwner.Addrs)
utils.Sort(delegationRewardsOwner.Addrs)
return &txs.AddPermissionlessValidatorTx{
tx := &txs.AddPermissionlessValidatorTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: constants.PlatformChainID,
Expand All @@ -770,7 +780,8 @@ func (b *builder) NewAddPermissionlessValidatorTx(
ValidatorRewardsOwner: validationRewardsOwner,
DelegatorRewardsOwner: delegationRewardsOwner,
DelegationShares: shares,
}, nil
}
return tx, b.initCtx(tx)
}

func (b *builder) NewAddPermissionlessDelegatorTx(
Expand All @@ -796,7 +807,7 @@ func (b *builder) NewAddPermissionlessDelegatorTx(
}

utils.Sort(rewardsOwner.Addrs)
return &txs.AddPermissionlessDelegatorTx{
tx := &txs.AddPermissionlessDelegatorTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: constants.PlatformChainID,
Expand All @@ -808,7 +819,8 @@ func (b *builder) NewAddPermissionlessDelegatorTx(
Subnet: vdr.Subnet,
StakeOuts: stakeOutputs,
DelegationRewardsOwner: rewardsOwner,
}, nil
}
return tx, b.initCtx(tx)
}

func (b *builder) getBalance(
Expand Down Expand Up @@ -1117,3 +1129,13 @@ func (b *builder) authorizeSubnet(subnetID ids.ID, options *common.Options) (*se
SigIndices: inputSigIndices,
}, nil
}

func (b *builder) initCtx(tx txs.UnsignedTx) error {
ctx, err := newSnowContext(b.backend)
if err != nil {
return err
}

tx.InitCtx(ctx)
return nil
}
17 changes: 17 additions & 0 deletions wallet/chain/p/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ import (

"github.com/ava-labs/avalanchego/api/info"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/vms/avm"
)

const Alias = "P"

var _ Context = (*context)(nil)

type Context interface {
Expand Down Expand Up @@ -144,3 +149,15 @@ func (c *context) AddSubnetValidatorFee() uint64 {
func (c *context) AddSubnetDelegatorFee() uint64 {
return c.addSubnetDelegatorFee
}

func newSnowContext(c Context) (*snow.Context, error) {
lookup := ids.NewAliaser()
return &snow.Context{
NetworkID: c.NetworkID(),
SubnetID: constants.PrimaryNetworkID,
ChainID: constants.PlatformChainID,
AVAXAssetID: c.AVAXAssetID(),
Log: logging.NoLog{},
BCLookup: lookup,
}, lookup.Alias(constants.PlatformChainID, Alias)
}
Loading
Loading