mirror of
https://github.com/alexgo-io/stacks-puppet-node.git
synced 2026-04-30 12:42:10 +08:00
test: add unit tests for set-signer-key-authorization
This commit is contained in:
@@ -74,3 +74,14 @@
|
||||
(extend-count uint))
|
||||
(contract-call? 'ST000000000000000000002AMW42H.pox-4 delegate-stack-extend stacker pox-addr extend-count)
|
||||
)
|
||||
|
||||
(define-public (set-signer-key-authorization (pox-addr { version: (buff 1), hashbytes: (buff 32)})
|
||||
(period uint)
|
||||
(reward-cycle uint)
|
||||
(topic (string-ascii 14))
|
||||
(signer-key (buff 33))
|
||||
(allowed bool)
|
||||
(max-amount uint)
|
||||
(auth-id uint))
|
||||
(contract-call? 'ST000000000000000000002AMW42H.pox-4 set-signer-key-authorization pox-addr period reward-cycle topic signer-key allowed max-amount auth-id)
|
||||
)
|
||||
|
||||
@@ -372,6 +372,33 @@ export const stackAggregationIncrease = (
|
||||
);
|
||||
};
|
||||
|
||||
export const setSignerKeyAuthorization = (
|
||||
stacker: StackerInfo,
|
||||
period: bigint | number,
|
||||
rewardCycle: bigint | number,
|
||||
topic: Pox4SignatureTopic,
|
||||
allowed: boolean,
|
||||
maxAmount: bigint | number,
|
||||
authId: bigint | number,
|
||||
) => {
|
||||
const args = [
|
||||
poxAddressToTuple(stacker.btcAddr),
|
||||
Cl.uint(period),
|
||||
Cl.uint(rewardCycle),
|
||||
Cl.stringAscii(topic),
|
||||
Cl.bufferFromHex(stacker.signerPubKey),
|
||||
Cl.bool(allowed),
|
||||
Cl.uint(maxAmount),
|
||||
Cl.uint(authId),
|
||||
];
|
||||
return simnet.callPublicFn(
|
||||
POX_CONTRACT,
|
||||
"set-signer-key-authorization",
|
||||
args,
|
||||
stacker.stxAddress
|
||||
);
|
||||
};
|
||||
|
||||
// Validate a pox-4 event and return the value of the event.
|
||||
export const checkPox4Event = (event: ClarityEvent): TupleCV => {
|
||||
expect(event.event).toEqual("print_event");
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
StackerInfo,
|
||||
allowContractCaller,
|
||||
getStackingMinimum,
|
||||
setSignerKeyAuthorization,
|
||||
stackStx,
|
||||
stackers,
|
||||
} from "./helpers";
|
||||
@@ -14,6 +15,7 @@ import {
|
||||
const accounts = simnet.getAccounts();
|
||||
const deployer = accounts.get("deployer")!;
|
||||
const address1 = accounts.get("wallet_1")!;
|
||||
const address3 = accounts.get("wallet_3")!;
|
||||
|
||||
beforeEach(() => {
|
||||
simnet.setEpoch("3.0");
|
||||
@@ -1424,3 +1426,179 @@ describe("test `consume-signer-key-authorization`", () => {
|
||||
expect(response.result).toBeErr(Cl.int(ERRORS.ERR_SIGNER_AUTH_USED));
|
||||
});
|
||||
});
|
||||
|
||||
describe("test `set-signer-key-authorization`", () => {
|
||||
it("returns `(ok true)` for a valid authorization", () => {
|
||||
const stacker = stackers[0];
|
||||
const period = 1;
|
||||
const rewardCycle = 1;
|
||||
const topic = Pox4SignatureTopic.AggregateCommit;
|
||||
const allowed = true;
|
||||
const maxAmount = getStackingMinimum() * 2n;
|
||||
const authId = 1;
|
||||
|
||||
const response = setSignerKeyAuthorization(
|
||||
stacker,
|
||||
period,
|
||||
rewardCycle,
|
||||
topic,
|
||||
allowed,
|
||||
maxAmount,
|
||||
authId
|
||||
);
|
||||
expect(response.result).toBeOk(Cl.bool(true));
|
||||
});
|
||||
|
||||
it("returns `(ok false)` for a valid deauthorization", () => {
|
||||
const stacker = stackers[0];
|
||||
const period = 1;
|
||||
const rewardCycle = 1;
|
||||
const topic = Pox4SignatureTopic.AggregateCommit;
|
||||
const allowed = false;
|
||||
const maxAmount = getStackingMinimum() * 2n;
|
||||
const authId = 1;
|
||||
|
||||
const response = setSignerKeyAuthorization(
|
||||
stacker,
|
||||
period,
|
||||
rewardCycle,
|
||||
topic,
|
||||
allowed,
|
||||
maxAmount,
|
||||
authId
|
||||
);
|
||||
expect(response.result).toBeOk(Cl.bool(false));
|
||||
});
|
||||
|
||||
it("cannot be called indirectly by an unauthorized caller", () => {
|
||||
const stacker = stackers[0];
|
||||
const period = 1;
|
||||
const rewardCycle = 1;
|
||||
const topic = Pox4SignatureTopic.AggregateCommit;
|
||||
const allowed = false;
|
||||
const maxAmount = getStackingMinimum() * 2n;
|
||||
const authId = 1;
|
||||
|
||||
const args = [
|
||||
poxAddressToTuple(stacker.btcAddr),
|
||||
Cl.uint(period),
|
||||
Cl.uint(rewardCycle),
|
||||
Cl.stringAscii(topic),
|
||||
Cl.bufferFromHex(stacker.signerPubKey),
|
||||
Cl.bool(allowed),
|
||||
Cl.uint(maxAmount),
|
||||
Cl.uint(authId),
|
||||
];
|
||||
const response = simnet.callPublicFn(
|
||||
"indirect",
|
||||
"set-signer-key-authorization",
|
||||
args,
|
||||
stacker.stxAddress
|
||||
);
|
||||
expect(response.result).toBeErr(Cl.int(ERRORS.ERR_NOT_ALLOWED));
|
||||
});
|
||||
|
||||
it("can be called indirectly by an authorized caller", () => {
|
||||
const stacker = stackers[0];
|
||||
const period = 1;
|
||||
const rewardCycle = 1;
|
||||
const topic = Pox4SignatureTopic.AggregateCommit;
|
||||
const allowed = true;
|
||||
const maxAmount = getStackingMinimum() * 2n;
|
||||
const authId = 1;
|
||||
|
||||
allowContractCaller(`${deployer}.indirect`, null, stacker.stxAddress);
|
||||
|
||||
const args = [
|
||||
poxAddressToTuple(stacker.btcAddr),
|
||||
Cl.uint(period),
|
||||
Cl.uint(rewardCycle),
|
||||
Cl.stringAscii(topic),
|
||||
Cl.bufferFromHex(stacker.signerPubKey),
|
||||
Cl.bool(allowed),
|
||||
Cl.uint(maxAmount),
|
||||
Cl.uint(authId),
|
||||
];
|
||||
const response = simnet.callPublicFn(
|
||||
"indirect",
|
||||
"set-signer-key-authorization",
|
||||
args,
|
||||
stacker.stxAddress
|
||||
);
|
||||
expect(response.result).toBeOk(Cl.bool(true));
|
||||
});
|
||||
|
||||
it("cannot be called by a different principal", () => {
|
||||
const stacker = stackers[0];
|
||||
const period = 1;
|
||||
const rewardCycle = 1;
|
||||
const topic = Pox4SignatureTopic.AggregateCommit;
|
||||
const allowed = true;
|
||||
const maxAmount = getStackingMinimum() * 2n;
|
||||
const authId = 1;
|
||||
|
||||
const args = [
|
||||
poxAddressToTuple(stacker.btcAddr),
|
||||
Cl.uint(period),
|
||||
Cl.uint(rewardCycle),
|
||||
Cl.stringAscii(topic),
|
||||
Cl.bufferFromHex(stacker.signerPubKey),
|
||||
Cl.bool(allowed),
|
||||
Cl.uint(maxAmount),
|
||||
Cl.uint(authId),
|
||||
];
|
||||
const response = simnet.callPublicFn(
|
||||
POX_CONTRACT,
|
||||
"set-signer-key-authorization",
|
||||
args,
|
||||
address3
|
||||
);
|
||||
expect(response.result).toBeErr(Cl.int(ERRORS.ERR_NOT_ALLOWED));
|
||||
});
|
||||
|
||||
it("returns an error for a period of 0", () => {
|
||||
const stacker = stackers[0];
|
||||
const period = 0;
|
||||
const rewardCycle = 1;
|
||||
const topic = Pox4SignatureTopic.AggregateCommit;
|
||||
const allowed = true;
|
||||
const maxAmount = getStackingMinimum() * 2n;
|
||||
const authId = 1;
|
||||
|
||||
const response = setSignerKeyAuthorization(
|
||||
stacker,
|
||||
period,
|
||||
rewardCycle,
|
||||
topic,
|
||||
allowed,
|
||||
maxAmount,
|
||||
authId
|
||||
);
|
||||
expect(response.result).toBeErr(
|
||||
Cl.int(ERRORS.ERR_STACKING_INVALID_LOCK_PERIOD)
|
||||
);
|
||||
});
|
||||
|
||||
it("returns an error for a reward cycle in the past", () => {
|
||||
const stacker = stackers[0];
|
||||
const period = 1;
|
||||
const rewardCycle = 1;
|
||||
const topic = Pox4SignatureTopic.AggregateCommit;
|
||||
const allowed = true;
|
||||
const maxAmount = getStackingMinimum() * 2n;
|
||||
const authId = 1;
|
||||
|
||||
simnet.mineEmptyBlocks(1050 * 2);
|
||||
|
||||
const response = setSignerKeyAuthorization(
|
||||
stacker,
|
||||
period,
|
||||
rewardCycle,
|
||||
topic,
|
||||
allowed,
|
||||
maxAmount,
|
||||
authId
|
||||
);
|
||||
expect(response.result).toBeErr(Cl.int(ERRORS.ERR_INVALID_REWARD_CYCLE));
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user