Blockchain 使用web3j在Geth中实现智能合约

Blockchain 使用web3j在Geth中实现智能合约,blockchain,go-ethereum,web3-java,Blockchain,Go Ethereum,Web3 Java,我正在尝试在以太坊testrpc网络上部署我的solidity智能合约。 这是我的合同: pragma solidity ^0.4.11; contract Fibonacci { event Notify(uint input, uint result); function fibonacci(uint number) constant returns(uint result) { if (number == 0) return 0; el

我正在尝试在以太坊testrpc网络上部署我的solidity智能合约。 这是我的合同:

pragma solidity ^0.4.11;

contract Fibonacci {

    event Notify(uint input, uint result);

    function fibonacci(uint number) constant returns(uint result) {
        if (number == 0) return 0;
        else if (number == 1) return 1;
        else return Fibonacci.fibonacci(number - 1) + Fibonacci.fibonacci(number - 2);
    }

    function fibonacciNotify(uint number) returns(uint result) {
        result = fibonacci(number);
        Notify(number, result);
    }
}
public final class Fibonacci extends Contract {
    private static final String BINARY = "6060604052341561000f57600080fd5b5b61015a8061001f6000396000f300606060405263ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416633c7fdc70811461004857806361047ff414610070575b600080fd5b341561005357600080fd5b61005e600435610098565b60405190815260200160405180910390f35b341561007b57600080fd5b61005e6004356100e5565b60405190815260200160405180910390f35b60006100a3826100e5565b90507f71e71a8458267085d5ab16980fd5f114d2d37f232479c245d523ce8d23ca40ed828260405191825260208201526040908101905180910390a15b919050565b60008115156100f6575060006100e0565b8160011415610107575060016100e0565b610113600283036100e5565b61011f600184036100e5565b0190506100e0565b5b5b9190505600a165627a7a723058208c691bf2e824f14fefeb8b2e4932d9a9122cbe5835242992958daf1fc4a81af60029";

private Fibonacci(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
    super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);
}

private Fibonacci(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
    super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}

public List<NotifyEventResponse> getNotifyEvents(TransactionReceipt transactionReceipt) {
    final Event event = new Event("Notify", 
            Arrays.<TypeReference<?>>asList(),
            Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}, new TypeReference<Uint256>() {}));
    List<EventValues> valueList = extractEventParameters(event, transactionReceipt);
    ArrayList<NotifyEventResponse> responses = new ArrayList<NotifyEventResponse>(valueList.size());
    for (EventValues eventValues : valueList) {
        NotifyEventResponse typedResponse = new NotifyEventResponse();
        typedResponse.input = (Uint256) eventValues.getNonIndexedValues().get(0);
        typedResponse.result = (Uint256) eventValues.getNonIndexedValues().get(1);
        responses.add(typedResponse);
    }
    return responses;
}

public Observable<NotifyEventResponse> notifyEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
    final Event event = new Event("Notify", 
            Arrays.<TypeReference<?>>asList(),
            Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}, new TypeReference<Uint256>() {}));
    EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
    filter.addSingleTopic(EventEncoder.encode(event));
    return web3j.ethLogObservable(filter).map(new Func1<Log, NotifyEventResponse>() {
        @Override
        public NotifyEventResponse call(Log log) {
            EventValues eventValues = extractEventParameters(event, log);
            NotifyEventResponse typedResponse = new NotifyEventResponse();
            typedResponse.input = (Uint256) eventValues.getNonIndexedValues().get(0);
            typedResponse.result = (Uint256) eventValues.getNonIndexedValues().get(1);
            return typedResponse;
        }
    });
}

public Future<TransactionReceipt> fibonacciNotify(Uint256 number) {
    Function function = new Function("fibonacciNotify", Arrays.<Type>asList(number), Collections.<TypeReference<?>>emptyList());
    return executeTransactionAsync(function);
}

public Future<Uint256> fibonacci(Uint256 number) {
    Function function = new Function("fibonacci", 
            Arrays.<Type>asList(number), 
            Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));
    return executeCallSingleValueReturnAsync(function);
}

public static Future<Fibonacci> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialWeiValue) {
    return deployAsync(Fibonacci.class, web3j, credentials, gasPrice, gasLimit, BINARY, "", initialWeiValue);
}

public static Future<Fibonacci> deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialWeiValue) {
    return deployAsync(Fibonacci.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "", initialWeiValue);
}

public static Fibonacci load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
    return new Fibonacci(contractAddress, web3j, credentials, gasPrice, gasLimit);
}

public static Fibonacci load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
    return new Fibonacci(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}

public static class NotifyEventResponse {
    public Uint256 input;

    public Uint256 result;
}
我使用web3j在以太坊testrpc网络中部署此合约,该网络现在只在一台计算机上运行。 以下是我在JAVA控制台中使用的命令:

Future<FibonacciZKF> fib= FibonacciZKF.deploy(Web3j.build(new HttpService()), ALICE, GAS_PRICE, GAS_LIMIT, BigInteger.valueOf(5));
Future fib=FibonacciZKF.deploy(Web3j.build(new HttpService()),ALICE,GAS_PRICE,GAS_LIMIT,biginger.valueOf(5));
我已经为上述合同生成了所有必要的abi和bin

我甚至为上述合同生成了一个包装器。 这是我的合同包装:

pragma solidity ^0.4.11;

contract Fibonacci {

    event Notify(uint input, uint result);

    function fibonacci(uint number) constant returns(uint result) {
        if (number == 0) return 0;
        else if (number == 1) return 1;
        else return Fibonacci.fibonacci(number - 1) + Fibonacci.fibonacci(number - 2);
    }

    function fibonacciNotify(uint number) returns(uint result) {
        result = fibonacci(number);
        Notify(number, result);
    }
}
public final class Fibonacci extends Contract {
    private static final String BINARY = "6060604052341561000f57600080fd5b5b61015a8061001f6000396000f300606060405263ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416633c7fdc70811461004857806361047ff414610070575b600080fd5b341561005357600080fd5b61005e600435610098565b60405190815260200160405180910390f35b341561007b57600080fd5b61005e6004356100e5565b60405190815260200160405180910390f35b60006100a3826100e5565b90507f71e71a8458267085d5ab16980fd5f114d2d37f232479c245d523ce8d23ca40ed828260405191825260208201526040908101905180910390a15b919050565b60008115156100f6575060006100e0565b8160011415610107575060016100e0565b610113600283036100e5565b61011f600184036100e5565b0190506100e0565b5b5b9190505600a165627a7a723058208c691bf2e824f14fefeb8b2e4932d9a9122cbe5835242992958daf1fc4a81af60029";

private Fibonacci(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
    super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);
}

private Fibonacci(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
    super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}

public List<NotifyEventResponse> getNotifyEvents(TransactionReceipt transactionReceipt) {
    final Event event = new Event("Notify", 
            Arrays.<TypeReference<?>>asList(),
            Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}, new TypeReference<Uint256>() {}));
    List<EventValues> valueList = extractEventParameters(event, transactionReceipt);
    ArrayList<NotifyEventResponse> responses = new ArrayList<NotifyEventResponse>(valueList.size());
    for (EventValues eventValues : valueList) {
        NotifyEventResponse typedResponse = new NotifyEventResponse();
        typedResponse.input = (Uint256) eventValues.getNonIndexedValues().get(0);
        typedResponse.result = (Uint256) eventValues.getNonIndexedValues().get(1);
        responses.add(typedResponse);
    }
    return responses;
}

public Observable<NotifyEventResponse> notifyEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
    final Event event = new Event("Notify", 
            Arrays.<TypeReference<?>>asList(),
            Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}, new TypeReference<Uint256>() {}));
    EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
    filter.addSingleTopic(EventEncoder.encode(event));
    return web3j.ethLogObservable(filter).map(new Func1<Log, NotifyEventResponse>() {
        @Override
        public NotifyEventResponse call(Log log) {
            EventValues eventValues = extractEventParameters(event, log);
            NotifyEventResponse typedResponse = new NotifyEventResponse();
            typedResponse.input = (Uint256) eventValues.getNonIndexedValues().get(0);
            typedResponse.result = (Uint256) eventValues.getNonIndexedValues().get(1);
            return typedResponse;
        }
    });
}

public Future<TransactionReceipt> fibonacciNotify(Uint256 number) {
    Function function = new Function("fibonacciNotify", Arrays.<Type>asList(number), Collections.<TypeReference<?>>emptyList());
    return executeTransactionAsync(function);
}

public Future<Uint256> fibonacci(Uint256 number) {
    Function function = new Function("fibonacci", 
            Arrays.<Type>asList(number), 
            Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));
    return executeCallSingleValueReturnAsync(function);
}

public static Future<Fibonacci> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialWeiValue) {
    return deployAsync(Fibonacci.class, web3j, credentials, gasPrice, gasLimit, BINARY, "", initialWeiValue);
}

public static Future<Fibonacci> deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialWeiValue) {
    return deployAsync(Fibonacci.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "", initialWeiValue);
}

public static Fibonacci load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
    return new Fibonacci(contractAddress, web3j, credentials, gasPrice, gasLimit);
}

public static Fibonacci load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
    return new Fibonacci(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}

public static class NotifyEventResponse {
    public Uint256 input;

    public Uint256 result;
}
public final类Fibonacci扩展了契约{
私有静态最终字符串二进制="6060604052341561000F57600080FD5B61015A8061001F60000396000F3006060405263FFFFFF7C010000000000000000000000000000000000000000006000350416633C7FDC70811461004857806361047FF414610070575B600008FD5B34156100357600080FD5B61005E60043566100958565B60190815260200160405180910390F3045000000000000000000000000000000000000000000000000000000000000000000350416633C7FDC7FDC70811461004857806361005B560056019060200160405180910390F35B60006100A3826100E5565B90507F71EA8458267085D5AB16980FD5F114D2D37F232479C245D523CE8D23CA40ED82604051918252602082015260908101905180910390A15B919050565B6008111515F6575060006100E0565B8160011415610107570506006100E0565B6101836006100E565B6100111F600036100E50565B61005B184036100E5056905B1905BF62907B6008BF628080907B914F14FEB8B2E4932D9A9122CBE583542992958DAF1FC4A81AF60029”;
私有Fibonacci(字符串contractAddress、Web3j、Web3j、凭证凭证、biginger-gasPrice、biginger-gasLimit){
超级(二进制、合同地址、web3j、凭证、gasPrice、gasLimit);
}
私有Fibonacci(字符串契约地址、Web3j、Web3j、TransactionManager、TransactionManager、BigInteger gasPrice、BigInteger gasLimit){
超级(二进制、合同地址、web3j、transactionManager、gasPrice、gasLimit);
}
公共列表getNotifyEvents(TransactionReceipt TransactionReceipt){
最终事件=新事件(“通知”,
数组.asList(newtypereference(){},newtypereference(){});
List valueList=extractEventParameters(事件、事务接收);
ArrayList responses=新的ArrayList(valueList.size());
对于(EventValues EventValues:valueList){
NotifyEventResponse类型响应=新的NotifyEventResponse();
typedResponse.input=(Uint256)eventValues.getNonIndexedValues().get(0);
typedResponse.result=(Uint256)eventValues.getNonIndexedValues().get(1);
回复。添加(类型响应);
}
回应;
}
公共可观察notifyEventObservable(DefaultBlockParameter开始块、DefaultBlockParameter结束块){
最终事件=新事件(“通知”,
数组.asList(newtypereference(){},newtypereference(){});
EthFilter filter=newethfilter(startBlock、endBlock、getContractAddress());
filter.addSingleTopic(EventEncoder.encode(event));
返回web3j.ethLogObservable(filter.map)(new Func1(){
@凌驾
公共NotifyEventResponse调用(日志){
EventValues EventValues=extractEventParameters(事件、日志);
NotifyEventResponse类型响应=新的NotifyEventResponse();
typedResponse.input=(Uint256)eventValues.getNonIndexedValues().get(0);
typedResponse.result=(Uint256)eventValues.getNonIndexedValues().get(1);
返回类型响应;
}
});
}
公共未来光纤通信(Uint256号){
函数Function=new函数(“fibonacciNotify”,Arrays.asList(number),Collections.>asList(newtypereference(){}));
return executeCallSingleValueReturnAsync(函数);
}
公共静态未来部署(Web3j Web3j、凭据凭据、BigInteger gasPrice、BigInteger gasLimit、BigInteger initialWeiValue){
返回deployAsync(Fibonacci.class、web3j、凭证、gasPrice、gasLimit、BINARY、“、initialWeiValue);
}
公共静态未来部署(Web3j Web3j、TransactionManager TransactionManager、BigInteger gasPrice、BigInteger gasLimit、BigInteger initialWeiValue){
返回deployAsync(Fibonacci.class、web3j、transactionManager、gasPrice、gasLimit、BINARY、“、initialWeiValue);
}
公共静态Fibonacci加载(字符串contractAddress、Web3j、Web3j、凭据、BigInteger gasPrice、BigInteger gasLimit){
返回新的Fibonacci(合同地址、web3j、凭证、gasPrice、gasLimit);
}
公共静态Fibonacci加载(字符串contractAddress、Web3j、Web3j、TransactionManager、TransactionManager、BigInteger gasPrice、BigInteger gasLimit){
返回新的Fibonacci(合同地址、web3j、transactionManager、gasPrice、gasLimit);
}
公共静态类NotifyEventResponse{
公共Uint256输入;
公开Uint256结果;
}
}


但是我无法在这个tetrpc以太坊网络上部署。有人能回答为什么吗?

您必须检查您的客户端是否已连接到testnet并正在运行节点,或者是否已连接到连接到testnet的geth节点

您能否提供代码来说明如何尝试连接到testnet

我可以通过以下方式连接到我的geth节点:

    try {
        web3j = Web3j.build(new HttpService("Http://localhost:8545"));
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }

您必须检查您的客户端是否已连接到testnet并正在运行节点,或者是否已连接到连接到testnet的geth节点

您能否提供代码来说明如何尝试连接到testnet

我可以通过以下方式连接到我的geth节点:

    try {
        web3j = Web3j.build(new HttpService("Http://localhost:8545"));
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }

你试过在上面运行它吗?你能提供运行代码时收到的错误的详细信息吗?你试过在上面运行它吗?你能提供运行代码时收到的错误的详细信息吗?