0

The set of contract-functions give expected behaviour when tested on Remix. But when the same functions are deployed on TestRPC using Truffle & Web3 and accessed using a Web Interface, it doesn't give the required logical output.

The Structures are as follow:

struct ProofEntry {
bytes32 owner;
bytes32 privateKey;
bytes32 previousTrackingId;}


struct registerEntry{
bytes32 owner;
bytes32 privateKey;}

The Mappings required are as follows:

mapping (bytes32 => ProofEntry) proofs; // trackingId to ownerBlock

mapping (string => bytes32) items;  // productId to trackingId

mapping (bytes32 => registerEntry) register; // ownerHash to registerEntry

Please consider the following functions: 1] addProduct

function addProduct(string password, string productId) public returns(bool){
    if(hasEntry(productId))
      return false;
    else {
    bytes32 owner = keccak256(bytes32ToString(keccak256(password)));
    bytes32 privateKey =  keccak256(password);
    bytes32 previousTrackingId = keccak256("root");
    bytes32 trackingId = keccak256(productId);
    proofs[trackingId] = ProofEntry(owner, privateKey, previousTrackingId);
    register[owner] = registerEntry(owner , privateKey);
    items[productId] = trackingId;
    productAdded(password,owner,productId);
    return true;
  }
}

2] getOwner

function getOwner(string productId) constant returns(bytes32) {
 bytes32 trackingId = items[productId];
 ProofEntry memory record = proofs[trackingId];
 return (record.owner);}

Now when I test these functions on Remix,ie by giving a password & productId for addproduct() and giving the productId in getOwner, I get the corresponding Hash of the owner.

But When I deploy it on testRPC using Truffle,Web3 and access it using a WebUI, addproduct() returns true indicating that it has added the product but when I pass the same productId to getOwner(), it returns default value of bytes32 and not the OwnerHash, which indicates that the values have not been reflected in the mappings in the blockchain which i cannot understand why. Need help Urgently!!!!!

2 Answers 2

0

It sounds like you are neglecting the wait for the add product transaction to be mined. Then, you are going ahead and checking and finding nothing has changed.

Outside Remix, which does it for "free", you have to wait for the mined transaction to see the effect.

The below function lets you pass in the transaction receipt (which you will have) and pause until the transaction of interest has been mined.

https://gist.github.com/xavierlepretre/88682e871f4ad07be4534ae560692ee6

Hope it helps.

2
  • How much time should it typically take to mine a transaction on testRPC? I have waited for long, also in my terminal where testRPC is running it shows details of gas usage, block number etc. Commented Dec 28, 2017 at 6:12
  • TestRPC is more or less immediate, so if the event doesn't arrive it's likely the contract did not emit it. Commented Dec 28, 2017 at 16:41
0

The addproduct() function isn't reflecting the changes in the blockchain because it may have been invoked using the .call() ,and a call is a local invocation of a contract function that does not broadcast or publish anything on the blockchain.

For Reflecting changes to the blockchain, it should be invoked using .sendTransaction(), because a transaction is broadcasted to the network, processed by miners, and if valid, is published on the blockchain.

Refer the following link for more information on the difference between transaction and call.

What is the difference between a transaction and a call?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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