区块链hello world 思路:
根据学习笔记一知道,一个区块链需要包含如下信息
1:交易数据
2:区块信息(hash值 工作量等)
基础方法:
交易功能 functon(从A,给B,100元)
addTransactions(A,B,100)
"message": [
"这笔交易会被增加到区块4"
]
}
以上数据是在我请求接口时候截取,之所以放在第4个块,是因为我写笔记的时候已经挖到了4个块,前3个块已经打包。
挖出新块functon(最近的交易打包成区块后,新增一个区块)
做了以下三件事
计算工作量证明PoW
通过新增一个交易授予矿工(自己)一个币
构造新区块并将其添加到链中
addNewBlock(新交易数据,上一个区块hash值)
{
"index": 5,
"proof": 146502,
"message": "New Block Forged",
"transactions": [
{
"amount": 1,
"sender": "0",
"recipient": "a9b92e8a11d447639a1f1cdb1954fc48"
}
],
"previous_hash": "40d003f3f756200731c4d2109be15fa718056d95bf57fe0ccaa18f8195a6e841"
}
查询当前区块所有信息functon(当前所有区块,所有的交易)
{
"chain": [
{
"index": 1,
"proof": 100,
"transactions": [],
"timestamp": 1523528930232,
"previous_hash": "0"
},
{
"index": 2,
"proof": 35293,
"transactions": [
{
"amount": 126,
"sender": "程小凡",
"recipient": "张小"
},
{
"amount": 1,
"sender": "0",
"recipient": "a9b92e8a11d447639a1f1cdb1954fc48"
}
],
"timestamp": 1523528945659,
"previous_hash": "77f2390ba87ce4f8427a7f3f2427cc463e9d0a9086e104ea2f99ed2070c08656"
},
{
"index": 3,
"proof": 35089,
"transactions": [
{
"amount": 1260,
"sender": "程二凡",
"recipient": "张小二"
},
{
"amount": 1,
"sender": "0",
"recipient": "a9b92e8a11d447639a1f1cdb1954fc48"
}
],
"timestamp": 1523528972115,
"previous_hash": "4c1799601a670d28a1ce508c3c89de94c4f87ad59f5d89b73a1288d9f234b17e"
},
{
"index": 4,
"proof": 119678,
"transactions": [
{
"amount": 1260,
"sender": "李浩",
"recipient": "金正"
},
{
"amount": 1260,
"sender": "李浩",
"recipient": "金正"
},
{
"amount": 1260,
"sender": "李浩",
"recipient": "金正"
},
{
"amount": 1260,
"sender": "李浩",
"recipient": "金正"
},
{
"amount": 1,
"sender": "0",
"recipient": "a9b92e8a11d447639a1f1cdb1954fc48"
}
],
"timestamp": 1523529024979,
"previous_hash": "9e4c840d8f01fa8decea1eac4fcbb8e15357fb7ddea7bd64d8a99c222ef6f147"
},
{
"index": 5,
"proof": 146502,
"transactions": [
{
"amount": 1,
"sender": "0",
"recipient": "a9b92e8a11d447639a1f1cdb1954fc48"
}
],
"timestamp": 1523585237009,
"previous_hash": "40d003f3f756200731c4d2109be15fa718056d95bf57fe0ccaa18f8195a6e841"
}
],
"length": 5
}
区块的结构
首先需要说明一下区块的结构,每个区块包含属性:索引(index),时间戳(timestamp),交易列表(transactions),工作量证明,以及前一个区块的Hash值。
创世区块是没有前一个区块的,类比Java的 List
区块的结构基础信息:
block = {
'index': 1,
'timestamp': 1506057125.900785,
'transactions': [
{
'sender': "发送者的银行卡号",
'recipient': "接受者的银行卡号",
'amount': 5,
} ,{
"amount": 1260,
"sender": "李浩",
"recipient": "金正"
},
{
"amount": 1260,
"sender": "李浩",
"recipient": "金正"
}
],
'proof': 愿意给的转账手续费,可以自定义,
'previous_hash': "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
}
学习笔记只在学习区块链,了解区块链,属于helloworld程序,
和真实的场景,类比接口,复杂度等都不一样,仅在学习其原理,术语
甚至因为我自己可能有误解~,仅在为测试区块链做一些准备。
新手入门资料:
3cshool
http://www.runoob.com/w3cnote/blockchain-intro.html
高手进阶 以太坊
http://xc.hubwiz.com/course/5a952991adb3847553d205d1?affid=51cto