Skip to content

Commit

Permalink
Refactor POST body fields from strings to integers and float
Browse files Browse the repository at this point in the history
  • Loading branch information
michielbdejong committed Sep 23, 2024
1 parent c78749d commit 7550a8a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
14 changes: 9 additions & 5 deletions src/feeder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@ if (cluster.isPrimary) {
console.log("Feeding the server...");
const data = readFileSync(TESTNET_CSV, 'utf8')
const lines = data.split('\n').map(line => {
const [ from, to, weight ] = line.split(' ')
return { from, to, weight }
}).filter(line => line.from !== 'from' && line.from !== '');
const [ fromStr, toStr, amountStr ] = line.split(' ')
return {
from: parseInt(fromStr),
to: parseInt(toStr),
amount: parseFloat(amountStr)
};
}).filter(obj => !isNaN(obj.from) && !isNaN(obj.to) && !isNaN(obj.amount));
for (let lineNo = mod; lineNo < lines.length; lineNo += numCPUs) {
// console.log(process.pid, mod, lineNo, cumm);
let cmd;
if (lines[lineNo].from === '0') {
if (lines[lineNo].from === 0) {
cmd = 'DISBURSEMENT';
} else if (lines[lineNo].to === '0') {
} else if (lines[lineNo].to === 0) {
cmd = 'RECLAMATION';
} else {
cmd = 'STANDARD';
Expand Down
4 changes: 2 additions & 2 deletions src/inmem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class InMemStores {
constructor() {
this.balances = {};
}
ensureBalance(thisParty: string, otherParty: string): void {
ensureBalance(thisParty: number, otherParty: number): void {
if (typeof this.balances[thisParty] === 'undefined') {
this.balances[thisParty] = {};
}
Expand All @@ -21,7 +21,7 @@ export class InMemStores {
async disconnect(): Promise<void> {
// noop
}
async storeTransaction({ thisParty, otherParty, amount }: { thisParty: string, otherParty: string, amount: number }): Promise<number> {
async storeTransaction({ thisParty, otherParty, amount }: { thisParty: number, otherParty: number, amount: number }): Promise<number> {
this.ensureBalance(thisParty, otherParty);
this.balances[thisParty][otherParty] += amount;
return this.balances[thisParty][otherParty];
Expand Down
2 changes: 1 addition & 1 deletion src/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class RedisStores {
async disconnect(): Promise<void> {
await this.client.quit();
}
async storeTransaction({ thisParty, otherParty, amount }: { thisParty: string, otherParty: string, amount: number }): Promise<number> {
async storeTransaction({ thisParty, otherParty, amount }: { thisParty: number, otherParty: number, amount: number }): Promise<number> {
// don't wait for this to finish:
this.client.incrByFloat(`${thisParty}:${otherParty}`, amount);
// console.log('stored transaction', thisParty, otherParty, amount, newValueString);
Expand Down
10 changes: 5 additions & 5 deletions src/tigerbeetle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export class TigerBeetleStores {
client;
constructor() {
}
async ensureBalance(thisParty: string, otherParty: string): Promise<{ ledgerId: bigint, thisPartyId: bigint, otherPartyId: bigint}> {
async ensureBalance(thisParty: number, otherParty: number): Promise<{ ledgerId: bigint, thisPartyId: bigint, otherPartyId: bigint}> {
// `thisParty` is 1,2,3,... and is multiplied by 1,000,000 to create the ledgerId.
// It is multiplied by 1,000,001 to create thisPartyId.
// `otherParty` is 0,1,2,3,... (where 0 is the bank for DISBURSEMENT and RECLAMATION) and
Expand All @@ -13,9 +13,9 @@ export class TigerBeetleStores {
// ledgerId: 37,000,000
// thisPartyId: 37,000,037
// otherPartyId: 37,054,235
const ledgerId = BigInt(parseInt(thisParty)) * BigInt(1000 * 1000);
const thisPartyId = BigInt(parseInt(thisParty)) * BigInt(1000 * 1000 + 1);
const otherPartyId = BigInt(parseInt(otherParty)) * BigInt(1000 * 1000 + 1);
const ledgerId = BigInt(thisParty) * BigInt(1000 * 1000);
const thisPartyId = BigInt(thisParty) * BigInt(1000 * 1000 + 1);
const otherPartyId = BigInt(otherParty) * BigInt(1000 * 1000 + 1);

const mainAccount = {
id: thisPartyId,
Expand Down Expand Up @@ -71,7 +71,7 @@ export class TigerBeetleStores {
async disconnect(): Promise<void> {
// noop
}
async storeTransaction({ thisParty, otherParty, amount }: { thisParty: string, otherParty: string, amount: number }): Promise<number> {
async storeTransaction({ thisParty, otherParty, amount }: { thisParty: number, otherParty: number, amount: number }): Promise<number> {
const absAmount = Math.abs(amount);
const firstChunk = Math.round(absAmount);
const afterFirst = 1000 * 1000 * (absAmount - firstChunk);
Expand Down

0 comments on commit 7550a8a

Please sign in to comment.