Skip to content

Commit

Permalink
refactor(validator): validators including execSyncFunctions
Browse files Browse the repository at this point in the history
Signed-off-by: Takuma TAKEUCHI <[email protected]>
  • Loading branch information
takeutak authored and petermetz committed Dec 18, 2020
1 parent cff3b23 commit deae3e4
Show file tree
Hide file tree
Showing 150 changed files with 3,504 additions and 4,244 deletions.
25 changes: 19 additions & 6 deletions examples/cartrade/BusinessLogicCartrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ export class BusinessLogicCartrade extends BusinessLogicBase {
const transactionData: TransactionData = new TransactionData("escrow", "ledger001", result.txId);
this.transactionInfoManagement.setTransactionData(tradeInfo, transactionData);

// Set LedgerOperation
// Set Parameter
logger.debug('firstTransaction data : ' + JSON.stringify(result.data));
const ledgerOperation: LedgerOperation = new LedgerOperation("sendRawTransaction", "", result.data);
const contract = {}; // NOTE: Since contract does not need to be specified, specify an empty object.
const method = {type: "web3Eth", command: "sendRawTransaction"};
const args = {"args": [result.data["serializedTx"]]};

// Run Verifier (Ethereum)
verifierEthereum.requestLedgerOperation(ledgerOperation);
// Run Verifier (Fabric)
verifierEthereum.requestLedgerOperationNeo(contract, method, args);
})
.catch(err => {
logger.error(err);
Expand Down Expand Up @@ -175,7 +177,7 @@ export class BusinessLogicCartrade extends BusinessLogicBase {
// Set Parameter
//logger.debug('secondTransaction data : ' + JSON.stringify(result.data));
const contract = {"channelName": "mychannel"};
const method = {"method": "sendSignedProposal"};
const method = {"type": "sendSignedTransaction"};
const args = {"args": [result.data]};

// Run Verifier (Fabric)
Expand Down Expand Up @@ -222,7 +224,18 @@ export class BusinessLogicCartrade extends BusinessLogicBase {
const ledgerOperation: LedgerOperation = new LedgerOperation("sendRawTransaction", "", result.data);

// Run Verifier (Ethereum)
verifierEthereum.requestLedgerOperation(ledgerOperation);
// verifierEthereum.requestLedgerOperation(ledgerOperation);

// TODO: Neo!!
// Set Parameter
const contract = {}; // NOTE: Since contract does not need to be specified, specify an empty object.
const method = {type: "web3Eth", command: "sendRawTransaction"};
const args = {"args": [result.data["serializedTx"]]};

// Run Verifier (Fabric)
verifierEthereum.requestLedgerOperationNeo(contract, method, args);


})
.catch(err => {
logger.error(err);
Expand Down
85 changes: 43 additions & 42 deletions examples/cartrade/README.md
Original file line number Diff line number Diff line change
@@ -1,63 +1,64 @@
# example-cartrade

## Prerequisites

Before you begin, you need to check that you have all the prerequisites installed as follows:
- Docker (recommend: v17.06.2-ce or greater)
- Docker-compose (recommend: v1.14.0 or greater)
- node.js (recommend: v10.23.0 or greater)
- The ports 5034, 5040, 5050 are available (If they are already used, the following processes can be done by changing the port number setting)

## Boot method

1. Before booting, please modify the following information for your environment
- applicationHostInfo.hostName (URL of the host on the Location header) on `/packages/config/default.json`
- applicationHostInfo.hostPort (The port number of Routing-interface http server) on `/packages/config/default.json`
- `applicationHostInfo.hostName` (IP address of the host on the Location header) on `/packages/config/default.json`
- `applicationHostInfo.hostPort` (The port number of Routing-interface http server, the default is 5034) on `/packages/config/default.json`

1. Start ledgers:
- `./script-start-ledgers.sh`
1. Build validators, packages, and the cartrade app:
- `./script-build-all.sh`
1. Start validators and the cartrade app
- Please open three consoles and execute the following:.
- Start the validator for Fabric:
- Start the validator for Fabric on the first console:
`./script-start-validator-fabric.sh`
- Start the validator for Ethereum:
- Start the validator for Ethereum on the second console:
`./script-start-validator-ethereum.sh`
- Start the cartrade app:
- Start the cartrade app on the third console:
`./script-start-cartrade.sh`

## How to use this application
- Run with curl, etc. Currently only POST is available.
- Examples of curl POSTs:.
- **transaction execution**
- `curl localhost:5034/api/v1/bl/trades/ -XPOST -H "Content-Type: application/json" -d '{"businessLogicID":"guks32pf","tradeParams":["0xec709e1774f0ce4aba47b52a499f9abaaa159f71", "0x9d624f7995e8bd70251f8265f2f9f2b49f169c55", "user01", "user02", 50, "CAR1"],"authParams":["none"]}'`
- **Transaction Reference**
- `curl localhost:5034/api/v1/bl/trades/XXXXXXXXXXXXXX-XXX -XGET`
- **Login**
- `curl localhost:5034/api/v1/bl/login/ -XPOST -H "Content-Type: application/json" -d '{"userid":"user01","pwd":"hoge"}'`

- In the case of the above "transaction execution", the console log is the following:
```
[2020-08-21T19:55:24.207] [INFO] TransactionManagement - tradeID: 20200821195524-001
[2020-08-21T19:55:24.282] [INFO] BusinessLogicCartrade - firstTransaction txId : 0xafe7c812ab55c02feb691d2133bbba2c38abaf7f221794c3ca833a29708f4653
[2020-08-21T19:56:20.005] [INFO] BusinessLogicCartrade - ##INFO: underEscrow -> underTransfer, businessLogicID: guks32pf, tradeID: 20200821195524-001
[2020-08-21T19:56:20.608] [INFO] BusinessLogicCartrade - secondTransaction txId : 17c7577f73560ea5955f3151ed678833aa45d1252b34c6f933a7123757e82969
[2020-08-21T19:56:23.691] [INFO] BusinessLogicCartrade - ##INFO: underTransfer -> underSettlement, businessLogicID: guks32pf, tradeID: 20200821195524-001
[2020-08-21T19:56:23.703] [INFO] BusinessLogicCartrade - thirdTransaction txId : 0x61acb066349e24319afdf272b35429d198046e10f8fca3972f17a9e9a4dca75d
[2020-08-21T19:56:31.518] [INFO] BusinessLogicCartrade - ##INFO: completed cartrade, businessLogicID: guks32pf, tradeID: 20200821195524-001
```
- When the message `"##INFO: completed cartrade"` happens, the transaction is completed.

## How to confirm operation results
- How to check your account balance
- `/packages/ledger-plugin/go-ethereum/validator/unit-test/validatorDriver_getNumericBalance.js`
- Example: `node validatorDriver_getNumericBalance.js`
- The balance of the account is displayed on the Validator's console.
- Example output
```
[2020-08-18T17:06:15.795] [INFO] connector_main[6710] - Response :{"status":200,"amount":1900}
```
- How to identify the owner of the car
- `/packages/ledger-plugin/fabric/validator/unit-test/queryCar.js`
- Example: `node queryCar.js CAR1`
- The owner of the car is displayed on the console.
- Example output
1. (Optional) Check the balance on Ethereum and the fabcar ownership on Fabric using the following script
- `./script-build-get-app.sh` (only the first time)
- `./script-get-app.sh`
- The result is as the following:
```
[process] Execute an app for getting Balance on Ethereum
The balance of fromAccount:
BigNumber { s: 1, e: 4, c: [ 100000 ] }
The balance of toAccount:
BigNumber { s: 1, e: 1, c: [ 0 ] }
[process] Execute an app for getting ownership on Fabcar
##queryCar Params: CAR1
Transaction has been evaluated, result is: {"colour":"red","make":"Ford","model":"Mustang","owner":"user02"}
Transaction has been evaluated, result is: {"colour":"red","make":"Ford","model":"Mustang","owner":"Brad"}
```
1. Run the transaction execution using the following script
- `./script-post-cartrade-sample.sh`
- After this, the transactions are executed by order. When the following log appears on the above third console (the console of `./script-start-cartrade.sh`), the transactions are completed.
```
- How to change the ownership of the car by hand
- ``/packages/ledger-plugin/fabric/validator/unit-test/validatorDriver_signTransactionOffline.js``
[INFO] BusinessLogicCartrade - ##INFO: completed cartrade, businessLogicID: guks32pf, tradeID: *******-001
```
1. (Optional) Check the balance on Ethereum and the fabcar ownership on Fabric using the following script
- `./script-get-app.sh`
- The result is as the following. In the following case, 50 coins from fromAccount was transferred to toAccount, and the car ownership ("owner") was transferred.
```
[process] Execute an app for getting Balance on Ethereum
The balance of fromAccount:
BigNumber { s: 1, e: 4, c: [ 99950 ] }
The balance of toAccount:
BigNumber { s: 1, e: 1, c: [ 50 ] }
[process] Execute an app for getting ownership on Fabcar
##queryCar Params: CAR1
Transaction has been evaluated, result is: {"colour":"red","make":"Ford","model":"Mustang","owner":"Cathy"}
```
3 changes: 0 additions & 3 deletions examples/cartrade/TransactionEthereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,11 @@ export function makeRawTransaction(txParam: { fromAddress: string, fromAddressPk
// const gasPrice: string = web3.eth.getGasPrice();

const latestNonce = getLatestNonce(txParam.fromAddress);
logger.debug(`#####(A) _nonce: ${_nonce}, latestNonce: ${latestNonce}`);
logger.debug(`####makeRawTransaction(): fromAddress: ${txParam.fromAddress}, txnCount: ${web3.utils.toHex(txnCount)}, latestNonce: ${web3.utils.toHex(latestNonce)}`);
if (txnCount <= latestNonce) {
txnCount = latestNonce + 1;
logger.debug(`####makeRawTransaction(): Adjust txnCount, fromAddress: ${txParam.fromAddress}, txnCount: ${web3.utils.toHex(txnCount)}, latestNonce: ${web3.utils.toHex(latestNonce)}`);
}
logger.debug(`#####(B) _nonce: ${_nonce}, latestNonce: ${latestNonce}, txnCount: ${txnCount}`);
setLatestNonce(txParam.fromAddress, txnCount);

const privKey: Buffer = Buffer.from(txParam.fromAddressPkey, 'hex');
Expand Down Expand Up @@ -99,7 +97,6 @@ function getLatestNonce(fromAddress: string): number {
if (mapFromAddressNonce.has(fromAddress)) {
return mapFromAddressNonce.get(fromAddress);
}
//return 0;
return -1;
}

Expand Down
14 changes: 8 additions & 6 deletions examples/cartrade/script-build-all.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
cd ../..

## Build validator for Fabric
cd ../../packages/ledger-plugin/fabric/validator/src/
cd ./packages/ledger-plugin/fabric/validator/src
./script-build-validator.sh
cp -a ../../../../../examples/cartrade/build/wallet/ .
cd ../../../../../
cp -a ../../../../../examples/cartrade/build/wallet .
cd ../../../../..

## build validator for Ethereum
cd ./packages/ledger-plugin/go-ethereum/validator/src/
cd ./packages/ledger-plugin/go-ethereum/validator/src
./script-build-validator.sh
cd ../../../../../
cd ../../../../..

## build packages
cd ./packages/
cd ./packages
./script-build-packages.sh
cd ..

Expand Down
2 changes: 1 addition & 1 deletion examples/cartrade/script-build-cartrade.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Build a cartrade app
echo "[process] Build a cartrade app"
npm install
npm run build
npm run build
7 changes: 7 additions & 0 deletions examples/cartrade/script-build-get-app.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Build a getting app
echo "[process] Build an app for getting Balance on Ethereum"
cd script-test-getFunctions/go-ethereum
npm install
echo "[process] Build an app for getting ownership on Fabcar"
cd ../fabric
npm install
7 changes: 7 additions & 0 deletions examples/cartrade/script-get-app.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Execute a getting app
echo "[process] Execute an app for getting Balance on Ethereum"
cd script-test-getFunctions/go-ethereum
node getBalance.js
echo "[process] Execute an app for getting ownership on Fabcar"
cd ../fabric
node queryCar.js CAR1
2 changes: 1 addition & 1 deletion examples/cartrade/script-post-cartrade-sample.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
curl localhost:5034/api/v1/bl/trades/ -XPOST -H "Content-Type: application/json" -d '{"businessLogicID":"guks32pf","tradeParams":["0xec709e1774f0ce4aba47b52a499f9abaaa159f71", "0x9d624f7995e8bd70251f8265f2f9f2b49f169c55", "user01", "user02", 50, "CAR1"],"authParams":["param"]}'
curl localhost:5034/api/v1/bl/trades/ -XPOST -H "Content-Type: application/json" -d '{"businessLogicID":"guks32pf","tradeParams":["0x06fc56347d91c6ad2dae0c3ba38eb12ab0d72e97", "0x9d624f7995e8bd70251f8265f2f9f2b49f169c55", "Brad", "Cathy", 50, "CAR1"],"authParams":["none"]}'
2 changes: 1 addition & 1 deletion examples/cartrade/script-start-cartrade.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cd ../../
cd ../..

## Start cartrade app
echo "[process] Start the routing interface and the cartrade app"
Expand Down
10 changes: 6 additions & 4 deletions examples/cartrade/script-start-ledgers.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
## Start docker environment for Fabric testnet
cd ../../tools/docker/fabric14-fabcar-testnet
cd ../..

## Start docker environment for Go-Ethereum testnet
cd ./tools/docker/geth-testnet
./script-start-docker.sh
cd ../../..

## Start docker environment for Go-Ethereum testnet
cd ../../tools/docker/geth-testnet
## Start docker environment for Fabric testnet
cd ./tools/docker/fabric14-fabcar-testnet
./script-start-docker.sh
2 changes: 1 addition & 1 deletion examples/cartrade/script-start-validator-ethereum.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cd ..
cd ../..

echo "[process] Start the validator for Go-Ethereum"
cd ./packages/ledger-plugin/go-ethereum/validator/src
Expand Down
2 changes: 1 addition & 1 deletion examples/cartrade/script-start-validator-fabric.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cd ..
cd ../..

echo "[process] Start the validator for Fabric"
cd ./packages/ledger-plugin/fabric/validator/src
Expand Down
7 changes: 7 additions & 0 deletions examples/cartrade/script-stop-cartrade.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
echo "[process] Stop the validators and the cartrade app"
# stop the validator for Ethereum
kill -9 $(lsof -t -i:5050)
# stop the validator for Fabric
kill -9 $(lsof -t -i:5040)
# stop the cartrade app
kill -9 $(lsof -t -i:5034)
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "basic-network",
"version": "1.0.0",
"client": {
"organization": "Org1",
"connection": {
"timeout": {
"peer": {
"endorser": "300"
},
"orderer": "300"
}
}
},
"channels": {
"mychannel": {
"orderers": [
"orderer.example.com"
],
"peers": {
"peer0.org1.example.com": {}
}
}
},
"organizations": {
"Org1": {
"mspid": "Org1MSP",
"peers": [
"peer0.org1.example.com"
],
"certificateAuthorities": [
"ca.example.com"
]
}
},
"orderers": {
"orderer.example.com": {
"url": "grpc://localhost:7050"
}
},
"peers": {
"peer0.org1.example.com": {
"url": "grpc://localhost:7051"
}
},
"certificateAuthorities": {
"ca.example.com": {
"url": "http://localhost:7054",
"caName": "ca.example.com"
}
}
}
11 changes: 11 additions & 0 deletions examples/cartrade/script-test-getFunctions/fabric/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "validatorDriver",
"version": "0.0.0",
"private": true,
"dependencies": {
"config": "^1.26.1",
"socket.io": "^2.0.4",
"fabric-ca-client": "~1.4.0",
"fabric-network": "~1.4.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
* queryCar.js
*/

////////
// Usage
// TODO:
//
////////

'use strict';

const { FileSystemWallet, Gateway } = require('fabric-network');
Expand Down Expand Up @@ -73,4 +67,4 @@ async function main() {
}
}

main();
main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const Web3 = require('web3');
const web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider("http://localhost:8545"));
const fromAccount = "0x06fc56347d91c6ad2dae0c3ba38eb12ab0d72e97"
const toAccount = "0x9d624f7995e8bd70251f8265f2f9f2b49f169c55"
console.log("The balance of fromAccount:");
var res = web3.eth.getBalance(fromAccount);
console.log(res);
console.log("The balance of toAccount:");
var res = web3.eth.getBalance(toAccount);
console.log(res);
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"dependencies": {
"fs": "^0.0.1-security",
"jsonfile": "^5.0.0",
"web3": "^0.20.7"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"license": "ISC"
}
Loading

0 comments on commit deae3e4

Please sign in to comment.