Skip to content

Commit

Permalink
fix(AddProposalTx): fix reading description from buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
aeddaqqa committed May 27, 2024
1 parent acc00d8 commit f063aa0
Show file tree
Hide file tree
Showing 14 changed files with 289 additions and 635 deletions.
23 changes: 17 additions & 6 deletions examples/platformvm/addProposalTx.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
AddMemberProposal,
KeyChain,
PlatformVMAPI
AddMemberProposal,
KeyChain,
PlatformVMAPI
} from "caminojs/apis/platformvm"
import { Avalanche, BinTools, Buffer } from "caminojs/index"
import { DefaultLocalGenesisPrivateKey, PrivateKeyPrefix } from "caminojs/utils"
Expand All @@ -23,7 +23,7 @@ let privKey: string = `${PrivateKeyPrefix}${DefaultLocalGenesisPrivateKey}`
let pchain: PlatformVMAPI
let pKeychain: KeyChain
let pAddressStrings: string[]
const targetAddress = "P-kopernikus1dty07uqqndf22dqkgfq4ysa9nucg33q2ut5sq6"
const targetAddress = "P-kopernikus122gtala73kjrf34xtdq0d9vssqlccxjjam7kk8"
const bintools: BinTools = BinTools.getInstance()
const InitAvalanche = async () => {
await avalanche.fetchNetworkSettings()
Expand All @@ -36,8 +36,19 @@ const InitAvalanche = async () => {

const main = async (): Promise<any> => {
await InitAvalanche()
const proposal = new AddMemberProposal( 1714431600,1719615600, targetAddress)
let startDate = new Date()
startDate.setDate(startDate.getDate() + 1)
let endDate = new Date(startDate)
endDate.setDate(endDate.getDate() + 60)

let startTimestamp = Math.floor(startDate.getTime() / 1000)
let endTimestamp = Math.floor(endDate.getTime() / 1000)
const txs = await pchain.getUTXOs(pAddressStrings)
const proposal = new AddMemberProposal(
startTimestamp,
endTimestamp,
targetAddress
)
try {
let unsignedTx = await pchain.buildAddProposalTx(
txs.utxos, // utxoset
Expand All @@ -47,7 +58,7 @@ const main = async (): Promise<any> => {
proposal, // proposal
pKeychain.getAddresses()[0], // proposerAddress
0, // version
Buffer.alloc(20), // memo
Buffer.alloc(20) // memo
)
const tx = unsignedTx.sign(pKeychain)
const txid: string = await pchain.issueTx(tx)
Expand Down
34 changes: 19 additions & 15 deletions examples/platformvm/buildAddressStateTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { ExamplesConfig } from "../common/examplesConfig"
import { DefaultLocalGenesisPrivateKey2 } from "caminojs/utils"
import { ZeroBN } from "caminojs/common"


const config: ExamplesConfig = require("../common/examplesConfig.json")
const avalanche: Avalanche = new Avalanche(
config.host,
Expand Down Expand Up @@ -49,12 +48,12 @@ const main = async (): Promise<any> => {
"Utility function to create an AddressStateTx transaction"
)

let unsignedTx: UnsignedTx
let unsignedTx: UnsignedTx
let tx: Tx
let txid: string
let status: any

try{
try {
unsignedTx = await pchain.buildAddressStateTx(
0,
undefined,
Expand All @@ -70,13 +69,15 @@ const main = async (): Promise<any> => {
txid = await pchain.issueTx(tx)
console.log(`Success! TXID: ${txid}`)

while ((status = (await pchain.getTxStatus(txid))as GetTxStatusResponse).status !== 'Committed'){
await new Promise(resolve => setTimeout(resolve, 1000));
while (
(status = (await pchain.getTxStatus(txid)) as GetTxStatusResponse)
.status !== "Committed"
) {
await new Promise((resolve) => setTimeout(resolve, 1000))
}
console.log('Status', status)

} catch(e) {
console.log('Failed: ', e.message)
console.log("Status", status)
} catch (e) {
console.log("Failed: ", e.message)
}

// This should fail because UV1 requires auth
Expand All @@ -95,7 +96,7 @@ const main = async (): Promise<any> => {
tx = unsignedTx.sign(pKeychain)
txid = await pchain.issueTx(tx)
console.log(`This should not happen: ${txid}`)
} catch(e) {
} catch (e) {
console.log(`Successfully failed!`)
}

Expand All @@ -119,12 +120,15 @@ const main = async (): Promise<any> => {
txid = await pchain.issueTx(tx)
console.log(`Success! TXID: ${txid}`)

while ((status = (await pchain.getTxStatus(txid))as GetTxStatusResponse).status !== 'Committed'){
await new Promise(resolve => setTimeout(resolve, 1000));
while (
(status = (await pchain.getTxStatus(txid)) as GetTxStatusResponse)
.status !== "Committed"
) {
await new Promise((resolve) => setTimeout(resolve, 1000))
}
console.log('Status', status)
} catch(e) {
console.log('Failed: ', e.message)
console.log("Status", status)
} catch (e) {
console.log("Failed: ", e.message)
}
}

Expand Down
33 changes: 19 additions & 14 deletions src/apis/platformvm/addproposaltx/addmemberproposal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Buffer } from 'buffer/'
import BinTools from '../../../utils/bintools'
import { Serialization, SerializedEncoding } from '../../../utils/serialization'
import { Buffer } from "buffer/"
import BinTools from "../../../utils/bintools"
import { Serialization, SerializedEncoding } from "../../../utils/serialization"
import { PlatformVMConstants } from "../constants"
import { EssentialProposal } from "./essentialproposal"

Expand All @@ -10,18 +10,18 @@ const bintools = BinTools.getInstance()
export class AddMemberProposal extends EssentialProposal {
private readonly _typeID = PlatformVMConstants.ADDMEMBERPORPOSAL_TYPE_ID

serialize(encoding: SerializedEncoding = 'hex'): object {
serialize(encoding: SerializedEncoding = "hex"): object {
return {
start: serialization.encoder(
this.start,
start: serialization.encoder(this.start, encoding, "Buffer", "number"),
end: serialization.encoder(this.end, encoding, "Buffer", "number"),
applicantAddress: serialization.encoder(
this.applicantAddress,
encoding,
"Buffer",
"number"
),
end: serialization.encoder(this.end, encoding, "Buffer", "number"),
applicantAddress: serialization.encoder(this.applicantAddress, encoding, "Buffer", "cb58"),
"cb58"
)
}
};
}

deserialize(fields: object, encoding: SerializedEncoding = "hex"): this {
this.start = serialization.decoder(
Expand Down Expand Up @@ -62,11 +62,16 @@ export class AddMemberProposal extends EssentialProposal {
*/
toBuffer(): Buffer {
const barr: Buffer[] = [this.applicantAddress, this.start, this.end]
const bsize = this.applicantAddress.length + this.start.length + this.end.length
const bsize =
this.applicantAddress.length + this.start.length + this.end.length
return Buffer.concat(barr, bsize)
}

constructor(start?: number, end?: number, applicantAddress?: string | Buffer) {
constructor(
start?: number,
end?: number,
applicantAddress?: string | Buffer
) {
const startTime = Buffer.alloc(8)
startTime.writeUInt32BE(start, 4)
const endTime = Buffer.alloc(8)
Expand Down Expand Up @@ -101,4 +106,4 @@ export class AddMemberProposal extends EssentialProposal {
getApplicantAddress(): Buffer {
return this.applicantAddress
}
}
}
32 changes: 21 additions & 11 deletions src/apis/platformvm/addproposaltx/adminproposal.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Buffer } from 'buffer/'
import BinTools from '../../../utils/bintools'
import { Serialization, SerializedEncoding } from '../../../utils/serialization'
import { Buffer } from "buffer/"
import BinTools from "../../../utils/bintools"
import { Serialization, SerializedEncoding } from "../../../utils/serialization"
import { PlatformVMConstants } from "../constants"
import { EssentialProposal } from "./essentialproposal"
import { AddMemberProposal } from './addmemberproposal'
import { ExcludeMemberProposal } from './excludememberproposal'
import { AddMemberProposal } from "./addmemberproposal"
import { ExcludeMemberProposal } from "./excludememberproposal"

const serialization = Serialization.getInstance()
const bintools = BinTools.getInstance()
Expand All @@ -24,7 +24,12 @@ export class AdminProposal extends EssentialProposal {
serialize(encoding: SerializedEncoding = "hex"): object {
return {
proposal: this._proposal.serialize(encoding),
optionIndex: serialization.encoder(this._optionIndex, encoding, "Buffer", "number"),
optionIndex: serialization.encoder(
this._optionIndex,
encoding,
"Buffer",
"number"
)
}
}

Expand Down Expand Up @@ -55,15 +60,17 @@ export class AdminProposal extends EssentialProposal {
// try to parse addmember proposal
this._optionIndex = bintools.copyFrom(bytes, offset, offset + 4)
offset += 4
const proposalTypeID = bintools.copyFrom(bytes, offset, offset + 4).readUInt32BE(0)
const proposalTypeID = bintools
.copyFrom(bytes, offset, offset + 4)
.readUInt32BE(0)
offset += 4
switch (proposalTypeID) {
case PlatformVMConstants.ADDMEMBERPORPOSAL_TYPE_ID:
this._proposal = new AddMemberProposal()
break;
break
case PlatformVMConstants.EXCLUDEMEMBERPORPOSAL_TYPE_ID:
this._proposal = new ExcludeMemberProposal()
break;
break
default:
throw `Unsupported proposal type: ${proposalTypeID}`
}
Expand All @@ -79,6 +86,9 @@ export class AdminProposal extends EssentialProposal {
const typeIdBuff = Buffer.alloc(4)
typeIdBuff.writeUInt32BE(this._proposal.getTypeID(), 0)
const proposalBuff = this._proposal.toBuffer()
return Buffer.concat([buff,typeIdBuff, proposalBuff], buff.length + typeIdBuff.length + proposalBuff.length)
return Buffer.concat(
[buff, typeIdBuff, proposalBuff],
buff.length + typeIdBuff.length + proposalBuff.length
)
}
}
}
4 changes: 2 additions & 2 deletions src/apis/platformvm/addproposaltx/basefeeproposal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Buffer } from 'buffer/'
import { Buffer } from "buffer/"
import { PlatformVMConstants } from "../constants"
import { EssentialProposal, VoteOption } from "./essentialproposal"

Expand All @@ -23,4 +23,4 @@ export class BaseFeeProposal extends EssentialProposal {
voteOption.fromBuffer(optionBuf)
return super.addOption(voteOption)
}
}
}
25 changes: 11 additions & 14 deletions src/apis/platformvm/addproposaltx/essentialproposal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Buffer } from 'buffer/'
import { Buffer } from "buffer/"
import BinTools from "../../../utils/bintools"
import { Serialization, SerializedEncoding } from "../../../utils/serialization"
import { NBytes } from '../../../common'
import { NBytes } from "../../../common"

/**
* @ignore
Expand Down Expand Up @@ -71,19 +71,14 @@ export abstract class EssentialProposal {
return this.options.length - 1
}

serialize(encoding: SerializedEncoding = 'hex'): object {
serialize(encoding: SerializedEncoding = "hex"): object {
let fields = {
start: serialization.encoder(
this.start,
encoding,
"Buffer",
"number"
),
start: serialization.encoder(this.start, encoding, "Buffer", "number"),
end: serialization.encoder(this.end, encoding, "Buffer", "number"),
options: this.options.map(opt => opt.serialize(encoding))
options: this.options.map((opt) => opt.serialize(encoding))
}
return fields
};
}

deserialize(fields: object, encoding: SerializedEncoding = "hex"): this {
this.start = serialization.decoder(
Expand All @@ -99,7 +94,9 @@ export abstract class EssentialProposal {
"Buffer"
)
this.numOptions.writeUInt32BE(this.options.length, 0)
this.options = fields['options'].map(opt => new VoteOption().deserialize(opt, encoding))
this.options = fields["options"].map((opt) =>
new VoteOption().deserialize(opt, encoding)
)

return this
}
Expand Down Expand Up @@ -128,7 +125,7 @@ export abstract class EssentialProposal {
const barr: Buffer[] = [this.numOptions]
let bsize: number = this.numOptions.length

this.options.forEach(opt => {
this.options.forEach((opt) => {
bsize += opt.getSize()
barr.push(opt.toBuffer())
})
Expand All @@ -138,4 +135,4 @@ export abstract class EssentialProposal {

return Buffer.concat(barr, bsize)
}
}
}
27 changes: 14 additions & 13 deletions src/apis/platformvm/addproposaltx/excludememberproposal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Buffer } from 'buffer/'
import BinTools from '../../../utils/bintools'
import { Serialization, SerializedEncoding } from '../../../utils/serialization'
import { Buffer } from "buffer/"
import BinTools from "../../../utils/bintools"
import { Serialization, SerializedEncoding } from "../../../utils/serialization"
import { PlatformVMConstants } from "../constants"
import { EssentialProposal } from "./essentialproposal"

Expand All @@ -10,18 +10,18 @@ const bintools = BinTools.getInstance()
export class ExcludeMemberProposal extends EssentialProposal {
private readonly _typeID = PlatformVMConstants.EXCLUDEMEMBERPORPOSAL_TYPE_ID

serialize(encoding: SerializedEncoding = 'hex'): object {
serialize(encoding: SerializedEncoding = "hex"): object {
return {
start: serialization.encoder(
this.start,
start: serialization.encoder(this.start, encoding, "Buffer", "number"),
end: serialization.encoder(this.end, encoding, "Buffer", "number"),
memberAddress: serialization.encoder(
this.memberAddress,
encoding,
"Buffer",
"number"
),
end: serialization.encoder(this.end, encoding, "Buffer", "number"),
memberAddress: serialization.encoder(this.memberAddress, encoding, "Buffer", "cb58"),
"cb58"
)
}
};
}

deserialize(fields: object, encoding: SerializedEncoding = "hex"): this {
this.start = serialization.decoder(
Expand Down Expand Up @@ -62,7 +62,8 @@ export class ExcludeMemberProposal extends EssentialProposal {
*/
toBuffer(): Buffer {
const barr: Buffer[] = [this.memberAddress, this.start, this.end]
const bsize = this.memberAddress.length + this.start.length + this.end.length
const bsize =
this.memberAddress.length + this.start.length + this.end.length
return Buffer.concat(barr, bsize)
}

Expand Down Expand Up @@ -101,4 +102,4 @@ export class ExcludeMemberProposal extends EssentialProposal {
getMemberAddress(): Buffer {
return this.memberAddress
}
}
}
Loading

0 comments on commit f063aa0

Please sign in to comment.