Skip to content

Commit

Permalink
fix: CIS should return null if nothing found
Browse files Browse the repository at this point in the history
  • Loading branch information
Tadeuchi authored and ppedziwiatr committed Sep 27, 2023
1 parent b321456 commit d21d121
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,31 @@ describe('Testing deep internal reads', () => {
});

it('root contract should have the latest balance', async () => {
await leafContract.writeInteraction({ function: 'increase', target: 'asd', qty: 200 });
await leafContract.writeInteraction({ function: 'increase', target: 'asd', qty: 25 });
await leafContract.writeInteraction({ function: 'increase', target: 'asd', qty: 25 });
await leafContract.writeInteraction({ function: 'increase', target: 'asd', qty: 50 });
await leafContract.writeInteraction({ function: 'increase', target: 'asd', qty: 50 });
await leafContract.writeInteraction({ function: 'increase', target: 'asd', qty: 50 });
await mineBlock(warp);
await node20Contract.writeInteraction({ function: 'readBalanceFrom', tokenAddress: leafId, contractTxId: 'asd' });
await mineBlock(warp);
await leafContract.writeInteraction({ function: 'increase', target: 'asd', qty: 400 });
await leafContract.writeInteraction({ function: 'increase', target: 'asd', qty: 200 });
await leafContract.writeInteraction({ function: 'increase', target: 'asd', qty: 100 });
await leafContract.writeInteraction({ function: 'increase', target: 'asd', qty: 100 });
await mineBlock(warp);
await leafContract.writeInteraction({ function: 'increase', target: 'asd', qty: 100 });
await mineBlock(warp);
await leafContract.writeInteraction({ function: 'increase', target: 'asd', qty: 100 });
await mineBlock(warp);
await leafContract.writeInteraction({ function: 'increase', target: 'asd', qty: 100 });
await mineBlock(warp);
await leafContract.writeInteraction({ function: 'increase', target: 'asd', qty: 400 });
await leafContract.writeInteraction({ function: 'increase', target: 'asd', qty: 100 });
await mineBlock(warp);
await node22Contract.writeInteraction({ function: 'readBalanceFrom', tokenAddress: leafId, contractTxId: 'asd' });
await mineBlock(warp);
await leafContract.writeInteraction({ function: 'increase', target: 'asd', qty: 250 });
await leafContract.writeInteraction({ function: 'increase', target: 'asd', qty: 50 });
await leafContract.writeInteraction({ function: 'increase', target: 'asd', qty: 100 });
await leafContract.writeInteraction({ function: 'increase', target: 'asd', qty: 100 });
await mineBlock(warp);
await node21Contract.writeInteraction({ function: 'readBalanceFrom', tokenAddress: leafId, contractTxId: 'asd' });
await mineBlock(warp);
Expand All @@ -154,7 +168,10 @@ describe('Testing deep internal reads', () => {
await mineBlock(warp);


const rootResult = await warp.pst(rootId).readState();
const rootResult = await warp.pst(rootId)
.setEvaluationOptions({
cacheEveryNInteractions: 1,
}).readState();
expect(rootResult.cachedValue.state.balances['asd']).toEqual(1100);

const node20Result = await warp.pst(node20Id).readState();
Expand Down
8 changes: 5 additions & 3 deletions src/contract/states/ContractInteractionState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export class ContractInteractionState implements InteractionState {
}
keys = keys.sort((a, b) => a.localeCompare(b));
const resultSortKey = keys[keys.length - 1];
return new SortKeyCacheResult<EvalStateResult<unknown>>(resultSortKey, states.get(resultSortKey));
if (states.get(resultSortKey)) {
return new SortKeyCacheResult<EvalStateResult<unknown>>(resultSortKey, states.get(resultSortKey));
}
}
return null;
}
Expand Down Expand Up @@ -87,9 +89,9 @@ export class ContractInteractionState implements InteractionState {
this._kv.clear();
}

async rollback(interaction: GQLNodeInterface): Promise<void> {
async rollback(interaction: GQLNodeInterface, forceStateStoreToCache: boolean): Promise<void> {
try {
await this.doStoreJson(this._initialJson, interaction);
await this.doStoreJson(this._initialJson, interaction, forceStateStoreToCache);
await this.rollbackKVs();
} finally {
this.reset();
Expand Down
2 changes: 1 addition & 1 deletion src/contract/states/InteractionState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface InteractionState {
* - IFF the result.type != 'ok'.
* This ensures atomicity of state changes withing any given interaction - also in case of internal contract calls.
*/
rollback(interaction: GQLNodeInterface): Promise<void>;
rollback(interaction: GQLNodeInterface, forceStateStoreToCache: boolean): Promise<void>;

has(contractTxId: string, sortKey: string): boolean;

Expand Down
5 changes: 0 additions & 5 deletions src/core/modules/impl/CacheableStateEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ export class CacheableStateEvaluator extends DefaultStateEvaluator {
);
}

// {
// contractTxId: this.contractDefinition.txId,
// interactionTxId: this.swGlobal.transaction.id
// }

async onStateEvaluated<State>(
transaction: GQLNodeInterface,
executionContext: ExecutionContext<State>,
Expand Down
30 changes: 12 additions & 18 deletions src/core/modules/impl/DefaultStateEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { TagsParser } from './TagsParser';
import { VrfPluginFunctions } from '../../WarpPlugin';
import { BasicSortKeyCache } from '../../../cache/BasicSortKeyCache';
import { KnownErrors } from './handler/JsHandlerApi';
import { genesisSortKey } from "./LexicographicalInteractionsSorter";

type EvaluationProgressInput = {
contractTxId: string;
Expand Down Expand Up @@ -91,17 +90,12 @@ export abstract class DefaultStateEvaluator implements StateEvaluator {
break;
}

const missingInteraction = missingInteractions[i];
currentSortKey = missingInteraction.sortKey;
contract
.interactionState()
.setInitial(
contract.txId(),
new EvalStateResult(currentState, validity, errorMessages),
lastConfirmedTxState?.tx?.sortKey || executionContext.cachedState?.sortKey || genesisSortKey
);

const missingInteraction = missingInteractions[i];
.setInitial(contract.txId(), new EvalStateResult(currentState, validity, errorMessages), currentSortKey);
const singleInteractionBenchmark = Benchmark.measure();
currentSortKey = missingInteraction.sortKey;

if (missingInteraction.vrf) {
if (!vrfPlugin) {
Expand Down Expand Up @@ -311,27 +305,27 @@ export abstract class DefaultStateEvaluator implements StateEvaluator {
}
}

const forceStateStoreToCache =
executionContext.evaluationOptions.cacheEveryNInteractions > 0 &&
i % executionContext.evaluationOptions.cacheEveryNInteractions === 0;
// if that's the end of the root contract's interaction - commit all the uncommitted states to cache.
if (contract.isRoot()) {
contract.clearChildren();
// update the uncommitted state of the root contract
if (lastConfirmedTxState) {
contract.interactionState().update(contract.txId(), lastConfirmedTxState.state, lastConfirmedTxState.tx.sortKey);
contract
.interactionState()
.update(contract.txId(), lastConfirmedTxState.state, lastConfirmedTxState.tx.sortKey);
if (validity[missingInteraction.id]) {
let forceStateStoreToCache = false;
if (executionContext.evaluationOptions.cacheEveryNInteractions > 0) {
forceStateStoreToCache = i % executionContext.evaluationOptions.cacheEveryNInteractions === 0;
}
await contract.interactionState().commit(missingInteraction, forceStateStoreToCache);
} else {
await contract.interactionState().rollback(missingInteraction);
await contract.interactionState().rollback(missingInteraction, forceStateStoreToCache);
}
}
} else {
// if that's an inner contract call - only update the state in the uncommitted states
contract
.interactionState()
.update(contract.txId(), new EvalStateResult(currentState, validity, errorMessages), currentSortKey);
const interactionState = new EvalStateResult(currentState, validity, errorMessages);
contract.interactionState().update(contract.txId(), interactionState, currentSortKey);
}
}
const evalStateResult = new EvalStateResult<State>(currentState, validity, errorMessages);
Expand Down

0 comments on commit d21d121

Please sign in to comment.