zoukankan      html  css  js  c++  java
  • Solidity编译器和简单调试

    Solidity编译器和简单调试

    1 安装Solidity编译器

    1.1 通过docker安装Solc

    搜索docker的Solc镜像

    docker search —no-trunc ethereum/solc

    通过docker安装Solc

    docker pull docker.io/ethereum/solc:stable

    1.2 运行Solc容器

    运行如下命令

    docker run —rm -it —privileged=true —net=host -v /home/hpbroot/ethereum_go/contract:/contract —name solc ethereum/solc:stable –version

    查看是否成功

    2 新建spring boot工程

    2.1 通过Eclipse新建工程

    首先,新建ContractCompile工程

    在springboot配置文件application.properties中添加如下

    1
    2
    3
    4
    5
    6
    7
    
    web3j.contract.solcCmd=docker run --rm -it --privileged=true --net=host -v /home/hpbroot/ethereum_go/contract:/contract --name solc ethereum/solc:stable
    在pom文件添加依赖
    <dependency>
      <groupId>org.ethereum</groupId>
      <artifactId>solcJ-all</artifactId>
      <version>0.4.10</version>
    </dependency>
    

    新建ContractConfig类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    @Component
    @ConfigurationProperties(prefix = "web3j.contract")
    public class ContractConfig {
      private String solcCmd;
      public String getSolcCmd() {
          return solcCmd;
      }
      public void setSolcCmd(String solcCmd) {
          this.solcCmd = solcCmd;
      }
    }

    2.2 调用智能合约编译器的代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    String soliditySrcCode =MapUtils.getString(preParam, "soliditySrcCode");
    if(StringUtils.isBlank(soliditySrcCode)) {
      param.put(ContractConstant.RETURN_CODE, ContractConstant.ERROR_CODE);
      param.put(ContractConstant.RETURN_MSG, ContractConstant.NOSRCCODE);
      return param;
    }
    SolidityCompiler solidityCompiler = SolidityCompiler.getInstance(getLog(),contractConfig.getSolcCmd());
    byte[] source = soliditySrcCode.getBytes(StandardCharsets.UTF_8);
    CompilerResult compilerResult = solidityCompiler.compileSrc(source,
    SolidityCompiler.Options.ABI, SolidityCompiler.Options.BIN);
    param.put(ContractConstant.RETURN_CODE, ContractConstant.SUCCESS_CODE);
    param.put(ContractConstant.RETURN_MSG, ContractConstant.SUCCESS_MSG);
    if(compilerResult.isFailed()) {
      param.put(ContractConstant.RETURN_CODE, ContractConstant.ERROR_CODE);
      param.put(ContractConstant.RETURN_MSG, compilerResult.getErrors());
    }

    3 调用编译智能合约源文件的代码

    3.1 编写智能合约源码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    contract SampleRecipientSuccess {
      address public from;
      uint256 public value;
      address public tokenContract;
      bytes public extraData;
      event ReceivedApproval(uint256 _value);
      function receiveApproval(address _from, uint256 _value, address _tokenContract, bytes _extraData) {
        from = _from;
        value = _value;
        tokenContract = _tokenContract;
        extraData = _extraData;
        ReceivedApproval(_value);
      }
    }

    3.2 通过HTTP调用智能合约的J2EE组件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    String contractPath="/SampleRecipientSuccess.sol";
          String contractString = FileUtils.readFileToString(new File(contractPath),StandardCharsets.UTF_8);
          HashMap<String, Object> hashMap = new HashMap<String,Object>();
          hashMap.put("soliditySrcCode", contractString);
          String url = "http://192.168.3.43:18080/ContractCompile/compileContractCmd";
          ResponseEntity<Map> postForEntity = getRestTemplate().postForEntity(url, hashMap, Map.class);
          Map body = postForEntity.getBody();
          String returnCode = MapUtils.getString(body, ContractConstant.RETURN_CODE);
          if(ContractConstant.SUCCESS_CODE.equals(returnCode)) {
              String result = MapUtils.getString(body, "result");
              Map<String, Object> parseResult = parseResult(result);
              System.out.println(AppObjectUtil.toJson(parseResult));
          }

    3.3 智能合约编译器组件返回的编译数据

    1
    
    {"abis":[{"constant":true,"inputs":[],"name":"value","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"tokenContract","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"extraData","outputs":[{"name":"","type":"bytes"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_value","type":"uint256"},{"name":"_tokenContract","type":"address"},{"name":"_extraData","type":"bytes"}],"name":"receiveApproval","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"from","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"_value","type":"uint256"}],"name":"ReceivedApproval","type":"event"}],"bin":"6060604052341561000c57fe5b5b6103d38061001c6000396000f300606060405263ffffffff60e060020a6000350416633fa4f245811461004d57806355a373d61461006f578063609d33341461009b5780638f4ffcb11461012b578063d5ce338914610199575bfe5b341561005557fe5b61005d6101c5565b60408051918252519081900360200190f35b341561007757fe5b61007f6101cb565b60408051600160a060020a039092168252519081900360200190f35b34156100a357fe5b6100ab6101da565b6040805160208082528351818301528351919283929083019185019080838382156100f1575b8051825260208311156100f157601f1990920191602091820191016100d1565b505050905090810190601f16801561011d5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561013357fe5b604080516020600460643581810135601f8101849004840285018401909552848452610197948235600160a060020a039081169560248035966044359093169594608494929391019190819084018382808284375094965061026895505050505050565b005b34156101a157fe5b61007f6102f8565b60408051600160a060020a039092168252519081900360200190f35b60015481565b600254600160a060020a031681565b6003805460408051602060026001851615610100026000190190941693909304601f810184900484028201840190925281815292918301828280156102605780601f1061023557610100808354040283529160200191610260565b820191906000526020600020905b81548152906001019060200180831161024357829003601f168201915b505050505081565b60008054600160a060020a0380871673ffffffffffffffffffffffffffffffffffffffff19928316179092556001859055600280549285169290911691909117905580516102bd906003906020840190610307565b506040805184815290517f2db24179b782aab7c5ab64add7f84d4f6c845d0779695371f29be1f658d043cd9181900360200190a15b50505050565b600054600160a060020a031681565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061034857805160ff1916838001178555610375565b82800160010185558215610375579182015b8281111561037557825182559160200191906001019061035a565b5b50610382929150610386565b5090565b6103a491905b80821115610382576000815560010161038c565b5090565b905600a165627a7a723058209522849948e8cc25a7d6717d5c10836c97c36425936be5edf399206b3e5d7fa30029"}
    

    总结

    通过J2EE组件的接口调用,可以为大多数基于java的区块链应用提供了便利,可以利用J2EE成熟稳定的框架无缝集成到项目中,也是为了以后安卓开发和联盟链提供在线编译智能合约功能,如果是私有的局域网络的企业级联盟链,可以发布该智能合约的J2EE组件到该局域网的机器上去,可以实现联盟中的智能合约统一的编译器,便于快速升级编译器。

  • 相关阅读:
    .Net工具 SocanCode代码生成器
    .net开发中两个“属性”引起的歧异
    读取Excel文件时出现null的解决方法
    Spring中XML配置的12个技巧
    oracle中的NVL,NVL2,NULLIF,COALESCE几个通用函数
    VB6各数据类型序列化和反序列化
    Oracle分页查询语句(四)
    ASP.NET知识库
    你知道.NET框架下的自动内存管理吗?
    构建支持 Ajax 的自动完成和级联式下拉控件
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13313396.html
Copyright © 2011-2022 走看看