3

Assume a smart contract is used to add potatoes. Every time a person gets a new potato they send a transaction to the smart contract which adds 1 to the total potato count.

Let's assume, within a single block, a person accidentally sends two transactions to add to the potato count when they only meant to send one. Considering they are being processed in the same block, is there any way to ensure that only one adds to the global potato count?

How can you tell miners to only include one transaction of a specific type per address per block?

1 Answer 1

2

An obvious way is to use a mapping to track which accounts have sent a transaction:

contract C {
  mapping (address => bool) sentTx;
  uint public globalCount;

  function potato() {
    if (!sentTx[msg.sender]) {
      globalCount++;
      sentTx[msg.sender] = true;
    }
  }
}

This works because contract storage is updated after every transaction.

How can you tell miners to only include one transaction of a specific type per address per block?

Miners are basically in full control (subject to the mining software they are running) and decide what transactions to include in a block and in what order. The protocol imposes few rules on miners: if a transaction is valid, and a block is valid, miners can do what they want: miners don't have to exclude or include any particular transactions.

4
  • "Contract storage is updated after every transaction." So if the same person sends "add 1 to potato count", and sends another transaction called "add 1 to apple count" with the same input, is there a temporary fork of 1 apple vs 1 potato chains? Does this end up just unforking through natural PoW? Commented May 13, 2018 at 5:05
  • @nickcarraway Unclear what you are asking. Are you talking about same contract above, or a contract that has another variable for appleCount?
    – eth
    Commented May 20, 2018 at 9:49
  • In the comment above, "input" refers to a transaction input (a coin basically). So, this may be more of a question of blockchains in general. If you send Ether to one node to have it do one thing, and simultaneously you send the same ether to another node to have it do another thing, it may cause a (temporary) blockchain fork... This is of course why you wait for confirmations! (I answered my own question i think) Commented May 20, 2018 at 19:16
  • 1
    @nickcarraway I see. The "input" you refer is a UTXO in those types of blockchains, but the model in Ethereum is different: you can have many different variables to represent different things like counts and tokens, and for the native ETH there's a "balance" property that all accounts have.
    – eth
    Commented Jun 3, 2018 at 17:09

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.