0

I'm trying to start my first ethereum project with truffle. I noticed that one of the contracts that comes with truffle init is:

contract MetaCoin {
  mapping (address => uint) balances;

  event Transfer(address indexed _from, address indexed _to, uint256 _value);

  function MetaCoin() {
    balances[tx.origin] = 10000;
  }

  function sendCoin(address receiver, uint amount) returns(bool sufficient) {
    if (balances[msg.sender] < amount) return false;
    balances[msg.sender] -= amount;
    balances[receiver] += amount;
    Transfer(msg.sender, receiver, amount);
    return true;
  }

  function getBalanceInEth(address addr) returns(uint){
    return ConvertLib.convert(getBalance(addr),2);
  }

  function getBalance(address addr) returns(uint) {
    return balances[addr];
  }
}

However, to my understanding everyone has their own copy of the contract. So how would something like balances[receiver] += amount; or balances[msg.sender] -= amount; work?

If Person A sends Person B money, then on Person A's contract what's the point of adding to Person B's balance locally (through balances[receiver] += amount;)? Person B's contract should just update itself. It's not like anyone will ever ask Person A what Person B's balance is, so I don't see why it's stored.

Or perhaps it's the opposite? There's only 1 copy shared among every user in the network?

1

There's only one logical copy of the contract and the contract state. It's the state that the nodes eventually agree upon after they evaluate transactions independently. In the physical sense, they all have it but they can't (successfully) change anything independently. That would be like editing your local blockchain and expecting it to spend. It just won't work.

The tokens are non-local, just like ether, bitcoin, altcoins. The contract just keeps track of quantities assigned to addresses, and the only way to move them is to sign and send a transaction to sendCoin().

This bit here:

if (balances[msg.sender] < amount) return false;

ensures that the transaction is signed (msg.sender) and it looks up a balance from a DHT that holds simple integers. Whomever sent the transaction to sendCoin() can only take from the balance associated with their own address.

Then it does the math. Take from Alice to pay Bob. If the transaction is properly signed and Alice has the funds, then all nodes will agree on the new state. If not, then all nodes will agree the transaction was invalid.

Usual disclaimer: That contract is for illustrative purposes only and not suitable for real life use. ;-)

Hope it helps.

  • ooooo so that means mapping (address => uint) balances; is shared across all the nodes? like a database? – bigpotato Jun 25 '17 at 3:48
  • Yes. You can think of it as a key/value store with every possible key (address) initialized to 0 and one-step random access. Every node has a copy of the whole thing, so about (35,000?) replicas and climbing. I've never found a good ELI5 for mappings. I took a stab at it here,...scroll down to "basic tools - mappings" if curious. medium.com/@robhitchens/solidity-crud-part-1-824ffa69509a – Rob Hitchens - B9lab Jun 25 '17 at 4:01
  • wow interesting... so are conventional databases not even used? like lets say i have User contract with a blogPosts attribute. Every node just have a copy of all the blogPosts (including ones that other people wrote)? This seems inefficient – bigpotato Jun 25 '17 at 4:09
  • 2
    Well, that's a gotcha. You wouldn't put a blog post on the chain ... too expensive. You would use another form of storage such as IPFS that can store blobs economically. You might store the hash of the true document on the chain so everyone can independently confirm the fidelity of the data they fetch from somewhere else. It won't make any sense now, but maybe later. If you can do your app without blockchain then that's what you should do, in my opinion. If you need to establish trust, then blockchain can help. – Rob Hitchens - B9lab Jun 25 '17 at 4:18

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.