区块链-以太坊

以太坊白皮书中文

以太坊白皮书上说比特币系统的脚本语言存在一些严重的限制,缺少图灵完备性

什么是图灵机

什么是图灵机

只要图灵机可以被实现,就可以用来解决任何可计算问题

什么是图灵完备

能模拟图灵机,可以解决所有可计算的问题

区块链2.0核心-智能合约

在比特币系统中,转账记录被封存在主链中,不能被更改。而在2.0中,思考了如果每个默克树的叶子节点不单单一个转账记录的哈希,而是一份代码的哈希,那么这个代码也具有不可串改的特性。所以区块链2.0的重点是开发一个可编程的区块链。

其次,在比特币系统中,要维护这样的一个系统需要自己上主网,每个主网都有若干个旷工节点。而如果开发另一种币,是否也需要这一整套的系统。而智能合约代币就可以解决这个痛点。通过合约发布的代币也可以具有流通属性,并且多个币种可以共用一套旷工节点完成交易。

什么是智能合约

智能合约其实就是一段代码,或者说一个对象。它能干什么,取决于你的代码写了什么。比如这个合约例子

在线智能合约IDE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
pragma solidity ^0.4.0;
contract Ballot {

struct Voter {
uint weight;
bool voted;
uint8 vote;
address delegate;
}
struct Proposal {
uint voteCount;
}

address chairperson;
mapping(address => Voter) voters;
Proposal[] proposals;

/// Create a new ballot with $(_numProposals) different proposals.
function Ballot(uint8 _numProposals) public {
chairperson = msg.sender;
voters[chairperson].weight = 1;
proposals.length = _numProposals;
}

/// Give $(toVoter) the right to vote on this ballot.
/// May only be called by $(chairperson).
function giveRightToVote(address toVoter) public {
if (msg.sender != chairperson || voters[toVoter].voted) return;
voters[toVoter].weight = 1;
}

/// Delegate your vote to the voter $(to).
function delegate(address to) public {
Voter storage sender = voters[msg.sender]; // assigns reference
if (sender.voted) return;
while (voters[to].delegate != address(0) && voters[to].delegate != msg.sender)
to = voters[to].delegate;
if (to == msg.sender) return;
sender.voted = true;
sender.delegate = to;
Voter storage delegateTo = voters[to];
if (delegateTo.voted)
proposals[delegateTo.vote].voteCount += sender.weight;
else
delegateTo.weight += sender.weight;
}

/// Give a single vote to proposal $(toProposal).
function vote(uint8 toProposal) public {
Voter storage sender = voters[msg.sender];
if (sender.voted || toProposal >= proposals.length) return;
sender.voted = true;
sender.vote = toProposal;
proposals[toProposal].voteCount += sender.weight;
}

function winningProposal() public constant returns (uint8 _winningProposal) {
uint256 winningVoteCount = 0;
for (uint8 prop = 0; prop < proposals.length; prop++)
if (proposals[prop].voteCount > winningVoteCount) {
winningVoteCount = proposals[prop].voteCount;
_winningProposal = prop;
}
}
}

如果以上代码被编译为以太坊虚拟机的字节码,经过个人账户发布后,被封存在了以太坊主链中,他就是一个永远不可更改的协议。这份代码实现了一个投票选举的例子。

代币部署

同理,如果代码中实现了代币逻辑,那么执行代币空投,代币转账流通功能也就是一个智能合约的方法调用的过程而已。

这个代币不知道为何看不到合约,可能是我看不懂

看这个被部署的PNS 代币

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
pragma solidity ^0.4.8;

contract ERC20Interface {
function totalSupply() public constant returns (uint256 supply);
function balance() public constant returns (uint256);
function balanceOf(address _owner) public constant returns (uint256);
function transfer(address _to, uint256 _value) public returns (bool success);
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success);
function approve(address _spender, uint256 _value) public returns (bool success);
function allowance(address _owner, address _spender) public constant returns (uint256 remaining);

event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}

// penispenispenispenis
// YOU get a penis, and YOU get a penis, and YOU get a penis!
contract Penis is ERC20Interface {
string public constant symbol = "PNS";
string public constant name = "Penis";
uint8 public constant decimals = 2;

uint256 _totalSupply = 0;
uint256 _airdropAmount = 8008135;
uint256 _cutoff = _airdropAmount * 80085;

mapping(address => uint256) balances;
mapping(address => bool) initialized;

// Penis accepts request to tip-touch another Penis
mapping(address => mapping (address => uint256)) allowed;

function Penis() {
initialized[msg.sender] = true;
balances[msg.sender] = _airdropAmount * 8008;
_totalSupply = balances[msg.sender];
}

function totalSupply() constant returns (uint256 supply) {
return _totalSupply;
}

// What's my girth?
function balance() constant returns (uint256) {
return getBalance(msg.sender);
}

// What is the length of a particular Penis?
function balanceOf(address _address) constant returns (uint256) {
return getBalance(_address);
}

// Tenderly remove hand from Penis and place on another Penis
function transfer(address _to, uint256 _amount) returns (bool success) {
initialize(msg.sender);

if (balances[msg.sender] >= _amount
&& _amount > 0) {
initialize(_to);
if (balances[_to] + _amount > balances[_to]) {

balances[msg.sender] -= _amount;
balances[_to] += _amount;

Transfer(msg.sender, _to, _amount);

return true;
} else {
return false;
}
} else {
return false;
}
}

// Perform the inevitable actions which cause release of that which each Penis
// is built to deliver. In EtherPenisLand there are only Penises, so this
// allows the transmission of one Penis's payload (or partial payload but that
// is not as much fun) INTO another Penis. This causes the Penisae to change
// form such that all may see the glory they each represent. Erections.
function transferFrom(address _from, address _to, uint256 _amount) returns (bool success) {
initialize(_from);

if (balances[_from] >= _amount
&& allowed[_from][msg.sender] >= _amount
&& _amount > 0) {
initialize(_to);
if (balances[_to] + _amount > balances[_to]) {

balances[_from] -= _amount;
allowed[_from][msg.sender] -= _amount;
balances[_to] += _amount;

Transfer(_from, _to, _amount);

return true;
} else {
return false;
}
} else {
return false;
}
}

// Allow splooger to cause a payload release from your Penis, multiple times, up to
// the point at which no further release is possible..
function approve(address _spender, uint256 _amount) returns (bool success) {
allowed[msg.sender][_spender] = _amount;
Approval(msg.sender, _spender, _amount);
return true;
}

function allowance(address _owner, address _spender) constant returns (uint256 remaining) {
return allowed[_owner][_spender];
}

// internal privats
function initialize(address _address) internal returns (bool success) {
if (_totalSupply < _cutoff && !initialized[_address]) {
initialized[_address] = true;
balances[_address] = _airdropAmount;
_totalSupply += _airdropAmount;
}
return true;
}

function getBalance(address _address) internal returns (uint256) {
if (_totalSupply < _cutoff && !initialized[_address]) {
return balances[_address] + _airdropAmount;
}
else {
return balances[_address];
}
}
}

这个代币,就是简单的几个方法而已。