Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ppe/uncommitted state #311

Merged
merged 5 commits into from
Jan 23, 2023
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
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- name: Install modules
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"@idena/vrf-js": "^1.0.1",
"archiver": "^5.3.0",
"arweave": "1.11.8",
"async-mutex": "^0.4.0",
"elliptic": "^6.5.4",
"events": "3.3.0",
"fast-copy": "^3.0.0",
Expand Down
4 changes: 4 additions & 0 deletions src/__tests__/integration/data/staking/erc-20.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ export function handle(state, action) {
const recipient = _input.recipient;
const amount = _input.amount;

if (amount == 0 ) {
throw new ContractError('TransferFromZero');
Copy link
Contributor

@rpiszczatowski rpiszczatowski Jan 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is amount the sender balance (transfer-FROM-zero suggests that)? Shouldn't we check whether amount <= 0?

Copy link
Contributor Author

@ppedziwiatr ppedziwiatr Jan 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a real contract - yes.

This contract is a simplified, stripped-down version (I believe there's a warn in the header -

* This is an example token contract that mimics the
) used only for testing certain features (like this specific case, where a transfer from zero was an indication of some issue in the internal writes feature in the whole test scenario)

}

const currentAllowance = _allowances[sender][_msgSender];

if (currentAllowance === undefined || currentAllowance < amount) {
Expand Down
41 changes: 41 additions & 0 deletions src/__tests__/integration/data/thethar/simple-thethar-contract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
(() => {
// src/thetAR/actions/read/userOrder.ts
var create = async (state, action) => {
const param = action.input.params;

const tokenState = await SmartWeave.contracts.readContractState(state.token);
//let orderQuantity = param.price;
let orderQuantity = tokenState.allowances[action.caller][SmartWeave.contract.id];
logger.error(" CREATE Taking tokens: " + orderQuantity);
await SmartWeave.contracts.write(state.token, { function: "transferFrom", sender: action.caller, recipient: SmartWeave.contract.id, amount: orderQuantity });
state.orders.push(orderQuantity);

//await SmartWeave.contracts.readContractState(state.token);
return { state };
};

var cancel = async (state, action) => {
const param = action.input.params;

let orderQuantity = state.orders[param.orderId];
logger.error("CANCEL Returning tokens: " + orderQuantity);
await SmartWeave.contracts.write(state.token, { function: "transfer", to: action.caller, amount: orderQuantity });

state.orders.splice(param.orderId, 1);
return { state };
};

// src/thetAR/contract.ts
async function handle(state, action) {
const func = action.input.function;
switch (func) {
case "create":
return await create(state, action);
case "cancel":
return await cancel(state, action);
default:
throw new ContractError(`No function supplied or function not recognised: "${func}"`);
}
}
})();

Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ describe('Testing internal writes', () => {
expect((await contractB.readState()).cachedValue.state.counter).toEqual(2060);
});

xit('should properly evaluate state with a new client', async () => {
it('should properly evaluate state with a new client', async () => {
const contractA2 = WarpFactory.forLocal(port)
.contract<any>(contractATxId)
.setEvaluationOptions({ internalWrites: true })
Expand Down Expand Up @@ -279,7 +279,7 @@ describe('Testing internal writes', () => {
expect((await contractB.readState()).cachedValue.state.counter).toEqual(2060);
});

xit('should properly evaluate state with a new client', async () => {
it('should properly evaluate state with a new client', async () => {
rpiszczatowski marked this conversation as resolved.
Show resolved Hide resolved
const contractA2 = WarpFactory.forLocal(port)
.contract<any>(contractATxId)
.setEvaluationOptions({ internalWrites: true })
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import fs from 'fs';
import ArLocal from 'arlocal';
import path from 'path';
import {mineBlock} from '../_helpers';
import {WarpFactory} from '../../../core/WarpFactory';
import {LoggerFactory} from '../../../logging/LoggerFactory';

const PORT = 1970;

var simpleThetharTxId;
var arlocal, arweave, warp, walletJwk;
var erc20Contract, simpleThetarContract;

describe('flow with broken behaviour', () => {

beforeAll(async () => {
// note: each tests suit (i.e. file with tests that Jest is running concurrently
// with another files has to have ArLocal set to a different port!)
arlocal = new ArLocal(PORT, false);
await arlocal.start();
LoggerFactory.INST.logLevel('error');

warp = WarpFactory.forLocal(PORT);
({jwk: walletJwk} = await warp.generateWallet());
arweave = warp.arweave;
});

afterAll(async () => {
await arlocal.stop();
});

const deployJS = async () => {
const walletAddress = await arweave.wallets.jwkToAddress(walletJwk);

// deploy TAR pst
const erc20Src = fs.readFileSync(path.join(__dirname, '../data/staking/erc-20.js'), 'utf8');

const tarInit = {
symbol: 'TAR',
name: 'ThetAR exchange token',
decimals: 2,
totalSupply: 20000,
balances: {
[walletAddress]: 10000,
},
allowances: {},
settings: null,
owner: walletAddress,
canEvolve: true,
evolve: '',
};

const erc20TxId = (await warp.createContract.deploy({
wallet: walletJwk,
initState: JSON.stringify(tarInit),
src: erc20Src,
})).contractTxId;
erc20Contract = warp.contract(erc20TxId);
erc20Contract.setEvaluationOptions({
internalWrites: true,
allowUnsafeClient: true,
allowBigInt: true,
}).connect(walletJwk);

// deploy thetAR contract
const simpleThetharSrc = fs.readFileSync(path.join(__dirname, '../data/thethar/simple-thethar-contract.js'), 'utf8');
const contractInit = {
token: erc20TxId,
orders: []
};

simpleThetharTxId = (await warp.createContract.deploy({
wallet: walletJwk,
initState: JSON.stringify(contractInit),
src: simpleThetharSrc,
})).contractTxId;
simpleThetarContract = warp.contract(simpleThetharTxId);
simpleThetarContract.setEvaluationOptions({
internalWrites: true,
allowUnsafeClient: true,
allowBigInt: true,
}).connect(walletJwk);
};

const create = async (quantity) => {
await erc20Contract.writeInteraction({
function: 'approve',
spender: simpleThetharTxId,
amount: quantity
});

await mineBlock(warp);

const txId = (await simpleThetarContract.writeInteraction({
function: 'create'
})).originalTxId;

await mineBlock(warp);

console.log('AFTER: ', JSON.stringify(await simpleThetarContract.readState()));
}

const cancel = async (orderId) => {
console.log('cancel order...');

const txId = await simpleThetarContract.writeInteraction({
function: 'cancel',
params: {
orderId: orderId
}
});
await mineBlock(warp);

console.log('AFTER: ', JSON.stringify(await simpleThetarContract.readState()));
}


const readFull = async () => {
const warp = WarpFactory.forLocal(PORT);

let contract = warp.contract(simpleThetharTxId);
contract.setEvaluationOptions({
internalWrites: true,
allowUnsafeClient: true,
allowBigInt: true
}).connect(walletJwk);

const result = await contract.readState();

console.log('Contract: ', JSON.stringify(result, null, " "));

return result;
}

it('correctly evaluate deferred state', async () => {
await deployJS();
await create(1);
await cancel(0);

console.error("========= READ FULL ==========")
const result = await readFull();
expect(result.cachedValue.state.orders.length).toEqual(0);

const errorMessages = result.cachedValue.errorMessages;
for (let errorMessageKey in errorMessages) {
expect(errorMessages[errorMessageKey]).not.toContain('TransferFromZero');
}
});

});
20 changes: 16 additions & 4 deletions src/__tests__/regression/read-state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ describe.each(chunked)('v1 compare.suite %#', (contracts: string[]) => {
console.log('readState', contractTxId);
try {
console.log = function () {}; // to hide any logs from contracts...
const result2 = await WarpFactory.custom(

const warp = WarpFactory.custom(
arweave,
{
...defaultCacheOptions,
Expand All @@ -67,17 +68,25 @@ describe.each(chunked)('v1 compare.suite %#', (contracts: string[]) => {
inMemory: true
}
)
.build()
.build();

const result2 = await warp
.contract(contractTxId)
.setEvaluationOptions({
unsafeClient: 'allow',
allowBigInt: true
})
.readState(blockHeight);
const result2String = stringify(result2.cachedValue.state).trim();

await warp.stateEvaluator.getCache().prune(1);

expect(result2String).toEqual(resultString);
} finally {
console.log = originalConsoleLog;
const heap = Math.round((process.memoryUsage().heapUsed / 1024 / 1024) * 100) / 100;
const rss = Math.round((process.memoryUsage().rss / 1024 / 1024) * 100) / 100;
console.log('Memory', { heap, rss });
}
},
800000
Expand All @@ -94,7 +103,7 @@ describe.each(chunkedVm)('v1 compare.suite (VM2) %#', (contracts: string[]) => {
.readFileSync(path.join(__dirname, 'test-cases', 'contracts', `${contractTxId}.json`), 'utf-8')
.trim();
console.log('readState', contractTxId);
const result2 = await WarpFactory.custom(
const warp = WarpFactory.custom(
arweave,
{
...defaultCacheOptions,
Expand All @@ -109,14 +118,17 @@ describe.each(chunkedVm)('v1 compare.suite (VM2) %#', (contracts: string[]) => {
inMemory: true
}
)
.build()
.build();

const result2 = await warp
.contract(contractTxId)
.setEvaluationOptions({
useVM2: true,
unsafeClient: 'allow',
allowBigInt: true
})
.readState(blockHeight);

const result2String = stringify(result2.cachedValue.state).trim();
expect(result2String).toEqual(resultString);
},
Expand Down
19 changes: 12 additions & 7 deletions src/contract/Contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { ArTransfer, Tags, ArWallet } from './deploy/CreateContract';
import { CustomSignature } from './Signature';
import { EvaluationOptionsEvaluator } from './EvaluationOptionsEvaluator';

export type CurrentTx = { interactionTxId: string; contractTxId: string };
export type BenchmarkStats = { gatewayCommunication: number; stateEvaluation: number; total: number };

interface BundlrResponse {
Expand Down Expand Up @@ -96,7 +95,7 @@ export interface Contract<State = unknown> {
*/
readState(
sortKeyOrBlockHeight?: string | number,
currentTx?: CurrentTx[],
caller?: string,
interactions?: GQLNodeInterface[]
): Promise<SortKeyCacheResult<EvalStateResult<State>>>;

Expand Down Expand Up @@ -157,11 +156,7 @@ export interface Contract<State = unknown> {
transfer?: ArTransfer
): Promise<InteractionResult<State, unknown>>;

dryWriteFromTx<Input>(
input: Input,
transaction: GQLNodeInterface,
currentTx?: CurrentTx[]
): Promise<InteractionResult<State, unknown>>;
applyInput<Input>(input: Input, transaction: GQLNodeInterface): Promise<InteractionResult<State, unknown>>;

/**
* Writes a new "interaction" transaction - i.e. such transaction that stores input for the contract.
Expand Down Expand Up @@ -234,4 +229,14 @@ export interface Contract<State = unknown> {
isRoot(): boolean;

getStorageValues(keys: string[]): Promise<SortKeyCacheResult<Map<string, any>>>;

getUncommittedState(contractTxId: string): EvalStateResult<unknown>;

setUncommittedState(contractTxId: string, result: EvalStateResult<unknown>): void;

hasUncommittedState(contractTxId: string): boolean;

resetUncommittedState(): void;

commitStates(interaction: GQLNodeInterface): Promise<void>;
}
Loading