Skip to content

Commit

Permalink
enable wallet users to also pass in previous owner transfer txs
Browse files Browse the repository at this point in the history
  • Loading branch information
felipemadero committed Jan 24, 2024
1 parent c1890a3 commit 5218d46
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 46 deletions.
68 changes: 53 additions & 15 deletions wallet/chain/p/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,22 @@ type backend struct {
// txID -> tx
txs map[ids.ID]*txs.Tx

subnetOwnerTransferLock sync.RWMutex
subnetOwnerLock sync.RWMutex
// subnetID -> owner
subnetOwnerTransfer map[ids.ID]*secp256k1fx.OutputOwners
subnetOwner map[ids.ID]*secp256k1fx.OutputOwners
}

func NewBackend(ctx Context, utxos common.ChainUTXOs, txs map[ids.ID]*txs.Tx) Backend {
return &backend{
Context: ctx,
ChainUTXOs: utxos,
txs: txs,
func NewBackend(ctx Context, utxos common.ChainUTXOs, txs map[ids.ID]*txs.Tx) (Backend, error) {
subnetOwner, err := getSubnetOwnerMap(txs)
if err != nil {
return nil, err
}
return &backend{
Context: ctx,
ChainUTXOs: utxos,
txs: txs,
subnetOwner: subnetOwner,
}, nil
}

func (b *backend) AcceptTx(ctx stdcontext.Context, tx *txs.Tx) error {
Expand Down Expand Up @@ -103,16 +108,49 @@ func (b *backend) GetTx(_ stdcontext.Context, txID ids.ID) (*txs.Tx, error) {
return tx, nil
}

func (b *backend) setSubnetOwnerTransfer(_ stdcontext.Context, subnetID ids.ID, owner *secp256k1fx.OutputOwners) {
b.subnetOwnerTransferLock.Lock()
defer b.subnetOwnerTransferLock.Unlock()
func (b *backend) setSubnetOwner(_ stdcontext.Context, subnetID ids.ID, owner *secp256k1fx.OutputOwners) {
b.subnetOwnerLock.Lock()
defer b.subnetOwnerLock.Unlock()

b.subnetOwnerTransfer[subnetID] = owner
b.subnetOwner[subnetID] = owner
}

func (b *backend) GetSubnetOwnerTransfer(_ stdcontext.Context, subnetID ids.ID) *secp256k1fx.OutputOwners {
b.subnetOwnerTransferLock.RLock()
defer b.subnetOwnerTransferLock.RUnlock()
func (b *backend) GetSubnetOwner(_ stdcontext.Context, subnetID ids.ID) (*secp256k1fx.OutputOwners, error) {
b.subnetOwnerLock.RLock()
defer b.subnetOwnerLock.RUnlock()

owner, exists := b.subnetOwner[subnetID]
if !exists {
return nil, database.ErrNotFound
}
return owner, nil
}

return b.subnetOwnerTransfer[subnetID]
func getSubnetOwnerMap(pChainTxs map[ids.ID]*txs.Tx) (map[ids.ID]*secp256k1fx.OutputOwners, error) {
subnetOwner := map[ids.ID]*secp256k1fx.OutputOwners{}
// first get owners from original CreateSubnetTx
for txID, tx := range pChainTxs {
createSubnetTx, ok := tx.Unsigned.(*txs.CreateSubnetTx)
if !ok {
continue
}
owner, ok := createSubnetTx.Owner.(*secp256k1fx.OutputOwners)
if !ok {
return nil, errUnknownOwnerType
}
subnetOwner[txID] = owner
}
// then check TransferSubnetOwnershipTx
for txID, tx := range pChainTxs {
transferSubnetOwnershipTx, ok := tx.Unsigned.(*txs.TransferSubnetOwnershipTx)
if !ok {
continue
}
owner, ok := transferSubnetOwnershipTx.Owner.(*secp256k1fx.OutputOwners)
if !ok {
return nil, errUnknownOwnerType
}
subnetOwner[txID] = owner
}
return subnetOwner, nil
}
2 changes: 1 addition & 1 deletion wallet/chain/p/backend_visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (b *backendVisitor) TransferSubnetOwnershipTx(tx *txs.TransferSubnetOwnersh
if !ok {
return errUnknownOwnerType
}
b.b.setSubnetOwnerTransfer(
b.b.setSubnetOwner(
b.ctx,
tx.Subnet,
owner,
Expand Down
17 changes: 2 additions & 15 deletions wallet/chain/p/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ type BuilderBackend interface {
Context
UTXOs(ctx stdcontext.Context, sourceChainID ids.ID) ([]*avax.UTXO, error)
GetTx(ctx stdcontext.Context, txID ids.ID) (*txs.Tx, error)
GetSubnetOwnerTransfer(ctx stdcontext.Context, subnetID ids.ID) *secp256k1fx.OutputOwners
GetSubnetOwner(ctx stdcontext.Context, subnetID ids.ID) (*secp256k1fx.OutputOwners, error)
}

type builder struct {
Expand Down Expand Up @@ -1147,27 +1147,14 @@ func (b *builder) spend(
}

func (b *builder) authorizeSubnet(subnetID ids.ID, options *common.Options) (*secp256k1fx.Input, error) {
subnetTx, err := b.backend.GetTx(options.Context(), subnetID)
owner, err := b.backend.GetSubnetOwner(options.Context(), subnetID)
if err != nil {
return nil, fmt.Errorf(
"failed to fetch subnet %q: %w",
subnetID,
err,
)
}
subnet, ok := subnetTx.Unsigned.(*txs.CreateSubnetTx)
if !ok {
return nil, errWrongTxType
}

owner, ok := subnet.Owner.(*secp256k1fx.OutputOwners)
if !ok {
return nil, errUnknownOwnerType
}
ownerTransfer := b.backend.GetSubnetOwnerTransfer(options.Context(), subnetID)
if ownerTransfer != nil {
owner = ownerTransfer
}

addrs := options.Addresses(b.addrs)
minIssuanceTime := options.MinIssuanceTime()
Expand Down
2 changes: 1 addition & 1 deletion wallet/chain/p/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Signer interface {
type SignerBackend interface {
GetUTXO(ctx stdcontext.Context, chainID, utxoID ids.ID) (*avax.UTXO, error)
GetTx(ctx stdcontext.Context, txID ids.ID) (*txs.Tx, error)
GetSubnetOwnerTransfer(ctx stdcontext.Context, subnetID ids.ID) *secp256k1fx.OutputOwners
GetSubnetOwner(ctx stdcontext.Context, subnetID ids.ID) (*secp256k1fx.OutputOwners, error)
}

type txSigner struct {
Expand Down
15 changes: 1 addition & 14 deletions wallet/chain/p/signer_visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,27 +246,14 @@ func (s *signerVisitor) getSubnetSigners(subnetID ids.ID, subnetAuth verify.Veri
return nil, errUnknownSubnetAuthType
}

subnetTx, err := s.backend.GetTx(s.ctx, subnetID)
owner, err := s.backend.GetSubnetOwner(s.ctx, subnetID)
if err != nil {
return nil, fmt.Errorf(
"failed to fetch subnet %q: %w",
subnetID,
err,
)
}
subnet, ok := subnetTx.Unsigned.(*txs.CreateSubnetTx)
if !ok {
return nil, errWrongTxType
}

owner, ok := subnet.Owner.(*secp256k1fx.OutputOwners)
if !ok {
return nil, errUnknownOwnerType
}
ownerTransfer := s.backend.GetSubnetOwnerTransfer(s.ctx, subnetID)
if ownerTransfer != nil {
owner = ownerTransfer
}

authSigners := make([]keychain.Signer, len(subnetInput.SigIndices))
for sigIndex, addrIndex := range subnetInput.SigIndices {
Expand Down

0 comments on commit 5218d46

Please sign in to comment.