Skip to content

Commit

Permalink
Revert "Add iteration of minters"
Browse files Browse the repository at this point in the history
This reverts commit 73964de.
  • Loading branch information
eth-r committed Aug 1, 2023
1 parent 5c00165 commit 7a6df17
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 120 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
error::TbtcError,
state::{Config, MinterIndex, MinterInfo},
state::{Config, MinterInfo},
};
use anchor_lang::prelude::*;

Expand All @@ -26,15 +26,6 @@ pub struct AddMinter<'info> {
)]
minter_info: Account<'info, MinterInfo>,

#[account(
init,
payer = authority,
space = 8 + MinterIndex::INIT_SPACE,
seeds = [MinterIndex::SEED_PREFIX, &[config.num_minters]],
bump
)]
minter_index: Account<'info, MinterIndex>,

/// CHECK: Required authority to mint tokens. This pubkey lives in `MinterInfo`.
minter: AccountInfo<'info>,

Expand All @@ -44,15 +35,9 @@ pub struct AddMinter<'info> {
pub fn add_minter(ctx: Context<AddMinter>) -> Result<()> {
ctx.accounts.minter_info.set_inner(MinterInfo {
minter: ctx.accounts.minter.key(),
index: ctx.accounts.config.num_minters,
bump: ctx.bumps["minter_info"],
});

ctx.accounts.minter_index.set_inner(MinterIndex {
minter_info: ctx.accounts.minter_info.key(),
bump: ctx.bumps["minter_index"],
});

ctx.accounts.config.num_minters += 1;
Ok(())
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
error::TbtcError,
state::{Config, MinterIndex, MinterInfo},
state::{Config, MinterInfo},
};
use anchor_lang::prelude::*;

Expand All @@ -25,36 +25,6 @@ pub struct RemoveMinter<'info> {
)]
minter_info: Account<'info, MinterInfo>,

// the minter info at the last index.
// This gets its index swapped to the position of the removed minter info.
#[account(
mut,
constraint = minter_info_swap.index == config.num_minters - 1,
seeds = [MinterInfo::SEED_PREFIX, minter_info_swap.minter.as_ref()],
bump = minter_info_swap.bump,
)]
minter_info_swap: Account<'info, MinterInfo>,

// The index account of the minter to remove.
// We replace minter_info in this with minter_info_swap.
#[account(
mut,
seeds = [MinterIndex::SEED_PREFIX, &[minter_info.index]],
bump = minter_index_swap.bump,
)]
minter_index_swap: Account<'info, MinterIndex>,

// The last minter index account.
// This gets removed, and its minter_info(_swap) gets put into minter_index_swap instead.
#[account(
mut,
close = authority,
seeds = [MinterIndex::SEED_PREFIX, &[config.num_minters - 1]],
bump = minter_index_tail.bump,
constraint = minter_index_tail.minter_info == minter_info_swap.key(),
)]
minter_index_tail: Account<'info, MinterIndex>,

/// CHECK: Required authority to mint tokens. This pubkey lives in `MinterInfo`.
minter: AccountInfo<'info>,
}
Expand Down
12 changes: 0 additions & 12 deletions cross-chain/solana/programs/tbtc/src/state/minter_index.rs

This file was deleted.

1 change: 0 additions & 1 deletion cross-chain/solana/programs/tbtc/src/state/minter_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use anchor_lang::prelude::*;
#[derive(Debug, InitSpace)]
pub struct MinterInfo {
pub minter: Pubkey,
pub index: u8,
pub bump: u8,
}

Expand Down
3 changes: 0 additions & 3 deletions cross-chain/solana/programs/tbtc/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,5 @@ pub use guardian_index::*;
mod guardian_info;
pub use guardian_info::*;

mod minter_index;
pub use minter_index::*;

mod minter_info;
pub use minter_info::*;
76 changes: 19 additions & 57 deletions cross-chain/solana/tests/01__tbtc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,41 +160,21 @@ function getMinterPDA(
);
}

function getMinterIndexPDA(
program: Program<Tbtc>,
index
): [anchor.web3.PublicKey, number] {
let indexArr = new Uint8Array(1);
indexArr[0] = index;
return web3.PublicKey.findProgramAddressSync(
[
Buffer.from('minter-index'),
indexArr,
],
program.programId
);
}

async function addMinter(
program: Program<Tbtc>,
authority,
minter
minter,
payer
): Promise<anchor.web3.PublicKey> {
const [config,] = getConfigPDA(program);
const [minterInfoPDA, _] = getMinterPDA(program, minter);

let configState = await program.account.config.fetch(config);

const [minterIndexPDA, __] = getMinterIndexPDA(program, configState.numMinters);

await program.methods
.addMinter()
.accounts({
config,
authority: authority.publicKey,
minterInfo: minterInfoPDA,
minterIndex: minterIndexPDA,
minter: minter.publicKey,
minterInfo: minterInfoPDA,
})
.signers(maybeAuthorityAnd(authority, []))
.rpc();
Expand All @@ -203,22 +183,13 @@ async function addMinter(

async function checkMinter(
program: Program<Tbtc>,
minter,
expectedIndex
minter
) {
const [minterInfoPDA, bump] = getMinterPDA(program, minter);
let minterInfo = await program.account.minterInfo.fetch(minterInfoPDA);

const [minterIndexPDA, indexBump] = getMinterIndexPDA(program, minterInfo.index);
let minterIndex = await program.account.minterIndex.fetch(minterIndexPDA);

expect(minterInfo.minter).to.eql(minter.publicKey);
expect(minterInfo.bump).to.equal(bump);

expect(minterIndex.minterInfo).to.eql(minterInfoPDA);
expect(minterIndex.bump).to.equal(indexBump);

expect(minterInfo.index).to.equal(expectedIndex);
}

async function removeMinter(
Expand All @@ -228,24 +199,12 @@ async function removeMinter(
minterInfo
) {
const [config,] = getConfigPDA(program);
const configState = await program.account.config.fetch(config);
const minterInfoState = await program.account.minterInfo.fetch(minterInfo);

const [lastIndex,] = getMinterIndexPDA(program, configState.numMinters - 1);
const [swapIndex,] = getMinterIndexPDA(program, minterInfoState.index);

const lastIndexState = await program.account.minterIndex.fetch(lastIndex);
const swapInfo = lastIndexState.minterInfo;

await program.methods
.removeMinter()
.accounts({
config,
authority: authority.publicKey,
minterInfo: minterInfo,
minterInfoSwap: swapInfo,
minterIndexSwap: swapIndex,
minterIndexTail: lastIndex,
minter: minter.publicKey
})
.signers(maybeAuthorityAnd(authority, []))
Expand Down Expand Up @@ -522,8 +481,8 @@ describe("tbtc", () => {

it('add minter', async () => {
await checkState(program, authority, 0, 0, 0);
await addMinter(program, authority, minterKeys);
await checkMinter(program, minterKeys, 0);
await addMinter(program, authority, minterKeys, authority);
await checkMinter(program, minterKeys);
await checkState(program, authority, 1, 0, 0);

// Transfer lamports to imposter.
Expand All @@ -540,7 +499,7 @@ describe("tbtc", () => {
);

try {
await addMinter(program, impostorKeys, minter2Keys);
await addMinter(program, impostorKeys, minter2Keys, authority);
chai.assert(false, "should've failed but didn't");
} catch (_err) {
expect(_err).to.be.instanceOf(AnchorError);
Expand All @@ -553,7 +512,7 @@ describe("tbtc", () => {
it('mint', async () => {
await checkState(program, authority, 1, 0, 0);
const [minterInfoPDA, _] = getMinterPDA(program, minterKeys);
await checkMinter(program, minterKeys, 0);
await checkMinter(program, minterKeys);

// await setupMint(program, authority, recipientKeys);
await mint(program, minterKeys, minterInfoPDA, recipientKeys, 1000, authority);
Expand All @@ -574,7 +533,7 @@ describe("tbtc", () => {
it('won\'t mint', async () => {
await checkState(program, authority, 1, 0, 1000);
const [minterInfoPDA, _] = getMinterPDA(program, minterKeys);
await checkMinter(program, minterKeys, 0);
await checkMinter(program, minterKeys);

// await setupMint(program, authority, recipientKeys);

Expand All @@ -592,9 +551,9 @@ describe("tbtc", () => {
it('use two minters', async () => {
await checkState(program, authority, 1, 0, 1000);
const [minterInfoPDA, _] = getMinterPDA(program, minterKeys);
await checkMinter(program, minterKeys, 0);
const minter2InfoPDA = await addMinter(program, authority, minter2Keys);
await checkMinter(program, minter2Keys, 1);
await checkMinter(program, minterKeys);
const minter2InfoPDA = await addMinter(program, authority, minter2Keys, authority);
await checkMinter(program, minter2Keys);
await checkState(program, authority, 2, 0, 1000);
// await setupMint(program, authority, recipientKeys);

Expand Down Expand Up @@ -627,15 +586,15 @@ describe("tbtc", () => {
it('remove minter', async () => {
await checkState(program, authority, 2, 0, 1500);
const [minter2InfoPDA, _] = getMinterPDA(program, minter2Keys);
await checkMinter(program, minter2Keys, 1);
await checkMinter(program, minter2Keys);
await removeMinter(program, authority, minter2Keys, minter2InfoPDA);
await checkState(program, authority, 1, 0, 1500);
});

it('won\'t remove minter', async () => {
await checkState(program, authority, 1, 0, 1500);
const [minterInfoPDA, _] = getMinterPDA(program, minterKeys);
await checkMinter(program, minterKeys, 0);
await checkMinter(program, minterKeys);

try {
await removeMinter(program, impostorKeys, minterKeys, minterInfoPDA);
Expand All @@ -654,7 +613,10 @@ describe("tbtc", () => {
await removeMinter(program, authority, minterKeys, minterInfoPDA);
chai.assert(false, "should've failed but didn't");
} catch (_err) {
expect(_err.message).to.include('Account does not exist or has no data');
expect(_err).to.be.instanceOf(AnchorError);
const err: AnchorError = _err;
expect(err.error.errorCode.code).to.equal('AccountNotInitialized');
expect(err.program.equals(program.programId)).is.true;
}
});

Expand Down Expand Up @@ -741,7 +703,7 @@ describe("tbtc", () => {

it('won\'t mint when paused', async () => {
await checkState(program, authority, 0, 1, 1500);
const minterInfoPDA = await addMinter(program, authority, minterKeys);
const minterInfoPDA = await addMinter(program, authority, minterKeys, authority);
await pause(program, guardianKeys);
// await setupMint(program, authority, recipientKeys);

Expand Down

0 comments on commit 7a6df17

Please sign in to comment.