Skip to content

Commit

Permalink
moved wallet builder backend to pchain
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 committed Feb 20, 2024
1 parent e26bec9 commit dd17dbb
Show file tree
Hide file tree
Showing 21 changed files with 284 additions and 218 deletions.
35 changes: 35 additions & 0 deletions vms/platformvm/txs/backends/backends.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package backends

import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/platformvm/fx"

stdcontext "context"
)

var (
_ BuilderBackend = WalletBackend(nil)
_ SignerBackend = WalletBackend(nil)
)

type WalletBackend interface {
Context
UTXOs(ctx stdcontext.Context, sourceChainID ids.ID) ([]*avax.UTXO, error)
GetSubnetOwner(ctx stdcontext.Context, subnetID ids.ID) (fx.Owner, error)
GetUTXO(ctx stdcontext.Context, chainID, utxoID ids.ID) (*avax.UTXO, error)
}

type SignerBackend interface {
GetUTXO(ctx stdcontext.Context, chainID, utxoID ids.ID) (*avax.UTXO, error)
GetSubnetOwner(ctx stdcontext.Context, subnetID ids.ID) (fx.Owner, error)
}

type BuilderBackend interface {
Context
UTXOs(ctx stdcontext.Context, sourceChainID ids.ID) ([]*avax.UTXO, error)
GetSubnetOwner(ctx stdcontext.Context, subnetID ids.ID) (fx.Owner, error)
}
100 changes: 100 additions & 0 deletions vms/platformvm/txs/backends/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package backends

import "github.com/ava-labs/avalanchego/ids"

var _ Context = (*builderCtx)(nil)

type Context interface {
NetworkID() uint32
AVAXAssetID() ids.ID
BaseTxFee() uint64
CreateSubnetTxFee() uint64
TransformSubnetTxFee() uint64
CreateBlockchainTxFee() uint64
AddPrimaryNetworkValidatorFee() uint64
AddPrimaryNetworkDelegatorFee() uint64
AddSubnetValidatorFee() uint64
AddSubnetDelegatorFee() uint64
}

type builderCtx struct {
networkID uint32
avaxAssetID ids.ID
baseTxFee uint64
createSubnetTxFee uint64
transformSubnetTxFee uint64
createBlockchainTxFee uint64
addPrimaryNetworkValidatorFee uint64
addPrimaryNetworkDelegatorFee uint64
addSubnetValidatorFee uint64
addSubnetDelegatorFee uint64
}

func NewContext(
networkID uint32,
avaxAssetID ids.ID,
baseTxFee uint64,
createSubnetTxFee uint64,
transformSubnetTxFee uint64,
createBlockchainTxFee uint64,
addPrimaryNetworkValidatorFee uint64,
addPrimaryNetworkDelegatorFee uint64,
addSubnetValidatorFee uint64,
addSubnetDelegatorFee uint64,
) Context {
return &builderCtx{
networkID: networkID,
avaxAssetID: avaxAssetID,
baseTxFee: baseTxFee,
createSubnetTxFee: createSubnetTxFee,
transformSubnetTxFee: transformSubnetTxFee,
createBlockchainTxFee: createBlockchainTxFee,
addPrimaryNetworkValidatorFee: addPrimaryNetworkValidatorFee,
addPrimaryNetworkDelegatorFee: addPrimaryNetworkDelegatorFee,
addSubnetValidatorFee: addSubnetValidatorFee,
addSubnetDelegatorFee: addSubnetDelegatorFee,
}
}

func (c *builderCtx) NetworkID() uint32 {
return c.networkID
}

func (c *builderCtx) AVAXAssetID() ids.ID {
return c.avaxAssetID
}

func (c *builderCtx) BaseTxFee() uint64 {
return c.baseTxFee
}

func (c *builderCtx) CreateSubnetTxFee() uint64 {
return c.createSubnetTxFee
}

func (c *builderCtx) TransformSubnetTxFee() uint64 {
return c.transformSubnetTxFee
}

func (c *builderCtx) CreateBlockchainTxFee() uint64 {
return c.createBlockchainTxFee
}

func (c *builderCtx) AddPrimaryNetworkValidatorFee() uint64 {
return c.addPrimaryNetworkValidatorFee
}

func (c *builderCtx) AddPrimaryNetworkDelegatorFee() uint64 {
return c.addPrimaryNetworkDelegatorFee
}

func (c *builderCtx) AddSubnetValidatorFee() uint64 {
return c.addSubnetValidatorFee
}

func (c *builderCtx) AddSubnetDelegatorFee() uint64 {
return c.addSubnetDelegatorFee
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package signer
package backends

import (
"context"

"github.com/ava-labs/avalanchego/utils/crypto/keychain"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"

stdcontext "context"
)

var _ Signer = (*txSigner)(nil)
Expand All @@ -21,22 +21,22 @@ type Signer interface {
//
// If the signer doesn't have the ability to provide a required signature,
// the signature slot will be skipped without reporting an error.
Sign(ctx context.Context, tx *txs.Tx) error
Sign(ctx stdcontext.Context, tx *txs.Tx) error
}

type txSigner struct {
kc keychain.Keychain
backend Backend
backend SignerBackend
}

func New(kc keychain.Keychain, backend Backend) Signer {
func New(kc keychain.Keychain, backend SignerBackend) Signer {
return &txSigner{
kc: kc,
backend: backend,
}
}

func (s *txSigner) Sign(ctx context.Context, tx *txs.Tx) error {
func (s *txSigner) Sign(ctx stdcontext.Context, tx *txs.Tx) error {
return tx.Unsigned.Visit(&Visitor{
kc: s.kc,
backend: s.backend,
Expand All @@ -46,7 +46,7 @@ func (s *txSigner) Sign(ctx context.Context, tx *txs.Tx) error {
}

func SignUnsigned(
ctx context.Context,
ctx stdcontext.Context,
s Signer,
utx txs.UnsignedTx,
) (*txs.Tx, error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package signer
package backends

import (
"context"
Expand Down Expand Up @@ -38,7 +38,7 @@ var (
// Visitor handles signing transactions for the signer
type Visitor struct {
kc keychain.Keychain
backend Backend
backend SignerBackend
ctx context.Context
tx *txs.Tx
}
Expand Down
Loading

0 comments on commit dd17dbb

Please sign in to comment.