Calling collect: Ways for a contract to pay for itself.

Is it possible for a contract to pay for its own execution?

I'm contemplating the possibilities of an online space-explore-mine-trade-shoot-up-type game (think a distributed turn-based EVE Online) on the Ethereum network. One enormous possibility would be for "player"-controlled ships to be controlled by contracts instead. For these contracts to act is perfectly possible, but how about reacting?

Suppose you wanted a contract to automatically order a counterattack against anyone entering its territory. It can't check by itself, because contracts cannot call themselves. The naive solution is for the game contract to call the player contract under certain conditions, but the obvious abuse case is for the contract to deliberately waste gas so that whoever called the game contract in the first place (most likely the opponent) will have to pay an extraneous amount of real money to begin the attack.

Yes, the game contract could call player contracts with gas limits, but the next abuse case is to create insane numbers of player contracts (or in my contemplated case, insane numbers of ships), so that anyone who would make a move would have to sub-call player contract after player contract and still waste extraneous amounts of real money. And a player obviously cannot be allowed to pick which other player contracts to pay for.

And, mind you, this is not limited to games. Imagine a distributed exchange with contract-based bots (ignore for the moment that their strategy would be public.) An attacker could essentially DDoS the exchange's bot system with useless bots.

The options I have thought of are two:

1) The player who wants a reactive bot will have to write a client-side one, and either host it himself or let a centralized server host it. This is essentially reinventing the wheel, poorly.

2) Have the reacting contracts reimburse the acting player for the gas (via the game contract, to prevent oh-I'll-you-later shenanigans.) This feels a little strange, and the acting player would still have to pay the full cost up front. Still, I can't see why it wouldn't work.

Am I correct in believing that there is no "turn-own-ETH-into-gas" opcode for the sake of miners knowing what they are getting into? If so, these might be the only two options.


  • SmithgiftSmithgift Member Posts: 64
    After a walk in the park, I think I've found a third, generalized option in pure Ethereum. I call it "reactors."

    A reactor contract contains a list of waiting contracts, along with their balances. A waiting contract is added to the reactor by depositing some ETH under its name. A waiting contract must have a precondition function which will return a gas reimbursement rate as well as how much gas required, depending on the situation. A waiting contract also has a main function, which does whatever it is waiting to do.

    The reactor "workers" are externally owned accounts (probably owned by programs) that watch whatever situations that might be of interest to the waiting contracts. When a worker believes such a situation has arisen, it calls the reactor contract, which calls the specific waiting contract's precondition. If the precondition does not return interest, or ran out of gas itself, the worker gets nothing whatsoever. If precondition returns that the waiting contract is interested, and the worker has enough gas remaining to fulfill the precondition's requirement, the reactor calls the waiting contract's main function, limiting this call's gas depending on the precondition.

    Afterwards, no matter the calls' result, the worker is reimbursed for (reimbursement rate * (gas used by precondition + gas used by main function)) ETH by the reactor. This is subtracted from the waiting contract's balance.

    A worker is profitable when it can pay for gas cheaper than the reimbursement rate of a waiting contract and absorb the overhead of the reactor and its own calculation of preconditions. Therefore (many) someone(s) out there will probably be running workers to seek out those waiting contracts. Waiting contracts will obviously be paying more for their gas than if run directly, but that is reasonable.

    A flaw I see is that only one worker can be reimbursed, as the precondition will doubtlessly return something different, and possibly nothing, after the main has finished. Network latency will also ensure that multiple transactions from different workers after the same precondition will happen frequently (if not almost always). This seems suspiciously like the problems with mining.

    Then again, miners would benefit the most, if they were willing to evaluate preconditions. They could slide in a free-to-them* worker transaction and get the entire reimbursement, with no need to fear another worker beating them to the virtual punch. Actually, a miner could simply sit and wait for any worker transaction heading to a reactor and preempt them. This may be a miners-only game. That's not an unreasonable circumstance, though.

    * Are transaction/gas fees burned or not, currently? I've heard it both ways in the past.
  • GeorgeHallamGeorgeHallam Member, Administrator, Moderator Posts: 229 admin
    Hi @Smithgift.
    Is it possible for a contract to pay for its own execution?
    Contracts can do a lot of thing (they can talk to other contracts, they can create new contracts, they can exchange funds between contracts and they can kill themselves too through something called the “suicide op code”) but the cannot make them directly pay for their own functions, but you can make it refund any transaction costs.
    Am I correct in believing that there is no "turn-own-ETH-into-gas" opcode for the sake of miners knowing what they are getting into?
    There is no way to change the gas apart from consuming it of course, and I think this has to do with the block gas limit.
    After a walk in the park, I think I've found a third, generalized option in pure Ethereum. I call it "reactors."
    This is very interesting! Could we schedule a call (happy to talk about some of the other questions in greater detail too)?

    You can add me on Skype at: george_hallam
  • SmithgiftSmithgift Member Posts: 64
    @GeorgeHallam: I'd be glad to, but at the moment my throat is sore from a cold so skype wouldn't be too good right now. Maybe later?
Sign In or Register to comment.