Skip to content

Commit

Permalink
feat: Adds test actions for modifying the block (timestamp and level)
Browse files Browse the repository at this point in the history
  • Loading branch information
RomarQ committed Jun 10, 2022
1 parent 3382853 commit 9b7c2b2
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"@tezwell/michelson-sdk": "0.4.3",
"@tezwell/smartts-sdk": "0.8.8",
"@tezwell/tezmonitor": "0.0.4",
"@tezwell/tezos-testing-sdk": "0.0.17",
"@tezwell/tezos-testing-sdk": "0.0.18",
"@types/crypto-js": "4.1.1",
"@types/jest": "28.1.0",
"@types/react": "18.0.10",
Expand Down
2 changes: 2 additions & 0 deletions src/blocks/enums/BlockKind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ export enum BlockKind {
test__assert_account_balance_action = 'test__assert_account_balance_action',
test__assert_contract_storage_action = 'test__assert_contract_storage_action',
test__modify_chain_id_action = 'test__modify_chain_id_action',
test__modify_block_level = 'test__modify_block_level',
test__modify_block_timestamp = 'test__modify_block_timestamp',
test__pack_data_action = 'test__pack_data_action',

test__address_of_account = 'test__address_of_account',
Expand Down
19 changes: 6 additions & 13 deletions src/blocks/testing/call_contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ Blockly.Blocks[BlockKind.test__call_contract_action] = {
this.appendValueInput('AMOUNT').setCheck(['Mutez']).appendField('Amount');
this.appendValueInput('ARGUMENT').setCheck(['Literal']).appendField('Argument');

this.appendValueInput('LEVEL').setCheck(['Nat']).appendField('Mock block level');
this.appendValueInput('TIMESTAMP').setCheck(['Timestamp']).appendField('Mock block timestamp');
// IMPORTANT: Keep this for backwards compatibility
this.appendValueInput('LEVEL').setCheck(['Nat']).appendField('Mock block level').setVisible(false);
this.appendValueInput('TIMESTAMP')
.setCheck(['Timestamp'])
.appendField('Mock block timestamp')
.setVisible(false);

this.appendDummyInput('FAILWITH_INPUT')
.appendField('Expecting transaction to fail?')
Expand All @@ -60,7 +64,6 @@ Testing.addBlock(BlockKind.test__call_contract_action, {
const sender: string = extractVariableName(block, 'SENDER');
const entrypoint: string = block.getFieldValue('ENTRYPOINT');
const amount = String(block.getInputTargetBlock('AMOUNT')?.getFieldValue('value') || 0);
const level = Number(block.getInputTargetBlock('LEVEL')?.getFieldValue('nat_value') || 1);
const argument = Michelson.toMichelson(block, 'ARGUMENT');

const action: ICallContractPayload = {
Expand All @@ -87,16 +90,6 @@ Testing.addBlock(BlockKind.test__call_contract_action, {
action.expect_failwith = Michelson.toMichelson(block, 'EXPECTED_ERROR', Unit()) as any;
}

const timestampBlock = block.getInputTargetBlock('TIMESTAMP');
if (timestampBlock) {
action.timestamp = validateTimestamp(timestampBlock);
}

if (level < 1 || level > 99999999) {
throw new Error(`The block level must be between 1 and 99999999. ${buildBlockErrorString(block)}`);
}
action.level = level;

return buildAction(ActionKind.CallContract, action);
},
});
2 changes: 2 additions & 0 deletions src/blocks/testing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import './assert_account_balance';
import './assert_contract_storage';
import './call_contact';
import './modify_chain_id';
import './modify_block_level';
import './modify_block_timestamp';
import './pack_data';

import './address_of_account';
Expand Down
42 changes: 42 additions & 0 deletions src/blocks/testing/modify_block_level.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Blockly from 'blockly';

import type { Block } from 'src/typings/blockly';
import { buildAction } from '@tezwell/tezos-testing-sdk';
import { ActionKind } from '@tezwell/tezos-testing-sdk/action';

import BlockKind from '../enums/BlockKind';
import Testing from '../generators/Testing';
import { buildBlockErrorString } from '../utils/errorHandling';

const ModifyBlock = {
type: BlockKind.test__modify_block_level,
message0: 'Modify next block level to %1',
args0: [
{
type: 'input_value',
name: 'LEVEL',
check: ['Nat'],
},
],
colour: 300,
};

Blockly.Blocks[ModifyBlock.type] = {
init: function () {
this.jsonInit(ModifyBlock);
this.setPreviousStatement(true, ['TestAction']);
this.setNextStatement(true, ['TestAction']);
},
};

Testing.addBlock(ModifyBlock.type, {
toAction: (block: Block) => {
const level = Number(block.getInputTargetBlock('LEVEL')?.getFieldValue('nat_value') || 1);

if (level < 1 || level > 99999999) {
throw new Error(`The block level must be between 1 and 99999999. ${buildBlockErrorString(block)}`);
}

return buildAction(ActionKind.ModifyBlockLevel, { level });
},
});
38 changes: 38 additions & 0 deletions src/blocks/testing/modify_block_timestamp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Blockly from 'blockly';

import type { Block } from 'src/typings/blockly';
import { buildAction } from '@tezwell/tezos-testing-sdk';
import { ActionKind } from '@tezwell/tezos-testing-sdk/action';

import BlockKind from '../enums/BlockKind';
import Testing from '../generators/Testing';
import { validateTimestamp } from '../values/Timestamp';

const ModifyBlock = {
type: BlockKind.test__modify_block_timestamp,
message0: 'Modify next block timestamp to %1',
args0: [
{
type: 'input_value',
name: 'TIMESTAMP',
check: ['Timestamp'],
},
],
colour: 300,
};

Blockly.Blocks[ModifyBlock.type] = {
init: function () {
this.jsonInit(ModifyBlock);
this.setPreviousStatement(true, ['TestAction']);
this.setNextStatement(true, ['TestAction']);
},
};

Testing.addBlock(ModifyBlock.type, {
toAction: (block: Block) => {
const timestamp = validateTimestamp(block.getInputTargetBlock('TIMESTAMP')!);

return buildAction(ActionKind.ModifyBlockTimestamp, { timestamp });
},
});
26 changes: 18 additions & 8 deletions src/components/blockly/blocks/Testing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,19 @@ export const TestingAction_CallContract = () => (
<Value name="ARGUMENT">
<Shadow type={BlockKind.unit_literal} />
</Value>
<Value name="TIMESTAMP">
<Shadow type={BlockKind.timestamp_literal}>
<Field name="value">1970-01-01T00:00:00Z</Field>
</Shadow>
</Block>
);

export const TestingAction_ModifyChainID = () => (
<Block type={BlockKind.test__modify_chain_id_action} tags={['test', 'testing']}>
<Value name="CHAIN_ID">
<Shadow type={BlockKind.chain_id_literal} />
</Value>
</Block>
);

export const TestingAction_ModifyBlockLevel = () => (
<Block type={BlockKind.test__modify_block_level} tags={['test', 'testing']}>
<Value name="LEVEL">
<Shadow type={BlockKind.nat_literal}>
<Field name="nat_value">1</Field>
Expand All @@ -57,10 +65,12 @@ export const TestingAction_CallContract = () => (
</Block>
);

export const TestingAction_ModifyChainID = () => (
<Block type={BlockKind.test__modify_chain_id_action} tags={['test', 'testing']}>
<Value name="CHAIN_ID">
<Shadow type={BlockKind.chain_id_literal} />
export const TestingAction_ModifyBlockTimestamp = () => (
<Block type={BlockKind.test__modify_block_timestamp} tags={['test', 'testing']}>
<Value name="TIMESTAMP">
<Shadow type={BlockKind.timestamp_literal}>
<Field name="value">1970-01-01T00:00:00Z</Field>
</Shadow>
</Value>
</Block>
);
Expand Down
49 changes: 48 additions & 1 deletion src/pages/editor/toolbar/modal/TestModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,33 @@ const getActionLabel = (action: IAction): string | React.ReactElement => {
return 'Assert Account Balance';
case ActionKind.AssertContractStorage:
return 'Assert Contract Storage';
case ActionKind.ModifyBlockLevel:
return (
<span>
Modify next block level to{' '}
<span className="text-amber-800 font-bold border border-amber-800 px-1">
{action.payload.level}
</span>
</span>
);
case ActionKind.ModifyBlockTimestamp:
return (
<span>
Modify next block timestamp to{' '}
<span className="text-amber-800 font-bold border border-amber-800 px-1">
{action.payload.timestamp}
</span>
</span>
);
case ActionKind.ModifyChainID:
return 'Modify Chain Identifier';
return (
<span>
Modify chain identifier to{' '}
<span className="text-amber-800 font-bold border border-amber-800 px-1">
{action.payload.chain_id}
</span>
</span>
);
case ActionKind.PackData:
return 'Pack Data';
}
Expand Down Expand Up @@ -165,6 +190,28 @@ const ResultDetails = ({ result }: { result: IActionResult }) => {
/>
</div>
);
case ActionKind.ModifyBlockLevel:
return (
<div>
<p className="my-2 font-bold">Next block level</p>
<CodeBlock
language="json"
text={JSON.stringify(result.result['level'], null, 4)}
showLineNumbers={false}
/>
</div>
);
case ActionKind.ModifyBlockTimestamp:
return (
<div>
<p className="my-2 font-bold">Next block timestamp</p>
<CodeBlock
language="json"
text={JSON.stringify(result.result['timestamp'], null, 4)}
showLineNumbers={false}
/>
</div>
);
case ActionKind.ModifyChainID:
return (
<div>
Expand Down
4 changes: 4 additions & 0 deletions src/pages/editor/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ import {
TestingAction_ModifyChainID,
TestingAction_PrintPackedData,
TestingAction_OriginateContractFromCode,
TestingAction_ModifyBlockLevel,
TestingAction_ModifyBlockTimestamp,
} from 'src/components/blockly/blocks/Testing';
import { StringEndsWith, StringStartsWith } from 'src/components/blockly/blocks/expressions/string';
import { AddressIsKT1 } from 'src/components/blockly/blocks/expressions/address';
Expand Down Expand Up @@ -824,6 +826,8 @@ const EditorView: React.FC<EditorViewProps> = ({ workspaceRef, compile, onError

<Label text="Context Modifiers" web-class="defaultLabel" />

<TestingAction_ModifyBlockLevel />
<TestingAction_ModifyBlockTimestamp />
<TestingAction_ModifyChainID />

<Label text="Assertions" web-class="defaultLabel" />
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2041,10 +2041,10 @@
axios "0.26.1"
rxjs "7.5.5"

"@tezwell/[email protected].17":
version "0.0.17"
resolved "https://registry.yarnpkg.com/@tezwell/tezos-testing-sdk/-/tezos-testing-sdk-0.0.17.tgz#145f2553aa4bbb3bb628eaef1057f096da2286a3"
integrity sha512-ElmAKn7wKVPzGaYjoZABYiLDrfMk7UFRHGKQJn5vcVw1GeriR22BpCabJvcRmAe/9tENt6/Lba4+jIGV3QiNCQ==
"@tezwell/[email protected].18":
version "0.0.18"
resolved "https://registry.yarnpkg.com/@tezwell/tezos-testing-sdk/-/tezos-testing-sdk-0.0.18.tgz#8d9811583b4f62730078ae3b81486fba9288dc52"
integrity sha512-gwhfxrVo/To4eKGFxbwbA+r6j4AQmELjfLx0KrpbQwlmVrnS6SX9Ijsfi2n17OSaCTYk6wlCoP0+1ciD0cYy2A==

"@tootallnate/once@1":
version "1.1.2"
Expand Down

0 comments on commit 9b7c2b2

Please sign in to comment.