Skip to content

Commit d80b456

Browse files
authored
feat: add DisableSubnetValidatorTx and SetSubnetValidatorWeightTx (#890)
* feat: add DisableSubnetValidatorTx * feat: add SetSubnetValidatorWeightTx * fix: add missing typeguard usage
1 parent 683b56f commit d80b456

17 files changed

+578
-45
lines changed

src/fixtures/primitives.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,20 @@ export const blsSignatureBytes = () =>
6060
0x4d, 0xdf, 0xad, 0xd1, 0xc1, 0x74, 0x3f, 0x7f, 0x54, 0x9f, 0x1d, 0x07,
6161
0xd5, 0x9d, 0x55, 0x65, 0x59, 0x27, 0xf7, 0x2b, 0xc6, 0xbf, 0x7c, 0x12,
6262
]);
63+
64+
export const warpMessageBytes = () =>
65+
new Uint8Array([
66+
0, 0, 0, 215, 0, 0, 0, 0, 48, 57, 112, 95, 61, 68, 21, 249, 144, 34, 93, 61,
67+
245, 206, 67, 125, 122, 242, 170, 50, 75, 27, 188, 232, 84, 238, 52, 171,
68+
111, 57, 136, 34, 80, 210, 0, 0, 0, 68, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
69+
0, 54, 0, 0, 0, 0, 0, 3, 56, 230, 233, 254, 49, 198, 208, 112, 168, 199,
70+
146, 219, 172, 246, 208, 174, 251, 142, 172, 42, 222, 212, 156, 192, 170,
71+
159, 66, 45, 31, 221, 158, 205, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
72+
5, 0, 0, 0, 0, 0, 0, 0, 1, 1, 135, 244, 187, 44, 66, 134, 156, 86, 240, 35,
73+
161, 202, 129, 4, 90, 255, 3, 74, 205, 73, 11, 143, 21, 181, 6, 144, 37,
74+
249, 130, 230, 5, 224, 119, 0, 127, 197, 136, 247, 213, 99, 105, 166, 93,
75+
247, 87, 77, 243, 183, 15, 240, 40, 234, 23, 55, 57, 199, 137, 82, 90, 183,
76+
238, 191, 203, 92, 17, 91, 19, 204, 168, 240, 43, 54, 33, 4, 183, 0, 199,
77+
91, 201, 82, 52, 16, 159, 63, 19, 96, 221, 203, 78, 195, 202, 246, 176, 232,
78+
33, 203,
79+
]);

src/fixtures/pvm.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import {
2020
TransferSubnetOwnershipTx,
2121
TransformSubnetTx,
2222
IncreaseBalanceTx,
23+
DisableSubnetValidatorTx,
24+
SetSubnetValidatorWeightTx,
2325
} from '../serializable/pvm';
2426
import {
2527
baseTx,
@@ -306,6 +308,14 @@ export const transformSubnetTxBytes = () =>
306308
inputBytes(),
307309
);
308310

311+
export const pChainOwner = () => new PChainOwner(int(), addresses()());
312+
313+
export const pChainOwnerBytes = () =>
314+
concatBytes(
315+
intBytes(), // threshold
316+
addressesBytes(),
317+
);
318+
309319
export const convertSubnetValidator = () =>
310320
new ConvertSubnetValidator(
311321
bytes(),
@@ -347,19 +357,23 @@ export const convertSubnetTxBytes = () =>
347357
inputBytes(),
348358
);
349359

360+
export const setSubnetValidatorWeightTx = () =>
361+
new SetSubnetValidatorWeightTx(baseTx(), bytes());
362+
363+
export const setSubnetValidatorWeightTxBytes = () =>
364+
concatBytes(baseTxbytes(), bytesBytes());
365+
350366
export const increaseBalanceTx = () =>
351367
new IncreaseBalanceTx(baseTx(), id(), bigIntPr());
352368

353369
export const increaseBalanceTxBytes = () =>
354370
concatBytes(baseTxbytes(), idBytes(), bigIntPrBytes());
355371

356-
export const pChainOwner = () => new PChainOwner(int(), addresses()());
372+
export const disableSubnetValidatorTx = () =>
373+
new DisableSubnetValidatorTx(baseTx(), id(), input());
357374

358-
export const pChainOwnerBytes = () =>
359-
concatBytes(
360-
intBytes(), // threshold
361-
addressesBytes(),
362-
);
375+
export const disableSubnetValidatorTxBytes = () =>
376+
concatBytes(baseTxbytes(), idBytes(), bytesForInt(10), inputBytes());
363377

364378
export const feeState = (): FeeState => ({
365379
capacity: 1n,

src/serializable/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ export enum TypeSymbols {
8282

8383
ConvertSubnetTx = 'pvm.ConvertSubnetTx',
8484
ConvertSubnetValidator = 'pvm.ConvertSubnetValidator',
85+
SetSubnetValidatorWeightTx = 'pvm.SetSubnetValidatorWeightTx',
8586
IncreaseBalanceTx = 'pvm.IncreaseBalanceTx',
87+
DisableSubnetValidatorTx = 'pvm.DisableSubnetValidatorTx',
8688

8789
PChainOwner = 'pvm.PChainOwner',
8890

src/serializable/pvm/codec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import { TransformSubnetTx } from './transformSubnetTx';
2020
import { BaseTx } from './baseTx';
2121
import { ConvertSubnetTx } from './convertSubnetTx';
2222
import { IncreaseBalanceTx } from './increaseBalanceTx';
23+
import { DisableSubnetValidatorTx } from './disableSubnetValidatorTx';
24+
import { SetSubnetValidatorWeightTx } from './setSubnetValidatorWeightTx';
2325

2426
/**
2527
* @see https://github.com/ava-labs/avalanchego/blob/master/vms/platformvm/txs/codec.go#L35
@@ -62,9 +64,10 @@ export const codec = new Codec([
6264

6365
ConvertSubnetTx, // 35
6466
// Replace these with the actual txs when they are implemented
65-
...new Array(2), // 36-37 RegisterSubnetValidatorTx, SetSubnetValidatorWeightTx
67+
...new Array(1), // 36 RegisterSubnetValidatorTx
68+
SetSubnetValidatorWeightTx, // 37
6669
IncreaseBalanceTx, // 38
67-
// DisableSubnetValidatorTx, // 39
70+
DisableSubnetValidatorTx, // 39
6871
]);
6972

7073
let manager: Manager;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { testPVMCodec } from '../../fixtures/codec';
2+
import {
3+
disableSubnetValidatorTx,
4+
disableSubnetValidatorTxBytes,
5+
} from '../../fixtures/pvm';
6+
import { testSerialization } from '../../fixtures/utils/serializable';
7+
import { DisableSubnetValidatorTx } from './disableSubnetValidatorTx';
8+
9+
testSerialization(
10+
'DisableSubnetValidatorTx',
11+
DisableSubnetValidatorTx,
12+
disableSubnetValidatorTx,
13+
disableSubnetValidatorTxBytes,
14+
testPVMCodec,
15+
);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { concatBytes } from '@noble/hashes/utils';
2+
import { pack, unpack } from '../../utils/struct';
3+
import { BaseTx } from '../avax/baseTx';
4+
import { Codec } from '../codec/codec';
5+
import { serializable, type Serializable } from '../common/types';
6+
import { TypeSymbols } from '../constants';
7+
import { Id } from '../fxs/common';
8+
import { PVMTx } from './abstractTx';
9+
import type { Input } from '../fxs/secp256k1';
10+
11+
@serializable()
12+
export class DisableSubnetValidatorTx extends PVMTx {
13+
_type = TypeSymbols.DisableSubnetValidatorTx;
14+
15+
constructor(
16+
public readonly baseTx: BaseTx,
17+
public readonly validationId: Id,
18+
public readonly disableAuth: Serializable,
19+
) {
20+
super();
21+
}
22+
23+
static fromBytes(
24+
bytes: Uint8Array,
25+
codec: Codec,
26+
): [disableSubnetValidatorTx: DisableSubnetValidatorTx, rest: Uint8Array] {
27+
const [baseTx, validationId, disableAuth, rest] = unpack(
28+
bytes,
29+
[BaseTx, Id, Codec],
30+
codec,
31+
);
32+
return [
33+
new DisableSubnetValidatorTx(baseTx, validationId, disableAuth),
34+
rest,
35+
];
36+
}
37+
38+
toBytes(codec: Codec) {
39+
return concatBytes(
40+
pack([this.baseTx, this.validationId], codec),
41+
codec.PackPrefix(this.disableAuth),
42+
);
43+
}
44+
45+
getDisableAuth() {
46+
return this.disableAuth as Input;
47+
}
48+
}

src/serializable/pvm/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import { AbstractSubnetTx } from './abstractSubnetTx';
2020
import { TransferSubnetOwnershipTx } from './transferSubnetOwnershipTx';
2121
import { TransformSubnetTx } from './transformSubnetTx';
2222
import { ConvertSubnetTx } from './convertSubnetTx';
23+
import { SetSubnetValidatorWeightTx } from './setSubnetValidatorWeightTx';
2324
import { IncreaseBalanceTx } from './increaseBalanceTx';
25+
import { DisableSubnetValidatorTx } from './disableSubnetValidatorTx';
2426

2527
export * from './typeGuards';
2628
export {
@@ -47,5 +49,7 @@ export {
4749
TransferSubnetOwnershipTx,
4850
TransformSubnetTx,
4951
ConvertSubnetTx,
52+
SetSubnetValidatorWeightTx,
5053
IncreaseBalanceTx,
54+
DisableSubnetValidatorTx,
5155
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { testPVMCodec } from '../../fixtures/codec';
2+
import {
3+
setSubnetValidatorWeightTx,
4+
setSubnetValidatorWeightTxBytes,
5+
} from '../../fixtures/pvm';
6+
import { testSerialization } from '../../fixtures/utils/serializable';
7+
import { SetSubnetValidatorWeightTx } from './setSubnetValidatorWeightTx';
8+
9+
testSerialization(
10+
'SetSubnetValidatorWeightTx',
11+
SetSubnetValidatorWeightTx,
12+
setSubnetValidatorWeightTx,
13+
setSubnetValidatorWeightTxBytes,
14+
testPVMCodec,
15+
);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { pack, unpack } from '../../utils/struct';
2+
import { BaseTx } from '../avax/baseTx';
3+
import type { Codec } from '../codec';
4+
import { serializable } from '../common/types';
5+
import { TypeSymbols } from '../constants';
6+
import { Bytes } from '../primitives';
7+
import { PVMTx } from './abstractTx';
8+
9+
@serializable()
10+
export class SetSubnetValidatorWeightTx extends PVMTx {
11+
_type = TypeSymbols.SetSubnetValidatorWeightTx;
12+
13+
constructor(public readonly baseTx: BaseTx, public readonly message: Bytes) {
14+
super();
15+
}
16+
17+
static fromBytes(
18+
bytes: Uint8Array,
19+
codec: Codec,
20+
): [SetSubnetValidatorWeightTx, Uint8Array] {
21+
const [baseTx, message, rest] = unpack(bytes, [BaseTx, Bytes], codec);
22+
23+
return [new SetSubnetValidatorWeightTx(baseTx, message), rest];
24+
}
25+
26+
toBytes(codec: Codec): Uint8Array {
27+
return pack([this.baseTx, this.message], codec);
28+
}
29+
}

src/serializable/pvm/typeGuards.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import {
2020
isTransformSubnetTx,
2121
isConvertSubnetTx,
2222
isIncreaseBalanceTx,
23+
isDisableSubnetValidatorTx,
24+
isSetSubnetValidatorWeightTx,
2325
} from './typeGuards';
2426

2527
const cases: [any, TypeSymbols][] = [
@@ -41,7 +43,9 @@ const cases: [any, TypeSymbols][] = [
4143
[isTransferSubnetOwnershipTx, TypeSymbols.TransferSubnetOwnershipTx],
4244
[isTransformSubnetTx, TypeSymbols.TransformSubnetTx],
4345
[isConvertSubnetTx, TypeSymbols.ConvertSubnetTx],
46+
[isSetSubnetValidatorWeightTx, TypeSymbols.SetSubnetValidatorWeightTx],
4447
[isIncreaseBalanceTx, TypeSymbols.IncreaseBalanceTx],
48+
[isDisableSubnetValidatorTx, TypeSymbols.DisableSubnetValidatorTx],
4549
];
4650

4751
const guards = cases.map((caseItem) => caseItem[0]);

src/serializable/pvm/typeGuards.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import { TypeSymbols } from '../constants';
1818
import type { TransformSubnetTx } from './transformSubnetTx';
1919
import type { ConvertSubnetTx } from './convertSubnetTx';
2020
import type { IncreaseBalanceTx } from './increaseBalanceTx';
21+
import type { DisableSubnetValidatorTx } from './disableSubnetValidatorTx';
22+
import type { SetSubnetValidatorWeightTx } from './setSubnetValidatorWeightTx';
2123

2224
export function isPvmBaseTx(tx: Transaction): tx is BaseTx {
2325
return tx._type === TypeSymbols.PvmBaseTx;
@@ -93,10 +95,22 @@ export function isConvertSubnetTx(tx: Transaction): tx is ConvertSubnetTx {
9395
return tx._type === TypeSymbols.ConvertSubnetTx;
9496
}
9597

98+
export function isSetSubnetValidatorWeightTx(
99+
tx: Transaction,
100+
): tx is SetSubnetValidatorWeightTx {
101+
return tx._type === TypeSymbols.SetSubnetValidatorWeightTx;
102+
}
103+
96104
export function isIncreaseBalanceTx(tx: Transaction): tx is IncreaseBalanceTx {
97105
return tx._type === TypeSymbols.IncreaseBalanceTx;
98106
}
99107

108+
export function isDisableSubnetValidatorTx(
109+
tx: Transaction,
110+
): tx is DisableSubnetValidatorTx {
111+
return tx._type === TypeSymbols.DisableSubnetValidatorTx;
112+
}
113+
100114
export function isEmptySigner(
101115
signer: Signer | SignerEmpty,
102116
): signer is SignerEmpty {

src/utils/validateBurnedAmount/validateBurnedAmount.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ import {
1818
isConvertSubnetTx,
1919
isCreateChainTx,
2020
isCreateSubnetTx,
21+
isDisableSubnetValidatorTx,
2122
isIncreaseBalanceTx,
2223
isPvmBaseTx,
2324
isExportTx as isPvmExportTx,
2425
isImportTx as isPvmImportTx,
2526
isRemoveSubnetValidatorTx,
27+
isSetSubnetValidatorWeightTx,
2628
isTransferSubnetOwnershipTx,
2729
} from '../../serializable/pvm';
2830

@@ -49,7 +51,9 @@ const isEtnaSupported = (tx: Transaction) => {
4951
isRemoveSubnetValidatorTx(tx) ||
5052
isTransferSubnetOwnershipTx(tx) ||
5153
isConvertSubnetTx(tx) ||
52-
isIncreaseBalanceTx(tx)
54+
isSetSubnetValidatorWeightTx(tx) ||
55+
isIncreaseBalanceTx(tx) ||
56+
isDisableSubnetValidatorTx(tx)
5357
);
5458
};
5559

@@ -60,7 +64,7 @@ const isEtnaSupported = (tx: Transaction) => {
6064
* @param burnedAmount: burned amount in nAVAX
6165
* @param baseFee
6266
** c-chain: fetched from the network and converted into nAvax (https://docs.avax.network/quickstart/transaction-fees#c-chain-fees)
63-
** x/p-chain: pvm dynamic fee caculator, https://github.com/ava-labs/avalanchego/blob/master/vms/platformvm/txs/fee/dynamic_calculator.go
67+
** x/p-chain: pvm dynamic fee calculator, https://github.com/ava-labs/avalanchego/blob/master/vms/platformvm/txs/fee/dynamic_calculator.go
6468
* @param feeTolerance: tolerance percentage range where the burned amount is considered valid. e.g.: with FeeTolerance = 20% -> (expectedFee <= burnedAmount <= expectedFee * 1.2)
6569
* @return {boolean} isValid: : true if the burned amount is valid, false otherwise.
6670
* @return {bigint} txFee: burned amount in nAVAX

0 commit comments

Comments
 (0)