Skip to content

Commit

Permalink
wip: chaining p-chain txbuilder with wallet builder and signer
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 committed Feb 20, 2024
1 parent 0904787 commit 0f321cc
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 66 deletions.
2 changes: 1 addition & 1 deletion vms/platformvm/txs/backends/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ func (b *builder) authorizeSubnet(subnetID ids.ID, options *common.Options) (*se
}

func (b *builder) initCtx(tx txs.UnsignedTx) error {
ctx, err := newSnowContext(b.backend)
ctx, err := NewSnowContext(b.backend.NetworkID(), b.backend.AVAXAssetID())
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions vms/platformvm/txs/backends/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ func (c *builderCtx) AddSubnetDelegatorFee() uint64 {
return c.addSubnetDelegatorFee
}

func newSnowContext(c Context) (*snow.Context, error) {
func NewSnowContext(networkID uint32, avaxAssetID ids.ID) (*snow.Context, error) {
lookup := ids.NewAliaser()
return &snow.Context{
NetworkID: c.NetworkID(),
NetworkID: networkID,
SubnetID: constants.PrimaryNetworkID,
ChainID: constants.PlatformChainID,
AVAXAssetID: c.AVAXAssetID(),
AVAXAssetID: avaxAssetID,
Log: logging.NoLog{},
BCLookup: lookup,
}, lookup.Alias(constants.PlatformChainID, Alias)
Expand Down
91 changes: 35 additions & 56 deletions vms/platformvm/txs/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/platformvm/config"
Expand All @@ -25,6 +25,7 @@ import (
"github.com/ava-labs/avalanchego/vms/platformvm/txs/backends"
"github.com/ava-labs/avalanchego/vms/platformvm/utxo"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"
)

// Max number of items allowed in a page
Expand Down Expand Up @@ -462,40 +463,23 @@ func (b *builder) NewCreateChainTx(
changeAddr ids.ShortID,
memo []byte,
) (*txs.Tx, error) {
timestamp := b.state.GetTimestamp()
createBlockchainTxFee := b.cfg.GetCreateBlockchainTxFee(timestamp)
toBurn := map[ids.ID]uint64{
b.ctx.AVAXAssetID: createBlockchainTxFee,
}
toStake := make(map[ids.ID]uint64)
ins, outs, _, err := b.Spend(b.state, keys, toBurn, toStake, changeAddr)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
addrs := set.NewSet[ids.ShortID](len(keys))
for _, key := range keys {
addrs.Add(key.Address())
}
builderBackend := NewBuilderBackend(b.ctx, b.cfg, addrs, b.state)
pBuilder := backends.NewBuilder(addrs, builderBackend)

subnetAuth, _, err := b.Authorize(b.state, subnetID, keys)
opts := common.UnionOptions(
[]common.Option{common.WithChangeOwner(&secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{changeAddr},
})},
[]common.Option{common.WithMemo(memo)},
)
utx, err := pBuilder.NewCreateChainTx(subnetID, genesisData, vmID, fxIDs, chainName, opts...)
if err != nil {
return nil, fmt.Errorf("couldn't authorize tx's subnet restrictions: %w", err)
}

// Sort the provided fxIDs
utils.Sort(fxIDs)

// Create the tx
utx := &txs.CreateChainTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.ctx.NetworkID,
BlockchainID: b.ctx.ChainID,
Ins: ins,
Outs: outs,
Memo: memo,
}},
SubnetID: subnetID,
ChainName: chainName,
VMID: vmID,
FxIDs: fxIDs,
GenesisData: genesisData,
SubnetAuth: subnetAuth,
return nil, fmt.Errorf("failed building create chain tx: %w", err)
}

s := backends.New(
Expand All @@ -516,33 +500,28 @@ func (b *builder) NewCreateSubnetTx(
changeAddr ids.ShortID,
memo []byte,
) (*txs.Tx, error) {
timestamp := b.state.GetTimestamp()
createSubnetTxFee := b.cfg.GetCreateSubnetTxFee(timestamp)
toBurn := map[ids.ID]uint64{
b.ctx.AVAXAssetID: createSubnetTxFee,
}
toStake := make(map[ids.ID]uint64)
ins, outs, _, err := b.Spend(b.state, keys, toBurn, toStake, changeAddr)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
addrs := set.NewSet[ids.ShortID](len(keys))
for _, key := range keys {
addrs.Add(key.Address())
}
builderBackend := NewBuilderBackend(b.ctx, b.cfg, addrs, b.state)
pBuilder := backends.NewBuilder(addrs, builderBackend)

// Sort control addresses
utils.Sort(ownerAddrs)
subnetOwner := &secp256k1fx.OutputOwners{
Threshold: threshold,
Addrs: ownerAddrs,
}

// Create the tx
utx := &txs.CreateSubnetTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.ctx.NetworkID,
BlockchainID: b.ctx.ChainID,
Ins: ins,
Outs: outs,
Memo: memo,
}},
Owner: &secp256k1fx.OutputOwners{
Threshold: threshold,
Addrs: ownerAddrs,
},
opts := common.UnionOptions(
[]common.Option{common.WithChangeOwner(&secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{changeAddr},
})},
[]common.Option{common.WithMemo(memo)},
)
utx, err := pBuilder.NewCreateSubnetTx(subnetOwner, opts...)
if err != nil {
return nil, fmt.Errorf("failed building create subnet tx: %w", err)
}

s := backends.New(
Expand Down
12 changes: 8 additions & 4 deletions vms/platformvm/txs/builder/builder_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ package builder

import (
"context"
"errors"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/platformvm/config"
"github.com/ava-labs/avalanchego/vms/platformvm/fx"
Expand All @@ -21,13 +21,14 @@ var _ backends.BuilderBackend = (*buiderBackend)(nil)
func NewBuilderBackend(
ctx *snow.Context,
cfg *config.Config,
addrs set.Set[ids.ShortID],
state state.State,
) backends.BuilderBackend {
backendCtx := backends.NewContext(
ctx.NetworkID,
ctx.AVAXAssetID,
cfg.TxFee,
cfg.CreateSubnetTxFee,
cfg.GetCreateSubnetTxFee(state.GetTimestamp()),
cfg.TransformSubnetTxFee,
cfg.CreateBlockchainTxFee,
cfg.AddPrimaryNetworkValidatorFee,
Expand All @@ -37,18 +38,21 @@ func NewBuilderBackend(
)
return &buiderBackend{
Context: backendCtx,
addrs: addrs,
state: state,
}
}

type buiderBackend struct {
backends.Context

addrs set.Set[ids.ShortID]
state state.State
}

func (*buiderBackend) UTXOs(_ context.Context /*sourceChainID*/, _ ids.ID) ([]*avax.UTXO, error) {
return nil, errors.New("not yet implemented")
// TODO ABENEGIA: handle non-P-chain UTXOs case
func (b *buiderBackend) UTXOs(_ context.Context /*sourceChainID*/, _ ids.ID) ([]*avax.UTXO, error) {
return avax.GetAllUTXOs(b.state, b.addrs) // The UTXOs controlled by [keys]
}

func (b *buiderBackend) GetSubnetOwner(_ context.Context, subnetID ids.ID) (fx.Owner, error) {
Expand Down
4 changes: 2 additions & 2 deletions vms/platformvm/txs/executor/create_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestCreateChainTxInsufficientControlSigs(t *testing.T) {
require.NoError(err)

// Remove a signature
tx.Creds[0].(*secp256k1fx.Credential).Sigs = tx.Creds[0].(*secp256k1fx.Credential).Sigs[1:]
tx.Creds[1].(*secp256k1fx.Credential).Sigs = tx.Creds[1].(*secp256k1fx.Credential).Sigs[1:]

stateDiff, err := state.NewDiff(lastAcceptedID, env)
require.NoError(err)
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestCreateChainTxWrongControlSig(t *testing.T) {
// Replace a valid signature with one from another key
sig, err := key.SignHash(hashing.ComputeHash256(tx.Unsigned.Bytes()))
require.NoError(err)
copy(tx.Creds[0].(*secp256k1fx.Credential).Sigs[0][:], sig)
copy(tx.Creds[1].(*secp256k1fx.Credential).Sigs[0][:], sig)

stateDiff, err := state.NewDiff(lastAcceptedID, env)
require.NoError(err)
Expand Down
4 changes: 4 additions & 0 deletions vms/platformvm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import (
"github.com/ava-labs/avalanchego/vms/platformvm/signer"
"github.com/ava-labs/avalanchego/vms/platformvm/status"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/backends"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"

smcon "github.com/ava-labs/avalanchego/snow/consensus/snowman"
Expand Down Expand Up @@ -2143,6 +2144,9 @@ func TestTransferSubnetOwnershipTx(t *testing.T) {
keys[0].PublicKey().Address(),
},
}
ctx, err := backends.NewSnowContext(vm.ctx.NetworkID, vm.ctx.AVAXAssetID)
require.NoError(err)
expectedOwner.InitCtx(ctx)
require.Equal(expectedOwner, subnetOwner)

transferSubnetOwnershipTx, err := vm.txBuilder.NewTransferSubnetOwnershipTx(
Expand Down

0 comments on commit 0f321cc

Please sign in to comment.