Skip to content

Commit

Permalink
logs refine
Browse files Browse the repository at this point in the history
  • Loading branch information
kongliangzhong committed Oct 12, 2018
1 parent 3f48337 commit 243b335
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 38 deletions.
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from "./src/context";
export * from "./src/ensure";
export * from "./src/exchange_deserializer";
export * from "./src/expectThrow";
export * from "./src/logs";
export * from "./src/mining";
export * from "./src/multihash";
export * from "./src/order";
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "protocol2-js",
"version": "0.1.24",
"version": "0.1.25",
"description": "loopring protocol simulator core lib",
"main": "index.ts",
"repository": {
Expand Down
4 changes: 2 additions & 2 deletions src/bitstream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class Bitstream {

public addRawBytes(bs: string, forceAppend = true) {
const bsHex = web3.toHex(bs);
// console.log("bsHex:", bsHex);
// logDebug("bsHex:", bsHex);
return this.insert(bsHex.slice(2), forceAppend);
}

Expand Down Expand Up @@ -117,7 +117,7 @@ export class Bitstream {
start = this.data.indexOf(x, start);
if (start !== -1) {
if ((start % 2) === 0) {
// console.log("++ Reused " + x + " at location " + start / 2);
// logDebug("++ Reused " + x + " at location " + start / 2);
return start / 2;
} else {
start++;
Expand Down
3 changes: 2 additions & 1 deletion src/ensure.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { logDebug } from "./logs";

export function ensure(valid: boolean, description: string) {
if (!valid) {
console.log(description);
logDebug(description);
}
return valid;
}
12 changes: 12 additions & 0 deletions src/logs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

export function logInfo(...args: any[]) {
if (process.argv.indexOf("-i") > -1) {
console.log(...args);
}
}

export function logDebug(...args: any[]) {
if (process.argv.indexOf("-x") > -1) {
console.log(...args);
}
}
13 changes: 7 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs = require("fs");
import { logDebug } from "./logs";
import { ProtocolSimulator } from "./protocol_simulator";
import { RingsInfo } from "./types";

Expand All @@ -20,7 +21,7 @@ function parseArgs(): [boolean, string] {
}
} else if (args[0] === "-b") {
const binData = args[1];
console.log(binData);
logDebug(binData);
} else {
if (args[0].startsWith("-")) {
throw new Error("error: invalid argument:" + args[0]);
Expand All @@ -38,17 +39,17 @@ async function main() {

const protocolSimulator = new ProtocolSimulator(undefined);
if (isBinData) {
console.log("receive binary data, deserializing...");
logDebug("receive binary data, deserializing...");
ringsInfo = protocolSimulator.deserialize(data, "");
console.log("deserialize result:", ringsInfo);
logDebug("deserialize result:", ringsInfo);
} else {
ringsInfo = JSON.parse(data);
console.log("receive ringsInfo data:", ringsInfo);
logDebug("receive ringsInfo data:", ringsInfo);
}

console.log("run simulator and report:");
logDebug("run simulator and report:");
const report = await protocolSimulator.simulateAndReport(ringsInfo);
console.log(report);
logDebug(report);
}

main();
5 changes: 3 additions & 2 deletions src/multihash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ABI = require("ethereumjs-abi");
import ethUtil = require("ethereumjs-util");
import Web3 = require("web3");
import { Bitstream } from "./bitstream";
import { logDebug } from "./logs";
import { OrderInfo, RingsInfo, SignAlgorithm } from "./types";

export class MultiHashUtil {
Expand All @@ -19,7 +20,7 @@ export class MultiHashUtil {
this.web3Instance = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
} catch (err) {
console.log("get web3 instance in Order class failed. err:", err);
logDebug("get web3 instance in Order class failed. err:", err);
}
}

Expand Down Expand Up @@ -103,7 +104,7 @@ export class MultiHashUtil {
// method: "eth_signTypedData",
// params: [msgParams, order.owner],
// from: order.owner,
// }, (err?: Error, result?: Web3.JSONRPCResponsePayload) => { console.log("Hashing: " + result.result); });
// }, (err?: Error, result?: Web3.JSONRPCResponsePayload) => { logDebug("Hashing: " + result.result); });
sig.addNumber(1 + 32 + 32, 1);
sig.addNumber(v, 1);
Expand Down
4 changes: 4 additions & 0 deletions src/protocol_validator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BigNumber } from "bignumber.js";
import { Context } from "./context";
import { logDebug } from "./logs";
import { OrderUtil } from "./order";
import { OrderExpectation, OrderInfo, RingsInfo, SimulatorReport } from "./types";

Expand Down Expand Up @@ -95,6 +96,7 @@ export class ProtocolValidator {
feePayments);

// Balances

const totalS = orderSettlement.amountS.minus(orderSettlement.rebateS);
const totalB = orderSettlement.amountB.minus(orderSettlement.amountFeeB).plus(orderSettlement.rebateB);
const totalFee = orderSettlement.amountFee.minus(orderSettlement.rebateFee);
Expand Down Expand Up @@ -128,6 +130,7 @@ export class ProtocolValidator {
for (const owner of Object.keys(expectedBalances[token])) {
// const ownerName = addressBook[owner];
// const tokenSymbol = this.testContext.tokenAddrSymbolMap.get(token);

// console.log("[Sim]" + ownerName + ": " +
// report.balancesAfter[token][owner].toNumber() / 1e18 + " " + tokenSymbol);
// console.log("[Exp]" + ownerName + ": " +
Expand All @@ -146,6 +149,7 @@ export class ProtocolValidator {
for (const owner of Object.keys(expectedFeeBalances[token])) {
// const ownerName = addressBook[owner];
// const tokenSymbol = this.testContext.tokenAddrSymbolMap.get(token);

// console.log("[Sim]" + ownerName + ": " + report.feeBalancesAfter[token][owner] / 1e18 + " " + tokenSymbol);
// console.log("[Exp]" + ownerName + ": " + expectedFeeBalances[token][owner] / 1e18 + " " + tokenSymbol);
this.assertAlmostEqual(report.feeBalancesAfter[token][owner].toNumber(),
Expand Down
44 changes: 23 additions & 21 deletions src/ring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ABI = require("ethereumjs-abi");
import { Bitstream } from "./bitstream";
import { Context } from "./context";
import { ensure } from "./ensure";
import { logDebug } from "./logs";
import { Mining } from "./mining";
import { OrderUtil } from "./order";
import { DetailedTokenTransfer, OrderInfo, OrderPayments, Participation, RingPayments, TransferItem } from "./types";
Expand Down Expand Up @@ -530,29 +531,30 @@ export class Ring {
const p = this.participations[i];
const prevP = this.participations[prevIndex];

console.log("p.spendableS: " + p.ringSpendableS.toNumber() / 1e18);
console.log("p.spendableFee: " + p.ringSpendableFee.toNumber() / 1e18);
console.log("order.amountS: " + p.order.amountS / 1e18);
console.log("order.amountB: " + p.order.amountB / 1e18);
console.log("order.feeAmount: " + p.order.feeAmount / 1e18);
console.log("order expected rate: " + p.order.amountS / p.order.amountB);
console.log("p.fillAmountS: " + p.fillAmountS.toNumber() / 1e18);
console.log("p.fillAmountB: " + p.fillAmountB.toNumber() / 1e18);
console.log("p.splitS: " + p.splitS.toNumber() / 1e18);
console.log("order actual rate: " + p.fillAmountS.plus(p.splitS).div(p.fillAmountB).toNumber());
console.log("p.feeAmount: " + p.feeAmount.toNumber() / 1e18);
console.log("p.feeAmountS: " + p.feeAmountS.toNumber() / 1e18);
console.log("p.feeAmountB: " + p.feeAmountB.toNumber() / 1e18);
console.log("p.rebateFee: " + p.rebateFee.toNumber() / 1e18);
console.log("p.rebateS: " + p.rebateS.toNumber() / 1e18);
console.log("p.rebateB: " + p.rebateB.toNumber() / 1e18);
console.log("tokenS percentage: " + (p.order.P2P ? p.order.tokenSFeePercentage : 0) /

logDebug("p.spendableS: " + p.ringSpendableS.toNumber() / 1e18);
logDebug("p.spendableFee: " + p.ringSpendableFee.toNumber() / 1e18);
logDebug("order.amountS: " + p.order.amountS / 1e18);
logDebug("order.amountB: " + p.order.amountB / 1e18);
logDebug("order.feeAmount: " + p.order.feeAmount / 1e18);
logDebug("order expected rate: " + p.order.amountS / p.order.amountB);
logDebug("p.fillAmountS: " + p.fillAmountS.toNumber() / 1e18);
logDebug("p.fillAmountB: " + p.fillAmountB.toNumber() / 1e18);
logDebug("p.splitS: " + p.splitS.toNumber() / 1e18);
logDebug("order actual rate: " + p.fillAmountS.plus(p.splitS).div(p.fillAmountB).toNumber());
logDebug("p.feeAmount: " + p.feeAmount.toNumber() / 1e18);
logDebug("p.feeAmountS: " + p.feeAmountS.toNumber() / 1e18);
logDebug("p.feeAmountB: " + p.feeAmountB.toNumber() / 1e18);
logDebug("p.rebateFee: " + p.rebateFee.toNumber() / 1e18);
logDebug("p.rebateS: " + p.rebateS.toNumber() / 1e18);
logDebug("p.rebateB: " + p.rebateB.toNumber() / 1e18);
logDebug("tokenS percentage: " + (p.order.P2P ? p.order.tokenSFeePercentage : 0) /
this.context.feePercentageBase);
console.log("tokenS real percentage: " + p.feeAmountS.toNumber() / p.order.amountS);
console.log("tokenB percentage: " +
logDebug("tokenS real percentage: " + p.feeAmountS.toNumber() / p.order.amountS);
logDebug("tokenB percentage: " +
(p.order.P2P ? p.order.tokenBFeePercentage : p.order.feePercentage) / this.context.feePercentageBase);
console.log("tokenB real percentage: " + p.feeAmountB.toNumber() / p.fillAmountB.toNumber());
console.log("----------------------------------------------");
logDebug("tokenB real percentage: " + p.feeAmountB.toNumber() / p.fillAmountB.toNumber());
logDebug("----------------------------------------------");

// Sanity checks
assert(p.fillAmountS.gte(0), "fillAmountS should be positive");
Expand Down
9 changes: 5 additions & 4 deletions src/rings_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ethUtil = require("ethereumjs-util");
import Web3 = require("web3");
import { Bitstream } from "./bitstream";
import { Context } from "./context";
import { logDebug } from "./logs";
import { MultiHashUtil } from "./multihash";
import { OrderUtil } from "./order";
import { Ring } from "./ring";
Expand Down Expand Up @@ -164,10 +165,10 @@ export class RingsGenerator {
param.ringSpecs = ringsInfo.rings;
ringsInfo.orders.map((o) => this.createOrderTable(o, param));

// console.log("transactionOrigin: " + ringsInfo.transactionOrigin);
// console.log("feeRecipient: " + ringsInfo.feeRecipient);
// console.log("miner: " + ringsInfo.miner);
ringsInfo.orders.forEach((o) => console.log(o));
// logDebug("transactionOrigin: " + ringsInfo.transactionOrigin);
// logDebug("feeRecipient: " + ringsInfo.feeRecipient);
// logDebug("miner: " + ringsInfo.miner);
ringsInfo.orders.forEach((o) => logDebug(o));
return param;
}

Expand Down

0 comments on commit 243b335

Please sign in to comment.