Skip to content

Commit

Permalink
[CN]Update smart contract (neo-project#489)
Browse files Browse the repository at this point in the history
Added the following:
test, notices, overview

Updated:
introduction, python
  • Loading branch information
Celia18305 authored May 3, 2018
1 parent 44fedd1 commit 6bc9800
Show file tree
Hide file tree
Showing 10 changed files with 515 additions and 270 deletions.
Binary file added assets/test1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/test2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/test3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/test4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 10 additions & 4 deletions zh-cn/sc/introduction.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
# NEO 智能合约介绍
# NEO 智能合约基础知识

## 什么是智能合约
## 智能合约介绍

### 什么是智能合约

智能合约是一套以数字形式定义的承诺,包括合约参与方可以在上面执行这些承诺的协议。区块链技术给我们带来了一个去中心化的,不可篡改的,高可靠性的系统,在这种环境下,智能合约才大有用武之地。智能合约是区块链最重要的特性之一,也是区块链能够被称为颠覆性技术的主要原因。

## NEO 智能合约有哪些特点
### NEO 智能合约有哪些特点

NEO 智能合约 2.0包括以下特性:确定性、高性能、拓展性。其合约类型包括:验证合约、函数合约和应用合约。

从性能角度来说,NEO 采用了轻量级的 NeoVM(NEO Virtual Machine)作为其智能合约的执行环境,其启动速度非常快,占用资源也很小,适合像智能合约这样短小的程序。通过 JIT(即时编译器)技术对热点智能合约进行静态编译和缓存可以显著提升。NEO 虚拟机的指令集中内建提供了一系列的密码学指令,以优化智能合约中用到密码学算法时的执行效率。此外,数据操作指令直接对数组及复杂数据结构提供支持。这些都会提升 NEO 智能合约 2.0的运行性能。

NEO 智能合约 2.0实现可拓展性的方法是通过高并发和动态分区的形式,结合其低耦合的设计完成的。低耦合合约程序在一个虚拟机(NEO 虚拟机)中执行,并通过交互服务层与外部通信。因此,对智能合约功能的绝大部分升级,都可以通过增加交互服务层的 API 来实现。

## 用什么语言编写智能合约
### 用什么语言编写智能合约

从语言角度看 NEO 智能合约 2.0与以太坊的区别更为直观:与以太坊原创的 Solidity 语言不同,NEO 智能合约开发者可以直接使用几乎任何他们擅长的高级语言来进行 NEO 智能合约的开发工作。NEO 提供了这些语言的编译器和插件,用于将高级语言编译成 NEO 虚拟机所支持的指令集。由于编译器会针对 MSIL(微软中间语言)来进行编译,所以理论上任何.Net 中的语言或者可被转译成 MSIL 的语言都可以直接支持。

Expand Down Expand Up @@ -41,6 +43,10 @@ NEO 的智能合约有两种触发方式:
- 合约用户的鉴权:智能合约作为一个合约账户,当用户使用该合约账户里的一笔资产时,会触发该智能合约。
- 手动发送交易调用智能合约:用户发送一笔交易(Invocation Transaction )来触发一段智能合约的执行。

一个合约可以同时由以上两种方式触发。由于鉴权触发的合约是UTXO模型的鉴证过程,是在交易被写入区块之前执行。如果合约返回false或者发生异常,则交易不会被写入区块。

而由交易调用触发的合约,它的调用时机是交易被写入区块以后,此时无论应用合约返回为何以及是否失败,交易都已经发生,无法影响交易中的 UTXO 资产状态。

## NeoVM 虚拟机

NeoVM 是执行 NEO 智能合约代码的虚拟机。这里所讲述的虚拟机概念比较狭义,并非是借助于操作系统对物理机器的一种模拟,这里的虚拟机与 vmware 或者 Hyper-V 不同,是针对具体语言所实现的虚拟机。
Expand Down
181 changes: 1 addition & 180 deletions zh-cn/sc/quickstart/getting-started-python.md
Original file line number Diff line number Diff line change
@@ -1,182 +1,3 @@
# 如何开始(neo-python)

*作者:[@localhuman](https://github.com/localhuman)*

neo-python,NEO区块链的python节点和SDK,能帮助您在NEO区块链上构建、测试和运行智能合约。其主要功能如下:

- 运行一个基于Python的P2P节点
- 提供用于配置节点和检查区块链的交互式CLI
- 编译、测试、部署和运行python编写的智能合约以及任何.avm格式的智能合约
- 符合[NEP2](https://github.com/neo-project/proposals/blob/master/nep-2.mediawiki)[NEP5](https://github.com/neo-project/proposals/blob/master/nep-5.mediawiki) 标准的钱包功能
- RPC 客户端
- `Runtime.Log``Runtime.Notify` 事件监视

## 安装 Python 库

您需要安装 libleveldb 库。建议安装 [Python 3.5](https://www.python.org/downloads/release/python-354/) 以免遇到因为与当前维护者版本不同而出现的问题。由于不兼容字节播放模块,当前不支持Python 3.6。

**对于 OSX**,输入以下命令:

```python
brew install leveldb
```

如果遇到以下类似问题:

```python
from ._plyvel import ( # noqa
ImportError: dlopen(neo-python/venv/lib/python3.5/site-packages/plyvel/_plyvel.cpython-35m-darwin.so, 2): Symbol not found: __ZN7leveldb2DB4OpenERKNS_7Options
ERKSsPPS0_
Referenced from: neo-python/venv/lib/python3.5/site-packages/plyvel/_plyvel.cpython-35m-darwin.so
Expected in: flat namespace
```

可以卸载 plyvel(python libleveldb 库)然后使用以下 cflags 重新安装:

```python
pip uninstall plyvel
CFLAGS='-mmacosx-version-min=10.7 -stdlib=libc++' pip install --no-use-wheel plyvel --no-cache-dir --global-option=build_ext --global-option="-I/usr/local/Cellar/leveldb/1.20_2/include/" --global-option="-L/usr/local/lib"
```

如果在OSX上安装 pycrypto 时遇到以下问题:

```python
src/_fastmath.c:36:11: fatal error: 'gmp.h' file not found
# include <gmp.h>
^~~~~~~
330 warnings and 1 error generated.
error: command 'clang' failed with exit status 1
```

可以使用 homebrew 安装 gmp库,并使用以下命令运行pip install:

```python
brew install gmp
CFLAGS='-mmacosx-version-min=10.7 -stdlib=libc++' pip install --no-use-wheel pycrypto --no-cache-dir --global-option=build_ext --global-option="-I/usr/local/Cellar/gmp/6.1.2/include/" --global-option="-L/usr/local/lib"
```

**对于 Ubuntu/Debian**,输入以下命令:

```python
apt-get install libleveldb-dev python3.5-dev python3-pip libssl-dev
```

**对于 Centos/Redhat/Fedora**,输入以下命令:

```python
yum -y install development tools python35 python35-devel python35-pip readline-devel leveldb-devel libffi-devel
```

您可能需要为 leveldb-devel 包启用epel存储库,可以编辑`/etc/yum.repos.d/epel.repo `来启用。

> [!Note]
>
> 对于以上系统,请确保项目中的Chains目录具有正确的写入权限。

**对于 Windows:**

尚未测试。安装 Python 包 plyvel 可能需要 C ++ 编译器支持绑定到Visual Studio和库。

## 建立虚拟环境

1. 克隆 <https://github.com/CityOfZion/neo-python/> 上的存储库并导航到项目目录。

2. 创建一个 Python 3 虚拟环境并使用以下命令激活:

```python
python3 m venv venv
source venv/bin/activate
```

或显示安装 Python 3.5:

```python
virtualenv -p /usr/local/bin/python3.5 venv
source venv/bin/activate
```

3. 安装需求文件

```python
pip install -r requirements.txt
```

4. 安装一个对neo工作目录的引用,以允许从项目的任何地方导入neo,例如

```python
pip install -e
```

> [!Note]
>
> - 如果使用`git pull`更新 neo-python,请同样使用 `pip install -r requirements.txt`更新依赖关系。
>
>
> - 如果是第一次使用 neo-python,则需要同步区块链,这可能需要很长时间。 包含在这个项目中的bootstrap.py将自动下载一个链式目录。要在测试网引导程序,运行 `python bootstrap.py`;要在主网引导程序,使用 `python bootstrap.py -m`.

## 运行基于 python 的节点

在安装需求并激活环境之后,使用`prompt.py`文件来运行节点和基本的交互功能。

以下示例在测试网启动`prompt.py`

```python
python prompt.py
NEO cli. Type 'help' to get started

neo> state
Progress: 1054913 / 1237188

neo>
```

您可以使用`help`命令查看所有可用参数:

```python
python prompt.py -h
usage: prompt.py [-h] [-m] [-p] [-c CONFIG] [-t {dark,light}] [--version]

optional arguments:
-h, --help show this help message and exit
-m, --mainnet Use MainNet instead of the default TestNet
-p, --privnet Use PrivNet instead of the default TestNet
-c CONFIG, --config CONFIG
Use a specific config file
-t {dark,light}, --set-default-theme {dark,light}
Set the default theme to be loaded from the config
file. Default: 'dark'
--version show program's version number and exit
```

您可以通过散列或区块索引查询当前服务器中的区块:

```python
python prompt.py
NEO cli. Type 'help' to get started

neo> block 122235
{
"index": 122235,
"script": "",
"merkleroot": "1d5a895ea34509a83becb5d2f9391018a3f59d670d94a2c3f8deb509a07464bd",
"previousblockhash": "98ae05cb68ab857659cc6c8379eb7ba68b57ef1f5317904c295341d82d0a1713",
"tx": [
"1d5a895ea34509a83becb5d2f9391018a3f59d670d94a2c3f8deb509a07464bd"
],
"version": 0,
"time": 1479110368,
"hash": "74671375033f506325ef08d35632f74083cca564dc7ea6444c94d3b9dec3f61b",
"consensus data": 16070047272025254767,
"next_consensus": "59e75d652b5d3827bf04c165bbe9ef95cca4bf55"
}
neo>
```

## 扩展阅读

- [使用命令行与NEO区块链交互](python\prompt.md)
- [设置和日志](python\logging.md)
- [与智能合约交互](python\smartcont.md)
- [测试](python\tests.md)
- [python编译器](python\compiler.md)

与 Python 相关的内容由 [neo-python](https://github.com/CityOfZion/neo-python/blob/master/README.rst) 项目维护。
Loading

0 comments on commit 6bc9800

Please sign in to comment.