0

I'm trying out a publisher-subscriber smart contracts in ganache.

Both the publisher and subscriber work independently, but when I try to invoke the subscriber from publisher through the contract address, I get below error:

"Error: VM Exception while processing transaction: revert".

Been searching on the net, but didn't find any solution.


Here is my code:

Producer.sol

//Mocking subscriber contract

contract Receiver {

function notify(uint count) returns(uint);

function getCurrentCounter() constant returns(uint);

}

//Publisher

contract Producer {

uint private count;
uint private subscriberCount;
mapping(uint => Subscriber) subscribers;
event CountIncreased(uint num);

struct Subscriber {
    string subscriberName;
    address subscriberAddress;
}

function Producer() {
    count = 0;
    subscriberCount = 0;
}

function increaseCount() returns(uint,address) {
    count++;
    Receiver receiver = Receiver(subscribers[0].subscriberAddress);
    receiver.notify(count);
    CountIncreased(count);
    return (count,subscribers[0].subscriberAddress);
}

function addSubscriber(string name, address subAddress) {
    subscriberCount++;
    subscribers[subscriberCount].subscriberName = name ;
    subscribers[subscriberCount].subscriberAddress = subAddress ;
}

function removeSubscriber(string subscriberName) {
    for (uint i=0; i<subscriberCount; i++) {
        Subscriber sub = subscribers[i];
        if(keccak256(sub.subscriberName) == keccak256(subscriberName)) {
            delete subscribers[i];
            subscriberCount--;
        }
    }

}


}

Subscriber.sol

pragma solidity ^0.4.0;

contract Bookkeeping {

uint counter;

function Bookkeeping() {
    counter = 0;
}

function notify(uint count) returns(uint) {
    counter = count+5;
    return counter;
}

function getCurrentCounter() constant returns(uint) {
    return counter;
}


}
  • Is it possible that you use call instead of sendTransaction when calling addSubscriber before increaseCount? – Roman Frolov Mar 31 '18 at 15:59
  • You have to be more precise. The error happened when you called which function? – Eli Drion Mar 31 '18 at 16:01
0

I'm assuming you call addSubscriber first and then call increaseCount.

At the beginning subscriberCount is 0, when you addSubscriber it will increment to 1

function addSubscriber(string name, address subAddress) {
    subscriberCount++;

And will store the subscriber info at subscribers[1], leaving subscribers[0] with default zero values

    subscribers[subscriberCount].subscriberName = name ;
    subscribers[subscriberCount].subscriberAddress = subAddress ;
}

Now when you try to call notify from increaseCount it will fail

Receiver receiver = Receiver(subscribers[0].subscriberAddress);

Because subscriberAddress is zero.


I'd suggest to move Receiver to its own file and include them in both Producer and Subscriber, and make Bookkeeping inherit from it.

  • Hi, Thanks for your answer. As you said, it was more a logical issue. I incremented the subscriberCount after adding to mapping, looks like this: function addSubscriber(string name, address subAddress) { subscribers[subscriberCount].subscriberName = name ; subscribers[subscriberCount].subscriberAddress = subAddress ; subscriberCount++; } – ShankaraNarayanan Apr 1 '18 at 11:54

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.