java调用以太坊私链上的合约方法_java 如何使用eth_senduseroperation

总目录展示

该笔记共八个节点(由浅入深),分为三大模块。

高性能。 秒杀涉及大量的并发读和并发写,因此支持高并发访问这点非常关键。该笔记将从设计数据的动静分离方案、热点的发现与隔离、请求的削峰与分层过滤、服务端的极致优化这4个方面重点介绍。

一致性。 秒杀中商品减库存的实现方式同样关键。可想而知,有限数量的商品在同一时刻被很多倍的请求同时来减库存,减库存又分为“拍下减库存”“付款减库存”以及预扣等几种,在大并发更新的过程中都要保证数据的准确性,其难度可想而知。因此,将用一个节点来专门讲解如何设计秒杀减库存方案。

高可用。 虽然介绍了很多极致的优化思路,但现实中总难免出现一些我们考虑不到的情况,所以要保证系统的高可用和正确性,还要设计一个PlanB来兜底,以便在最坏情况发生时仍然能够从容应对。笔记的最后,将带你思考可以从哪些环节来设计兜底方案。


篇幅有限,无法一个模块一个模块详细的展示(这些要点都收集在了这份《高并发秒杀顶级教程》里),麻烦各位转发一下(可以帮助更多的人看到哟!)

由于内容太多,这里只截取部分的内容。

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

* @return hash
*/
String transact(String fromAddr, String fromPrivateKey, String hashVal, String month, BigInteger gasPrice, BigInteger gasLimit, List inputParameters);

}


### 实现类



import com.alibaba.fastjson.JSONObject;
import com.blockchain.server.contractGzhz.web3j.IBaseWeb3j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import org.web3j.abi.FunctionEncoder;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Bool;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.RawTransaction;
import org.web3j.crypto.TransactionEncoder;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.*;

import org.web3j.protocol.http.HttpService;
import org.web3j.utils.Numeric;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
* @Datetime: 2020/6/23 10:36
* @Author:
* @title
*/

@Component
public class BaseWeb3jImpl implements IBaseWeb3j {

private static final Logger LOG = LoggerFactory.getLogger(BaseWeb3jImpl.class);


static Web3j web3j;

@Value("${contract.url}")
private   String URL;


@Value("${contract.addGas}")
private BigInteger addGas;



@Value("${contract.isAddGas}")
private boolean isAddGas;




public String transact(String fromAddr, String fromPrivateKey, String hashVal, String month, BigInteger gasPrice, BigInteger gasLimit, List<Type> inputParameters) {
    EthSendTransaction ethSendTransaction = null;
    BigInteger nonce = BigInteger.ZERO;
    String hash = null;
    try {
        if(web3j == null){
            web3j = Web3j.build(new HttpService(URL));
        }
        EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
                fromAddr,
                DefaultBlockParameterName.PENDING
        ).send();
        //根据配置是否开启根据实时市场gas费用,增加指定gas费用,加快打包速率
        if(isAddGas){
             BigInteger gas  = web3j.ethGasPrice().send().getGasPrice();
             LOG.info("获取到的gasPrice{}",gas);
             gasPrice = addGas.add(gas);
        }
        //返回指定地址发生的交易数量。
        nonce =  ethGetTransactionCount.getTransactionCount();
        List outputParameters = new ArrayList();
        TypeReference<Bool> typeReference = new TypeReference<Bool>() {
        };
        outputParameters.add(typeReference);
        LOG.info("付给矿工的gasPrice为:{}",gasPrice);
        Function function = new Function(
                month,
                inputParameters,
                outputParameters);
        String encodedFunction = FunctionEncoder.encode(function);
        Credentials credentials = Credentials.create(fromPrivateKey);
        RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit, hashVal,
                encodedFunction);
        byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
        String hexValue = Numeric.toHexString(signedMessage);
        ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get();
        hash = ethSendTransaction.getTransactionHash();
        LOG.info(JSONObject.toJSONString(ethSendTransaction));
    } catch (Exception e) {
        if (null != ethSendTransaction) {
            LOG.info("失败的原因:" + ethSendTransaction.getError().getMessage());
            LOG.info("参数:fromAddr = " + fromAddr);
            LOG.info("参数:month = " + month);
            LOG.info("参数:gasPrice = " + gasPrice);
            LOG.info("参数:gasLimit = " + gasLimit);
            LOG.info("参数:inputParameters = " + JSONObject.toJSONString(inputParameters));
        }
        e.printStackTrace();
    }

    return hash;
}

### 调用层



import com.blockchain.server.contractGzhz.service.SettlementService;
import com.blockchain.server.contractGzhz.web3j.IBaseWeb3j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.web3j.abi.datatypes.Type;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
* @Datetime: 2020/6/23 11:47
* @Author:
* @title
*/
@Component
public class SettlementServiceImpl {

private static final Logger LOG = LoggerFactory.getLogger(SettlementServiceImpl.class);


/\*合约地址\*/
@Value("${contract.ctAddr}")
private String ctAddr;
/\*初始地址\*/
@Value("${contract.startAddr}")
private String startAddr;
/\*发币地址\*/
@Value("${contract.sendAddr}")
private String sendAddr;
/\*发币地址私钥\*/
@Value("${contract.sendAddrPk}")
private String sendAddrPk;

@Value("${contract.gasLimit}")
private BigInteger CT_GAS_LIMIT;

@Value("${contract.gasPrice}")
private BigInteger CT_GAS_PRICE;

@Autowired

最后

面试是跳槽涨薪最直接有效的方式,马上金九银十来了,各位做好面试造飞机,工作拧螺丝的准备了吗?

掌握了这些知识点,面试时在候选人中又可以夺目不少,暴击9999点。机会都是留给有准备的人,只有充足的准备,才可能让自己可以在候选人中脱颖而出。

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  • 14
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要搭建本地以太坊私链并连接钱包实现转账,你需要按照以下步骤进行操作: 1. 安装以太坊客户端软件,比如 Geth、Parity、Quorum 等。 2. 创建私有网络,可以使用以下命令: ``` geth --datadir=./chaindata init ./genesis.json ``` 其中,`./chaindata` 是指定数据目录,`./genesis.json` 是指定创世区块文件。 3. 启动私有网络,可以使用以下命令: ``` geth --datadir=./chaindata --networkid=8888 --rpc --rpcaddr "0.0.0.0" --rpcport 8545 --rpcapi="eth,net,web3,personal" console ``` 其中,`--networkid` 是指定网络 ID,`--rpc` 是启用 JSON-RPC 接口,`--rpcaddr` 是指定 JSON-RPC 接口的 IP,`--rpcport` 是指定 JSON-RPC 接口的端口号,`--rpcapi` 是启用 JSON-RPC 接口的 API 列表。 4. 创建钱包地址,可以使用以下命令: ``` personal.newAccount("password") ``` 其中,`password` 是指定钱包密码。 5. 解锁钱包,可以使用以下命令: ``` personal.unlockAccount("address", "password", 0) ``` 其中,`address` 是指定钱包地址,`password` 是钱包密码,`0` 是指定解锁时间。 6. 发送转账交易,可以使用以下命令: ``` eth.sendTransaction({from:"from_address", to:"to_address", value: web3.toWei(1, "ether")}) ``` 其中,`from_address` 是发送方钱包地址,`to_address` 是接收方钱包地址,`1` 是转账金额,`ether` 是转账单位。 7. 确认交易状态,可以使用以下命令: ``` eth.getTransactionReceipt("transaction_hash") ``` 其中,`transaction_hash` 是指定交易哈希值。 以上是搭建本地以太坊私链并连接钱包实现转账的大致步骤,具体操作还需要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值