I need help on understanding a contract behavior

lucas_anseilucas_ansei Member Posts: 2
edited July 2016 in Smart Contracts and Dapps
Hi there!

I've made this example contract:
  contract Test {
address owner;

mapping (uint => bytes32[]) mydata;

modifier ownerOnly {
if (msg.sender == owner) _
}

function Example(){
owner = msg.sender;
}

function setData(uint id, bytes32 text) ownerOnly {
mydata[id].push(text);
msg.sender.send(msg.value);
}

function getData(uint id) constant returns (bytes32[]) {
return mydata[id];
}

function destroy() ownerOnly {
selfdestruct(owner);
}
}
And ran these lines on the geth console:
    > var source = 'contract Test { address owner; mapping (uint => bytes32[]) mydata; modifier ownerOnly { if (msg.sender == owner) _ } function Example(){ owner = msg.sender; } function setData(uint id, bytes32 text) ownerOnly { mydata[id].push(text); msg.sender.send(msg.value); } function getData(uint id) returns (bytes32[]) { return mydata[id]; } function destroy() ownerOnly { selfdestruct(owner); } }'
> var compiled = web3.eth.compile.solidity(source)
> var contract = web3.eth.contract(compiled.Test.info.abiDefinition);
> var obj = contract.new({
from: web3.eth.accounts[0],
data: compiled.Test.code, gas: 300000},
function(e, contract){
if(!e) {
if(!contract.address) {
console.log("Contract transaction send: TransactionHash: " + contract.transactionHash + " waiting to be mined...");
} else {
console.log("Contract mined! Address: " + contract.address);
console.log(contract);
}
}
else {
console.log("Error: " + e);
}
}
)
> obj.setData(9, "ecb7cfd20295fc78210c4da154b6bfbc");
"0xd1ca7d2b852c1843c0735a91395d56873ef90f9715d5b00d68a09a3c3b8b975e"
> obj.getData(9);
[]
> obj.destroy()
"0x8fe4d65811dbc52da10cc09abc88cf3cb9ee8927f8164c4f3dcb74910ded622e"
> obj.getData(9)
[]
I've got a few questions:
1- Why does the getData() function doesn't return anything?
2- And why does the getData() function works even after the destroy()?
Post edited by o0ragman0o on

Comments

  • o0ragman0oo0ragman0o Member, Moderator Posts: 1,291 mod
    edited July 2016
    @lucas_ansei
    > obj.setData(9, "ecb7cfd20295fc78210c4da154b6bfbc");

    You're missing the gas payer account...

    > obj.setData(9, "ecb7cfd20295fc78210c4da154b6bfbc",{from: web3.eth.accounts[0]})


        > obj.setData(9, "ecb7cfd20295fc78210c4da154b6bfbc");
    "0xd1ca7d2b852c1843c0735a91395d56873ef90f9715d5b00d68a09a3c3b8b975e"
    > obj.getData(9);
    []
    > obj.destroy()
    "0x8fe4d65811dbc52da10cc09abc88cf3cb9ee8927f8164c4f3dcb74910ded622e"
    > obj.getData(9)
    []
    I've got a few questions:
    1- Why does the getData() function doesn't return anything?
    2- And why does the getData() function works even after the destroy()?
    In both cases the return appears to be a character interpretation of NULL(edit formatting made [ ] appear as one character []) is an empty array of bytes. In the first case because setData() failed and in the second because I believe the JS object is still aware of what the the return type should be.

Sign In or Register to comment.