Rangesh show a main point, and then i get a simple demo. At first, we create a simple contract MappingTest
contract MappingTest {
CustomMap balances;
struct CustomMap {
mapping (address => uint) maps;
address[] keys;
}
function put() payable public {
address sender = msg.sender;
uint256 value = msg.value;
bool contain = contains(sender);
if (contain) {
balances.maps[sender] = balances.maps[sender] + value;
} else {
balances.maps[sender] = value;
balances.keys.push(sender);
}
}
function iterator() constant returns (address[],uint[]){
uint len = balances.keys.length;
address[] memory keys = new address[](len);
uint[] memory values = new uint[](len);
for (uint i = 0 ; i < len ; i++) {
address key = balances.keys[i];
keys[i] = key;
values[i] = balances.maps[key];
}
return (keys,values);
}
function remove(address _addr) payable returns (bool) {
int index = indexOf(_addr);
if (index < 0) {
return false;
}
delete balances.maps[_addr];
delete balances.keys[uint(index)];
return true;
}
function indexOf(address _addr) constant returns (int) {
uint len = balances.keys.length;
if (len == 0) {
return -1;
}
for (uint i = 0 ; i < len ;i++) {
if (balances.keys[i] == _addr) {
return int(i);
}
}
return -1;
}
function contains(address _addr) constant returns (bool) {
if (balances.keys.length == 0) {
return false;
}
uint len = balances.keys.length;
for (uint i = 0 ; i < len ; i++) {
if (balances.keys[i] == _addr) {
return true;
}
}
return false;
}
}
and then compile, deploy contract by geth. Now, we unlock account and then execute put function
browser_test_sol_mappingtest.put({from:eth.accounts[0],value:web3.toWei(1,'ether')})
browser_test_sol_mappingtest.put({from:eth.accounts[1],value:web3.toWei(1,'ether')})
now, there are two txs waitting for mined, then execute mine op
miner.start(1);admin.sleepBlock(1);miner.stop()
two txs was packed to new block, now we execute interator function to show result
> browser_test_sol_mappingtest.iterator()
[["0x0b46c35d2e823f9b1e69ff616f9e9bf2d9d52dd0", "0xc4b232913cb195f649086d1eea0f1eb3fd0ff825"], [1000000000000000000, 1000000000000000000]]
It's same to remove, contains, indexOf function.
For JSON RPC , How to call a contract method using the eth_call JSON-RPC API will give you detail step.
Hope this helps you ~