Skip to content

Commit 2746ee8

Browse files
authored
feat: add RegisterSubnetValidatorTx (#891)
* feat: add RegisterSubnetValidatorTx * docs: adjust RegisterSubnetValidatorTxProps
1 parent d80b456 commit 2746ee8

File tree

18 files changed

+379
-42
lines changed

18 files changed

+379
-42
lines changed

src/fixtures/primitives.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { BlsSignature } from '../serializable/fxs/common/blsSignature';
12
import {
23
BigIntPr,
34
Byte,
@@ -30,7 +31,7 @@ export const bytes = () => new Bytes(new Uint8Array([0x01, 0x02]));
3031
export const bigIntPrBytes = () =>
3132
new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x84, 0x80]);
3233

33-
export const bigIntPr = () => new BigIntPr(2000000n);
34+
export const bigIntPr = () => new BigIntPr(2_000_000n);
3435

3536
export const stringPr = () => new Stringpr('Avax');
3637

@@ -61,19 +62,28 @@ export const blsSignatureBytes = () =>
6162
0xd5, 0x9d, 0x55, 0x65, 0x59, 0x27, 0xf7, 0x2b, 0xc6, 0xbf, 0x7c, 0x12,
6263
]);
6364

65+
export const blsSignature = () =>
66+
BlsSignature.fromBytes(blsSignatureBytes())[0];
67+
6468
export const warpMessageBytes = () =>
6569
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,
70+
0x00, 0x00, 0x00, 0xd7, 0x00, 0x00, 0x00, 0x00, 0x30, 0x39, 0x70, 0x5f,
71+
0x3d, 0x44, 0x15, 0xf9, 0x90, 0x22, 0x5d, 0x3d, 0xf5, 0xce, 0x43, 0x7d,
72+
0x7a, 0xf2, 0xaa, 0x32, 0x4b, 0x1b, 0xbc, 0xe8, 0x54, 0xee, 0x34, 0xab,
73+
0x6f, 0x39, 0x88, 0x22, 0x50, 0xd2, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00,
74+
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36,
75+
0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x38, 0xe6, 0xe9, 0xfe, 0x31, 0xc6,
76+
0xd0, 0x70, 0xa8, 0xc7, 0x92, 0xdb, 0xac, 0xf6, 0xd0, 0xae, 0xfb, 0x8e,
77+
0xac, 0x2a, 0xde, 0xd4, 0x9c, 0xc0, 0xaa, 0x9f, 0x42, 0x2d, 0x1f, 0xdd,
78+
0x9e, 0xcd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
79+
0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
80+
0x00, 0x01, 0x01, 0x87, 0xf4, 0xbb, 0x2c, 0x42, 0x86, 0x9c, 0x56, 0xf0,
81+
0x23, 0xa1, 0xca, 0x81, 0x04, 0x5a, 0xff, 0x03, 0x4a, 0xcd, 0x49, 0x0b,
82+
0x8f, 0x15, 0xb5, 0x06, 0x90, 0x25, 0xf9, 0x82, 0xe6, 0x05, 0xe0, 0x77,
83+
0x00, 0x7f, 0xc5, 0x88, 0xf7, 0xd5, 0x63, 0x69, 0xa6, 0x5d, 0xf7, 0x57,
84+
0x4d, 0xf3, 0xb7, 0x0f, 0xf0, 0x28, 0xea, 0x17, 0x37, 0x39, 0xc7, 0x89,
85+
0x52, 0x5a, 0xb7, 0xee, 0xbf, 0xcb, 0x5c, 0x11, 0x5b, 0x13, 0xcc, 0xa8,
86+
0xf0, 0x2b, 0x36, 0x21, 0x04, 0xb7, 0x00, 0xc7, 0x5b, 0xc9, 0x52, 0x34,
87+
0x10, 0x9f, 0x3f, 0x13, 0x60, 0xdd, 0xcb, 0x4e, 0xc3, 0xca, 0xf6, 0xb0,
88+
0xe8, 0x21, 0xcb,
7989
]);

src/fixtures/pvm.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import {
5252
stringPrBytes,
5353
byte,
5454
byteByte,
55+
blsSignature,
5556
} from './primitives';
5657
import {
5758
input,
@@ -69,6 +70,7 @@ import type { FeeState } from '../vms/pvm';
6970
import { ConvertSubnetTx } from '../serializable/pvm/convertSubnetTx';
7071
import { ConvertSubnetValidator } from '../serializable/fxs/pvm/convertSubnetValidator';
7172
import { PChainOwner } from '../serializable/fxs/pvm/pChainOwner';
73+
import { RegisterSubnetValidatorTx } from '../serializable/pvm/registerSubnetValidatorTx';
7274

7375
export const validator = () =>
7476
new Validator(nodeId(), bigIntPr(), bigIntPr(), bigIntPr());
@@ -357,6 +359,17 @@ export const convertSubnetTxBytes = () =>
357359
inputBytes(),
358360
);
359361

362+
export const registerSubnetValidatorTx = () =>
363+
new RegisterSubnetValidatorTx(baseTx(), bigIntPr(), blsSignature(), bytes());
364+
365+
export const registerSubnetValidatorTxBytes = () =>
366+
concatBytes(
367+
baseTxbytes(),
368+
bigIntPrBytes(),
369+
blsSignatureBytes(),
370+
bytesBytes(),
371+
);
372+
360373
export const setSubnetValidatorWeightTx = () =>
361374
new SetSubnetValidatorWeightTx(baseTx(), bytes());
362375

src/serializable/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export enum TypeSymbols {
2020
Id = 'common.Id',
2121
Address = 'common.Address',
2222
NodeId = 'common.NodeId',
23+
BlsSignature = 'common.BlsSignature',
2324

2425
// PRIMITIVES
2526
Int = 'primitives.Int',
@@ -82,6 +83,7 @@ export enum TypeSymbols {
8283

8384
ConvertSubnetTx = 'pvm.ConvertSubnetTx',
8485
ConvertSubnetValidator = 'pvm.ConvertSubnetValidator',
86+
RegisterSubnetValidatorTx = 'pvm.RegisterSubnetValidatorTx',
8587
SetSubnetValidatorWeightTx = 'pvm.SetSubnetValidatorWeightTx',
8688
IncreaseBalanceTx = 'pvm.IncreaseBalanceTx',
8789
DisableSubnetValidatorTx = 'pvm.DisableSubnetValidatorTx',
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { blsSignature, blsSignatureBytes } from '../../../fixtures/primitives';
2+
import { testSerialization } from '../../../fixtures/utils/serializable';
3+
import { BlsSignature } from './blsSignature';
4+
5+
testSerialization(
6+
'BlsSignature',
7+
BlsSignature,
8+
blsSignature,
9+
blsSignatureBytes,
10+
);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import type { Signature } from '../../../crypto/bls';
2+
import {
3+
SIGNATURE_LENGTH,
4+
signatureFromBytes,
5+
signatureToBytes,
6+
} from '../../../crypto/bls';
7+
import { hexToBuffer } from '../../../utils/buffer';
8+
import { serializable } from '../../common/types';
9+
import { TypeSymbols } from '../../constants';
10+
import { Primitives } from '../../primitives/primatives';
11+
12+
@serializable()
13+
export class BlsSignature extends Primitives {
14+
_type = TypeSymbols.BlsSignature;
15+
16+
constructor(public readonly signature: Signature) {
17+
super();
18+
}
19+
20+
static fromSignatureBytes(signatureBytes: Uint8Array): BlsSignature {
21+
return new BlsSignature(signatureFromBytes(signatureBytes));
22+
}
23+
24+
static fromBytes(
25+
bytes: Uint8Array,
26+
): [blsSignature: BlsSignature, rest: Uint8Array] {
27+
const blsSignatureBytes = bytes.slice(0, SIGNATURE_LENGTH);
28+
const signature = signatureFromBytes(blsSignatureBytes);
29+
const rest = bytes.slice(SIGNATURE_LENGTH);
30+
31+
return [new BlsSignature(signature), rest];
32+
}
33+
34+
static fromHex(hex: string): BlsSignature {
35+
return new BlsSignature(signatureFromBytes(hexToBuffer(hex)));
36+
}
37+
38+
toBytes() {
39+
return signatureToBytes(this.signature);
40+
}
41+
42+
toString() {
43+
return this.signature.toHex();
44+
}
45+
46+
toJSON() {
47+
return this.toString();
48+
}
49+
}

src/serializable/fxs/common/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Address } from './address';
2+
import { BlsSignature } from './blsSignature';
23
import { Id } from './id';
34
import { NodeId } from './nodeId';
45

5-
export { Address, Id, NodeId };
6+
export { Address, BlsSignature, Id, NodeId };

src/serializable/pvm/codec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { ConvertSubnetTx } from './convertSubnetTx';
2222
import { IncreaseBalanceTx } from './increaseBalanceTx';
2323
import { DisableSubnetValidatorTx } from './disableSubnetValidatorTx';
2424
import { SetSubnetValidatorWeightTx } from './setSubnetValidatorWeightTx';
25+
import { RegisterSubnetValidatorTx } from './registerSubnetValidatorTx';
2526

2627
/**
2728
* @see https://github.com/ava-labs/avalanchego/blob/master/vms/platformvm/txs/codec.go#L35
@@ -63,8 +64,7 @@ export const codec = new Codec([
6364
BaseTx, // 34
6465

6566
ConvertSubnetTx, // 35
66-
// Replace these with the actual txs when they are implemented
67-
...new Array(1), // 36 RegisterSubnetValidatorTx
67+
RegisterSubnetValidatorTx, // 36
6868
SetSubnetValidatorWeightTx, // 37
6969
IncreaseBalanceTx, // 38
7070
DisableSubnetValidatorTx, // 39

src/serializable/pvm/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { AbstractSubnetTx } from './abstractSubnetTx';
2020
import { TransferSubnetOwnershipTx } from './transferSubnetOwnershipTx';
2121
import { TransformSubnetTx } from './transformSubnetTx';
2222
import { ConvertSubnetTx } from './convertSubnetTx';
23+
import { RegisterSubnetValidatorTx } from './registerSubnetValidatorTx';
2324
import { SetSubnetValidatorWeightTx } from './setSubnetValidatorWeightTx';
2425
import { IncreaseBalanceTx } from './increaseBalanceTx';
2526
import { DisableSubnetValidatorTx } from './disableSubnetValidatorTx';
@@ -49,6 +50,7 @@ export {
4950
TransferSubnetOwnershipTx,
5051
TransformSubnetTx,
5152
ConvertSubnetTx,
53+
RegisterSubnetValidatorTx,
5254
SetSubnetValidatorWeightTx,
5355
IncreaseBalanceTx,
5456
DisableSubnetValidatorTx,
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+
registerSubnetValidatorTx,
4+
registerSubnetValidatorTxBytes,
5+
} from '../../fixtures/pvm';
6+
import { testSerialization } from '../../fixtures/utils/serializable';
7+
import { RegisterSubnetValidatorTx } from './registerSubnetValidatorTx';
8+
9+
testSerialization(
10+
'RegisterSubnetValidatorTx',
11+
RegisterSubnetValidatorTx,
12+
registerSubnetValidatorTx,
13+
registerSubnetValidatorTxBytes,
14+
testPVMCodec,
15+
);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { pack, unpack } from '../../utils/struct';
2+
import { BaseTx } from '../avax/baseTx';
3+
import type { Codec } from '../codec/codec';
4+
import { serializable } from '../common/types';
5+
import { TypeSymbols } from '../constants';
6+
import { BlsSignature } from '../fxs/common/blsSignature';
7+
import { BigIntPr, Bytes } from '../primitives';
8+
import { PVMTx } from './abstractTx';
9+
10+
@serializable()
11+
export class RegisterSubnetValidatorTx extends PVMTx {
12+
_type = TypeSymbols.RegisterSubnetValidatorTx;
13+
14+
constructor(
15+
public readonly baseTx: BaseTx,
16+
public readonly balance: BigIntPr,
17+
public readonly blsSignature: BlsSignature,
18+
public readonly message: Bytes,
19+
) {
20+
super();
21+
}
22+
23+
static fromBytes(
24+
bytes: Uint8Array,
25+
codec: Codec,
26+
): [registerSubnetValidatorTx: RegisterSubnetValidatorTx, rest: Uint8Array] {
27+
const [baseTx, balance, blsSignature, message, rest] = unpack(
28+
bytes,
29+
[BaseTx, BigIntPr, BlsSignature, Bytes],
30+
codec,
31+
);
32+
return [
33+
new RegisterSubnetValidatorTx(baseTx, balance, blsSignature, message),
34+
rest,
35+
];
36+
}
37+
38+
toBytes(codec: Codec) {
39+
return pack(
40+
[this.baseTx, this.balance, this.blsSignature, this.message],
41+
codec,
42+
);
43+
}
44+
}

src/serializable/pvm/typeGuards.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
isIncreaseBalanceTx,
2323
isDisableSubnetValidatorTx,
2424
isSetSubnetValidatorWeightTx,
25+
isRegisterSubnetValidatorTx,
2526
} from './typeGuards';
2627

2728
const cases: [any, TypeSymbols][] = [
@@ -43,6 +44,7 @@ const cases: [any, TypeSymbols][] = [
4344
[isTransferSubnetOwnershipTx, TypeSymbols.TransferSubnetOwnershipTx],
4445
[isTransformSubnetTx, TypeSymbols.TransformSubnetTx],
4546
[isConvertSubnetTx, TypeSymbols.ConvertSubnetTx],
47+
[isRegisterSubnetValidatorTx, TypeSymbols.RegisterSubnetValidatorTx],
4648
[isSetSubnetValidatorWeightTx, TypeSymbols.SetSubnetValidatorWeightTx],
4749
[isIncreaseBalanceTx, TypeSymbols.IncreaseBalanceTx],
4850
[isDisableSubnetValidatorTx, TypeSymbols.DisableSubnetValidatorTx],

src/serializable/pvm/typeGuards.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type { ConvertSubnetTx } from './convertSubnetTx';
2020
import type { IncreaseBalanceTx } from './increaseBalanceTx';
2121
import type { DisableSubnetValidatorTx } from './disableSubnetValidatorTx';
2222
import type { SetSubnetValidatorWeightTx } from './setSubnetValidatorWeightTx';
23+
import type { RegisterSubnetValidatorTx } from './registerSubnetValidatorTx';
2324

2425
export function isPvmBaseTx(tx: Transaction): tx is BaseTx {
2526
return tx._type === TypeSymbols.PvmBaseTx;
@@ -95,6 +96,12 @@ export function isConvertSubnetTx(tx: Transaction): tx is ConvertSubnetTx {
9596
return tx._type === TypeSymbols.ConvertSubnetTx;
9697
}
9798

99+
export function isRegisterSubnetValidatorTx(
100+
tx: Transaction,
101+
): tx is RegisterSubnetValidatorTx {
102+
return tx._type === TypeSymbols.RegisterSubnetValidatorTx;
103+
}
104+
98105
export function isSetSubnetValidatorWeightTx(
99106
tx: Transaction,
100107
): tx is SetSubnetValidatorWeightTx {

src/utils/validateBurnedAmount/validateBurnedAmount.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
isPvmBaseTx,
2424
isExportTx as isPvmExportTx,
2525
isImportTx as isPvmImportTx,
26+
isRegisterSubnetValidatorTx,
2627
isRemoveSubnetValidatorTx,
2728
isSetSubnetValidatorWeightTx,
2829
isTransferSubnetOwnershipTx,
@@ -51,6 +52,7 @@ const isEtnaSupported = (tx: Transaction) => {
5152
isRemoveSubnetValidatorTx(tx) ||
5253
isTransferSubnetOwnershipTx(tx) ||
5354
isConvertSubnetTx(tx) ||
55+
isRegisterSubnetValidatorTx(tx) ||
5456
isSetSubnetValidatorWeightTx(tx) ||
5557
isIncreaseBalanceTx(tx) ||
5658
isDisableSubnetValidatorTx(tx)

0 commit comments

Comments
 (0)