I was following this tutorial.I have deployed the following two contracts on testrpc:
pragma solidity ^0.4.7;
contract MDBService {
mapping (address => address) accounts;
mapping (uint => address) ids;
uint numberAccounts;
function MDBService() {
numberAccounts = 0;
}
function register() {
require(accounts[msg.sender] == 0); //We check if the user is already registered
ids[numberAccounts] = msg.sender;
accounts[msg.sender] = new MDBAccount(msg.sender);
numberAccounts++;
}
}
contract MDBAccount {
struct Post {
uint timestamp;
string message;
}
uint public numberPosts;
address public owner;
mapping (uint => Post) posts;
modifier isOwner() {
require(owner == msg.sender);
_;
}
function MDBAccount(address _owner) {
owner = _owner;
numberPosts = 0;
}
function post(string message) isOwner() {
require(bytes(message).length <= 160);
posts[numberPosts].timestamp = now;
posts[numberPosts].message = message;
numberPosts++;
}
}
Now when I call
MDBService.register({gas:700000,from:web3.eth.accounts[1]})
a new instance of MDBAccount is created(i.e. a new account MDBAccount for the user with address of account[1]).So now I want to post a message from this account instead of the default account[0].
When I call
MDBAccount.post('Hello')
it works because it uses the instance created when the contract deployed.But when I use
MDBAccount.post('Hello',{from:web3.eth.accounts[1]})
it fails because isOwner is not satisfied as it is using the same instance and not the one created on calling the above register.So how can I call post for account[1].