Build a new Ethereum node with an image and deploy the contract (demo)

We have built a mirror ourselves, which contains the environment required by Ethereum and the genesis block of this private chain. Now run a container with this image, and we can quickly build a node connected to the private chain.
1 docker run -it –name node6 daocloud.io/ubuntu/node
2 The next operations are all in the node6 container. We have put the genesis block in the privategeth directory, so when we enter this directory, there is only the genesis.json file in the directory:

cd privategeth

Can see our genesis block

{
   "config": {
        "chainId": 12345,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
    "coinbase" : "0x0000000000000000000000000000000000000000",
    "difficulty" : "0x400",
    "extraData" : "0x123456",
    "gasLimit" : "0xffffffff",
    "nonce" : "0x0000000000000042",
    "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    "timestamp" : "0x00",
    "alloc": { }
}

2.1 Create the directory path for data storage and initialize the genesis block

geth –datadir ./data  init genesis.json 
  • List content

    The back of –datadir indicates the location where the blockchain network data is to be stored. Here it is placed in data. At this time, there is more data in the directory, and data stores the geth and keystore directories that were just initialized and generated. The main information of the blockchain is placed under geth, and the keystore is used to store the account of the node, which is empty at this time.

2.2 Enter the interactive platform of geth:

geth –identity “haha” –datadir ./data –networkid 12345 –rpcapi “db,eth,net,web3” –port 2220 –rpcport 3330 –bootnodes “enode://5d1f52b69d7473389e77475a1e972f45215b184ad6734e9b77088f00cc311fe493ed8352359685dee79c64db50041351da60785801328cb5ada82ab206287d1c@172.18.0.2:2220” console
  • –identity is used to indicate the name of the blockchain network, just write it
  • datadir is consistent with initialization, used to store blockchain data
  • --networked Set the network ID of the blockchain to identify different networks
  • --rpc starts rpc communication, which can be used for deployment and invocation of smart contracts
  • --port network listening port
  • --bootnodes is used to connect to a node, here we set the information of the master node

3 Now we enter the geth interactive platform and start some basic operations

  • View account information eth.accounts
  • Create a new account personal.newAccount()
  • View the address of account 0 eth.accounts[0]
  • View account 1 balance eth.getBalance(eth.accounts[1])
  • Account 0 unlocks personal.unlockAccount(eth.accounts[0])
  • Account 0 initiates a transaction transfer to account 1 and 3 ether, the account needs to be unlocked to initiate a transaction eth.sendTransaction({from:eth.accounts[0],to:eth.accounts,value:web3.toWei(3,'ether') })
  • View transaction pool txpool.status
  • View the specific information of the transaction through the transaction hash value eth.getTransaction("0x521c52450559bc98d41e29823c53be0036bfffbe142db489d19065e2e9c9887a")
  • Mining miner.start()
  • Stop mining minier.stop()
  • View block height eth.blockNumber
  • View the specific information of a block eth.getBlock(79)

4 Deploy and invoke smart contracts

 function Sample(uint v){
                value=v;
      }
      function set(uint v){
                value=v;
      }
      function get() constant returns (uint){
                return value;
      }
} 

1) First compile the contract in the browser compiler of the Ethereum smart contract, get the abi interface and binary code and
assign it to abi

abi=[{"constant":true,"inputs":[],"name":"value","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"v","type":"uint256"}],"name":"set","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"v","type":"uint256"}],"payable":false,"type":"constructor"}]

2) You need to use eth.contract to define a contract class

sample=eth.contract(abi)

3) Assign the binary code of the contract to SampleHEX for easy use SampleHEX="0x6060604052341561000c57fe5b60405160208061013a833981016040528080519060200190919050505b806000819055505b505b60f9806100416000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633fa4f24514604e57806360fe47b11460715780636d4ce63c14608e575bfe5b3415605557fe5b605b60b1565b6040518082815260200191505060405180910390f35b3415607857fe5b608c600480803590602001909190505060b7565b005b3415609557fe5b609b60c2565b6040518082815260200191505060405180910390f35b60005481565b806000819055505b50565b600060005490505b905600a165627a7a72305820208c8101070c8ba5a9b32db2bf4b8062a9ba50bc2869c39ac2297938756540e80029"
4) Deploy the contract and pass 1 to the contract for an initialization

thesample=sample.new(1,{from:eth.accounts[0],data:SampleHEX,gas:3000000})

5) View contract details through the transaction hash returned by the deployed contract

samplerecpt=eth.getTransactionReceipt("0x1d875c7ea3a19458c0ec1473f6c643ebb90c400406f3c160660d12aa3a520995")

6) Name the contract by the address of the contract obtained from the transaction details

samplecontract=sample.at("0x80a52a1e0daa54dc6ce3a5bea84cce9c39b162e5")

7) Call the contract
Call the get() function to view, and the value of the variable has not been changed, so you can use call without spending gas

samplecontract.get.call() 

Call the set() function to change the contract parameters, so it must be in the form of a transaction,

samplecontract.set.sendTransaction(9, {from:eth.accounts[0], gas:3000000}) 

Guess you like

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