0

我正在尝试从 fabric-samples 安装 facar 的 java 链代码,但出现错误。

https://github.com/hyperledger/fabric-samples/tree/release-1.4/chaincode/fabcar/java

在链码的实例化过程中,我正在尝试并得到错误

有人可以帮助我吗,非常感谢并提前感谢。

我正在使用基本网络文件夹和下面的脚本来创建和安装网络,实例化和调用链代码。

#!/bin/bash
#
# Copyright IBM Corp All Rights Reserved
#
# SPDX-License-Identifier: Apache-2.0
#
# Exit on first error, print all commands.
set -ev

# don't rewrite paths for Windows Git Bash users
export MSYS_NO_PATHCONV=1

docker-compose -f docker-compose.yml down

docker-compose -f docker-compose.yml up -d ca.example.com orderer.example.com peer0.org1.example.com couchdb
docker ps -a

# wait for Hyperledger Fabric to start
# incase of errors when running later commands, issue export FABRIC_START_TIMEOUT=<larger number>
export FABRIC_START_TIMEOUT=10
#echo ${FABRIC_START_TIMEOUT}
sleep ${FABRIC_START_TIMEOUT}

# Create the channel
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/Admin@org1.example.com/msp" peer0.org1.example.com peer channel create -o orderer.example.com:7050 -c mychannel -f /etc/hyperledger/configtx/channel.tx
# Join peer0.org1.example.com to the channel.
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/Admin@org1.example.com/msp" peer0.org1.example.com peer channel join -b mychannel.block

docker-compose -f docker-compose.yml up -d cli
sleep 5

#install chaincode
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp" cli peer chaincode install -n mycc -v 1.0 -p /opt/gopath/src/github.com/fabcar/java -l java

#instantiate chaincode
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp" cli peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n mycc -l java -v 1.0 -c '{"Args":[""]}' -P "OR ('Org1MSP.member','Org2MSP.member')"

sleep 5

#invoke chaincode
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_ADDRESS=peer0.org1.example.com:7051"  -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp" cli peer chaincode invoke -o orderer.example.com:7050 -C mychannel -n mycc -c '{"function":"initLedger","Args":[""]}'

下面给出的 docker logs peer0.org1.example.com 命令的输出

2019-08-06 09:54:26.150 UTC [endorser] SimulateProposal -> ERRO 043 [mychannel][e0746cf6] 未能调用链代码名称:“lscc”,错误:交易返回失败:调用未定义的合约方法

下面给出的 docker logs dev-peer0.org1.example.com-mycc-1.0 命令的输出

09:54:26:142 SEVERE  org.hyperledger.fabric.Logger error                                              Undefined contract method

调用 org.hyperledger.fabric.shim.ChaincodeException:在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect 的 org.hyperledger.fabric.contract.ContractInterface.unknownTransaction(ContractInterface.java:76) 调用未定义的合约方法。 NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.hyperledger.fabric.contract .execution.impl.ContractExecutionService.executeRequest(ContractExecutionService.java:57) 在 org.hyperledger.fabric.contract.ContractRouter.processRequest(ContractRouter.java:87) 在 org.hyperledger.fabric.contract.ContractRouter。init(ContractRouter.java:103) 在 org.hyperledger.fabric.shim.impl.Handler.lambda$handleInit$0(Handler.java:280) 在 java.lang.Thread.run(Thread.java:748)

:54:26:147 SEVERE  org.hyperledger.fabric.shim.impl.Handler lambda$handleInit$0                     [e0746cf6] Init failed.

发送错误

4

3 回答 3

1

也许你忘记了@Default装饰器

@Contract(name = "MyContract",
        info = @Info(
                title = "My Contract",
                description = "",
                version = "0.0.1"
        )
)
@Default
public class MyContract implements ContractInterface {
于 2020-04-18T14:40:37.383 回答
0

调用未定义的合约方法

在代码的前几行。这意味着您传递给调用的函数不存在于链代码中,您能否检查链代码并查看 initLedger 是否存在?或者是否有任何拼写错误。希望这很有用。

于 2019-08-06T12:23:31.367 回答
0

如果您在实例化链码时遇到此问题,则可以使用自定义实现覆盖ContractInterface中给出的以下方法。

/**
 * Invoked for any transaction that does not exist.
 *
 * This will throw an exception. If you wish to alter the exception thrown or 
 * if you wish to consider requests for transactions that don't exist as not an
 * error, subclass this method.
 *
 * @param ctx the context as created by {@link #createContext(ChaincodeStub)}.
 */
default void unknownTransaction(Context ctx) {
    throw new ChaincodeException("Undefined contract method called");
}

否则,如果在调用现有事务时出现问题,请确保注释 @Transaction() 存在于事务方法上。

于 2020-01-27T06:45:47.487 回答