Skip to content

Commit

Permalink
Unify P-chain UTXO handler interface (ava-labs#2570)
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 authored Feb 17, 2023
1 parent 31336c0 commit d2ba3bc
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 33 deletions.
2 changes: 1 addition & 1 deletion vms/platformvm/blocks/builder/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func newEnvironment(t *testing.T) *environment {

res.atomicUTXOs = avax.NewAtomicUTXOManager(res.ctx.SharedMemory, txs.Codec)
res.uptimes = uptime.NewManager(res.state)
res.utxosHandler = utxo.NewHandler(res.ctx, res.clk, res.state, res.fx)
res.utxosHandler = utxo.NewHandler(res.ctx, res.clk, res.fx)

res.txBuilder = txbuilder.New(
res.ctx,
Expand Down
4 changes: 2 additions & 2 deletions vms/platformvm/blocks/executor/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func newEnvironment(t *testing.T, ctrl *gomock.Controller) *environment {
if ctrl == nil {
res.state = defaultState(res.config, res.ctx, res.baseDB, rewardsCalc)
res.uptimes = uptime.NewManager(res.state)
res.utxosHandler = utxo.NewHandler(res.ctx, res.clk, res.state, res.fx)
res.utxosHandler = utxo.NewHandler(res.ctx, res.clk, res.fx)
res.txBuilder = p_tx_builder.New(
res.ctx,
res.config,
Expand All @@ -163,7 +163,7 @@ func newEnvironment(t *testing.T, ctrl *gomock.Controller) *environment {
genesisBlkID = ids.GenerateTestID()
res.mockedState = state.NewMockState(ctrl)
res.uptimes = uptime.NewManager(res.mockedState)
res.utxosHandler = utxo.NewHandler(res.ctx, res.clk, res.mockedState, res.fx)
res.utxosHandler = utxo.NewHandler(res.ctx, res.clk, res.fx)
res.txBuilder = p_tx_builder.New(
res.ctx,
res.config,
Expand Down
20 changes: 10 additions & 10 deletions vms/platformvm/txs/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func New(
cfg *config.Config,
clk *mockable.Clock,
fx fx.Fx,
state state.Chain,
state state.State,
atomicUTXOManager avax.AtomicUTXOManager,
utxoSpender utxo.Spender,
) Builder {
Expand All @@ -192,7 +192,7 @@ func New(
type builder struct {
avax.AtomicUTXOManager
utxo.Spender
state state.Chain
state state.State

cfg *config.Config
ctx *snow.Context
Expand Down Expand Up @@ -252,7 +252,7 @@ func (b *builder) NewImportTx(
switch {
case importedAVAX < b.cfg.TxFee: // imported amount goes toward paying tx fee
var baseSigners [][]*secp256k1.PrivateKey
ins, outs, _, baseSigners, err = b.Spend(keys, 0, b.cfg.TxFee-importedAVAX, changeAddr)
ins, outs, _, baseSigners, err = b.Spend(b.state, keys, 0, b.cfg.TxFee-importedAVAX, changeAddr)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
}
Expand Down Expand Up @@ -310,7 +310,7 @@ func (b *builder) NewExportTx(
if err != nil {
return nil, fmt.Errorf("amount (%d) + tx fee(%d) overflows", amount, b.cfg.TxFee)
}
ins, outs, _, signers, err := b.Spend(keys, 0, toBurn, changeAddr)
ins, outs, _, signers, err := b.Spend(b.state, keys, 0, toBurn, changeAddr)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
}
Expand Down Expand Up @@ -354,7 +354,7 @@ func (b *builder) NewCreateChainTx(
) (*txs.Tx, error) {
timestamp := b.state.GetTimestamp()
createBlockchainTxFee := b.cfg.GetCreateBlockchainTxFee(timestamp)
ins, outs, _, signers, err := b.Spend(keys, 0, createBlockchainTxFee, changeAddr)
ins, outs, _, signers, err := b.Spend(b.state, keys, 0, createBlockchainTxFee, changeAddr)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
}
Expand Down Expand Up @@ -398,7 +398,7 @@ func (b *builder) NewCreateSubnetTx(
) (*txs.Tx, error) {
timestamp := b.state.GetTimestamp()
createSubnetTxFee := b.cfg.GetCreateSubnetTxFee(timestamp)
ins, outs, _, signers, err := b.Spend(keys, 0, createSubnetTxFee, changeAddr)
ins, outs, _, signers, err := b.Spend(b.state, keys, 0, createSubnetTxFee, changeAddr)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
}
Expand Down Expand Up @@ -436,7 +436,7 @@ func (b *builder) NewAddValidatorTx(
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error) {
ins, unstakedOuts, stakedOuts, signers, err := b.Spend(keys, stakeAmount, b.cfg.AddPrimaryNetworkValidatorFee, changeAddr)
ins, unstakedOuts, stakedOuts, signers, err := b.Spend(b.state, keys, stakeAmount, b.cfg.AddPrimaryNetworkValidatorFee, changeAddr)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
}
Expand Down Expand Up @@ -478,7 +478,7 @@ func (b *builder) NewAddDelegatorTx(
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error) {
ins, unlockedOuts, lockedOuts, signers, err := b.Spend(keys, stakeAmount, b.cfg.AddPrimaryNetworkDelegatorFee, changeAddr)
ins, unlockedOuts, lockedOuts, signers, err := b.Spend(b.state, keys, stakeAmount, b.cfg.AddPrimaryNetworkDelegatorFee, changeAddr)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
}
Expand Down Expand Up @@ -519,7 +519,7 @@ func (b *builder) NewAddSubnetValidatorTx(
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error) {
ins, outs, _, signers, err := b.Spend(keys, 0, b.cfg.TxFee, changeAddr)
ins, outs, _, signers, err := b.Spend(b.state, keys, 0, b.cfg.TxFee, changeAddr)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
}
Expand Down Expand Up @@ -562,7 +562,7 @@ func (b *builder) NewRemoveSubnetValidatorTx(
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error) {
ins, outs, _, signers, err := b.Spend(keys, 0, b.cfg.TxFee, changeAddr)
ins, outs, _, signers, err := b.Spend(b.state, keys, 0, b.cfg.TxFee, changeAddr)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/txs/executor/create_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func TestCreateChainTxAP3FeeChange(t *testing.T) {
defer func() {
require.NoError(shutdownEnvironment(env))
}()
ins, outs, _, signers, err := env.utxosHandler.Spend(preFundedKeys, 0, test.fee, ids.ShortEmpty)
ins, outs, _, signers, err := env.utxosHandler.Spend(env.state, preFundedKeys, 0, test.fee, ids.ShortEmpty)
require.NoError(err)

subnetAuth, subnetSigners, err := env.utxosHandler.Authorize(env.state, testSubnet1.ID(), preFundedKeys)
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/txs/executor/create_subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestCreateSubnetTxAP3FeeChange(t *testing.T) {
require.NoError(shutdownEnvironment(env))
}()

ins, outs, _, signers, err := env.utxosHandler.Spend(preFundedKeys, 0, test.fee, ids.ShortEmpty)
ins, outs, _, signers, err := env.utxosHandler.Spend(env.state, preFundedKeys, 0, test.fee, ids.ShortEmpty)
require.NoError(err)

// Create the tx
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/txs/executor/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func newEnvironment(postBanff bool) *environment {

atomicUTXOs := avax.NewAtomicUTXOManager(ctx.SharedMemory, txs.Codec)
uptimes := uptime.NewManager(baseState)
utxoHandler := utxo.NewHandler(ctx, &clk, baseState, fx)
utxoHandler := utxo.NewHandler(ctx, &clk, fx)

txBuilder := builder.New(
ctx,
Expand Down
19 changes: 9 additions & 10 deletions vms/platformvm/utxo/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type Spender interface {
// the staking period
// - [signers] the proof of ownership of the funds being moved
Spend(
utxoReader avax.UTXOReader,
keys []*secp256k1.PrivateKey,
amount uint64,
fee uint64,
Expand Down Expand Up @@ -149,25 +150,23 @@ type Handler interface {
func NewHandler(
ctx *snow.Context,
clk *mockable.Clock,
utxoReader avax.UTXOReader,
fx fx.Fx,
) Handler {
return &handler{
ctx: ctx,
clk: clk,
utxosReader: utxoReader,
fx: fx,
ctx: ctx,
clk: clk,
fx: fx,
}
}

type handler struct {
ctx *snow.Context
clk *mockable.Clock
utxosReader avax.UTXOReader
fx fx.Fx
ctx *snow.Context
clk *mockable.Clock
fx fx.Fx
}

func (h *handler) Spend(
utxoReader avax.UTXOReader,
keys []*secp256k1.PrivateKey,
amount uint64,
fee uint64,
Expand All @@ -183,7 +182,7 @@ func (h *handler) Spend(
for _, key := range keys {
addrs.Add(key.PublicKey().Address())
}
utxos, err := avax.GetAllUTXOs(h.utxosReader, addrs) // The UTXOs controlled by [keys]
utxos, err := avax.GetAllUTXOs(utxoReader, addrs) // The UTXOs controlled by [keys]
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("couldn't get UTXOs: %w", err)
}
Expand Down
7 changes: 1 addition & 6 deletions vms/platformvm/utxo/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/database/memdb"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
Expand Down Expand Up @@ -41,11 +40,7 @@ func TestVerifySpendUTXOs(t *testing.T) {
h := &handler{
ctx: snow.DefaultContextTest(),
clk: &mockable.Clock{},
utxosReader: avax.NewUTXOState(
memdb.New(),
txs.Codec,
),
fx: fx,
fx: fx,
}

// The handler time during a test, unless [chainTimestamp] is set
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (vm *VM) Initialize(
}

vm.atomicUtxosManager = avax.NewAtomicUTXOManager(chainCtx.SharedMemory, txs.Codec)
utxoHandler := utxo.NewHandler(vm.ctx, &vm.clock, vm.state, vm.fx)
utxoHandler := utxo.NewHandler(vm.ctx, &vm.clock, vm.fx)
vm.uptimeManager = uptime.NewManager(vm.state)
vm.UptimeLockedCalculator.SetCalculator(&vm.bootstrapped, &chainCtx.Lock, vm.uptimeManager)

Expand Down

0 comments on commit d2ba3bc

Please sign in to comment.