You're trying to make the contract send funds it doesn't have. It needs ether to draw from. The balance would start at 0, so you need to add a function() payable{}
and send some ether to work with.
I've marked a section of the code *** unsafe ***
to avoid misunderstanding. There are deeper concerns about handling money this way. A good next step is to check the success/fail of the send()
operation and then respond differently. I have it emitting a failure message or a success message.
I added some event emitters so you can see what's going on.
Hope it helps.
contract project{
address public user;
bytes32[10] name;
address[10] customer;
uint public i=0;
event LogDep (address sender, uint amount, uint balance);
event LogSent(address recipient, uint amount, uint balance);
event LogErr (address recipient, uint amount, uint balance);
function project(){
user=msg.sender;
}
function depositFunds() public payable returns(bool success) {
LogDep(msg.sender, msg.value, this.balance);
return true;
}
function add(bytes32 _name){
name[i]=_name;
customer[i]=msg.sender;
i++;
}
function get_address() constant returns(address[10]){
return customer;
}
function reward() {
for(uint i=0;i<10;i++)
{
// *** unsafe pattern ***
if(customer[i].send(1)) {
LogSent(customer[i], 1, this.balance);
} else {
LogErr(customer[i], 1, this.balance);
}
}
}
}
Here it is in Remix to show it working.
If you haven't seen Remix before, go here https://ethereum.github.io/browser-solidity and just paste the code into the compiler. You can interact with the contract functions.
Step 1: Create the contract
Step 2: Send the contract some funds
10 wei is just enough to send 1 to all 10 customers. If you send less, you'll start seeing failed sends in Step 3.
Step 3: send txn to reward()
Be sure to NOT send wei/eth this time, because this function isn't payable.
Best Answer
All nodes have a copy of the same append-only ledger (called the blockchain) which stores the state at the time the block is mined. Miners and mining are explained super well in the Ethereum whitepaper.
The balance is changed locally in each node, yes, but nodes don't reach consensus on each individual contract execution/state change in real time as they occur, it is simply the miner who successfully mines the block that gets to choose which state changes and function calls are included. This question discusses further when each node actually executes each transaction's/contract call's code.
This newly mined block is appended to the blockchain, and the blockchain with the most work behind it is accepted as the 'correct' chain, so all nodes agree that all states in that block are the correct states for now.
As an example, imagine you called
benefactor.send(this.balance);
with the balance value 294820984, and I called it with the balance value 7429847298. If we sent these contract calls to the network at the same time, some miners might include your call, and update their state log accordingly, but some might include mine. They would only reach consensus on the one that gets mined into a block, however. This answer explores in more detail what nodes are agreeing on when consensus is reached.