-
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.
add validator, add validatorTX, subnetValidator (#88)
* wip * xchain * rename to avm * nits * wip * wip * add test
- Loading branch information
1 parent
1bc2617
commit 8710224
Showing
11 changed files
with
269 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
import { Short } from '../primitives'; | ||
import { getManager } from '../vms/avm/codec'; | ||
import { codec } from '../vms/pvm/codec'; | ||
|
||
// Check for circular imports in the fx type | ||
// registries if tests are throwing errors | ||
|
||
export const testManager = getManager; | ||
|
||
export const testCodec = () => testManager().getCodecForVersion(new Short(0)); | ||
|
||
export const testPVMCodec = () => 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { concatBytes } from '@noble/hashes/utils'; | ||
import { SubnetValidator } from '../vms/pvm/subnetValidator'; | ||
import { AddValidatorTx } from '../vms/pvm/addValidatorTx'; | ||
import { Validator } from '../vms/pvm/validator'; | ||
import { | ||
baseTx, | ||
baseTxbytes, | ||
transferableOutput, | ||
transferableOutputBytes, | ||
} from './avax'; | ||
import { id, idBytes } from './common'; | ||
import { bigIntPr, bigIntPrBytes, int, intBytes } from './primitives'; | ||
import { outputOwner, outputOwnerBytes } from './secp256k1'; | ||
import { makeList, makeListBytes } from './utils/makeList'; | ||
|
||
export const validator = () => | ||
new Validator(id(), bigIntPr(), bigIntPr(), bigIntPr()); | ||
|
||
export const validatorBytes = () => | ||
concatBytes(idBytes(), bigIntPrBytes(), bigIntPrBytes(), bigIntPrBytes()); | ||
|
||
export const addValidatorTx = () => | ||
new AddValidatorTx( | ||
baseTx(), | ||
validator(), | ||
makeList(transferableOutput)(), | ||
outputOwner(), | ||
int(), | ||
); | ||
export const addValidatorTxBytes = () => | ||
concatBytes( | ||
baseTxbytes(), | ||
validatorBytes(), | ||
makeListBytes(transferableOutputBytes)(), | ||
outputOwnerBytes(), | ||
intBytes(), | ||
); | ||
|
||
export const subnetValidator = () => new SubnetValidator(validator(), id()); | ||
|
||
export const subnetValidatorBytes = () => | ||
concatBytes(validatorBytes(), idBytes()); |
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 { addValidatorTx, addValidatorTxBytes } from '../../fixtures/pvm'; | ||
import { testSerialization } from '../../fixtures/utils/serializable'; | ||
import { AddValidatorTx } from './addValidatorTx'; | ||
|
||
testSerialization( | ||
'AddValidatorTx', | ||
AddValidatorTx, | ||
addValidatorTx, | ||
addValidatorTxBytes, | ||
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,56 @@ | ||
import type { Codec } from '../../codec/codec'; | ||
import { serializable } from '../../common/types'; | ||
import { BaseTx, TransferableOutput } from '../../components/avax'; | ||
import { OutputOwners } from '../../fxs/secp256k1'; | ||
import { Int } from '../../primitives'; | ||
import { concatBytes } from '../../utils/buffer'; | ||
import { convertListStruct, packList } from '../../utils/serializeList'; | ||
import { packSimpleWithCodec, unpack } from '../../utils/struct'; | ||
import { Validator } from './validator'; | ||
|
||
const _symbol = Symbol('pvm.AddValidatorTx'); | ||
|
||
/** | ||
* @see | ||
*/ | ||
@serializable() | ||
export class AddValidatorTx { | ||
_type = _symbol; | ||
|
||
constructor( | ||
public readonly baseTx: BaseTx, | ||
public readonly validator: Validator, | ||
public readonly stake: TransferableOutput[], | ||
public readonly rewardsOwner: OutputOwners, | ||
public readonly shares: Int, | ||
) {} | ||
|
||
static fromBytes( | ||
bytes: Uint8Array, | ||
codec: Codec, | ||
): [AddValidatorTx, Uint8Array] { | ||
const [baseTx, validator, stake, rewardsOwner, shares, rest] = unpack( | ||
bytes, | ||
[ | ||
BaseTx, | ||
Validator, | ||
convertListStruct(TransferableOutput), | ||
OutputOwners, | ||
Int, | ||
], | ||
codec, | ||
); | ||
return [ | ||
new AddValidatorTx(baseTx, validator, stake, rewardsOwner, shares), | ||
rest, | ||
]; | ||
} | ||
|
||
toBytes(codec: Codec) { | ||
return concatBytes( | ||
packSimpleWithCodec([this.baseTx, this.validator], codec), | ||
packList(this.stake, codec), | ||
packSimpleWithCodec([this.rewardsOwner, this.shares], 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,36 @@ | ||
import { Codec, Manager } from '../../codec'; | ||
import { BaseTx } from '../../components/avax'; | ||
import * as Secp256k1Fx from '../../fxs/secp256k1'; | ||
import { AddValidatorTx } from './addValidatorTx'; | ||
|
||
// https://github.com/ava-labs/avalanchego/blob/master/vms/platformvm/codec.go | ||
const manager = new Manager(); | ||
manager.RegisterCodec( | ||
0, | ||
new Codec([ | ||
undefined, // TODO: ProposalBlock | ||
undefined, // TODO: AbortBlock | ||
undefined, // TODO: CommitBlock | ||
undefined, // TODO: StandardBlock | ||
undefined, // TODO: AtomicBlock | ||
|
||
...Secp256k1Fx.TypeRegistry, | ||
Secp256k1Fx.Input, | ||
Secp256k1Fx.OutputOwners, | ||
|
||
undefined, // TODO: UnsignedAddValidatorTx | ||
undefined, // TODO: UnsignedAddSubnetValidatorTx | ||
undefined, // TODO: UnsignedAddDelegatorTx | ||
|
||
undefined, // TODO: UnsignedCreateChainTx | ||
undefined, // TODO: UnsignedCreateSubnetTx | ||
|
||
undefined, // TODO: UnsignedImportTx | ||
undefined, // TODO: UnsignedExportTx | ||
|
||
undefined, // TODO: UnsignedAdvanceTimeTx | ||
undefined, // TODO: UnsignedRewardValidatorTx | ||
|
||
undefined, // TODO: stakeable.LockIn | ||
undefined, // TODO: stakeable.LockOut | ||
]), | ||
); | ||
|
||
export { manager }; | ||
export const codec = new Codec([ | ||
BaseTx, // TODO: ProposalBlock | ||
undefined, // TODO: AbortBlock | ||
undefined, // TODO: CommitBlock | ||
undefined, // TODO: StandardBlock | ||
undefined, // TODO: AtomicBlock | ||
|
||
...Secp256k1Fx.TypeRegistry, | ||
Secp256k1Fx.Input, | ||
Secp256k1Fx.OutputOwners, | ||
|
||
AddValidatorTx, // TODO: UnsignedAddValidatorTx | ||
undefined, // TODO: UnsignedAddSubnetValidatorTx | ||
undefined, // TODO: UnsignedAddDelegatorTx | ||
|
||
undefined, // TODO: UnsignedCreateChainTx | ||
undefined, // TODO: UnsignedCreateSubnetTx | ||
|
||
undefined, // TODO: UnsignedImportTx | ||
undefined, // TODO: UnsignedExportTx | ||
|
||
undefined, // TODO: UnsignedAdvanceTimeTx | ||
undefined, // TODO: UnsignedRewardValidatorTx | ||
|
||
undefined, // TODO: stakeable.LockIn | ||
undefined, // TODO: stakeable.LockOut | ||
]); | ||
|
||
export const manager = new Manager(); | ||
manager.RegisterCodec(0, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { testPVMCodec } from '../../fixtures/codec'; | ||
import { subnetValidator, subnetValidatorBytes } from '../../fixtures/pvm'; | ||
import { testSerialization } from '../../fixtures/utils/serializable'; | ||
import { SubnetValidator } from './subnetValidator'; | ||
|
||
testSerialization( | ||
'SubnetValidator', | ||
SubnetValidator, | ||
subnetValidator, | ||
subnetValidatorBytes, | ||
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,32 @@ | ||
import type { Codec } from '../../codec/codec'; | ||
import { serializable } from '../../common/types'; | ||
import { Id } from '../../fxs/common'; | ||
import { packSimpleWithCodec, unpack } from '../../utils/struct'; | ||
import { Validator } from './validator'; | ||
|
||
const _symbol = Symbol('pvm.SubnetValidator'); | ||
|
||
/** | ||
* @see | ||
*/ | ||
@serializable() | ||
export class SubnetValidator { | ||
_type = _symbol; | ||
|
||
constructor( | ||
public readonly validator: Validator, | ||
public readonly subnetId: Id, | ||
) {} | ||
|
||
static fromBytes( | ||
bytes: Uint8Array, | ||
codec: Codec, | ||
): [SubnetValidator, Uint8Array] { | ||
const [validator, subnetId, rest] = unpack(bytes, [Validator, Id], codec); | ||
return [new SubnetValidator(validator, subnetId), rest]; | ||
} | ||
|
||
toBytes(codec: Codec) { | ||
return packSimpleWithCodec([this.validator, this.subnetId], 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { validator, validatorBytes } from '../../fixtures/pvm'; | ||
import { testSerialization } from '../../fixtures/utils/serializable'; | ||
import { Validator } from './validator'; | ||
|
||
testSerialization('Validator', Validator, validator, validatorBytes); |
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,38 @@ | ||
import type { Codec } from '../../codec/codec'; | ||
import { serializable } from '../../common/types'; | ||
import { Id } from '../../fxs/common'; | ||
import { BigIntPr } from '../../primitives'; | ||
import { packSimpleWithCodec, unpack } from '../../utils/struct'; | ||
|
||
const _symbol = Symbol('pvm.Validator'); | ||
|
||
/** | ||
* @see https://docs.avax.network/specs/platform-transaction-serialization#unsigned-add-validator-tx | ||
*/ | ||
@serializable() | ||
export class Validator { | ||
_type = _symbol; | ||
|
||
constructor( | ||
public readonly nodeId: Id, | ||
public readonly startTime: BigIntPr, | ||
public readonly endTime: BigIntPr, | ||
public readonly weight: BigIntPr, | ||
) {} | ||
|
||
static fromBytes(bytes: Uint8Array, codec: Codec): [Validator, Uint8Array] { | ||
const [nodeId, startTime, endTime, weight, rest] = unpack( | ||
bytes, | ||
[Id, BigIntPr, BigIntPr, BigIntPr], | ||
codec, | ||
); | ||
return [new Validator(nodeId, startTime, endTime, weight), rest]; | ||
} | ||
|
||
toBytes(codec: Codec) { | ||
return packSimpleWithCodec( | ||
[this.nodeId, this.startTime, this.endTime, this.weight], | ||
codec, | ||
); | ||
} | ||
} |