Skip to content

Commit

Permalink
feat: add DisableSubnetValidatorTx and SetSubnetValidatorWeightTx (
Browse files Browse the repository at this point in the history
…#890)

* feat: add DisableSubnetValidatorTx

* feat: add SetSubnetValidatorWeightTx

* fix: add missing typeguard usage
  • Loading branch information
erictaylor authored Oct 28, 2024
1 parent 683b56f commit d80b456
Show file tree
Hide file tree
Showing 17 changed files with 578 additions and 45 deletions.
17 changes: 17 additions & 0 deletions src/fixtures/primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,20 @@ export const blsSignatureBytes = () =>
0x4d, 0xdf, 0xad, 0xd1, 0xc1, 0x74, 0x3f, 0x7f, 0x54, 0x9f, 0x1d, 0x07,
0xd5, 0x9d, 0x55, 0x65, 0x59, 0x27, 0xf7, 0x2b, 0xc6, 0xbf, 0x7c, 0x12,
]);

export const warpMessageBytes = () =>
new Uint8Array([
0, 0, 0, 215, 0, 0, 0, 0, 48, 57, 112, 95, 61, 68, 21, 249, 144, 34, 93, 61,
245, 206, 67, 125, 122, 242, 170, 50, 75, 27, 188, 232, 84, 238, 52, 171,
111, 57, 136, 34, 80, 210, 0, 0, 0, 68, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 54, 0, 0, 0, 0, 0, 3, 56, 230, 233, 254, 49, 198, 208, 112, 168, 199,
146, 219, 172, 246, 208, 174, 251, 142, 172, 42, 222, 212, 156, 192, 170,
159, 66, 45, 31, 221, 158, 205, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
5, 0, 0, 0, 0, 0, 0, 0, 1, 1, 135, 244, 187, 44, 66, 134, 156, 86, 240, 35,
161, 202, 129, 4, 90, 255, 3, 74, 205, 73, 11, 143, 21, 181, 6, 144, 37,
249, 130, 230, 5, 224, 119, 0, 127, 197, 136, 247, 213, 99, 105, 166, 93,
247, 87, 77, 243, 183, 15, 240, 40, 234, 23, 55, 57, 199, 137, 82, 90, 183,
238, 191, 203, 92, 17, 91, 19, 204, 168, 240, 43, 54, 33, 4, 183, 0, 199,
91, 201, 82, 52, 16, 159, 63, 19, 96, 221, 203, 78, 195, 202, 246, 176, 232,
33, 203,
]);
26 changes: 20 additions & 6 deletions src/fixtures/pvm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
TransferSubnetOwnershipTx,
TransformSubnetTx,
IncreaseBalanceTx,
DisableSubnetValidatorTx,
SetSubnetValidatorWeightTx,
} from '../serializable/pvm';
import {
baseTx,
Expand Down Expand Up @@ -306,6 +308,14 @@ export const transformSubnetTxBytes = () =>
inputBytes(),
);

export const pChainOwner = () => new PChainOwner(int(), addresses()());

export const pChainOwnerBytes = () =>
concatBytes(
intBytes(), // threshold
addressesBytes(),
);

export const convertSubnetValidator = () =>
new ConvertSubnetValidator(
bytes(),
Expand Down Expand Up @@ -347,19 +357,23 @@ export const convertSubnetTxBytes = () =>
inputBytes(),
);

export const setSubnetValidatorWeightTx = () =>
new SetSubnetValidatorWeightTx(baseTx(), bytes());

export const setSubnetValidatorWeightTxBytes = () =>
concatBytes(baseTxbytes(), bytesBytes());

export const increaseBalanceTx = () =>
new IncreaseBalanceTx(baseTx(), id(), bigIntPr());

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

export const pChainOwner = () => new PChainOwner(int(), addresses()());
export const disableSubnetValidatorTx = () =>
new DisableSubnetValidatorTx(baseTx(), id(), input());

export const pChainOwnerBytes = () =>
concatBytes(
intBytes(), // threshold
addressesBytes(),
);
export const disableSubnetValidatorTxBytes = () =>
concatBytes(baseTxbytes(), idBytes(), bytesForInt(10), inputBytes());

export const feeState = (): FeeState => ({
capacity: 1n,
Expand Down
2 changes: 2 additions & 0 deletions src/serializable/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ export enum TypeSymbols {

ConvertSubnetTx = 'pvm.ConvertSubnetTx',
ConvertSubnetValidator = 'pvm.ConvertSubnetValidator',
SetSubnetValidatorWeightTx = 'pvm.SetSubnetValidatorWeightTx',
IncreaseBalanceTx = 'pvm.IncreaseBalanceTx',
DisableSubnetValidatorTx = 'pvm.DisableSubnetValidatorTx',

PChainOwner = 'pvm.PChainOwner',

Expand Down
7 changes: 5 additions & 2 deletions src/serializable/pvm/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { TransformSubnetTx } from './transformSubnetTx';
import { BaseTx } from './baseTx';
import { ConvertSubnetTx } from './convertSubnetTx';
import { IncreaseBalanceTx } from './increaseBalanceTx';
import { DisableSubnetValidatorTx } from './disableSubnetValidatorTx';
import { SetSubnetValidatorWeightTx } from './setSubnetValidatorWeightTx';

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

ConvertSubnetTx, // 35
// Replace these with the actual txs when they are implemented
...new Array(2), // 36-37 RegisterSubnetValidatorTx, SetSubnetValidatorWeightTx
...new Array(1), // 36 RegisterSubnetValidatorTx
SetSubnetValidatorWeightTx, // 37
IncreaseBalanceTx, // 38
// DisableSubnetValidatorTx, // 39
DisableSubnetValidatorTx, // 39
]);

let manager: Manager;
Expand Down
15 changes: 15 additions & 0 deletions src/serializable/pvm/disableSubnetValidatorTx.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { testPVMCodec } from '../../fixtures/codec';
import {
disableSubnetValidatorTx,
disableSubnetValidatorTxBytes,
} from '../../fixtures/pvm';
import { testSerialization } from '../../fixtures/utils/serializable';
import { DisableSubnetValidatorTx } from './disableSubnetValidatorTx';

testSerialization(
'DisableSubnetValidatorTx',
DisableSubnetValidatorTx,
disableSubnetValidatorTx,
disableSubnetValidatorTxBytes,
testPVMCodec,
);
48 changes: 48 additions & 0 deletions src/serializable/pvm/disableSubnetValidatorTx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { concatBytes } from '@noble/hashes/utils';
import { pack, unpack } from '../../utils/struct';
import { BaseTx } from '../avax/baseTx';
import { Codec } from '../codec/codec';
import { serializable, type Serializable } from '../common/types';
import { TypeSymbols } from '../constants';
import { Id } from '../fxs/common';
import { PVMTx } from './abstractTx';
import type { Input } from '../fxs/secp256k1';

@serializable()
export class DisableSubnetValidatorTx extends PVMTx {
_type = TypeSymbols.DisableSubnetValidatorTx;

constructor(
public readonly baseTx: BaseTx,
public readonly validationId: Id,
public readonly disableAuth: Serializable,
) {
super();
}

static fromBytes(
bytes: Uint8Array,
codec: Codec,
): [disableSubnetValidatorTx: DisableSubnetValidatorTx, rest: Uint8Array] {
const [baseTx, validationId, disableAuth, rest] = unpack(
bytes,
[BaseTx, Id, Codec],
codec,
);
return [
new DisableSubnetValidatorTx(baseTx, validationId, disableAuth),
rest,
];
}

toBytes(codec: Codec) {
return concatBytes(
pack([this.baseTx, this.validationId], codec),
codec.PackPrefix(this.disableAuth),
);
}

getDisableAuth() {
return this.disableAuth as Input;
}
}
4 changes: 4 additions & 0 deletions src/serializable/pvm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import { AbstractSubnetTx } from './abstractSubnetTx';
import { TransferSubnetOwnershipTx } from './transferSubnetOwnershipTx';
import { TransformSubnetTx } from './transformSubnetTx';
import { ConvertSubnetTx } from './convertSubnetTx';
import { SetSubnetValidatorWeightTx } from './setSubnetValidatorWeightTx';
import { IncreaseBalanceTx } from './increaseBalanceTx';
import { DisableSubnetValidatorTx } from './disableSubnetValidatorTx';

export * from './typeGuards';
export {
Expand All @@ -47,5 +49,7 @@ export {
TransferSubnetOwnershipTx,
TransformSubnetTx,
ConvertSubnetTx,
SetSubnetValidatorWeightTx,
IncreaseBalanceTx,
DisableSubnetValidatorTx,
};
15 changes: 15 additions & 0 deletions src/serializable/pvm/setSubnetValidatorWeightTx.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { testPVMCodec } from '../../fixtures/codec';
import {
setSubnetValidatorWeightTx,
setSubnetValidatorWeightTxBytes,
} from '../../fixtures/pvm';
import { testSerialization } from '../../fixtures/utils/serializable';
import { SetSubnetValidatorWeightTx } from './setSubnetValidatorWeightTx';

testSerialization(
'SetSubnetValidatorWeightTx',
SetSubnetValidatorWeightTx,
setSubnetValidatorWeightTx,
setSubnetValidatorWeightTxBytes,
testPVMCodec,
);
29 changes: 29 additions & 0 deletions src/serializable/pvm/setSubnetValidatorWeightTx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { pack, unpack } from '../../utils/struct';
import { BaseTx } from '../avax/baseTx';
import type { Codec } from '../codec';
import { serializable } from '../common/types';
import { TypeSymbols } from '../constants';
import { Bytes } from '../primitives';
import { PVMTx } from './abstractTx';

@serializable()
export class SetSubnetValidatorWeightTx extends PVMTx {
_type = TypeSymbols.SetSubnetValidatorWeightTx;

constructor(public readonly baseTx: BaseTx, public readonly message: Bytes) {
super();
}

static fromBytes(
bytes: Uint8Array,
codec: Codec,
): [SetSubnetValidatorWeightTx, Uint8Array] {
const [baseTx, message, rest] = unpack(bytes, [BaseTx, Bytes], codec);

return [new SetSubnetValidatorWeightTx(baseTx, message), rest];
}

toBytes(codec: Codec): Uint8Array {
return pack([this.baseTx, this.message], codec);
}
}
4 changes: 4 additions & 0 deletions src/serializable/pvm/typeGuards.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
isTransformSubnetTx,
isConvertSubnetTx,
isIncreaseBalanceTx,
isDisableSubnetValidatorTx,
isSetSubnetValidatorWeightTx,
} from './typeGuards';

const cases: [any, TypeSymbols][] = [
Expand All @@ -41,7 +43,9 @@ const cases: [any, TypeSymbols][] = [
[isTransferSubnetOwnershipTx, TypeSymbols.TransferSubnetOwnershipTx],
[isTransformSubnetTx, TypeSymbols.TransformSubnetTx],
[isConvertSubnetTx, TypeSymbols.ConvertSubnetTx],
[isSetSubnetValidatorWeightTx, TypeSymbols.SetSubnetValidatorWeightTx],
[isIncreaseBalanceTx, TypeSymbols.IncreaseBalanceTx],
[isDisableSubnetValidatorTx, TypeSymbols.DisableSubnetValidatorTx],
];

const guards = cases.map((caseItem) => caseItem[0]);
Expand Down
14 changes: 14 additions & 0 deletions src/serializable/pvm/typeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { TypeSymbols } from '../constants';
import type { TransformSubnetTx } from './transformSubnetTx';
import type { ConvertSubnetTx } from './convertSubnetTx';
import type { IncreaseBalanceTx } from './increaseBalanceTx';
import type { DisableSubnetValidatorTx } from './disableSubnetValidatorTx';
import type { SetSubnetValidatorWeightTx } from './setSubnetValidatorWeightTx';

export function isPvmBaseTx(tx: Transaction): tx is BaseTx {
return tx._type === TypeSymbols.PvmBaseTx;
Expand Down Expand Up @@ -93,10 +95,22 @@ export function isConvertSubnetTx(tx: Transaction): tx is ConvertSubnetTx {
return tx._type === TypeSymbols.ConvertSubnetTx;
}

export function isSetSubnetValidatorWeightTx(
tx: Transaction,
): tx is SetSubnetValidatorWeightTx {
return tx._type === TypeSymbols.SetSubnetValidatorWeightTx;
}

export function isIncreaseBalanceTx(tx: Transaction): tx is IncreaseBalanceTx {
return tx._type === TypeSymbols.IncreaseBalanceTx;
}

export function isDisableSubnetValidatorTx(
tx: Transaction,
): tx is DisableSubnetValidatorTx {
return tx._type === TypeSymbols.DisableSubnetValidatorTx;
}

export function isEmptySigner(
signer: Signer | SignerEmpty,
): signer is SignerEmpty {
Expand Down
8 changes: 6 additions & 2 deletions src/utils/validateBurnedAmount/validateBurnedAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ import {
isConvertSubnetTx,
isCreateChainTx,
isCreateSubnetTx,
isDisableSubnetValidatorTx,
isIncreaseBalanceTx,
isPvmBaseTx,
isExportTx as isPvmExportTx,
isImportTx as isPvmImportTx,
isRemoveSubnetValidatorTx,
isSetSubnetValidatorWeightTx,
isTransferSubnetOwnershipTx,
} from '../../serializable/pvm';

Expand All @@ -49,7 +51,9 @@ const isEtnaSupported = (tx: Transaction) => {
isRemoveSubnetValidatorTx(tx) ||
isTransferSubnetOwnershipTx(tx) ||
isConvertSubnetTx(tx) ||
isIncreaseBalanceTx(tx)
isSetSubnetValidatorWeightTx(tx) ||
isIncreaseBalanceTx(tx) ||
isDisableSubnetValidatorTx(tx)
);
};

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

0 comments on commit d80b456

Please sign in to comment.