{"id":134,"date":"2018-07-23T22:31:51","date_gmt":"2018-07-23T14:31:51","guid":{"rendered":"https:\/\/blog.indeex.club\/?p=134"},"modified":"2020-06-20T23:09:39","modified_gmt":"2020-06-20T15:09:39","slug":"%e5%8c%ba%e5%9d%97%e9%93%be%e5%9f%ba%e7%a1%80","status":"publish","type":"post","link":"https:\/\/blog.indeex.club\/index.php\/2018\/07\/23\/%e5%8c%ba%e5%9d%97%e9%93%be%e5%9f%ba%e7%a1%80\/","title":{"rendered":"\u533a\u5757\u94fe\u57fa\u7840"},"content":{"rendered":"<hr \/>\n<p>\u8fd9\u91cc\u8ba8\u8bba\u533a\u5757\u94fe\u7684\u57fa\u672c\u539f\u7406\u548c\u5b9e\u73b0\uff0c\u66f4\u591a\u6280\u672f\u53ef\u4e0e\u6211\u8054\u7cfb&#x1f3f8;<\/p>\n<h5>\u6280\u672f\u7b49\u7ea7<\/h5>\n<h5>\u9ad8\u7ea7<\/h5>\n<h2>Server<\/h2>\n<p>\u9996\u5148\u662f\u670d\u52a1\u642d\u5efa\uff0c\u5efa\u8bae\u4f7f\u7528ES\u8349\u6848\uff08\u5373\u5c06\u53d1\u5e03\u4e3a\u63a8\u8350\u6807\u51c6\uff09\uff0c\u5c06\u9ad8\u7ea7ES\u7f16\u8bd1\u4e3a\u4e8c\u8fdb\u5236\u4f4e\u7ea7ES\uff0c\u6027\u80fd\u548cC\/C++\u4e00\u6837\uff0c\u5c31\u4e0d\u7528\u62c5\u5fc3\u5404\u79cd\u6027\u80fd\u95ee\u9898\u4e86\u3002<\/p>\n<p>\u4e3a\u4e86\u7b80\u5316\u6b65\u9aa4\uff0c\u6211\u4f7f\u7528Express\uff0c\u6240\u6709\u4ee3\u7801\u5747\u4e3a\u6f14\u793a\uff0c\u4e0d\u5efa\u8bae\u5b9e\u9645\u9879\u76ee\u4f7f\u7528\u3002<\/p>\n<h4>\u542f\u52a8\u670d\u52a1<\/h4>\n<pre><code class=\"language-javascript line-numbers\">\/*\n * @Author: indeex\n * @Date: 2018-06-24 15:21:53\n * @LastEditors: indeex\n * @LastEditTime: 2018-06-25 15:50:39\n * @Description: This is blockchain base with indeex.\n * @WebSite: http:\/\/www.indeex.cc\n * @Email: indeex@qq.com\n *\/\n\nimport express from 'express';\nimport bodyParser from 'body-parser';\n\nconst app: express.Application = express();\n\nconst port: number = 3000;\n\napp.use(bodyParser.json());\n\napp.listen(port, () =&gt; {\n    console.log('server start...');\n});\n<\/code><\/pre>\n<h4>\u8bbe\u7f6e\u8def\u7531<\/h4>\n<pre><code class=\"language-javascript line-numbers\">import { Router, Request, Response} from 'express';\n\nconst router: Router = Router();\n\nrouter.get('\/', (req: Request, res: Response) =&gt; {\n    res.json({\n        index: 'index'\n    });\n});\n\nexport const HomeControllerRouter: Router = router;\n<\/code><\/pre>\n<h4>\u4f7f\u7528\u8def\u7531<\/h4>\n<pre><code class=\"language-javascript line-numbers\">import {HomeControllerRouter} from '.\/router\/homeController';\n\n\/\/...\n\napp.use('\/', HomeControllerRouter);\n<\/code><\/pre>\n<p>\u57fa\u672c\u670d\u52a1\u642d\u5efa\u5c31\u7eea\u3002<\/p>\n<p>\u4e3a\u4e86\u7b80\u5316\uff0c\u8fd9\u91cc\u4e0d\u518d\u5bf9\u8def\u7531\u8fdb\u884c\u62c6\u5206\u3002<\/p>\n<h3>\u8def\u7531\u5206\u914d<\/h3>\n<pre><code class=\"language-javascript line-numbers\">router.get('\/', (req: Request, res: Response) =&gt; {\n    console.log(req.params);\n});\n\nrouter.get('\/blockchain\/transactions', (req: Request, res: Response) =&gt; {\n    console.log(req.params);\n});\n\nrouter.get('\/blockchain\/mine', (req: Request, res: Response) =&gt; {\n    console.log(req.params);\n});\n\nrouter.get('\/blockchain\/blocks', (req: Request, res: Response) =&gt; {\n    console.log(req.params);\n});\n<\/code><\/pre>\n<p>\u8fd9\u91cc\u53ea\u505a\u4e86\u8def\u7531\u8bbf\u95ee\u7684\u6f14\u793a\uff0c\u4ee5\u4fbf\u7b80\u5316\u3002<\/p>\n<h2>Transaction<\/h2>\n<h3>\u4ea4\u6613\u63a5\u53e3\u53ca\u5b9e\u73b0<\/h3>\n<p>\u9996\u5148\u662f\u4ea4\u6613\u63a5\u53e3\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">interface TransactionData{\n    from: string;\n    to: string;\n    amount: number;\n    timestamp: number;\n}\n<\/code><\/pre>\n<p>\u53ea\u9700\u8981\u5b9e\u73b0\u63a5\u53e3\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">class Transaction implements TransactionData{\n    constructor(\n        public from: string,\n        public to: string,\n        public amount: number,\n        public timestamp:number\n    ){\n    }\n}\n<\/code><\/pre>\n<h2>Block<\/h2>\n<p>\u4ea4\u6613\u5757\u63a5\u53e3\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">interface BlockData{\n    index: number;\n    hash: string;\n    previousHash: string;\n    nonce: number;\/\/\u4e34\u65f6\u5339\u914d\n    transactions: TransactionData[];\n    key: string;\n}\n<\/code><\/pre>\n<p>\u63a5\u53e3\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">class Block implements BlockData{\n    constructor(\n        public index: number = 0,\n        public hash: string = '',\n        public previousHash: string = '',\n        public nonce: number = 0,\n        public transactions: TransactionData[] = []\n    ){\n    }\n}\n<\/code><\/pre>\n<p>\u63a5\u7740\u9700\u8981\u5728<code>Block<\/code>\u4e2d\u83b7\u53d6\u9700\u8981\u7684<code>key<\/code>\uff0c\u8fd9\u91cc\u4f7f\u7528<code>getter<\/code>\u3002<\/p>\n<pre><code class=\"language-javascript line-numbers\">get key():string{\n        return JSON.stringify(this.transactions) + this.index + this.previousHash + this.nonce;\n}\n<\/code><\/pre>\n<p>\u63a5\u7740\u521b\u5efa\u589e\u52a0\u4ea4\u6613\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">public addTransaction(transaction: TransactionData) :void{\n    this.transactions = [...this.transactions, transaction];\n}\n<\/code><\/pre>\n<p>\u5c06\u6bcf\u6b21\u4ea4\u6613\u6dfb\u52a0\u5230\u4ea4\u6613\u94fe\u4e2d\u3002<\/p>\n<h2>Blockchain<\/h2>\n<p>\u63a5\u4e0b\u6765\uff0c\u5904\u7406\u6240\u6709\u94fe\uff0c\u9996\u5148\u662f\u94fe\u63a5\u53e3\u548c\u57fa\u672c\u7c7b\u5b9e\u73b0\uff1a<\/p>\n<p>\u63a5\u53e3\uff1b<\/p>\n<pre><code class=\"language-javascript line-numbers\">interface BlockChainData{\n     blocks: BlockData[];\n }\n<\/code><\/pre>\n<p>\u63a5\u53e3\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">class BlockChain implements BlockChainData{\n    public blocks: BlockData[];\n    public difficulty: number;\n    constructor(genesisBlock: BlockData){\n        this.blocks = [];\n        this.difficulty = 2;\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u7c7b\u521d\u59cb\u5316\u65f6\uff0c\u9700\u8981\u521b\u5efa\u4ea4\u6613\u5757\uff0c\u5e76\u521b\u4e16\u5757\u5904\u7406\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">constructor(genesisBlock: BlockData){\n    this.addBlock(genesisBlock);\n}\n\n\/\/...\n\n\npublic addBlock(block: BlockData) :void{\n    if(this.blocks.length === 0){\n        block.previousHash = '00000000';\n        block.hash = null;\n    }\n    this.blocks = [...this.blocks, block];\n}\n<\/code><\/pre>\n<p>\u7136\u540e\u751f\u6210\u4ea4\u6613\u5757\u7684<code>hash<\/code>\uff0c\u8fd9\u91cc\u7b80\u5316\u5904\u7406\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\"> public generateHash(block: BlockData):string{\n    let hash = sha256(block.key);\n    while(!hash.startsWith(\"7a7\")){\n        block.nonce += 1;\n        hash = sha256(block.key);\n    }\n    return hash;\n}\n<\/code><\/pre>\n<p>\u83b7\u53d6\u524d\u4e00\u4e2a\u4ea4\u6613\u5757\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">public getPreviousBlock():BlockData{\n    return this.blocks[this.blocks.length - 1];\n}\n<\/code><\/pre>\n<p>\u83b7\u53d6\u540e\u4e00\u4e2a\u4ea4\u6613\u5757\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">public getNextBlock(transactions: TransactionData[]): BlockData{\n    let block = new Block();\n    transactions.map((t: TransactionData) =&gt; {\n        block.addTransaction(t);\n    });\n    let previousBlock = this.getPreviousBlock();\n    block.index = this.blocks.length;\n    block.previousHash = previousBlock.hash;\n    block.hash = this.generateHash(block);\n    return block;\n}\n<\/code><\/pre>\n<p>\u7136\u540e\u5f00\u59cb\u9a8c\u8bc1\u4ea4\u6613\u5757\uff0c\u8fd9\u91cc\u5efa\u8bae\u5f53\u4ea4\u6613\u5757\u4e0d\u662f\u6700\u65b0\u65f6\u53d6\u6700\u957f\u4ea4\u6613\u94fe\u3002<\/p>\n<h2>Valid<\/h2>\n<p>\u4ea4\u6613\u94fe\u9a8c\u8bc1\u7565\u3002<\/p>\n<h2>Publish<\/h2>\n<p>\u9a8c\u8bc1\u540e\u5c31\u53ef\u4ee5\u53d1\u5e03\u4e86\uff1a<\/p>\n<p>\u53d1\u5e03\u540e\u53ef\u4ee5\u67e5\u770b\u6240\u6709\u4ea4\u6613\u94fe\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">router.get('\/blockchain\/blocks', (req: Request, res: Response) =&gt; {\n    let timestamp = new Date();\n    let transaction = new Transaction('me', 'you', 100, Number(timestamp));\n    let newBlock = blockchain.getNextBlock([transaction]);\n    blockchain.addBlock(newBlock);\n    res.json(blockchain.blocks);\n});\n<\/code><\/pre>\n<p>\u63a5\u4e0b\u6765\u5c06\u6240\u6709\u4ea4\u6613\u5199\u5165\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u9009\u7528\u76ee\u524d\u4e3b\u6d41\u7684Oracle\uff0cMysql\uff0c\u6216\u8005mongo\uff0c\u7531\u4e8e\u6240\u6709\u7f16\u7a0b\u8bed\u8a00\u5bf9\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u901f\u5ea6\u3001\u6027\u80fd\u3001\u6548\u7387\u57fa\u672c\u4e00\u81f4\uff0c\u9700\u8981\u54ea\u79cd\u6570\u636e\u5e93\u4ee5\u9879\u76ee\u53ca\u6280\u672f\u65b9\u5411\u4e3a\u4e3b\u3002<\/p>\n<p>code enjoy! \u00d2\u072b\u00d3<\/p>\n<p>\u4f5c\u8005\uff1aindeex<\/p>\n<p>\u94fe\u63a5\uff1a<a class=\"wp-editor-md-post-content-link\" href=\"http:\/\/indeex.cc\/\">http:\/\/indeex.cc<\/a><\/p>\n<p>\u8457\u4f5c\u6743\u5f52\u4f5c\u8005\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u4f5c\u8005\u83b7\u5f97\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002<\/p>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u91cc\u8ba8\u8bba\u533a\u5757\u94fe\u7684\u57fa\u672c\u539f\u7406\u548c\u5b9e\u73b0\uff0c\u66f4\u591a\u6280\u672f\u53ef\u4e0e\u6211\u8054\u7cfb&#x1f3f8; \u6280\u672f\u7b49\u7ea7 \u9ad8\u7ea7 Server <a href=\"https:\/\/blog.indeex.club\/index.php\/2018\/07\/23\/%e5%8c%ba%e5%9d%97%e9%93%be%e5%9f%ba%e7%a1%80\/\" class=\"read-more\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[4],"_links":{"self":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/134"}],"collection":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/comments?post=134"}],"version-history":[{"count":1,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/134\/revisions"}],"predecessor-version":[{"id":141,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/134\/revisions\/141"}],"wp:attachment":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/media?parent=134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/categories?post=134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/tags?post=134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}