0

I have the code here -

pragma solidity ^0.4.18;

contract CollegeAdmin {
uint8 public studentAge=21;
uint16 public studentIncome=15000;
uint8 public studentScore=4;
uint8 public studentAttendance=96;
address public buyer;
address public owner;
event changedAge(uint age);

function CollegeAdmin() public {
owner = msg.sender;
}
function setAge(uint8 age) public {
buyer=msg.sender;
studentAge = age;
changedAge(age);
}

}

I deployed the contract using eth.coinbase and with eth.defaultAccount set.

Here is the output after deployment -

> con.buyer()
"0x"
> con.owner()
"0x"
>

con is the contract instance - eth.contract(abi).at(addr).

When is owner set? I thought it was set at deployment or when an instance is taken of the contract. Apparently not.

If the design is to set owner to ensure security in running the contract then when is that set? It seems to me it should be at the point of creation of the contract.

Here is the deployment -

con=eth.contract(abi).at(addr)
{
  abi: [{
      constant: true,
      inputs: [],
      name: "studentIncome",
      outputs: [{...}],
      payable: false,
      stateMutability: "view",
      type: "function"
  }, {
      constant: false,
      inputs: [{...}],
      name: "setAge",
      outputs: [],
      payable: false,
      stateMutability: "nonpayable",
      type: "function"
  }, {
      constant: true,
      inputs: [],
      name: "buyer",
      outputs: [{...}],
      payable: false,
      stateMutability: "view",
      type: "function"
  }, {

      constant: true,
      inputs: [],
      name: "owner",
      outputs: [{...}],
      payable: false,
      stateMutability: "view",
      type: "function"
  }, {
      constant: true,
      inputs: [],
      name: "studentAttendance",
      outputs: [{...}],
      payable: false,
      stateMutability: "view",
      type: "function"
  }, {
      constant: true,
      inputs: [],
      name: "studentAge",
      outputs: [{...}],
      payable: false,
      stateMutability: "view",
      type: "function"
  }, {
      constant: true,
      inputs: [],
      name: "studentScore",
      outputs: [{...}],
      payable: false,
      stateMutability: "view",
      type: "function"
  }, {
      inputs: [],
      payable: false,
      stateMutability: "nonpayable",
      type: "constructor"
  }, {
      anonymous: false,
      inputs: [{...}],
      name: "changedAge",
      type: "event"
  }],
  address: "0x438a135dc2d5466039c777d2bc59eeeb6b8f89c1",
  transactionHash: null,
  allEvents: function(),
  buyer: function(),
  changedAge: function(),
  owner: function(),
  setAge: function(),
  studentAge: function(),
  studentAttendance: function(),
  studentIncome: function(),
  studentScore: function()
}
> con.buyer
function()
> con.buyer()
"0x"
> con.owner()
"0x"
> con.address
"0x438a135dc2d5466039c777d2bc59eeeb6b8f89c1"


root@ubuntu-s-1vcpu-1gb-lon1-01:/geth/mybc/geth/zt/contracts# truffle migrate --reset
Compiling ./contracts/CAStorage.sol...
Writing artifacts to ./build/contracts

Using network 'development'.

Running migration: 1_initial_migration.js
  Replacing Migrations...
  ... 0x9804c03cc3a84417d8954fb54f3c874f44785400519f02d1ca48e96a0fe2a524
  Migrations: 0x28d9e2a0972fd59866581c1535b0bd1c447860d9
Saving successful migration to network...
  ... 0x7b0f04340c52e0ec55de27775ca205f3fd8e4bff718ca1353cba3c877f6799a0
Saving artifacts...
Running migration: 2_deploy_contracts.js
  Replacing CollegeAdmin...
  ... 0x6463e8e73e21373fa2611883896b9881576f5b1f6791ec95b3dd47ec17fba6bf
  CollegeAdmin: 0x438a135dc2d5466039c777d2bc59eeeb6b8f89c1
Saving successful migration to network...
  ... 0x1d3df599b04b8447913fc06bb00e37cfa6e9f1d5ac5fd8912d2f5c18da0aa689
Saving artifacts...
root@ubuntu-s-1vcpu-1gb-lon1-01:/geth/mybc/geth/zt/contracts# solc --bin --abi CollegeAdmin.sol
Warning: This is a pre-release compiler version, please do not use it in production.

======= CollegeAdmin.sol:CollegeAdmin =======
Binary: 
606060405260156000806101000a81548160ff021916908360ff160217905550613a98600060016101000a81548161ffff021916908361ffff1602179055506004600060036101000a81548160ff021916908360ff1602179055506060600060046101000a81548160ff021916908360ff160217905550341561008157600080fd5b33600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550610372806100d16000396000f300606060405260043610610083576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806311bc0d07146100885780635b0c8d70146100b95780637150d8ae146100df5780638da5cb5b14610134578063a0a628d614610189578063a918b856146101b8578063f4ea2d99146101e7575b600080fd5b341561009357600080fd5b61009b610216565b604051808261ffff1661ffff16815260200191505060405180910390f35b34156100c457600080fd5b6100dd600480803560ff1690602001909190505061022a565b005b34156100ea57600080fd5b6100f26102c2565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561013f57600080fd5b6101476102e8565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561019457600080fd5b61019c61030e565b604051808260ff1660ff16815260200191505060405180910390f35b34156101c357600080fd5b6101cb610321565b604051808260ff1660ff16815260200191505060405180910390f35b34156101f257600080fd5b6101fa610333565b604051808260ff1660ff16815260200191505060405180910390f35b600060019054906101000a900461ffff1681565b33600060056101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550806000806101000a81548160ff021916908360ff1602179055507f25d30a71a6b93533225c97277375c3d49d3451741fad4852f106d04a5f083b5a81604051808260ff16815260200191505060405180910390a150565b600060059054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600060049054906101000a900460ff1681565b6000809054906101000a900460ff1681565b600060039054906101000a900460ff16815600a165627a7a7230582054a9b41a66fdc2a697455eb8fb25e95227c80fce8e350c939a47d2c4e82942030029
Contract JSON ABI 
[{"constant":true,"inputs":[],"name":"studentIncome","outputs":[{"name":"","type":"uint16"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"age","type":"uint8"}],"name":"setAge","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"buyer","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"studentAttendance","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"studentAge","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"studentScore","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"age","type":"uint256"}],"name":"changedAge","type":"event"}]

I did more testing - and the issue is msg.sender. I just tested con.setAge(78) and that works. So the contract is working but the msg.sender is not working - it is finding 0x for msg.sender. This must be a truffle issue I assume.

Here is the truffle code -

root@ubuntu-s-1vcpu-1gb-lon1-01:/geth/mybc/geth/zt# more truffle.js
module.exports = {
   networks: {
   development: {
   host: "localhost",
   port: 8545,
   network_id: "*",
gas: 4712388
  }
 }
};

Here is the geth connection code -

root@ubuntu-s-1vcpu-1gb-lon1-01:/geth/mybc/geth# more gethlogin.sh
geth --datadir /geth/mybc --unlock "0x46fb9a22689c4a4bfb494baeafbb8b2993725305"  --rpc --rpcaddr "localhost" --rpcport 85
45 --networkid 1234 --rpcapi "admin,db,eth,debug,miner,shh,txpool,net,web3,personal" --verbosity 5  console 2> console.lo
g
2

Yes, the constructor is executed during contract deployment. If the contract is deployed successfully, then the constructor has run.

Given your results, I assume the contract was not deployed successfully (or con otherwise points to the wrong thing).

If you need help figuring out why your deployment didn't succeed, you'll need to provide details about how you're deploying. (What tool/framework, code you ran to deploy, output of that, etc.)

EDIT

From the comments: From your output, what's CAStorage.sol versus CollegeAdmin.sol? The former seems to be compiled by Truffle, and the latter seems to be where you're getting the ABI.

I believe this is a mismatch between the contract you meant to deploy and the one actually being deployed.

  • It did deploy. I tested setAge and that works. Somehow it is not updating owner and buyer. This is related to msg.sender somehow in geth I assume. It must be taking that to be 0x. I do not actually know how msg.sender is created but that is not being created correctly by the look of it. – Trevor Lee Oakley Feb 24 '18 at 5:05
  • So you called setAge(someValue) and then saw studentAge() return that value? If so, it looks like some contract was deployed successfully. My guess would be that the code for the contract that was deployed differs from what you've shared here. (Perhaps an older version of the code is being deployed?) – smarx Feb 24 '18 at 5:08
  • No, the issue is with msg.sender and truffle. I just updated the post. It is finding 0x from msg.sender. The con object shows owner so that must be current. There are getters for buyer and owner so it must be the current one. – Trevor Lee Oakley Feb 24 '18 at 5:10
  • That just means you're using the current ABI. – smarx Feb 24 '18 at 5:11
  • The ABI is shown in the post. It shows owner and buyer. The interface must be correct. I think the issue is msg.sender is set to 0x in solidity. – Trevor Lee Oakley Feb 24 '18 at 5:12
1

In ethereum the constructor of a contract gets executed when the contract is included in a block by a miner.

I created a private net using geth client and deployed your contract in it followed by mining for a couple of blocks. The results I am getting for

I set the variable "con" as

>con = eth.contract(CollegeAdmin1.abi).at("0xeea8a60269d0b1f8dab9134a1a2a771ec66ed5bd") {...all the info about contract goes here ..}

Where "0xeea8a60269d0b1f8dab9134a1a2a771ec66ed5bd" is the address of the contract after deployment.

> con.buyer()
"0x0000000000000000000000000000000000000000"

And for the

> con.owner()
"0xcc1cf13b816b12fb21325bb12906aac135a3af65"

"0xcc1cf13b816b12fb21325bb12906aac135a3af65" is a random address I created in my private network.

Make sure that you mine for a couple of blocks after deploying the contract.

  • This is a truffle issue - I just amended the post. – Trevor Lee Oakley Feb 24 '18 at 5:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.