(Windows) Geth Command Line Operation Smart Contract Notes

Preparation :

1. Compiler solc

2.Geth

3. Block data storage location change (windows, not necessary)

4. Contracts (such as: A  , B  )

Remarks :

solc I installed solcjs with local node.js. The online version is often auntie.

The corresponding compilation command:

solcjs --bin question.sol // binary

solcjs --abi question.sol // abi

---

Geth needs to be in the system environment variable

Get to work:

1. Open a command line anywhere 

2. To configure the private chain, please refer to Translation

geth  init init.json

{
	"nonce": "0x0000000000000042",
	"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
	"difficulty": "0x20000",
	"alloc": {},
	"coinbase": "0x0000000000000000000000000000000000000000",
	"timestamp": "0x00",
	"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
	"extraData": "blossom"
	"gasLimit": "0xffffffff"
}

 -- and enter the log into the specified file-- 2 may represent the mode here to indicate the background running log?

geth console 2>> D:/EthereumData/Ethereum/devnet/logs/geth.log  --dev --rpc

At this time, enter the console of geth


3. The contract used here is the above A (I changed the question attribute type to bytes before it was String)

pragma solidity ^ 0.4.0;
contract Questions {

  //global variables that aren't in a struct
  mapping(address => uint) public answers; //integer where 0 means hasn't answered, 1 means yes, 2 means no
  bytes question;
  address asker;
  uint trues;
  uint false;

  /// __init__
  function questions(bytes _question) public {
    asker = msg.sender;
question = _question;
  }

  //We need a way to validate whether or not they've answered before.
  //The default of a mapping is
  function answerQuestion (bool _answer) public {
    if (answers[msg.sender] == 0 && _answer) { //haven't answered yet
      answers[msg.sender] = 1; //they vote true
      trues + = 1;
}
    else if (answers[msg.sender] == 0 && !_answer) {
      answers[msg.sender] = 2; //falsity
      false + = 1;
    }
    else if (answers[msg.sender] == 2 && _answer) { // false switching to true
      answers[msg.sender] = 1; //true
      trues + = 1;
      false - = 1;
        }
    else if (answers[msg.sender] == 1 && !_answer) { // true switching to false
      answers[msg.sender] = 2; //falsity
      trues - = 1;
      false + = 1;
    }
  }

  function getQuestion() public constant returns (bytes, uint, uint, uint) {
    return (question, trues, falses, answers[msg.sender]);
      }
}

The corresponding abi is:

[{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"answers","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_question","type":"bytes"}],"name":"questions","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getQuestion","outputs":[{"name":"","type":"bytes"},{"name":"","type":"uint256"},{"name":"","type":"uint256"},{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_answer","type":"bool"}],"name":"answerQuestion","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}],"type":"function"},{"constant":false,"inputs":[{"name":"_answer","type":"bool"}],"name":"answerQuestion","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}],"type":"function"},{"constant":false,"inputs":[{"name":"_answer","type":"bool"}],"name":"answerQuestion","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]
The corresponding bin is
608060405234801561001057600080fd5b50610705806100206000396000f300608060405260043610610062576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680635e9618e714610067578063a869765a146100be578063eff38f9214610127578063f9e04961146101cc575b600080fd5b34801561007357600080fd5b506100a8600480360381019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506101fb565b6040518082815260200191505060405180910390f35b3480156100ca57600080fd5b50610125600480360381019080803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509192919290505050610213565b005b34801561013357600080fd5b5061013c61026e565b6040518080602001858152602001848152602001838152602001828103825286818151815260200191508051906020019080838360005b8381101561018e578082015181840152602081019050610173565b50505050905090810190601f1680156101bb5780820380516001836020036101000a031916815260200191505b509550505050505060405180910390f35b3480156101d857600080fd5b506101f9600480360381019080803515159060200190929190505050610366565b005b60006020528060005260406000206000915090505481565b33600260006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550806001908051906020019061026a929190610634565b5050565b6060600080600060016003546004546000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054838054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156103515780601f1061032657610100808354040283529160200191610351565b820191906000526020600020905b81548152906001019060200180831161033457829003601f168201915b50505050509350935093509350935090919293565b60008060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541480156103b15750805b156104105760016000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055506001600360008282540192505081905550610631565b60008060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205414801561045c575080155b156104bb5760026000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055506001600460008282540192505081905550610630565b60026000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541480156105065750805b156105765760016000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055506001600360008282540192505081905550600160046000828254039250508190555061062f565b60016000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541480156105c2575080155b1561062e5760026000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550600160036000828254039250508190555060016004600082825401925050819055505b5b5b5b50565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061067557805160ff19168380011785556106a3565b828001600101855582156106a3579182015b828111156106a2578251825591602001919060010190610687565b5b5090506106b091906106b4565b5090565b6106d691905b808211156106d25760008160009055506001016106ba565b5090565b905600a165627a7a7230582088bb66279f826e4efd502bbbfc2acbac3b299054775e7fff4464485d43b473000029

4. Define the contract

abi=[{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"answers","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_question","type":"bytes"}],"name":"questions","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getQuestion","outputs":[{"name":"","type":"bytes"},{"name":"","type":"uint256"},{"name":"","type":"uint256"},{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_answer","type":"bool"}],"name":"answerQuestion","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]view","type":"function"},{"constant":false,"inputs":[{"name":"_answer","type":"bool"}],"name":"answerQuestion","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]view","type":"function"},{"constant":false,"inputs":[{"name":"_answer","type":"bool"}],"name":"answerQuestion","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]

contractQ=eth.contract(abi)

5. Unlock account

personal.unlockAccount(eth.coinbase)


The coinbase of the default private chain has some money, the default password is empty and just press Enter

6. Create and initiate a contract

transQ=contractQ.new({from:eth.coinbase,data:'0x608060405234801561001057600080fd5b50610705806100206000396000f300608060405260043610610062576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680635e9618e714610067578063a869765a146100be578063eff38f9214610127578063f9e04961146101cc575b600080fd5b34801561007357600080fd5b506100a8600480360381019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506101fb565b6040518082815260200191505060405180910390f35b3480156100ca57600080fd5b50610125600480360381019080803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509192919290505050610213565b005b34801561013357600080fd5b5061013c61026e565b6040518080602001858152602001848152602001838152602001828103825286818151815260200191508051906020019080838360005b8381101561018e578082015181840152602081019050610173565b50505050905090810190601f1680156101bb5780820380516001836020036101000a031916815260200191505b509550505050505060405180910390f35b3480156101d857600080fd5b506101f9600480360381019080803515159060200190929190505050610366565b005b60006020528060005260406000206000915090505481565b33600260006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550806001908051906020019061026a929190610634565b5050565b6060600080600060016003546004546000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054838054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156103515780601f1061032657610100808354040283529160200191610351565b820191906000526020600020905b81548152906001019060200180831161033457829003601f168201915b50505050509350935093509350935090919293565b60008060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541480156103b15750805b156104105760016000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055506001600360008282540192505081905550610631565b60008060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205414801561045c575080155b156104bb5760026000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055506001600460008282540192505081905550610630565b60026000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541480156105065750805b156105765760016000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055506001600360008282540192505081905550600160046000828254039250508190555061062f565b60016000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541480156105c2575080155b1561062e5760026000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550600160036000828254039250508190555060016004600082825401925050819055505b5b5b5b50565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061067557805160ff19168380011785556106a3565b828001600101855582156106a3579182015b828111156106a2578251825591602001919060010190610687565b5b5090506106b091906106b4565b5090565b6106d691905b808211156106d25760008160009055506001016106ba565b5090565b905600a165627a7a7230582088bb66279f826e4efd502bbbfc2acbac3b299054775e7fff4464485d43b473000029',gas:'4700000'})

The data content is the binary bin of the contract

There will be an entry in the log


Just wait for the miners to mine

7. Check the contract release status

transReceipt=eth.getTransactionReceipt(transQ.transactionHash)


status 0x1 means success 0x0 means failure contractAddress is the contract address

8. Get the contract object through the contract address

entityQ = contractQ.at(transReceipt.contractAddress)

9. Call the query method getQuestion() of the contract

entityQ.getQuestion()


The first call to the question property should be null byte 0x, all others are 0

10. Call the contract method through the transaction transaction questions

txSend = eth.sendTransaction ({ "from": eth.coinbase, "": transReceipt.contractAddress, "data": "0xa869765a00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000014697320636f6e747261637420737563636573733f000000000000000000000000"})

Here is a brief description of the composition of data data, which is more complicated. . Start after 0x

The first 4 bytes--that is, the first 8 bits are the first 8 bits of the sha3 value of the method + parameter type questions(bytes)


The latter parameters are processed differently depending on whether the parameter type is dynamic or static. Dynamically create a placeholder first, and then fill in the specific content. Just fill it in statically, see the documentation for details

note:

This thing is more tiring to do by yourself, it is better to call the method directly and return the result. .

Get the contract object, select the corresponding method questions, call getData, and fill in the parameters will directly generate the required data binary parameters

entityQ.questions.getData("is contract success?")

11. Check the transaction status

eth.getTransaction (txSend)

12. Query the contract's getQuestion() to check for changes

entityQ.getQuestion()

Here the question attribute has changed, and the content is the hexadecimal content of "is contract success?".

This means that the contract has been called successfully. .

references:

contract document

solidity documentation

Example A   Example B

Geth Command Documentation

end

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325928357&siteId=291194637