-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad65b27
commit d7378fc
Showing
17 changed files
with
516 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
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 { TypeSymbols } from '../../constants'; | ||
import { ProofOfPossession } from '../../pvm/proofOfPossession'; | ||
import { PChainOwner } from './pChainOwner'; | ||
import { emptyNodeId } from '../../../constants/zeroValue'; | ||
|
||
/** | ||
* @see https://github.com/ava-labs/avalanchego/blob/master/vms/platformvm/txs/convert_subnet_tx.go#86 | ||
*/ | ||
@serializable() | ||
export class ConvertSubnetValidator { | ||
_type = TypeSymbols.ConvertSubnetValidator; | ||
|
||
constructor( | ||
public readonly nodeId: NodeId, | ||
public readonly weight: BigIntPr, | ||
public readonly balance: BigIntPr, | ||
public readonly signer: ProofOfPossession, | ||
public readonly remainingBalanceOwner: PChainOwner, | ||
public readonly deactivationOwner: PChainOwner, | ||
) {} | ||
|
||
static fromBytes( | ||
bytes: Uint8Array, | ||
codec: Codec, | ||
): [ConvertSubnetValidator, Uint8Array] { | ||
const [ | ||
nodeId, | ||
weight, | ||
balance, | ||
signer, | ||
remainingBalanceOwner, | ||
deactivationOwner, | ||
rest, | ||
] = unpack( | ||
bytes, | ||
[NodeId, BigIntPr, BigIntPr, ProofOfPossession, PChainOwner, PChainOwner], | ||
codec, | ||
); | ||
|
||
return [ | ||
new ConvertSubnetValidator( | ||
nodeId, | ||
weight, | ||
balance, | ||
signer, | ||
remainingBalanceOwner, | ||
deactivationOwner, | ||
), | ||
rest, | ||
]; | ||
} | ||
|
||
toBytes(codec: Codec) { | ||
return pack( | ||
[ | ||
this.nodeId, | ||
this.weight, | ||
this.balance, | ||
this.signer, | ||
this.remainingBalanceOwner, | ||
this.deactivationOwner, | ||
], | ||
codec, | ||
); | ||
} | ||
|
||
// static compare(nodeId1: NodeId, nodeId2: NodeId): number { | ||
// return NodeId.compare(nodeId1, nodeId2); | ||
// } | ||
|
||
verify(): boolean { | ||
if (this.weight === new BigIntPr(0n)) { | ||
throw new Error('Weight must be greater than 0'); | ||
} | ||
|
||
if (this.nodeId === emptyNodeId) { | ||
throw new Error('Node ID must be non-empty'); | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { pChainOwner, pChainOwnerBytes } from '../../../fixtures/pvm'; | ||
import { testSerialization } from '../../../fixtures/utils/serializable'; | ||
import { PChainOwner } from './pChainOwner'; | ||
|
||
testSerialization('PChainOwner', PChainOwner, pChainOwner, pChainOwnerBytes); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { concatBytes } from '@noble/hashes/utils'; | ||
import { packList, toListStruct } from '../../../utils/serializeList'; | ||
import { pack, unpack } from '../../../utils/struct'; | ||
import { serializable } from '../../common/types'; | ||
import { Int } from '../../primitives'; | ||
import { Address } from '../common/address'; | ||
import { TypeSymbols } from '../../constants'; | ||
import type { Codec } from '../../codec'; | ||
|
||
/** | ||
* @see https://github.com/ava-labs/avalanchego/blob/master/vms/platformvm/warp/message/register_subnet_validator.go | ||
*/ | ||
@serializable() | ||
export class PChainOwner { | ||
_type = TypeSymbols.PChainOwner; | ||
|
||
constructor( | ||
public readonly threshold: Int, | ||
public readonly addresses: Address[], | ||
) {} | ||
|
||
static fromBytes(bytes: Uint8Array, codec: Codec): [PChainOwner, Uint8Array] { | ||
const [threshold, addresses, remaining] = unpack( | ||
bytes, | ||
[Int, toListStruct(Address)], | ||
codec, | ||
); | ||
return [new PChainOwner(threshold, addresses), remaining]; | ||
} | ||
|
||
toBytes(codec: Codec) { | ||
return concatBytes( | ||
pack([this.threshold, this.addresses], codec), | ||
// packList(this.addresses, codec), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { testPVMCodec } from '../../fixtures/codec'; | ||
import { convertSubnetTx, convertSubnetTxBytes } from '../../fixtures/pvm'; | ||
import { testSerialization } from '../../fixtures/utils/serializable'; | ||
import { ConvertSubnetTx } from './convertSubnetTx'; | ||
|
||
testSerialization( | ||
'ConvertSubnetTx', | ||
ConvertSubnetTx, | ||
convertSubnetTx, | ||
convertSubnetTxBytes, | ||
testPVMCodec, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { concatBytes } from '../../utils/buffer'; | ||
import { toListStruct } from '../../utils/serializeList'; | ||
import { pack, unpack } from '../../utils/struct'; | ||
import { BaseTx } from '../avax/baseTx'; | ||
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 { AbstractSubnetTx } from './abstractSubnetTx'; | ||
|
||
@serializable() | ||
export class ConvertSubnetTx extends AbstractSubnetTx { | ||
_type = TypeSymbols.ConvertSubnetTx; | ||
|
||
constructor( | ||
public readonly baseTx: BaseTx, | ||
public readonly subnetID: Id, | ||
public readonly chainID: Id, | ||
public readonly address: Address, | ||
public readonly validators: ConvertSubnetValidator[], | ||
public readonly subnetAuth: Serializable, | ||
) { | ||
super(); | ||
} | ||
|
||
getSubnetID() { | ||
return this.subnetID; | ||
} | ||
|
||
static fromBytes( | ||
bytes: Uint8Array, | ||
codec: Codec, | ||
): [ConvertSubnetTx, Uint8Array] { | ||
const [baseTx, subnetID, chainID, address, validators, subnetAuth, rest] = | ||
unpack( | ||
bytes, | ||
[BaseTx, Id, Id, Address, toListStruct(ConvertSubnetValidator), Codec], | ||
codec, | ||
); | ||
return [ | ||
new ConvertSubnetTx( | ||
baseTx, | ||
subnetID, | ||
chainID, | ||
address, | ||
validators, | ||
subnetAuth, | ||
), | ||
rest, | ||
]; | ||
} | ||
|
||
toBytes(codec: Codec) { | ||
return concatBytes( | ||
pack( | ||
[ | ||
this.baseTx, | ||
this.subnetID, | ||
this.chainID, | ||
this.address, | ||
this.validators, | ||
], | ||
codec, | ||
), | ||
codec.PackPrefix(this.subnetAuth), | ||
); | ||
} | ||
} |
Oops, something went wrong.