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 183d948
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 35 deletions.
45 changes: 15 additions & 30 deletions vms/platformvm/txs/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"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 +26,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,42 +464,25 @@ 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,
}

s := backends.New(
secp256k1fx.NewKeychain(keys...),
NewSignerBackend(b.state, ids.Empty, nil),
Expand Down
10 changes: 7 additions & 3 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,6 +21,7 @@ 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(
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

0 comments on commit 183d948

Please sign in to comment.