Skip to content

Commit

Permalink
adding contract version and callingContractInfo object
Browse files Browse the repository at this point in the history
  • Loading branch information
harpagon210 committed Jun 27, 2019
1 parent 34bc049 commit 05182fe
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
20 changes: 20 additions & 0 deletions libs/SmartContracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ class SmartContracts {
BigNumber.set({ DECIMAL_PLACES: 3 });
}

const contractVersion = existingContract && existingContract.version
? existingContract.version
: 1;

// initialize the state that will be available in the VM
const vmState = {
api: {
Expand All @@ -145,6 +149,7 @@ class SmartContracts {
blockNumber,
refSteemBlockNumber,
steemBlockTimestamp: timestamp,
contractVersion,
db,
BigNumber,
validator,
Expand All @@ -158,6 +163,7 @@ class SmartContracts {
JSON.stringify(parameters),
blockNumber, timestamp,
refSteemBlockNumber, refSteemBlockId, prevRefSteemBlockId, jsVMTimeout,
name, contractVersion,
),
// emit an event that will be stored in the logs
emit: (event, data) => typeof event === 'string' && logs.events.push({ contract: name, event, data }),
Expand Down Expand Up @@ -187,12 +193,14 @@ class SmartContracts {
code: codeTemplate,
codeHash: SHA256(codeTemplate).toString(enchex),
tables,
version: 1,
};

// if contract already exists, update it
if (existingContract !== null) {
newContract.$loki = existingContract.$loki;
newContract.tables = Object.assign(existingContract.tables, newContract.tables);
newContract.version = existingContract.version + 1;

await ipc.send(
{
Expand Down Expand Up @@ -248,6 +256,7 @@ class SmartContracts {

const contractCode = contractInDb.code;
const contractOwner = contractInDb.owner;
const contractVersion = contractInDb.version;

// prepare the db object that will be available in the VM
const db = {
Expand Down Expand Up @@ -300,6 +309,7 @@ class SmartContracts {
owner: contractOwner,
refSteemBlockNumber,
steemBlockTimestamp: timestamp,
contractVersion,
transactionId,
blockNumber,
action,
Expand All @@ -317,6 +327,7 @@ class SmartContracts {
JSON.stringify(parameters),
blockNumber, timestamp,
refSteemBlockNumber, refSteemBlockId, prevRefSteemBlockId, jsVMTimeout,
contract, contractVersion,
),
// execute a smart contract from the current smart contract
// with the contractOwner authority level
Expand All @@ -327,6 +338,7 @@ class SmartContracts {
JSON.stringify(parameters),
blockNumber, timestamp,
refSteemBlockNumber, refSteemBlockId, prevRefSteemBlockId, jsVMTimeout,
contract, contractVersion,
),
// execute a token transfer from the contract balance
transferTokens: async (
Expand All @@ -342,6 +354,7 @@ class SmartContracts {
}),
blockNumber, timestamp,
refSteemBlockNumber, refSteemBlockId, prevRefSteemBlockId, jsVMTimeout,
contract, contractVersion,
),
// emit an event that will be stored in the logs
emit: (event, data) => typeof event === 'string' && results.logs.events.push({ contract, event, data }),
Expand Down Expand Up @@ -405,6 +418,7 @@ class SmartContracts {
timestamp,
refSteemBlockNumber, refSteemBlockId, prevRefSteemBlockId,
jsVMTimeout,
callingContractName, callingContractVersion,
) {
if (typeof contract !== 'string' || typeof action !== 'string' || (parameters && typeof parameters !== 'string')) return null;
const sanitizedParams = parameters ? JSON.parse(parameters) : null;
Expand All @@ -423,6 +437,12 @@ class SmartContracts {
sanitizedParams.isSignedWithActiveKey = originalParameters.isSignedWithActiveKey;
}

// pass the calling contract name and calling contract version to the contract
sanitizedParams.callingContractInfo = {
name: callingContractName,
version: callingContractVersion,
};

const results = {};
try {
const res = await SmartContracts.executeSmartContract(
Expand Down
7 changes: 7 additions & 0 deletions plugins/Streamer.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ function parseTransactions(refBlockNumber, block) {
delete contractPayload.permlink;
}

// callingContractInfo is a reserved property
// it is used to provide information about a contract when calling
// a contract action from another contract
if (contractPayload.callingContractInfo) {
delete contractPayload.callingContractInfo;
}

// set the sender to null when calling the comment action
// this way we allow people to create comments only via the comment operation
if (operation[0] === 'comment' && contractName === 'comments' && contractAction === 'comment') {
Expand Down
6 changes: 5 additions & 1 deletion test/steemsmartcontracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,10 @@ describe('Smart Contracts', () => {
}
actions.addBook = async (payload) => {
const { title } = payload;
const { title, callingContractInfo } = payload;
api.debug(callingContractInfo.name)
api.debug(callingContractInfo.version)
let user = await api.db.findOneInTable('usersContract', 'users', { "id": api.sender });
Expand Down Expand Up @@ -1598,6 +1601,7 @@ describe('Smart Contracts', () => {
let res = await send(database.PLUGIN_NAME, 'MASTER', { action: database.PLUGIN_ACTIONS.FIND_CONTRACT, payload: { name: 'testContract' } });
const contract = res.payload;

assert.equal(contract.version, 2);
assert.notEqual(contract.tables['testContract_testTable'], undefined);
assert.notEqual(contract.tables['testContract_testUpdateTable'], undefined);

Expand Down

0 comments on commit 05182fe

Please sign in to comment.