Skip to content

Commit c02bae6

Browse files
authored
Merge pull request #1611 from Phala-Network/sdk-adds-blocknum-in-query-response
SDK: Adds blocknum in query response
2 parents 1ec01d2 + e852671 commit c02bae6

File tree

9 files changed

+193
-50
lines changed

9 files changed

+193
-50
lines changed

frontend/packages/sdk/scripts/build_proto.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22
downloadProto() {
33
echo "Downloading $1.proto…"
4-
curl -o "proto/$1.proto" "https://raw.githubusercontent.com/Phala-Network/prpc-protos/master/$1.proto"
4+
curl -o "proto/$1.proto" "https://raw.githubusercontent.com/Phala-Network/phala-blockchain/master/crates/phactory/api/proto/$1.proto"
55
}
66

77
rm -rf proto

frontend/packages/sdk/src/actions/estimateContract.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export type EstimateContractParameters<T> = SendPinkQueryParameters<T> & {
2121

2222
export type EstimateContractResult = Omit<ContractCallOutcome, 'output'> & {
2323
request: SendPinkCommandParameters
24+
blockNum: number
2425
}
2526

2627
export async function estimateContract(
@@ -45,7 +46,7 @@ export async function estimateContract(
4546

4647
const cert = await provider.signCertificate()
4748

48-
const [clusterBalance, onchainBalance, inkResponse] = await Promise.all([
49+
const [clusterBalance, onchainBalance, [inkResponse, blockNum]] = await Promise.all([
4950
client.getClusterBalance(provider.address),
5051
client.api.query.system.account<FrameSystemAccountInfo>(provider.address),
5152
pinkQuery(client.phactory, argument, inkMessage.toHex(), cert),
@@ -95,5 +96,6 @@ export async function estimateContract(
9596
gasLimit,
9697
deposit: autoDeposit,
9798
},
99+
blockNum,
98100
}
99101
}

frontend/packages/sdk/src/actions/sendPinkQuery.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export type SendPinkQueryParameters<TArgs = any[]> = {
1919
export async function sendPinkQuery<TResult extends Codec = Codec>(
2020
client: OnChainRegistry,
2121
parameters: SendPinkQueryParameters
22-
): Promise<TResult | null> {
22+
): Promise<(TResult & { blockNum: number }) | null> {
2323
const { address, functionName, provider } = parameters
2424
if (!client.workerInfo?.pubkey) {
2525
throw new Error('Worker pubkey not found')
@@ -37,7 +37,7 @@ export async function sendPinkQuery<TResult extends Codec = Codec>(
3737
const argument = new WorkerAgreementKey(client.workerInfo.pubkey)
3838

3939
const cert = await provider.signCertificate()
40-
const inkResponse = await pinkQuery(client.phactory, argument, inkMessage.toHex(), cert)
40+
const [inkResponse, blockNum] = await pinkQuery(client.phactory, argument, inkMessage.toHex(), cert)
4141

4242
if (inkResponse.result.isErr) {
4343
// @FIXME: not sure this is enough as not yet tested
@@ -55,11 +55,13 @@ export async function sendPinkQuery<TResult extends Codec = Codec>(
5555
throw new Error(`ContractExecResult Error: ${result.asErr.toString()}`)
5656
}
5757
if (message.returnType) {
58-
return abi.registry.createTypeUnsafe<TResult>(
58+
const obj = abi.registry.createTypeUnsafe<TResult>(
5959
message.returnType.lookupName || message.returnType.type,
6060
[result.asOk.data.toU8a(true)],
6161
{ isPedantic: true }
62-
)
62+
) as TResult & { blockNum: number }
63+
obj.blockNum = blockNum
64+
return obj
6365
}
6466
return null
6567
}

frontend/packages/sdk/src/contracts/PinkBlueprint.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ interface MapMessageInkQuery<ApiType extends ApiTypes> {
4343

4444
interface PinkContractInstantiateResult extends ContractInstantiateResult {
4545
salt: string
46+
blockNum: number
4647
}
4748

4849
export interface PinkInstantiateQueryOptions {
@@ -326,7 +327,7 @@ export class PinkBlueprintPromise {
326327
options.deposit,
327328
options.transfer
328329
)
329-
const response = await pinkQuery(this.phatRegistry.phactory, agreement, payload.toHex(), cert)
330+
const [response, blockNum] = await pinkQuery(this.phatRegistry.phactory, agreement, payload.toHex(), cert)
330331
if (response.result.isErr) {
331332
return phalaTypes.createType<InkQueryError>('InkQueryError', response.result.asErr.toHex())
332333
}
@@ -345,6 +346,7 @@ export class PinkBlueprintPromise {
345346
throw new Error('Estimation failed: ' + JSON.stringify(result.result.asErr.toHuman()))
346347
}
347348

349+
;(result as PinkContractInstantiateResult).blockNum = blockNum
348350
return result
349351
}
350352

frontend/packages/sdk/src/contracts/PinkContract.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import signAndSend from '../utils/signAndSend'
2929

3030
export type PinkContractCallOutcome<ResultType> = {
3131
output: ResultType
32+
blocknum: number
3233
} & Omit<ContractCallOutcome, 'output'>
3334

3435
export interface ILooseResult<O, E extends Codec = Codec> extends IEnum {
@@ -469,7 +470,9 @@ export class PinkContractPromise<
469470

470471
const agreement = new WorkerAgreementKey(this.phatRegistry.remotePubkey!)
471472

472-
const inkQueryInternal = async (origin: string | AccountId | Uint8Array): Promise<ContractCallOutcome> => {
473+
const inkQueryInternal = async (
474+
origin: string | AccountId | Uint8Array
475+
): Promise<ContractCallOutcome & { blocknum: number }> => {
473476
if (typeof origin === 'string') {
474477
assert(origin === cert.address, 'origin must be the same as the certificate address')
475478
} else if (origin.hasOwnProperty('verify') && origin.hasOwnProperty('adddress')) {
@@ -485,7 +488,7 @@ export class PinkContractPromise<
485488
options.transfer,
486489
options.estimating !== undefined ? !!options.estimating : isEstimating
487490
)
488-
const inkResponse = await pinkQuery(this.phatRegistry.phactory, agreement, payload.toHex(), cert)
491+
const [inkResponse, blocknum] = await pinkQuery(this.phatRegistry.phactory, agreement, payload.toHex(), cert)
489492
if (inkResponse.result.isErr) {
490493
// @FIXME: not sure this is enough as not yet tested
491494
throw new Error(`InkResponse Error: ${inkResponse.result.asErr.toString()}`)
@@ -511,6 +514,7 @@ export class PinkContractPromise<
511514
)
512515
: null,
513516
result,
517+
blocknum,
514518
storageDeposit,
515519
}
516520
}

frontend/packages/sdk/src/contracts/PinkLoggerContract.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ function sidevmQueryWithReader({ phactory, remotePubkey, address, cert }: Sidevm
192192
return async function unsafeRunSidevmQuery<T>(sidevmMessage: Record<string, any>): Promise<T> {
193193
const encodedQuery = InkQuerySidevmMessage(address, sidevmMessage)
194194
const agreement = new WorkerAgreementKey(remotePubkey)
195-
const inkResponse = await pinkQuery(phactory, agreement, encodedQuery.toHex(), cert)
195+
const [inkResponse, _] = await pinkQuery(phactory, agreement, encodedQuery.toHex(), cert)
196196
if (inkResponse.result.isErr) {
197197
let error = `[${inkResponse.result.asErr.index}] ${inkResponse.result.asErr.type}`
198198
if (inkResponse.result.asErr.type === 'RuntimeError') {

frontend/packages/sdk/src/pruntime/pinkQuery.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export async function pinkQuery(
2828
agreement: WorkerAgreementKey,
2929
encodedQuery: string,
3030
{ certificate, pubkey, secret }: CertificateData
31-
): Promise<InkResponse> {
31+
): Promise<[InkResponse, number]> {
3232
// Encrypt the ContractQuery.
3333
const encryptedData = createEncryptedData(agreement.publicKey, encodedQuery, agreement.agreementKey)
3434
const encodedEncryptedData = phalaTypes.createType('EncryptedData', encryptedData).toU8a()
@@ -50,5 +50,7 @@ export async function pinkQuery(
5050

5151
const { data: encryptedResult, iv } = phalaTypes.createType<IEncryptedData>('EncryptedData', res.encodedEncryptedData)
5252
const data = hexAddPrefix(decrypt(encryptedResult.toString(), agreement.agreementKey, iv))
53-
return phalaTypes.createType<InkResponse>('InkResponse', data)
53+
const resp = phalaTypes.createType<InkResponse>('InkResponse', data)
54+
const blocknum = res.blocknum
55+
return [resp, blocknum]
5456
}

frontend/packages/sdk/src/pruntime/proto/index.d.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,9 @@ export namespace pruntime_rpc {
952952

953953
/** PhactoryInfo maxSupportedPinkRuntimeVersion */
954954
maxSupportedPinkRuntimeVersion?: (string|null);
955+
956+
/** PhactoryInfo supportedAttestationMethods */
957+
supportedAttestationMethods?: (string[]|null);
955958
}
956959

957960
/** Represents a PhactoryInfo. */
@@ -1029,6 +1032,9 @@ export namespace pruntime_rpc {
10291032
/** PhactoryInfo maxSupportedPinkRuntimeVersion. */
10301033
public maxSupportedPinkRuntimeVersion: string;
10311034

1035+
/** PhactoryInfo supportedAttestationMethods. */
1036+
public supportedAttestationMethods: string[];
1037+
10321038
/** PhactoryInfo _genesisBlockHash. */
10331039
public _genesisBlockHash?: "genesisBlockHash";
10341040

@@ -2277,6 +2283,9 @@ export namespace pruntime_rpc {
22772283

22782284
/** ContractQueryResponse encodedEncryptedData */
22792285
encodedEncryptedData?: (Uint8Array|null);
2286+
2287+
/** ContractQueryResponse blocknum */
2288+
blocknum?: (number|null);
22802289
}
22812290

22822291
/** Represents a ContractQueryResponse. */
@@ -2291,6 +2300,9 @@ export namespace pruntime_rpc {
22912300
/** ContractQueryResponse encodedEncryptedData. */
22922301
public encodedEncryptedData: Uint8Array;
22932302

2303+
/** ContractQueryResponse blocknum. */
2304+
public blocknum: number;
2305+
22942306
/**
22952307
* Creates a new ContractQueryResponse instance using the specified properties.
22962308
* @param [properties] Properties to set
@@ -3114,8 +3126,14 @@ export namespace pruntime_rpc {
31143126
/** ClusterInfo stateRoot */
31153127
stateRoot?: (string|null);
31163128

3117-
/** ClusterInfo contracts */
3118-
contracts?: (string[]|null);
3129+
/** ClusterInfo systemContract */
3130+
systemContract?: (string|null);
3131+
3132+
/** ClusterInfo loggerContract */
3133+
loggerContract?: (string|null);
3134+
3135+
/** ClusterInfo numberOfContracts */
3136+
numberOfContracts?: (number|Long|null);
31193137
}
31203138

31213139
/** Represents a ClusterInfo. */
@@ -3136,8 +3154,14 @@ export namespace pruntime_rpc {
31363154
/** ClusterInfo stateRoot. */
31373155
public stateRoot: string;
31383156

3139-
/** ClusterInfo contracts. */
3140-
public contracts: string[];
3157+
/** ClusterInfo systemContract. */
3158+
public systemContract: string;
3159+
3160+
/** ClusterInfo loggerContract. */
3161+
public loggerContract: string;
3162+
3163+
/** ClusterInfo numberOfContracts. */
3164+
public numberOfContracts: (number|Long);
31413165

31423166
/**
31433167
* Creates a new ClusterInfo instance using the specified properties.

0 commit comments

Comments
 (0)