zoukankan      html  css  js  c++  java
  • 区块链

    介绍区块链,搭建私链,智能合约以及开发DAPP。

    概念

    什么是区块链

    广义:

    区块链是分布式数据存储,点对点传输,共识机制,加密算法等计算机技术的新型应用模式。

    狭义:

    区块链是数据库的一种,它拥有大量的记录,并将这些记录全部存放在区块内,每个区块通过加密签名,链接到下一个区块。人们可以像使用账本一样使用区块链,可以共享,也可以被拥有适当权限的人查阅,通俗的说,区块链其实就是公开的分布式账簿系统。

    挖矿

    区块链需要存储记录,那么为什么要帮别人免费存储?——奖励

    即参与记录的机器,当对信息进行加密时,满足一定的条件时,就会得到一定的奖励。这个加密计算的过程,叫做挖矿。而运行计算的程序,称为矿机。

    智能合约

    一个智能合约是一套以数字形式定义的承诺(promises),包括合约参与方可以在上面执行这些承诺的协议。一个合约由一组代码(合约的函数)和数据(合约的状态)组成,通常运行在以太坊虚拟机上。当满足一定的条件时,自动履行并通知用户。

    以太坊

    目前最大的区块链开发者平台

    视频学习:尚硅谷_区块链以太坊核心技术

    geth搭建以太坊私链

    以太坊私有链搭建

    一台电脑上部署多个节点。

    安装go-ethereum

    # 下载代码
    git clone https://github.com/ethereum/go-ethereum.git
    
    # 进入目录
    cd go-ethereum
    
    # 切换分支
    git checkout v1.9.9
    
    # 编译打包
    make all
    
    # 移动到可执行目录
    cp build/bin/geth /usr/local/bin
    cp build/bin/puppeth /usr/local/bin
    

    创建节点目录

    由于在一台电脑上部署多个节点,所以需要创建多个目录来保存各自节点的账号、区块链数据。

    mkdir eth
    cd eth
    mkdir node1
    mkdir node2
    

    创建节点目录

    创建账号

    为每个节点各自创建一个账号。执行 account new 命令后会提示你输入密码,也可以不输入直接回车跳过即可,生成账号后会给出对应的地址,记录下新生成的账号地址,方便后续配置创世块使用。如果没记住也可以通过 eth.accounts 命令查询。

    geth --datadir ./node1/data account new
    # 0x2A7d60135d51c799321B191427FB72108e28b75C
    geth --datadir ./node2/data account new
    # 0xFE677A1EB388E4d04cFb6767547599460442aDf7
    

    创建账号

    配置创世块

    创世块是区块链中的第一个区块,可以通过 puppeth 来生成创世区块,puppeth 是 geth 中自带的工具,不用单独安装,运行 puppeth 命令后会提示你输入私链的名称。

    配置创世块

    初始化节点

    geth --datadir node1/data init private.json
    geth --datadir node2/data init private.json
    

    初始化节点

    启动节点

    # 启动第一个
    geth --datadir node1/data 
     --networkid 15 
     --rpc --rpcaddr 0.0.0.0 --rpcport 8545 
     --port 30303 
     --nodiscover 
     --allow-insecure-unlock 
     --rpccorsdomain https://remix.ethereum.org 
     console
    
    # 启动第二个
    geth --datadir node2/data 
     --networkid 15 
     --rpc --rpcaddr 0.0.0.0 --rpcport 18545 
     --port 60606 
     --nodiscover 
     --allow-insecure-unlock 
     console
    

    参数含义:

    • --identity:指定节点 ID
    • --rpc:表示开启 HTTP-RPC 服务
    • --rpcaddr:HTTP-RPC 服务ip地址
    • --rpcport:指定 HTTP-RPC 服务监听端口号(默认为 8545)
    • --datadir:指定区块链数据的存储位置
    • --port:指定和其他节点连接所用的端口号(默认为 30303)
    • --nodiscover:关闭节点发现机制,防止加入有同样初始配置的陌生节点
    • --allow-insecure-unlock:允许 HTTP 解锁账号

    建立节点之间的连接

    在各个节点的 console 输入 admin.nodeInfo.enode ,可以分别得到节点信息。

    node1:

    > admin.nodeInfo.enode
    "enode://9103e0b2de12611b2a51b41aa5ed526fa65b50b9d88815171017d547c2835f023c87439d7352fa2bcfa52957925441b5ac638f3ffcd5573bdbb5b811e52d50f2@127.0.0.1:30303?discport=0"
    >
    

    node2:

    > admin.nodeInfo.enode
    "enode://2f9304cd83d3a4e91b2e266efceecdf2a680e9fc89eac3cd6fd90e9ca72cd6e7bf807e03e45487859602ffb522f19c3163226ea5a5a1866b822fdbef00aef85b@127.0.0.1:60606?discport=0"
    >
    

    在 node2 的 console 中输入对 node1 的连接:

    > admin.addPeer("enode://9103e0b2de12611b2a51b41aa5ed526fa65b50b9d88815171017d547c2835f023c87439d7352fa2bcfa52957925441b5ac638f3ffcd5573bdbb5b811e52d50f2@127.0.0.1:30303?discport=0")
    true
    >
    

    在 node1 的 console 中验证 node2 是否成功建立连接:

    > admin.peers
    [{
        caps: ["eth/63", "eth/64"],
        enode: "enode://2f9304cd83d3a4e91b2e266efceecdf2a680e9fc89eac3cd6fd90e9ca72cd6e7bf807e03e45487859602ffb522f19c3163226ea5a5a1866b822fdbef00aef85b@127.0.0.1:54276",
        id: "9f471227e38b9b194c26cad6ce7874d2d504de0c8938099c6315a4fd293840c2",
        name: "Geth/v1.9.9-stable/linux-amd64/go1.13.4",
        network: {
          inbound: true,
          localAddress: "127.0.0.1:30303",
          remoteAddress: "127.0.0.1:54276",
          static: false,
          trusted: false
        },
        protocols: {
          eth: {
            difficulty: 3,
            head: "0x26896f9739ae0fff8d63c7793fe524052c37471830004e1f42f63d6ae4c937f3",
            version: 64
          }
        }
    }]
    >
    

    常用命令

    • personal.newAccount("password"):创建账户;
    • personal.listAccounts:查看账户,同eth.accounts
    • personal.unlockAccount("0xf3b6bbd4c05eeeb78cf574c5c826e8727cd95da1"):解锁账户;
    • eth.accounts:枚举系统中的账户;
    • eth.getBalance():查看账户余额,返回值的单位是 Wei(Wei 是以太坊中最小货币面额单位,类似比特币中的聪,1 ether = 10^18 Wei);
    • eth.blockNumber:列出区块总数;
    • eth.getTransaction():获取交易;
    • eth.getBlock():获取区块;
    • miner.start():开始挖矿;
    • miner.stop():停止挖矿;
    • eth.coinbase:挖矿奖励的账户
    • web3.fromWei():Wei 换算成以太币;
    • web3.toWei():以太币换算成 Wei;
    • txpool.status:交易池中的状态;
    • admin.addPeer():连接到其他节点;
    • admin.nodeInfo:查看节点摘要信息

    开启挖矿

    在 node2 开启挖矿:

    > miner.start()
    INFO [01-15|17:35:09.807] Transaction pool price threshold updated price=1000000000
    null
    > INFO [01-15|17:35:09.807] Commit new mining work                   number=1 sealhash=5a0d4f…a8b50e uncles=0 txs=0 gas=0 fees=0 elapsed=153.486µs
    WARN [01-15|17:35:09.807] Block sealing failed                     err="authentication needed: password or unlock"
    

    解锁挖矿用户,再挖矿:

    > personal.unlockAccount("0xFE677A1EB388E4d04cFb6767547599460442aDf7")
    Unlock account 0xFE677A1EB388E4d04cFb6767547599460442aDf7
    Password:
    true
    > miner.start()
    INFO [01-15|17:46:06.428] Transaction pool price threshold updated price=1000000000
    null
    > INFO [01-15|17:46:06.428] Commit new mining work                   number=1 sealhash=586bf3…585097 uncles=0 txs=0 gas=0 fees=0 elapsed=177.262µs
    INFO [01-15|17:46:06.429] Successfully sealed new block            number=1 sealhash=586bf3…585097 hash=cafedf…b7e662 elapsed=860.675µs
    INFO [01-15|17:46:06.429] :hammer: mined potential block                  number=1 hash=cafedf…b7e662
    INFO [01-15|17:46:06.430] Commit new mining work                   number=2 sealhash=eb8562…c8587c uncles=0 txs=0 gas=0 fees=0 elapsed=394.973µs
    INFO [01-15|17:46:06.430] Signed recently, must wait for others
    

    同理,在 node1 上也开启挖矿(因为只有2个节点,必须同时挖才行):

    > personal.unlockAccount("0x2A7d60135d51c799321B191427FB72108e28b75C")
    Unlock account 0x2A7d60135d51c799321B191427FB72108e28b75C
    Password:
    true
    > miner.start()
    INFO [01-15|17:46:06.428] Transaction pool price threshold updated price=1000000000
    null
    > INFO [01-15|17:46:06.428] Commit new mining work                   number=1 sealhash=586bf3…585097 uncles=0 txs=0 gas=0 fees=0 elapsed=177.262µs
    INFO [01-15|17:46:06.429] Successfully sealed new block            number=1 sealhash=586bf3…585097 hash=cafedf…b7e662 elapsed=860.675µs
    INFO [01-15|17:46:06.429] :hammer: mined potential block                  number=1 hash=cafedf…b7e662
    INFO [01-15|17:46:06.430] Commit new mining work                   number=2 sealhash=eb8562…c8587c uncles=0 txs=0 gas=0 fees=0 elapsed=394.973µs
    INFO [01-15|17:46:06.430] Signed recently, must wait for others
    

    正常挖矿

    DAPP

    基于区块链的APP

    remix

    remix IDE

    启用插件

    • Solidity compiler
    • Deploy & run transactions

    启用插件

    投票合约

    投票合约

    pragma solidity >=0.5.0 <6.0.0;
    
    contract Vote {
    
        uint[] _candidates;
        mapping(uint => uint) _voters;
    
        constructor(uint[] memory candidates) public {
            _candidates = candidates;
        }
    
        function doVote(uint candidate) public {
            require(validCandidate(candidate));
            _voters[candidate] += 1;
        }
    
        function display(uint candidate) public view returns (uint) {
            assert(validCandidate(candidate));
            return _voters[candidate];
        }
    
         function validCandidate(uint candidate) private view returns (bool) {
            for(uint i; i < _candidates.length; i++) {
                if (_candidates[i] == candidate) {
                    return true;
                }
            }
            return false;
        }
    
    }
    

    部署:

    部署

    投票:

    投票

    证件存储合约

    pragma solidity >=0.5.0 <6.0.0;
    
    contract IDCard {
    
        string _info;
    
        constructor(string memory info) public {
            _info = info;
        }
    
        function display() public view returns (string memory) {
            return _info;
        }
    
    }
    

    合约部署:

    证件存储合约

    truffle

    Truffle使开发者从智能合约和 DApp 模板开始,构建越来越复杂的应用程序。

    安装命令行工具:

    npm install -g truffle
    

    创建项目

    # 创建目录
    mkdir egova-dapp
    cd egova-dapp
    
    # 初始化环境
    truffle unbox metacoin
    

    创建项目

    如下是对项目结构的说明:

    • contracts/: Directory for Solidity contracts
    • migrations/: Directory for scriptable deployment files
    • test/: Directory for test files for testing your application and contracts
    • truffle.js: Truffle configuration file

    接下来进行合约的编译,部署等操作见文档即可。

    web3.js

    nodejs 项目使用 web3.js 更方便。

    下面采用 koa 框架搭建服务端,在 truffle 的基础上搭建。

    web3js项目接口

    编译合约

    采用 truffle 进行编译

    truffle compile
    

    编译文件会写入 build/constracts/ 目录下。

    搭建koa

    这里不详细展开,只介绍 nodejs 与区块链交互部分。

    与区块链交互

    项目中,server/blockchain.js 是封装的 web 接口。

    // web3js实例对象
    const web3 = require('../util/web3');
    // 这里是获取到编译后的合约,即上面 build/constracts/ 目录下的 IDCard.json 文件
    const { compiledContract } = require('../util/contract');
    const config = require('../config');
    
    // 合约
    const contract = new web3.eth.Contract(compiledContract.abi);
    
    // 服务接口
    const svr = {
        deploy: async (data) => {
            // 部署合约
            const instance = await contract.deploy({
                data: compiledContract.bytecode,
              	// 数组的参数,会传入部署的合约的构造函数,我这里就是 IDCard 合约
                arguments: [data],
            }).send({
              	// 部署合约的账号
                from: config.eth.from,
                value: 0,
            });
            return instance._address
        },
        display: async (address) => {
            // 引用存在的合约
            const instance = new web3.eth.Contract(compiledContract.abi, address);
            // 调用合约方法
            return await instance.methods.display().call();
        }
    }
    
    module.exports = {
        deploy: async (ctx) => {
            const body = ctx.request.body;
            if (!body.data) {
                ctx.throw(400, '.data required');
            }
            const address = await svr.deploy(body.data);
            ctx.body = {
                address: address,
            }
        },
        display: async (ctx) => {
            const params = ctx.params;
            const data = await svr.display(params.address);
            ctx.body = {
                data: data,
            }
        }
    };
    
    

    接口测试

    部署合约:

    部署合约

    查看信息:

    查看信息

    web3j

    web3j is a lightweight, highly modular, reactive, type safe Java and Android library for working with Smart Contracts and integrating with clients (nodes) on the Ethereum network。

    安装命令行工具

    依赖

    <dependency>
      <groupId>org.web3j</groupId>
      <artifactId>core</artifactId>
      <version>4.5.12</version>
    </dependency>
    

    使用

    // defaults to http://localhost:8545/
    Web3j web3 = Web3j.build(new HttpService());
    Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().sendAsync().get();
    String clientVersion = web3ClientVersion.getWeb3ClientVersion();
    

    spring-boot项目使用

    配置Web3j:

    /**
     * Web3j配置
     *
     * @author 奔波儿灞
     * @since 1.0
     */
    @Data
    @Validated
    @ConfigurationProperties(prefix = Web3jProperties.PREFIX)
    public class Web3jProperties {
    
        static final String PREFIX = "web3j";
    
        private String url = "http://localhost:8545/";
    
        @NotNull
        private Deploy deploy;
    
        @NotNull
        private Gas gas;
    
        @Data
        @Validated
        public static class Deploy {
    
            @NotBlank
            private String wallet;
    
            @NotBlank
            private String password;
    
        }
    
        @Data
        @Validated
        public static class Gas {
    
            @NotNull
            private Long price;
    
            @NotNull
            private Long limit;
    
        }
    
    }
    
    /**
     * 配置Web3j
     *
     * @author 奔波儿灞
     * @since 1.0
     */
    @Configuration
    @RequiredArgsConstructor
    @EnableConfigurationProperties(Web3jProperties.class)
    public class Web3jConfiguration {
    
        private final Web3jProperties properties;
    
        @Bean(destroyMethod = "shutdown")
        public Web3j web3j() {
            return Web3j.build(new HttpService(properties.getUrl()));
        }
    
        @Bean
        public Credentials deployCredentials() {
            Web3jProperties.Deploy deploy = properties.getDeploy();
            Credentials credentials;
            try {
                return WalletUtils.loadCredentials(deploy.getPassword(), deploy.getWallet());
            } catch (IOException e) {
                throw new RuntimeException("read wallet failed", e);
            } catch (CipherException e) {
                throw new RuntimeException("cipher failed", e);
            }
        }
    
        @Bean
        public ContractGasProvider gasProvider() {
            Web3jProperties.Gas gas = properties.getGas();
            return new StaticGasProvider(BigInteger.valueOf(gas.getPrice()), BigInteger.valueOf(gas.getLimit()));
        }
    
    }
    

    配置:

    web3j:
      url: http://47.101.136.24:8545
      deploy:
        wallet: wallet.json
        password: 123456
      gas:
        price: 1_000_000_000
        limit: 170294
    

    采用 truffle compile 先编译 .sol 文件成 json

    打包代码:

    bin/web3j/bin/web3j truffle generate /Users/xuanbo/Projects/egova/egova-dapp/build/contracts/IDCard.json 
     -o src/main/java 
     -p com.egova.dapp.contract
    

    复制到工程

    测试合约部署:

    /**
     * 测试Web3j
     *
     * @author 奔波儿灞
     * @since 1.0
     */
    @Slf4j
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = Application.class)
    public class Web3jTest {
    
        @Autowired
        private Web3j web3j;
    
        @Autowired
        private Credentials deployCredentials;
    
        @Autowired
        private ContractGasProvider gasProvider;
    
        @Test
        public void version() throws IOException {
            Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().send();
            String clientVersion = web3ClientVersion.getWeb3ClientVersion();
            log.info("clientVersion: {}", clientVersion);
        }
    
        @Test
        public void deployContract() {
            String info = "李四";
            RemoteCall<IdCard> deploy = IdCard.deploy(web3j, deployCredentials, gasProvider, info);
            deploy.sendAsync().whenComplete(((idCard, e) -> {
                if (e == null) {
                    String address = idCard.getContractAddress();
                    log.info("deploy success, contract address: {}", address);
                } else {
                    log.error("deploy failed", e);
                }
            }));
        }
    
    }
    

    测试结果

  • 相关阅读:
    考研408大纲梳理(持续更新)
    学习就是把抽象变具体,模糊变清楚,组合件拆单件的过程。
    20210402学习笔记---眼看这是要浪费一下午?(要放假了心里跟长草似的 浮躁啥自己也不知道 假期不还是学习么)不行,赶紧学。
    20210401学习笔记--概念必须都弄懂,你可能很熟悉,但你却不知道他的定义、为什么用它不用别的,这个不清楚学习就很难。(另外学习要把握重点,已经会的都不需要记)
    20210401日记
    20210331日记
    20210330学习笔记--第1天目标是刷完HadoopDay1 Day2(计划40天学完大数据)
    20210330日记
    这个老的大数据视频,用的工具还是Eclipse,不照着视频做,就各种报错出问题,也不知道咋解决。还是买个新点的视频看。
    20210329学习笔记--看来得找个更新的视频课了(用IDEA),一步一步跟着做了,而且每天要定量,必须完成多少。目前进度完全不行。
  • 原文地址:https://www.cnblogs.com/bener/p/12217653.html
Copyright © 2011-2022 走看看