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

Adding testing for Action return values #778

Merged
merged 3 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/eosjs-ci/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
### Current Version: v0.2.2
### Test:
#### docker build --tag eosjs-ci:test ./.github/eosjs-ci
#### docker run --publish 8888:8888 eosjs-ci:test
### Deploy:
#### docker build --tag eosio/eosjs-ci:v0.2.2 ./.github/eosjs-ci
#### docker push eosio/eosjs-ci:v0.2.2

FROM ubuntu:18.04
ENTRYPOINT ["nodeos", "--data-dir", "/root/.local/share", "-e", "-p", "eosio", "--replay-blockchain", "--plugin", "eosio::producer_plugin", "--plugin", "eosio::chain_api_plugin", "--plugin", "eosio::http_plugin", "--http-server-address=0.0.0.0:8888", "--access-control-allow-origin=*", "--contracts-console", "--http-validate-host=false", "--verbose-http-errors", "--max-transaction-time=100"]

Expand Down
5 changes: 5 additions & 0 deletions .github/eosjs-ci/scripts/deploy_contracts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,16 @@ setabi eosio $CONTRACTS_DIR/system/system.abi
sleep 1s
cleos wallet unlock --password $(cat "$CONFIG_DIR"/keys/default_wallet_password.txt) || true
create_account eosio.token $SYSTEM_ACCOUNT_PUBLIC_KEY $SYSTEM_ACCOUNT_PRIVATE_KEY
create_account returnvalue $SYSTEM_ACCOUNT_PUBLIC_KEY $SYSTEM_ACCOUNT_PRIVATE_KEY
create_account bob $EXAMPLE_ACCOUNT_PUBLIC_KEY $EXAMPLE_ACCOUNT_PRIVATE_KEY
create_account alice $EXAMPLE_ACCOUNT_PUBLIC_KEY $EXAMPLE_ACCOUNT_PRIVATE_KEY
create_account bobr1 $R1_EXAMPLE_ACCOUNT_PUBLIC_KEY $R1_EXAMPLE_ACCOUNT_PRIVATE_KEY
create_account alicer1 $R1_EXAMPLE_ACCOUNT_PUBLIC_KEY $R1_EXAMPLE_ACCOUNT_PRIVATE_KEY

sleep 1s
cleos set abi returnvalue $CONTRACTS_DIR/action_results/action_results.abi -p returnvalue@active -p eosio@active
cleos set code returnvalue $CONTRACTS_DIR/action_results/action_results.wasm -p returnvalue@active -p eosio@active

sleep 1s
cleos set abi eosio.token $CONTRACTS_DIR/token/token.abi -p eosio.token@active -p eosio@active
cleos set code eosio.token $CONTRACTS_DIR/token/token.wasm -p eosio.token@active -p eosio@active
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
git push origin ${GITHUB_REF#refs/*/}
services:
nodeos:
image: eosio/eosjs-ci:v0.2.1
image: eosio/eosjs-ci:v0.2.2

ports:
- 8888:8888
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish-edge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:

services:
nodeos:
image: eosio/eosjs-ci:v0.2.1
image: eosio/eosjs-ci:v0.2.2

ports:
- 8888:8888
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:

services:
nodeos:
image: eosio/eosjs-ci:v0.2.1
image: eosio/eosjs-ci:v0.2.2

ports:
- 8888:8888
Expand Down Expand Up @@ -117,7 +117,7 @@ jobs:

services:
nodeos:
image: eosio/eosjs-ci:v0.2.1
image: eosio/eosjs-ci:v0.2.2

ports:
- 8888:8888
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ dist-web/
node_modules/
docs-build/
*.tgz
.github/**/*.wasm
.github/**/*.abi

#cypress artifacts
cypress/screenshots/
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@
"devDependencies": {
"@blockone/eslint-config-blockone": "^3.0.0",
"@types/elliptic": "^6.4.12",
"@types/jest": "^26.0.4",
"@types/node": "^14.0.23",
"@types/jest": "^26.0.6",
"@types/node": "^14.0.24",
"@types/pako": "^1.0.1",
"cypress": "^4.10.0",
"cypress": "^4.11.0",
"eosjs-ecc": "^4.0.7",
"eslint": "^6.8.0",
"jest": "^26.1.0",
"jest-fetch-mock": "^3.0.3",
"ts-jest": "^26.1.2",
"ts-jest": "^26.1.3",
"ts-loader": "^7.0.5",
"typescript": "^3.9.6",
"typescript": "^3.9.7",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.12"
},
Expand Down
Binary file added src/tests/action_results_abi.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion src/tests/eosjs-api.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { TextEncoder, TextDecoder } = require('util');
import { Api, TransactionBuilder, ActionBuilder } from '../eosjs-api';
import { Api } from '../eosjs-api';
import { JsonRpc } from '../eosjs-jsonrpc';
import { JsSignatureProvider } from '../eosjs-jssig';
import { WasmAbiProvider, WasmAbi } from '../eosjs-wasmabi';
Expand Down
19 changes: 19 additions & 0 deletions src/tests/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ const setWasmAbi = async () => {
textEncoder: api.textEncoder,
textDecoder: api.textDecoder,
print: (x) => { process.stdout.write(x); },
}),
new WasmAbi({
account: 'returnvalue',
mod: new global.WebAssembly.Module(fs.readFileSync(path.join(__dirname + '/action_results_abi.wasm'))),
memoryThreshold: 32000,
textEncoder: api.textEncoder,
textDecoder: api.textDecoder,
print: (x) => { process.stdout.write(x); },
})
]);
};
Expand Down Expand Up @@ -126,6 +134,16 @@ const transactWithShorthandTxWasm = async () => {
});
};

const transactWithReturnValue = async () => {
await setWasmAbi();
const tx = api.buildTransaction();
tx.with('returnvalue').as('bob').actionresret();
return await tx.send({
blocksBehind: 3,
expireSeconds: 30
});
};

const broadcastResult = async (signaturesAndPackedTransaction) => await api.pushSignedTransaction(signaturesAndPackedTransaction);

const transactShouldFail = async () => await api.transact({
Expand Down Expand Up @@ -156,5 +174,6 @@ module.exports = {
transactWithShorthandApiWasm,
transactWithShorthandTxJson,
transactWithShorthandTxWasm,
transactWithReturnValue,
rpcShouldFail
};
6 changes: 6 additions & 0 deletions src/tests/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ describe('Node JS environment', () => {
expect(Object.keys(transactionResponse)).toContain('transaction_id');
});

it('confirms an action\'s return value can be verified', async () => {
const expectedValue = 10;
transactionResponse = await tests.transactWithReturnValue();
expect(transactionResponse.processed.action_traces[0].return_value).toEqual(expectedValue);
});

it('throws appropriate error message without configuration object or TAPOS in place', async () => {
try {
failedAsPlanned = true;
Expand Down
Binary file modified src/tests/token_abi.wasm
Binary file not shown.
64 changes: 58 additions & 6 deletions src/tests/web.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,40 @@
const signatureProvider = new eosjs_jssig.JsSignatureProvider([privateKey, r1PrivateKey]);
const wasmAbiProvider = new eosjs_wasmabi.WasmAbiProvider();
const api = new eosjs_api.Api({ rpc, signatureProvider, wasmAbiProvider });
let module;
let tokenModule;
let rvModule;

const setWasmAbi = async () => {
if (!module) {
const response = await fetch('token_abi.wasm');
const buffer = await response.arrayBuffer();
module = await WebAssembly.compile(buffer);
let response;
let buffer;
if (!tokenModule) {
response = await fetch('token_abi.wasm');
buffer = await response.arrayBuffer();
tokenModule = await WebAssembly.compile(buffer);
}

if (!rvModule) {
response = await fetch('action_results_abi.wasm');
buffer = await response.arrayBuffer();
rvModule = await WebAssembly.compile(buffer);
}

await api.wasmAbiProvider.setWasmAbis([
new eosjs_wasmabi.WasmAbi({
account: 'eosio.token',
mod: module,
mod: tokenModule,
memoryThreshold: 32000,
textEncoder: new TextEncoder(),
textDecoder: new TextDecoder('utf-8', { fatal: true }),
print: (x) => console.info(x),
}),
new eosjs_wasmabi.WasmAbi({
account: 'returnvalue',
mod: rvModule,
memoryThreshold: 32000,
textEncoder: new TextEncoder,
textDecoder: new TextDecoder('utf-8', { fatal: true }),
print: (x) => console.info(x),
})
]);
};
Expand Down Expand Up @@ -397,6 +414,40 @@
resultsLabel.innerText = FAILED;
return false;
};

const returnValueTx = async () => {
await setWasmAbi();
const tx = api.buildTransaction();
tx.with('returnvalue').as('bob').actionresret();
return tx.send({
blocksBehind: 3,
expireSeconds: 30
});
}

const testWithReturnValueTx = async (e) => {
const expectedValue = 10;
resultsLabel = e.target;
resultsLabel.innerText = EXECUTING;

try {
transactionResponse = await returnValueTx();
} catch (error) {
resultsLabel.className = 'failed';
resultsLabel.innerText = FAILED;
console.error('Transact Return Values Test Failure: ', error.message);
return false;
}

if (transactionResponse.processed.action_traces[0].return_value === expectedValue) {
resultsLabel.className = "success";
resultsLabel.innerText = SUCCESS;
return true;
}
resultsLabel.className = 'failed';
resultsLabel.innerText = FAILED;
return false;
}

const transactShouldFail = async () => await api.transact({
actions: [{
Expand Down Expand Up @@ -484,6 +535,7 @@ <h1>Web Build Integration Tests</h1>
<div><h2>Transact with .with() Using Api and Wasm Abi</h2><button onClick='testShorthandWithApiWasm(event);'>Test</button></div>
<div><h2>Transact with .with() Using Tx and Wasm Abi</h2><button onClick='testShorthandWithTxWasm(event);'>Test</button></div>
<div><h2>Transact elliptic p256/KeyType.r1 Keys and Signatures</h2><button onClick='testWithP256EllipticCurve(event);'>Test</button></div>
<div><h2>Transact Return Values</h2><button onClick='testWithReturnValueTx(event);'>Test</button></div>
<div><h2>Invalid Transaction Throws Error</h2><button onClick='testTransactShouldFail(event);'>Test</button></div>
<div><h2>Invalid Rpc Call Throws Rpc Error</h2><button onClick='testRpcShouldFail(event);'>Test</button></div>
</div>
Expand Down
Loading