Can someone help me understand why the function add
in the contract one is not working and in the contract two is?
Both are mined and are inserted into the blockchain.
Thanks.
Not working contract
contract one {
struct A {
address[] count;
}
struct B {
uint8[] count;
}
mapping (address => A) a;
mapping (address => B) b;
function add(address c, uint8 g) {
a[msg.sender].count.push(c);
b[msg.sender].count.push(g);
}
}
Working contract
contract two {
struct A {
address[] count;
}
mapping (address => A) a;
function add(address c) {
a[msg.sender].count.push(c);
}
}
Best Answer
Summary
Works perfectly. I do get the ["0x", 0] message if I call the functions
get1()
andget2()
immediately after the I send theadd(...)
transaction. But after waiting a while more, theget1()
andget2()
functions return the expected values.Details
Modification To Your Code
I've modified your code to add some functions to view the inserted data:
Running
geth
I'm running the Dev blockchain using the following parameters:
Flattened Your Code And Assigned To A Variable
You can use a service like Line Break Removal Tool to strip out your line breaks, or see How to load Solidity source file into geth for some alternatives.
I flattened your code, assigned it to a variable and pasted it into
geth
:Compiled Your Code
I compiled your code using the following command:
Inserted Your Code Into The Blockchain
I used the following commands to insert your code into the blockchain:
And waited for the following message:
Sent Transaction To Add Data
I inserted some sample data into the blockchain using the following command:
And waited for the transaction to be mined.
Check Data Inserted
If I immediately run
twoStructs.get1(eth.accounts[0])
, I get the following results:If I want a few more seconds:
as expected. If you get the
["0x", 0]
, wait for some time then try again.