0

Let's say that in my system I have different nodes. In regular intervals some of these nodes can be chosen as "gold nodes" which means for example that they need to do some computations. Is it possible to make a smart contract function that accepts an input from each "gold node" and runs only after every "gold node" put its input?

0

The question is slightly unclear, but...

If you mean Ethereum smart contracts:

Yes, contract can have public method, that accept input from anywhere (and filter it - i.e. determine by some predefined characteristics the "gold node" in your terms).

And yes, after collecting some number of input requests contract may do something else ("runs" something as you wrote).

But contract can not do something by its own initiative. It means that it should perform further actions "in" the last (from last "gold node") request. You duty here is to define what request is last (and what node is "gold", as I mentioned above)

0

The question is not clear enough, but if I understood you right. You need to perform an action when all goldNodes replied and until then you wait. I am sharing a sample code, where allGoldNodesReplied is false and it's said to true when all nodes reply.

For that, your contract must know all goldNodes and their count and maybe some more info to prevent one GoldNode replying twice, so you might have to manage lots of states for that. Hope given example helps.


contract GoldNodes {

    address goldNodeOwner ;

    // tracking all goldNodes
    mapping (address => bool) isGoldNode;
    uint goldNodeCount = 0;

    // prevent same node from replying twice
    mapping (address => bool) hasGoldNodeReplied;    
    uint replyCount = 0;

    // action that has to be performed when all GoldNodes reply
    bool allGoldNodesReplied = false;


    constructor() public{
        goldNodeOwner = msg.sender;
    }


    function addGoldNode(address _goldNode) public {
        // some admin that can invoke smart contract when some gold node is added to the system
        require(msg.sender == goldNodeOwner, "only gold owner allowed");
        isGoldNode[_goldNode] = true;
        goldNodeCount++; // use SafeMath here
    }

    function reply() public {
        require(isGoldNode[msg.sender], "only gold nodes can reply");
        require(!hasGoldNodeReplied[msg.sender], "Can not reply twice");
        replyCount++;
        hasGoldNodeReplied[msg.sender] = true;
        if(replyCount == goldNodeCount){
            allGoldNodesReplied = true;
        }

    }

}

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.