Skip to content

updated test-01-simple to test gateway client #547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ node_modules
.idea
.vscode
samples/invalid-fablo-config.json
.DS_Store
.DS_Store
.env
41 changes: 41 additions & 0 deletions e2e-network/docker/test-01-simple.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ FABLO_HOME="$TEST_TMP/../../.."

export FABLO_HOME

GATEWAY_CLIENT_DIR="$FABLO_HOME/samples/gateway/node"
GATEWAY_CLIENT_OUTPUT_FILE="$TEST_LOGS/gateway_client.log"

networkUp() {
"$FABLO_HOME/fablo-build.sh"
(cd "$TEST_TMP" && "$FABLO_HOME/fablo.sh" init node)
Expand Down Expand Up @@ -66,6 +69,44 @@ expectInvoke "peer1.org1.example.com" "my-channel1" "chaincode1" \
'{"Args":["KVContract:get", "name"]}' \
'{\"success\":\"Willy Wonka\"}'

# Test Node.js Gateway CLI client
echo "Testing Node.js Gateway client..."

echo "Installing gateway client dependencies..."
(cd "$GATEWAY_CLIENT_DIR" && npm install --silent --no-progress)

echo "Running Node.js Gateway client and checking output..."
(
cd "$GATEWAY_CLIENT_DIR" &&
export \
CHANNEL_NAME="my-channel1" \
CONTRACT_NAME="chaincode1" \
MSP_ID="Org1MSP" \
PEER_ORG_NAME="peer0.org1.example.com" \
PEER_GATEWAY_URL="localhost:7041" \
TLS_ROOT_CERT="$TEST_TMP/fablo-target/fabric-config/crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" \
CREDENTIALS="$TEST_TMP/fablo-target/fabric-config/crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp/signcerts/[email protected]" \
PRIVATE_KEY_PEM="$TEST_TMP/fablo-target/fabric-config/crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp/keystore/priv-key.pem" &&
node server.js > "$GATEWAY_CLIENT_OUTPUT_FILE" 2>&1
)
GATEWAY_EXIT_CODE=$?

if [ $GATEWAY_EXIT_CODE -ne 0 ]; then
echo "❌ failed: Node.js Gateway client script failed with exit code $GATEWAY_EXIT_CODE."
cat "$GATEWAY_CLIENT_OUTPUT_FILE"
exit 1
fi

if grep -qF 'Put result: {"success":"OK"}' "$GATEWAY_CLIENT_OUTPUT_FILE"; then
echo "✅ ok: Node.js Gateway client test passed!"
else
echo "❌ failed: Node.js Gateway client failed."
cat "$GATEWAY_CLIENT_OUTPUT_FILE"
exit 1
fi

echo "Node.js Gateway client test complete"

# Verify channel query scripts
(cd "$TEST_TMP" && "$FABLO_HOME/fablo.sh" channel fetch newest my-channel1 org1 peer1)
expectCommand "cat \"$TEST_TMP/newest.block\"" "KVContract:get"
Expand Down
22 changes: 19 additions & 3 deletions samples/gateway/node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions samples/gateway/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
"author": "",
"main": "server.js",
"type": "module",
"engines" : {
"node" : ">=22"
"engines": {
"node": ">=22"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"dependencies": {
"@grpc/grpc-js": "^1.13.3",
"@hyperledger/fabric-gateway": "^1.7.1"
"@hyperledger/fabric-gateway": "^1.7.1",
"dotenv": "^16.5.0"
}
}

15 changes: 9 additions & 6 deletions samples/gateway/node/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,34 @@ import { connect, hash, signers } from '@hyperledger/fabric-gateway';
import * as crypto from 'node:crypto';
import { promises as fs } from 'node:fs';
import { TextDecoder } from 'node:util';
import * as dotenv from 'dotenv';

dotenv.config();

const utf8Decoder = new TextDecoder();

async function connection() {
const credentials = await fs.readFile(process.env.CREDENTIALS);
const credentials = await fs.readFile(process.env.CREDENTIALS);
const privateKeyPem = await fs.readFile(process.env.PRIVATE_KEY_PEM);
const privateKey = crypto.createPrivateKey(privateKeyPem);
const signer = signers.newPrivateKeySigner(privateKey);
const signer = signers.newPrivateKeySigner(privateKey);
const tlsRootCert = await fs.readFile(
process.env.TLS_ROOT_CERT,
);
const client = new grpc.Client(process.env.PEER_GATEWAY_URL, grpc.credentials.createSsl(tlsRootCert), {
"grpc.ssl_target_name_override": process.env.PEER_ORG_NAME,
});
});
const gateway = connect({
identity: { mspId: process.env.MSP_ID, credentials },
signer,
hash: hash.sha256,
client,
});
});
try {
const network = gateway.getNetwork(process.env.CHANNEL_NAME);
const contract = network.getContract(process.env.CONTRACT_NAME);
const contract = network.getContract(process.env.CONTRACT_NAME);
const putResult = await contract.submitTransaction('put', 'time', new Date().toISOString());
console.log('Put result:', utf8Decoder.decode(putResult));
console.log('Put result:', utf8Decoder.decode(putResult));
const getResult = await contract.evaluateTransaction('get', 'time');
console.log('Get result:', utf8Decoder.decode(getResult));
} finally {
Expand Down
Loading