What is a smart contractAn intelligent contract is a set of commitments in digital form (promises), including agreements in which contract participants can implement these commitments. A contract consists of a set of code (a function of the contract) and data (the state of the contract) and runs on the etheric square virtual machine.
The Ethernet square virtual machine (EVM), which uses a 256-bit-length machine code, is a stack based virtual machine for the implementation of the etheric Square Intelligent Contract . Because EVM is designed for the etheric square system, the Ethernet square account model is used for value transmission . What is the capability of the contract code:
Read transaction data.
read or write the contract's own storage space.
Read environment variable "block high, hash value, gas"
sends an "insider deal" to another contract.
The architecture of the block chain platform
Architecture of block chain platform 1. What is solidity
Solidity is a high-level language for intelligent contracts, running on top of Ethereum virtual machines (EVM).
Solidity language Features
Its syntax is close to JavaScript and is an object-oriented language. But as a real sense of the network to run the central contract, it has a lot of different points: anomaly mechanism, similar to the atomic nature of the transaction. In the event of an exception, all executions will be withdrawn, primarily to ensure the atomic nature of the contract execution to avoid data inconsistencies in the intermediate state. The operating environment is on a centralized network, comparing the way in which a contract or function performs a call. Because a simple function call turned into a network of nodes in the code execution store is using a block chain on the network, each state of the data can be stored permanently. 2. Tools developed Online compiler remix Visual Studio Code + soliidty plugin 3 QuickStart Preparation: building block chains
One-click Deployment block chain Platform 3.1 for example
Complete steps:
1. Write contract
2. Build contract
3. Deployment Contract
4. Test contract
Get Demo For example
reference Operation Steps
$ git clone "Https://github.com/cristicmf/bcos-qucik-start-demo"
$ cd Startdemo
$ npm Install
$ babel-node Index.js
File Structure Description
Startdemo
├──readme.md
├──simplestartdemo.sol # Contract Code
├──codeutils.js
├──config.js # config file
├──index.js # Deployment contract and test contract
├──output # abi/bin/address output
│├──startdemo.abi
│├──star Tdemo.address
│└──startdemo.bin
├──package.json
├──sha3.js
└──web3sync.js
Get Examples
pragma solidity ^0.4.2;
Contract Simplestartdemo {
int256 storeddata;
Event addmsg (address indexed sender, Bytes32 msg);
Modifier only_with_at_least (int x) {
if (x >= 5) {
x = x+10;
_;
}
}
function Simplestartdemo () {
storeddata = 2;
}
function SetData (int256 x) public only_with_at_least (x) {
storeddata = x;
Addmsg (Msg.sender, "[In the ' Set () method]");
}
function GetData () constant public Returns (int256 _ret) {
addmsg (Msg.sender, "[in The Get () method]");
return _ret = Storeddata
}
}
3.2 Department Contracts
Give me an example get demo
$ Babel-node Index.js
1. Compile Contract
Execsync ("Solc--abi --bin --overwrite-o" + CONFIG.) Ouputpath + " " + filename + ". Sol");
2. Deployment contracts to the block chain
var Contract = await Web3sync.rawdeploy (config.account, config.privkey, filename);
3. Read and write the contract
var address = Fs.readfilesync (config. Ouputpath + filename + '. Address ', ' utf-8 ');
var abi = json.parse (fs.readfilesync (config). Ouputpath/*+filename+ ". Sol:" * * + filename + '. Abi ', ' utf-8 ');
var contract = Web3.eth.contract (ABI);
var instance = contract.at (address);
Gets the data var on the chain
= Instance.getdata ();
Modify the data on the chain
var func = "SetData (int256)";
var params = [ten];
var receipt = await web3sync.sendrawtransaction (Config.account, Config.privkey, Address, func, params);
3.2.1 Introduces concepts:
Address: The length of the Ethernet square, size 20 bytes, 160 bits, so you can use a uint160 encoding. Address is the basis of all contracts, all contracts will inherit the address object, you can always put an address string, the corresponding code to invoke. The address of the contract is based on the number of random accounts and the hash of the transaction data.
ABI: A message format that is called by an Ethernet house or when a message is sent. is to define the operation function signature, the parameter code, the return result code and so on.
Transaction: The "transaction" in the ether is a signature packet that stores messages sent from an external account.
Simple understanding is: As long as the block chain to write operations, there will be transactions.
Transaction receipt:
return value after transaction 3.2.2 Extended reading: Ethereum-contract-abi solidity-features ether White Paper 3.3 contract Document structure 1. Version Statement
pragma solidity ^0.4.10;
1. Referencing other source files
Import "filename";//Global Introduction
1. Status variable (state Variables)
int256 Storeddata;
For more information, see below 2. function (functions)
function SetData (int256 x) public {
storeddata = x;
Addmsg (Msg.sender, "[In the ' Set () method]");
}
function GetData () constant public Returns (Int256 _ret) {return
_ret = Storeddata;
}
3. Event (events)
//Events Declaration event addmsg (address indexed sender, Bytes32 msg); The use function of the event SetData (int256 x)