2

I have the below given SmartContract and the test. Please note that, when i try to execute the test, only after the second call to the grantAccess method, I am able to get the mapped value.

pragma solidity ^0.4.24;
pragma experimental ABIEncoderV2;

contract AccessControlListContract {
    mapping(address => mapping(string=> mapping(address => string[]))) _userAccessGrants;
    mapping(address => mapping(address => mapping(string => string[]))) _targetAccessGrants;

    /*
    * A transaction function does not return any values, it instead returns the transaction object as such for mining.
    * Hence, no return statements here....
    */
    function grantAccess(address userAddress, string documentHash, address targetUserAddress, string permission) public {
        _userAccessGrants[userAddress][documentHash][targetUserAddress].push(permission); 
        _targetAccessGrants[targetUserAddress][userAddress][documentHash].push(permission);       
    }

    function checkAccess(address requestorAddress, address userAddress, string documentHash) public view returns (string[]) {
        return _targetAccessGrants[requestorAddress][userAddress][documentHash];
    }
}

Unit Tests

const AccessControlListContract = artifacts.require(
  "AccessControlListContract"
);

contract("AccessControlListContract", async (accounts) => {
    // let constants = require('./constants.js')(accounts);
    let acl;
    let constants = {
        patient1Address: accounts[0],
        patient2Address: accounts[2],
        ViewDocumentPermission: "view_document",
        doctor1Address: accounts[1],
        pharma1Address: accounts[3],
        provider1Address: accounts[4],
        ECG_DOC_HASH: "ECG-DOC-HASH",
        ECHO_DOC_HASH: "ECHO-DOC-HASH",
        BLOOD_TEST_DOC_HASH: "BLOOD-TEST-DOC-HASH",
        NON_EXISTENT_USER_ADDRESS: 0x13579
    };

    before(async () => {
        acl = await AccessControlListContract.deployed();
    });

    it("should succeed always", async () => {
      assert.equal(true, true);
    });

    it('should add a new mapping for a patient and a doctor', async () =>{
        await acl.grantAccess(constants.patient1Address, constants.ECG_DOC_HASH, constants.doctor1Address, constants.ViewDocumentPermission);
        // await acl.grantAccess(constants.patient1Address, constants.ECG_DOC_HASH, constants.doctor1Address, constants.ViewDocumentPermission);
        console.log(await acl.checkAccess(constants.doctor1Address, constants.patient1Address, constants.ECG_DOC_HASH));
        assert.equal(true, true);
    });
}

Please let me know what is going wrong. I am following the steps given in the truffle suite for performing the testing with the async and await keywords.

Tried to use the old system without the async / await method, still the patient2 permissions are not returned from the smart contract

it('non-async test', () => {
    let meta;
    return AccessControlListContract.deployed().then(function (instance) {
        meta = instance;
        return meta.grantAccess(constants.patient1Address, constants.ECG_DOC_HASH, constants.doctor1Address, constants.ViewDocumentPermission);
    }).then(function () {
        return meta.grantAccess(constants.patient2Address, constants.ECG_DOC_HASH, constants.doctor1Address, constants.ViewDocumentPermission);
    }).then(function () {
        return meta.checkAccess(constants.doctor1Address, constants.patient1Address, constants.ECG_DOC_HASH);
    }).then(function(result){
        console.log('patient 1 permissions: ',result);
    }).then(function(){
        return meta.checkAccess(constants.doctor1Address, constants.patient2Address, constants.ECG_DOC_HASH);
    }).then(function(result2){
        console.log('patient 2 permissions: ',result2);
    })
});
0

Sometimes your changes to the state variables remained not mined for a long time. It may be from your proposed gas price, or other reasons.

And more probable occurrence may be that your changes took effects in blockchain, but your client cached previous values and by re-executing the function the client fetches the cached memory again and see new values of blockchain states.

  • Is this the same behavior for the tests as well and that too in the localhost environment with Ganache – Saran Nov 26 '18 at 6:55
  • I have not seen this behavior from local Ganache. – Alireza Zojaji Nov 26 '18 at 9:54
  • If you run the above code, you should be able to reproduce. – Saran Nov 26 '18 at 10:19
  • I will try to check from ropsten to see if its working fine – Saran Nov 26 '18 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.