Search code examples
blockchainsolidityremix

Checking value of 2 mapping with struct in the same address on blockchain


im new with blockchain and solidity and im trying to check 2 mapping result with the same key on one address, suppose i have this code

contract EventConfirmAccess{

struct eventStruct{
    string event_log_time;
    string event_name;
    string event_id;
    uint data_nik;
}
mapping(uint => eventStruct) public dictConfirmAccess;
uint[] public arrayOfData;

function loggingAccess(
    uint _nik, string memory _event_log_time,
    string memory _event_name, string memory _event_id,
    uint _data_nik
) public{
    dictConfirmAccess[_nik] = eventStruct(
      {
        event_log_time : _event_log_time,
        event_name : _event_name,
        event_id : _event_id,
        data_nik : _data_nik
      }
    );

    arrayOfData.push(_nik);
}
function checkData(uint _nik) view public returns(
        string memory ,string memory,
        string memory ,uint
    ){
        return (
            dictConfirmAccess[_nik].event_log_time,
            dictConfirmAccess[_nik].event_name,
            dictConfirmAccess[_nik].event_id,
            dictConfirmAccess[_nik].data_nik
        );
    }
 }

supposed I insert 1st data to blockchain :

  • loggingAccess(10001, "2020-10-11 08:10:00", "CONFIRM_1", "C_1", 10001)

after that I insert 2nd data :

  • loggingAccess(10001, "2020-10-11 08:20:00", "CONFIRM_2", "C_2", 10002)

when I check the array with checkData function I get result like this :

    Result {
  '0': '2020-10-11 08:20:00',
  '1': 'CONFIRM_2',
  '2': 'C_2',
  '3': BN {
    negative: 0,
    words: [ 51784655, 4, <1 empty item> ],
    length: 2,
    red: null
  }
}

the question is where is the result of 1st array ? how to retrieve it from blockchain? is this removed from blockchain (as far as i know blockchain data recorded forever ) ?

thankyou for you help and answer :)


Solution

  • Since you're storing to the mapping under the same key (dictConfirmAccess[_nik] = ...), the value gets overwritten.

    Using the Remix VM emulator, it's currently not possible to access the previous states.

    If this situation happened on a live network, you could connect to an archive node and retrieve the historical value using the combination of

    1. the storage slot number

      determinable, based on the property order in the contract and the mapping key - source

    2. and the block number

      for which you want to get the value

    For example using the web3 getStorageAt method

    const historicalValue = await web3.eth.getStorageAt(
        contractAddress,
        storageSlotNumber,
        blockNumber
    );
    

    Note that the historical values are accessible only using off-chain tools and are not accessible from the contract itself (or other contracts).