Skip to content
This repository was archived by the owner on Aug 2, 2022. It is now read-only.

Commit d1c37f4

Browse files
author
Bradley Hart
committed
Making adjustments to resource payer and read only following discussions
1 parent 9dafdfa commit d1c37f4

File tree

8 files changed

+55
-31
lines changed

8 files changed

+55
-31
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.DS_Store
22
.idea
3+
.vscode
34
dist/
45
dist-web/
56
node_modules/

docs/how-to-guides/20_how-to-set-a-payer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
After the release of v2.2 of nodeos, the transaction sponsorship feature is available to sponsor the resources for a transaction. To set a separate payer for the resources for a transaction, add a `resource_payer` object to your transaction that specifies the `payer`, `max_net_bytes`, `max_cpu_us`, and `max_memory_bytes`. This functionality requires the `RESOURCE_PAYER` feature to be enabled on the chain.
1+
After the release of v2.2 of nodeos, the resource payer feature is available to sponsor the resources for a transaction. To set a separate payer for the resources for a transaction, add a `resource_payer` object to your transaction that specifies the `payer`, `max_net_bytes`, `max_cpu_us`, and `max_memory_bytes`. This functionality requires the `RESOURCE_PAYER` protocol feature to be enabled on the chain.
22

33
A typical use-case for this feature has a service or application pay for the resources of a transaction instead of their users. Since authorization is required for both the user in the transaction and the payer, a possible workflow would have the transaction signed by the user's wallet application and then also signed by the service/application before sent to nodeos.
44

src/eosjs-api-interfaces.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ export interface SignatureProvider {
7474

7575
export interface ResourcePayer {
7676
payer: string;
77-
max_net_bytes: number|string;
78-
max_cpu_us: number|string;
79-
max_memory_bytes: number|string;
77+
max_net_bytes: number;
78+
max_cpu_us: number;
79+
max_memory_bytes: number;
8080
}
8181

8282
export interface Transaction {

src/eosjs-api.ts

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ export class Api {
250250
extensionBuffer.pushArray(ser.hexToUint8Array(extensionData[1]));
251251
const deserializedObj = types.get(transactionExtension.type).deserialize(extensionBuffer);
252252
if (extensionData[0] === 1) {
253+
deserializedObj.max_net_bytes = Number(deserializedObj.max_net_bytes);
254+
deserializedObj.max_cpu_us = Number(deserializedObj.max_cpu_us);
255+
deserializedObj.max_memory_bytes = Number(deserializedObj.max_memory_bytes);
253256
transaction.resource_payer = deserializedObj;
254257
}
255258
});
@@ -390,14 +393,18 @@ export class Api {
390393
});
391394
}
392395
if (broadcast) {
393-
let result;
394-
if (readOnlyTrx) {
395-
return this.rpc.push_ro_transaction(pushTransactionArgs, returnFailureTraces) as Promise<ReadOnlyTransactResult>;
396-
}
397396
if (compression) {
398-
return this.pushCompressedSignedTransaction(pushTransactionArgs) as Promise<TransactResult>;
397+
return this.pushCompressedSignedTransaction(
398+
pushTransactionArgs,
399+
readOnlyTrx,
400+
returnFailureTraces,
401+
) as Promise<TransactResult|ReadOnlyTransactResult>;
399402
}
400-
return this.pushSignedTransaction(pushTransactionArgs) as Promise<TransactResult>;
403+
return this.pushSignedTransaction(
404+
pushTransactionArgs,
405+
readOnlyTrx,
406+
returnFailureTraces,
407+
) as Promise<TransactResult|ReadOnlyTransactResult>;
401408
}
402409
return pushTransactionArgs as PushTransactionArgs;
403410
}
@@ -462,8 +469,17 @@ export class Api {
462469

463470
/** Broadcast a signed transaction */
464471
public async pushSignedTransaction(
465-
{ signatures, serializedTransaction, serializedContextFreeData }: PushTransactionArgs
466-
): Promise<TransactResult> {
472+
{ signatures, serializedTransaction, serializedContextFreeData }: PushTransactionArgs,
473+
readOnlyTrx = false,
474+
returnFailureTraces = false,
475+
): Promise<TransactResult|ReadOnlyTransactResult> {
476+
if (readOnlyTrx) {
477+
return this.rpc.push_ro_transaction({
478+
signatures,
479+
serializedTransaction,
480+
serializedContextFreeData,
481+
}, returnFailureTraces);
482+
}
467483
return this.rpc.push_transaction({
468484
signatures,
469485
serializedTransaction,
@@ -472,12 +488,22 @@ export class Api {
472488
}
473489

474490
public async pushCompressedSignedTransaction(
475-
{ signatures, serializedTransaction, serializedContextFreeData }: PushTransactionArgs
476-
): Promise<TransactResult> {
491+
{ signatures, serializedTransaction, serializedContextFreeData }: PushTransactionArgs,
492+
readOnlyTrx = false,
493+
returnFailureTraces = false,
494+
): Promise<TransactResult|ReadOnlyTransactResult> {
477495
const compressedSerializedTransaction = this.deflateSerializedArray(serializedTransaction);
478496
const compressedSerializedContextFreeData =
479497
this.deflateSerializedArray(serializedContextFreeData || new Uint8Array(0));
480498

499+
if (readOnlyTrx) {
500+
return this.rpc.push_ro_transaction({
501+
signatures,
502+
compression: 1,
503+
serializedTransaction: compressedSerializedTransaction,
504+
serializedContextFreeData: compressedSerializedContextFreeData
505+
}, returnFailureTraces);
506+
}
481507
return this.rpc.push_transaction({
482508
signatures,
483509
compression: 1,

src/tests/eosjs-api.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,18 +329,18 @@ describe('eosjs-api', () => {
329329
context_free_actions: [] as any,
330330
resource_payer: {
331331
payer: 'payer',
332-
max_net_bytes: '4096',
333-
max_cpu_us: '250',
334-
max_memory_bytes: '0'
332+
max_net_bytes: 4096,
333+
max_cpu_us: 250,
334+
max_memory_bytes: 0
335335
}
336336
};
337337
const serialized = [[1, '0000000080ABBCA90010000000000000FA000000000000000000000000000000']];
338338
const deserialized = {
339339
resource_payer: {
340340
payer: 'payer',
341-
max_net_bytes: '4096',
342-
max_cpu_us: '250',
343-
max_memory_bytes: '0'
341+
max_net_bytes: 4096,
342+
max_cpu_us: 250,
343+
max_memory_bytes: 0
344344
}
345345
};
346346

src/tests/node.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,17 +230,15 @@ const readOnlyQuery = async () => {
230230
const readOnlyFailureTrace = async () => {
231231
return await api.transact({
232232
actions: [{
233-
account: 'eosio.token',
234-
name: 'transfer',
233+
account: 'eosio',
234+
name: 'setpriv',
235235
authorization: [{
236-
actor: 'alice',
236+
actor: 'bob',
237237
permission: 'active',
238238
}],
239239
data: {
240-
from: 'alice',
241-
to: 'bob',
242-
quantity: '2000000.0000 SYS',
243-
memo: 'failureTrace',
240+
account: 'bob',
241+
is_priv: '1'
244242
},
245243
}]
246244
}, {

src/tests/node.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,10 @@ describe('Node JS environment', () => {
126126
try {
127127
await tests.readOnlyFailureTrace();
128128
} catch (e) {
129-
err = e.details;
129+
err = e;
130130
}
131-
expect(err.code).toEqual(3050003);
132-
expect(err.stack[0].data.s).toEqual('overdrawn balance');
131+
expect(err.details.code).toEqual(3090004);
132+
expect(err.details.stack[0].format).toEqual('missing authority of ${account}');
133133
});
134134

135135
it('throws appropriate error message without configuration object or TAPOS in place', async () => {

src/tests/web.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,6 @@
613613
try {
614614
await readOnlyFailureTrace();
615615
} catch (err) {
616-
console.log(err);
617616
if (err.details.code === 3050003 && err.details.stack[0].data.s === 'overdrawn balance') {
618617
resultsLabel.className = "success";
619618
resultsLabel.innerText = SUCCESS;

0 commit comments

Comments
 (0)