Skip to content

Commit

Permalink
Fix ccd-js-gen for U64 and I64
Browse files Browse the repository at this point in the history
  • Loading branch information
limemloh committed Sep 22, 2023
1 parent b3887e3 commit a4c6bce
Show file tree
Hide file tree
Showing 19 changed files with 97 additions and 62 deletions.
2 changes: 1 addition & 1 deletion examples/cis2/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ const client = createConcordiumClient(
const txHash = await contract.transfer(
{
senderAddress: from,
energy: Energy.create(10000n),
energy: Energy.create(10000),
},
{
from,
Expand Down
2 changes: 1 addition & 1 deletion examples/cis2/updateOperator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const client = createConcordiumClient(
const txHash = await contract.updateOperator(
{
senderAddress: owner,
energy: Energy.create(10000n),
energy: Energy.create(10000),
},
{
type: cli.flags.updateType as 'add' | 'remove',
Expand Down
2 changes: 1 addition & 1 deletion examples/cis4/registerCredential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ const signer = buildAccountSigner(wallet);
signer,
{
senderAddress: AccountAddress.fromBase58(wallet.value.address),
energy: Energy.create(10000n),
energy: Energy.create(10000),
},
credential,
cli.flags.data
Expand Down
2 changes: 1 addition & 1 deletion examples/cis4/registerRevocationKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const signer = buildAccountSigner(wallet);
signer,
{
senderAddress: AccountAddress.fromBase58(wallet.value.address),
energy: Energy.create(10000n),
energy: Energy.create(10000),
},
keys,
cli.flags.data
Expand Down
2 changes: 1 addition & 1 deletion examples/cis4/removeRevocationKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const signer = buildAccountSigner(wallet);
signer,
{
senderAddress: AccountAddress.fromBase58(wallet.value.address),
energy: Energy.create(10000n),
energy: Energy.create(10000),
},
cli.flags.keys,
cli.flags.data
Expand Down
2 changes: 1 addition & 1 deletion examples/cis4/revokeCredentialAsHolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const signer = buildAccountSigner(wallet);
signer,
{
senderAddress: AccountAddress.fromBase58(wallet.value.address),
energy: Energy.create(10000n),
energy: Energy.create(10000),
},
hSigner,
nonce,
Expand Down
2 changes: 1 addition & 1 deletion examples/cis4/revokeCredentialAsIssuer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const signer = buildAccountSigner(wallet);
signer,
{
senderAddress: AccountAddress.fromBase58(wallet.value.address),
energy: Energy.create(10000n),
energy: Energy.create(10000),
},
cli.flags.credId,
cli.flags.reason,
Expand Down
2 changes: 1 addition & 1 deletion examples/cis4/revokeCredentialAsOther.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const signer = buildAccountSigner(wallet);
signer,
{
senderAddress: AccountAddress.fromBase58(wallet.value.address),
energy: Energy.create(10000n),
energy: Energy.create(10000),
},
rSigner,
cli.flags.credId,
Expand Down
8 changes: 7 additions & 1 deletion packages/ccd-js-gen/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -843,12 +843,18 @@ function schemaToTypeAndMapper(
};
case 'U64':
case 'I64':
return {
type: 'number | bigint',
mapper(id) {
return `BigInt(${id})`;
},
};
case 'U128':
case 'I128':
return {
type: 'number | bigint',
mapper(id) {
return `BigInt(${id}).toString()`; // TODO: check that the schema JSON actually use a string here.
return `BigInt(${id}).toString()`;
},
};
case 'Amount':
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/accountTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class SimpleTransferHandler
}

serialize(transfer: SimpleTransferPayload): Buffer {
const serializedToAddress = transfer.toAddress.decodedAddress;
const serializedToAddress = AccountAddress.toBuffer(transfer.toAddress);
const serializedAmount = encodeWord64(transfer.amount.microCcdAmount);
return Buffer.concat([serializedToAddress, serializedAmount]);
}
Expand All @@ -71,7 +71,7 @@ export class SimpleTransferWithMemoHandler
implements AccountTransactionHandler<SimpleTransferWithMemoPayload>
{
serialize(transfer: SimpleTransferWithMemoPayload): Buffer {
const serializedToAddress = transfer.toAddress.decodedAddress;
const serializedToAddress = AccountAddress.toBuffer(transfer.toAddress);
const serializedMemo = encodeDataBlob(transfer.memo);
const serializedAmount = encodeWord64(transfer.amount.microCcdAmount);
return Buffer.concat([
Expand Down
8 changes: 4 additions & 4 deletions packages/common/src/deserialization.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Buffer } from 'buffer/index.js';
import { getAccountTransactionHandler } from './accountTransactions.js';
import { Cursor } from './deserializationHelpers.js';
import {
Expand All @@ -8,6 +7,7 @@ import {
isAccountTransactionType,
} from './types.js';
import * as AccountAddress from './types/AccountAddress.js';
import * as AccountSequenceNumber from './types/SequenceNumber.js';
import { TransactionExpiry } from './types/transactionExpiry.js';

/**
Expand Down Expand Up @@ -61,10 +61,10 @@ function deserializeAccountTransactionSignature(
function deserializeTransactionHeader(
serializedHeader: Cursor
): AccountTransactionHeader {
const sender = AccountAddress.fromBuffer(
Buffer.from(serializedHeader.read(32))
const sender = AccountAddress.fromBuffer(serializedHeader.read(32));
const nonce = AccountSequenceNumber.create(
serializedHeader.read(8).readBigUInt64BE(0)
);
const nonce = serializedHeader.read(8).readBigUInt64BE(0);
// TODO: extract payloadSize and energyAmount?
// energyAmount
serializedHeader.read(8).readBigUInt64BE(0);
Expand Down
11 changes: 6 additions & 5 deletions packages/common/src/energyCost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ChainParameters,
Ratio,
} from './types.js';
import * as Energy from './types/Energy.js';

/**
* These constants must be consistent with constA and constB in:
Expand All @@ -31,11 +32,11 @@ export function calculateEnergyCost(
signatureCount: bigint,
payloadSize: bigint,
transactionSpecificCost: bigint
): bigint {
return (
): Energy.Type {
return Energy.create(
constantA * signatureCount +
constantB * (accountTransactionHeaderSize + payloadSize) +
transactionSpecificCost
constantB * (accountTransactionHeaderSize + payloadSize) +
transactionSpecificCost
);
}

Expand All @@ -48,7 +49,7 @@ export function getEnergyCost(
transactionType: AccountTransactionType,
payload: AccountTransactionPayload,
signatureCount = 1n
): bigint {
): Energy.Type {
const handler = getAccountTransactionHandler(transactionType);
const size = handler.serialize(payload).length;
return calculateEnergyCost(
Expand Down
9 changes: 5 additions & 4 deletions packages/common/src/grpc/GRPCClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import * as TransactionHash from '../types/TransactionHash.js';
import * as ContractAddress from '../types/ContractAddress.js';
import * as Parameter from '../types/Parameter.js';
import * as Energy from '../types/Energy.js';
import * as SequenceNumber from '../types/SequenceNumber.js';

/**
* @hidden
Expand Down Expand Up @@ -364,7 +365,7 @@ export class ConcordiumGRPCClient {
*/
async sendRawAccountTransaction(
header: v1.AccountTransactionHeader,
energyAmount: bigint,
energyAmount: Energy.Type,
payload: Uint8Array,
signature: v1.AccountTransactionSignature
): Promise<TransactionHash.Type> {
Expand All @@ -373,9 +374,9 @@ export class ConcordiumGRPCClient {

// Put together sendBlockItemRequest
const convertedHeader: v2.AccountTransactionHeader = {
sender: { value: header.sender.decodedAddress },
sequenceNumber: { value: header.nonce },
energyAmount: { value: energyAmount },
sender: AccountAddress.toProto(header.sender),
sequenceNumber: SequenceNumber.toProto(header.nonce),
energyAmount: Energy.toProto(energyAmount),
expiry: { value: header.expiry.expiryEpochSeconds },
};
const accountTransaction: v2.AccountTransaction = {
Expand Down
5 changes: 3 additions & 2 deletions packages/common/src/grpc/translation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import * as ContractAddress from '../types/ContractAddress.js';
import * as Energy from '../types/Energy.js';
import * as Duration from '../types/Duration.js';
import * as Timestamp from '../types/Timestamp.js';
import * as SequenceNumber from '../types/SequenceNumber.js';

function unwrapToHex(bytes: Uint8Array | undefined): v1.HexString {
return Buffer.from(unwrap(bytes)).toString('hex');
Expand Down Expand Up @@ -374,7 +375,7 @@ export function accountInfo(acc: v2.AccountInfo): v1.AccountInfo {
};
const accInfoCommon: v1.AccountInfoSimple = {
accountAddress: AccountAddress.fromProto(unwrap(acc.address)),
accountNonce: unwrap(acc.sequenceNumber?.value),
accountNonce: SequenceNumber.fromProto(unwrap(acc.sequenceNumber)),
accountAmount: unwrap(acc.amount?.value),
accountIndex: unwrap(acc.index?.value),
accountThreshold: unwrap(acc.threshold?.value),
Expand Down Expand Up @@ -403,7 +404,7 @@ export function nextAccountSequenceNumber(
nasn: v2.NextAccountSequenceNumber
): v1.NextAccountNonce {
return {
nonce: unwrap(nasn.sequenceNumber?.value),
nonce: SequenceNumber.fromProto(unwrap(nasn.sequenceNumber)),
allFinal: nasn.allFinal,
};
}
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/pub/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export {

import * as ModuleClient from '../types/ModuleClient.js';
import * as Parameter from '../types/Parameter.js';
import * as AccountSequenceNumber from '../types/AccountSequenceNumber.js';
import * as SequenceNumber from '../types/SequenceNumber.js';
import * as Energy from '../types/Energy.js';
import * as TransactionHash from '../types/TransactionHash.js';
import * as BlockHash from '../types/BlockHash.js';
Expand All @@ -69,7 +69,7 @@ import * as Timestamp from '../types/Timestamp.js';
export {
ModuleClient,
Parameter,
AccountSequenceNumber,
SequenceNumber,
Energy,
TransactionHash,
BlockHash,
Expand Down
9 changes: 5 additions & 4 deletions packages/common/src/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
import { calculateEnergyCost } from './energyCost.js';
import { countSignatures } from './util.js';
import * as AccountAddress from './types/AccountAddress.js';
import * as Energy from './types/Energy.js';
import { sha256 } from './hash.js';

function serializeAccountTransactionType(type: AccountTransactionType): Buffer {
Expand All @@ -46,11 +47,11 @@ function serializeAccountTransactionType(type: AccountTransactionType): Buffer {
function serializeAccountTransactionHeader(
header: AccountTransactionHeader,
payloadSize: number,
energyAmount: bigint
energyAmount: Energy.Type
) {
const serializedSender = header.sender.decodedAddress;
const serializedNonce = encodeWord64(header.nonce);
const serializedEnergyAmount = encodeWord64(energyAmount);
const serializedSender = AccountAddress.toBuffer(header.sender);
const serializedNonce = encodeWord64(header.nonce.value);
const serializedEnergyAmount = encodeWord64(energyAmount.value);
const serializedPayloadSize = encodeWord32(payloadSize);
const serializedExpiry = encodeWord64(header.expiry.expiryEpochSeconds);
return Buffer.concat([
Expand Down
7 changes: 4 additions & 3 deletions packages/common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as Parameter from './types/Parameter.js';
import type * as InitName from './types/InitName.js';
import type * as ContractName from './types/ContractName.js';
import type * as ReceiveName from './types/ReceiveName.js';
import type * as AccountSequenceNumber from './types/SequenceNumber.js';
import { CcdAmount } from './types/ccdAmount.js';
import { DataBlob } from './types/DataBlob.js';
import { TransactionExpiry } from './types/transactionExpiry.js';
Expand Down Expand Up @@ -948,7 +949,7 @@ export interface CryptographicParameters {
}

export interface NextAccountNonce {
nonce: bigint;
nonce: AccountSequenceNumber.Type;
allFinal: boolean;
}

Expand Down Expand Up @@ -1349,7 +1350,7 @@ export type AccountCredential = Versioned<

interface AccountInfoCommon {
accountAddress: AccountAddress.Type;
accountNonce: bigint;
accountNonce: AccountSequenceNumber.Type;
accountAmount: bigint;
accountIndex: bigint;

Expand Down Expand Up @@ -1593,7 +1594,7 @@ export interface AccountTransactionHeader {
* the nonce for the transaction, usually acquired by
* getting the next account nonce from the node
*/
nonce: bigint;
nonce: AccountSequenceNumber.Type;

/** expiration of the transaction */
expiry: TransactionExpiry;
Expand Down
27 changes: 0 additions & 27 deletions packages/common/src/types/AccountSequenceNumber.ts

This file was deleted.

51 changes: 51 additions & 0 deletions packages/common/src/types/SequenceNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type * as Proto from '../grpc-api/v2/concordium/types.js';

/** Transaction sequence number. (Formerly refered as Nonce) */
class SequenceNumber {
/** Having a private field prevents similar structured objects to be considered the same type (similar to nominal typing). */
private __nominal = true;
constructor(
/** Internal value representing the sequence number. */
public readonly value: bigint
) {}
}

/** A transaction sequence number. (Formerly refered as Nonce) */
export type Type = SequenceNumber;

/**
* Construct an SequenceNumber type.
* @param {bigint | number} sequenceNumber The account sequence number.
* @throws If `sequenceNumber` is not at least 1.
* @returns {SequenceNumber}
*/
export function create(sequenceNumber: bigint | number): SequenceNumber {
if (sequenceNumber < 1) {
throw new Error(
'Invalid account sequence number: Must be 1 or higher.'
);
}
return new SequenceNumber(BigInt(sequenceNumber));
}

/**
* Convert a SequenceNumber from its protobuf encoding.
* @param {Proto.SequenceNumber} sequenceNumber The sequence number in protobuf.
* @returns {SequenceNumber} The sequence number.
*/
export function fromProto(
sequenceNumber: Proto.SequenceNumber
): SequenceNumber {
return create(sequenceNumber.value);
}

/**
* Convert a sequence number into its protobuf encoding.
* @param {SequenceNumber} sequenceNumber The duration.
* @returns {Proto.SequenceNumber} The protobuf encoding.
*/
export function toProto(sequenceNumber: SequenceNumber): Proto.SequenceNumber {
return {
value: sequenceNumber.value,
};
}

0 comments on commit a4c6bce

Please sign in to comment.