Skip to content

Commit

Permalink
Merge pull request #884 from ava-labs/feat/convertSubnetTx-fixes
Browse files Browse the repository at this point in the history
fix/convertsubnettx type fixes
  • Loading branch information
ruijialin-avalabs authored Oct 16, 2024
2 parents d7378fc + 941b39b commit aae826f
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 26 deletions.
11 changes: 5 additions & 6 deletions src/fixtures/pvm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import {
transferableOutputBytes,
} from './avax';
import {
address,
addressBytes,
addresses,
addressesBytes,
id,
Expand Down Expand Up @@ -309,7 +307,7 @@ export const transformSubnetTxBytes = () =>

export const convertSubnetValidator = () =>
new ConvertSubnetValidator(
nodeId(),
bytes(),
bigIntPr(),
bigIntPr(),
proofOfPossession(),
Expand All @@ -319,7 +317,7 @@ export const convertSubnetValidator = () =>

export const convertSubnetValidatorBytes = () =>
concatBytes(
nodeIdBytes(),
bytesBytes(),
bigIntPrBytes(),
bigIntPrBytes(),
proofOfPossessionBytes(),
Expand All @@ -332,7 +330,7 @@ export const convertSubnetTx = () =>
baseTx(),
id(),
id(),
address(),
bytes(),
makeList(convertSubnetValidator)(),
input(),
);
Expand All @@ -342,8 +340,9 @@ export const convertSubnetTxBytes = () =>
baseTxbytes(),
idBytes(),
idBytes(),
addressBytes(),
bytesBytes(),
makeListBytes(convertSubnetValidatorBytes)(),
bytesForInt(10),
inputBytes(),
);

Expand Down
4 changes: 0 additions & 4 deletions src/fixtures/utils/serializable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ export const testSerialization = (

describe(name, () => {
it('serializes correctly', () => {
if (name === 'ConvertSubnetTx') {
console.log('a', entityFixture().toBytes(codec()));
console.log('b', bytesFixture());
}
expect(entityFixture().toBytes(codec())).toStrictEqual(bytesFixture());
});
});
Expand Down
1 change: 0 additions & 1 deletion src/serializable/codec/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export class Codec {
UnpackPrefix = <T extends Serializable>(buf: Uint8Array): [T, Uint8Array] => {
let typeId: Int;
[typeId, buf] = unpack(buf, [Int]);
console.log('typeId', typeId, this.typeIdToType);
const type = this.typeIdToType[typeId.value()];

if (type === undefined) {
Expand Down
12 changes: 7 additions & 5 deletions src/serializable/fxs/pvm/convertSubnetValidator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { pack, unpack } from '../../../utils/struct';
import type { Codec } from '../../codec/codec';
import { serializable } from '../../common/types';
import { NodeId } from '../common/nodeId';
import { BigIntPr } from '../../primitives';
import { BigIntPr, Bytes } from '../../primitives';
import { TypeSymbols } from '../../constants';
import { ProofOfPossession } from '../../pvm/proofOfPossession';
import { PChainOwner } from './pChainOwner';
import { emptyNodeId } from '../../../constants/zeroValue';
import { NodeId } from '../common';

/**
* @see https://github.com/ava-labs/avalanchego/blob/master/vms/platformvm/txs/convert_subnet_tx.go#86
Expand All @@ -16,7 +16,7 @@ export class ConvertSubnetValidator {
_type = TypeSymbols.ConvertSubnetValidator;

constructor(
public readonly nodeId: NodeId,
public readonly nodeId: Bytes,
public readonly weight: BigIntPr,
public readonly balance: BigIntPr,
public readonly signer: ProofOfPossession,
Expand All @@ -38,7 +38,7 @@ export class ConvertSubnetValidator {
rest,
] = unpack(
bytes,
[NodeId, BigIntPr, BigIntPr, ProofOfPossession, PChainOwner, PChainOwner],
[Bytes, BigIntPr, BigIntPr, ProofOfPossession, PChainOwner, PChainOwner],
codec,
);

Expand Down Expand Up @@ -78,7 +78,9 @@ export class ConvertSubnetValidator {
throw new Error('Weight must be greater than 0');
}

if (this.nodeId === emptyNodeId) {
const nodeId = new NodeId(this.nodeId.toBytesWithoutLength());

if (nodeId === emptyNodeId) {
throw new Error('Node ID must be non-empty');
}
return true;
Expand Down
7 changes: 2 additions & 5 deletions src/serializable/fxs/pvm/pChainOwner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { concatBytes } from '@noble/hashes/utils';
import { packList, toListStruct } from '../../../utils/serializeList';
import { toListStruct } from '../../../utils/serializeList';
import { pack, unpack } from '../../../utils/struct';
import { serializable } from '../../common/types';
import { Int } from '../../primitives';
Expand Down Expand Up @@ -29,9 +29,6 @@ export class PChainOwner {
}

toBytes(codec: Codec) {
return concatBytes(
pack([this.threshold, this.addresses], codec),
// packList(this.addresses, codec),
);
return concatBytes(pack([this.threshold, this.addresses], codec));
}
}
4 changes: 4 additions & 0 deletions src/serializable/primitives/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export class Bytes extends Primitives {
return concatBytes(bytesForInt(this.bytes.length), this.bytes);
}

toBytesWithoutLength() {
return this.bytes;
}

/**
* Returns the length of the bytes (Uint8Array).
*
Expand Down
4 changes: 2 additions & 2 deletions src/serializable/pvm/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { RemoveSubnetValidatorTx } from './removeSubnetValidatorTx';
import { TransferSubnetOwnershipTx } from './transferSubnetOwnershipTx';
import { TransformSubnetTx } from './transformSubnetTx';
import { BaseTx } from './baseTx';
import { ConvertSubnetValidator } from '../fxs/pvm/convertSubnetValidator';
import { ConvertSubnetTx } from './convertSubnetTx';

/**
* @see https://github.com/ava-labs/avalanchego/blob/master/vms/platformvm/txs/codec.go#L35
Expand Down Expand Up @@ -59,7 +59,7 @@ export const codec = new Codec([
TransferSubnetOwnershipTx, // 33
BaseTx, // 34

ConvertSubnetValidator, // 35
ConvertSubnetTx, // 35
]);

let manager: Manager;
Expand Down
6 changes: 3 additions & 3 deletions src/serializable/pvm/convertSubnetTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { Codec } from '../codec/codec';
import type { Serializable } from '../common/types';
import { serializable } from '../common/types';
import { TypeSymbols } from '../constants';
import { Address } from '../fxs/common';
import { Id } from '../fxs/common';
import { ConvertSubnetValidator } from '../fxs/pvm/convertSubnetValidator';
import { Bytes } from '../primitives/bytes';
import { AbstractSubnetTx } from './abstractSubnetTx';

@serializable()
Expand All @@ -19,7 +19,7 @@ export class ConvertSubnetTx extends AbstractSubnetTx {
public readonly baseTx: BaseTx,
public readonly subnetID: Id,
public readonly chainID: Id,
public readonly address: Address,
public readonly address: Bytes,
public readonly validators: ConvertSubnetValidator[],
public readonly subnetAuth: Serializable,
) {
Expand All @@ -37,7 +37,7 @@ export class ConvertSubnetTx extends AbstractSubnetTx {
const [baseTx, subnetID, chainID, address, validators, subnetAuth, rest] =
unpack(
bytes,
[BaseTx, Id, Id, Address, toListStruct(ConvertSubnetValidator), Codec],
[BaseTx, Id, Id, Bytes, toListStruct(ConvertSubnetValidator), Codec],
codec,
);
return [
Expand Down

0 comments on commit aae826f

Please sign in to comment.