Skip to content
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

Eliminate the possibility of overlapping hash #6

Merged
merged 1 commit into from
Oct 2, 2024
Merged
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
2 changes: 1 addition & 1 deletion packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@types/chai": "^4.3.5",
"@types/mocha": "^10.0.0",
"@types/node": "^12.20.43",
"acc-save-purchase-sdk": "~2.2.0",
"acc-save-purchase-sdk": "~2.3.0",
"assert": "^2.0.0",
"chai": "^4.3.7",
"chai-http": "^4.3.7",
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "acc-save-purchase-sdk",
"version": "2.2.0",
"version": "2.3.0",
"description": "The TypeScript DMS Store Purchase SDK library",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
15 changes: 13 additions & 2 deletions packages/sdk/src/modules/blockchain/data/BlockHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,22 @@ export class BlockHeader {
*/
public timestamp: bigint;

public nonce: bigint;

/**
* Constructor
* @param prevBlock The Hash of the previous block in the chain of blocks
* @param merkleRoot The hash of the merkle root of the transactions
* @param height The block height
* @param timestamp
* @param nonce
*/
constructor(prevBlock: Hash, merkleRoot: Hash, height: bigint, timestamp: bigint) {
constructor(prevBlock: Hash, merkleRoot: Hash, height: bigint, timestamp: bigint, nonce: bigint = 0n) {
this.prevBlock = prevBlock;
this.merkleRoot = merkleRoot;
this.height = height;
this.timestamp = timestamp;
this.nonce = nonce;
}

/**
Expand All @@ -71,7 +75,8 @@ export class BlockHeader {
new Hash(value.prevBlock),
new Hash(value.merkleRoot),
BigInt(value.height),
BigInt(value.timestamp)
BigInt(value.timestamp),
BigInt(value.nonce)
);
}

Expand All @@ -84,6 +89,7 @@ export class BlockHeader {
merkleRoot: this.merkleRoot,
height: this.height.toString(),
timestamp: this.timestamp.toString(),
nonce: this.nonce.toString(),
};
}

Expand All @@ -96,5 +102,10 @@ export class BlockHeader {
this.merkleRoot.computeHash(buffer);
buffer.writeBigUInt64LE(this.height);
buffer.writeBigUInt64LE(this.timestamp);
buffer.writeBigUInt64LE(this.nonce);
}

public increaseNonce(): void {
this.nonce++;
}
}
5 changes: 4 additions & 1 deletion packages/sdk/src/modules/utils/JSONValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ export class JSONValidator {
timestamp: {
type: "string",
},
nonce: {
type: "string",
},
},
additionalProperties: false,
required: ["prevBlock", "merkleRoot", "height", "timestamp"],
required: ["prevBlock", "merkleRoot", "height", "timestamp", "nonce"],
},
],
[
Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"dependencies": {
"@aws-sdk/client-s3": "^3.658.1",
"acc-save-purchase-contracts": "~1.0.2",
"acc-save-purchase-sdk": "~2.2.0",
"acc-save-purchase-sdk": "~2.3.0",
"argparse": "^2.0.1",
"assert": "^2.0.0",
"axios": "^1.6.7",
Expand Down
18 changes: 13 additions & 5 deletions packages/server/scripts/getS3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@ import { HTTPClient } from "../src/service/utils/HTTPClient";

import URI from "urijs";

import axios from "axios";

dotenv.config({ path: "env/.env" });

async function main() {
const PURCHASE_STORAGE_ENDPOINT = `https://${process.env.NODE_S3_BUCKET}.s3.${process.env.NODE_S3_REGION}.amazonaws.com`;
console.log(`STORE_PURCHASE_ENDPOINT : ${PURCHASE_STORAGE_ENDPOINT}`);

const client = new HTTPClient({});
const client = axios.create();
const url = URI(PURCHASE_STORAGE_ENDPOINT)
.filename("0x1035197ef87cdef2ffce17acf76e8745de9d521a47893e5df341dc02306fcab2")
.filename("0x1035197ef87cdef2ffce17acf76e8745de9d521a47893e5df341dc02306fcab23")
.toString();
console.log(`URL : ${url}`);
const response = await client.get(url);

console.log(`RESULT: ${JSON.stringify(response.data)}`);
try {
const response = await client.get(url);
console.log(`status: ${JSON.stringify(response.status)}`);
console.log(`RESULT: ${JSON.stringify(response.data)}`);
} catch (reason: any) {
if (reason.response !== undefined && reason.response.status !== undefined) {
console.log(reason.response.status);
}
}

await ContractUtils.delay(500);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/server/src/service/network/IPFSManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,8 @@ export class IPFSManager implements IStorageManager {
});
});
}

public exists(cid: string): Promise<boolean> {
return Promise.resolve(false);
}
}
1 change: 1 addition & 0 deletions packages/server/src/service/network/IStorageManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface IStorageManager {
setTest(value: boolean): any;
add(data: string | Buffer, cid: string): Promise<string>;
exists(cid: string): Promise<boolean>;
}
23 changes: 22 additions & 1 deletion packages/server/src/service/network/S3Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import { IStorageManager } from "./IStorageManager";

import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";

import axios, { AxiosInstance } from "axios";

/**
* Store data in IPFS.
*/
export class S3Manager implements IStorageManager {
private test: boolean;
private config: Config;
private s3_client: S3Client;
private axios_client: AxiosInstance;

/**
* Constructor
Expand All @@ -23,6 +26,9 @@ export class S3Manager implements IStorageManager {
secretAccessKey: this.config.node.s3_secret_key,
},
});
this.axios_client = axios.create({
baseURL: `https://${this.config.node.s3_bucket}.s3.${this.config.node.s3_region}.amazonaws.com`,
});
this.test = false;
}

Expand Down Expand Up @@ -53,9 +59,24 @@ export class S3Manager implements IStorageManager {
return resolve(cid);
})
.catch((reason) => {
console.error(reason);
return reject(new Error(reason));
});
});
}

public exists(cid: string): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
this.axios_client
.get(cid)
.then((response) => {
return resolve(true);
})
.catch((reason) => {
if (reason.response !== undefined && reason.response.status !== undefined) {
if (reason.response.status === 404) return resolve(false);
else return reject(reason);
} else return reject(reason);
});
});
}
}
8 changes: 7 additions & 1 deletion packages/server/src/service/scheduler/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,19 @@ export class Node extends Scheduler {
const txList = DBTransaction.converterTxArray(txs);

const block = Block.createBlock(this.prev_hash, this.prev_height, txList);
let blockHash: string;
while (true) {
blockHash = hashFull(block).toString();
if (!(await this.blockStorage.exists(blockHash))) break;
else block.header.increaseNonce();
}

let cid: string = "";
let success: boolean = true;

try {
// Save block
cid = await this.blockStorage.add(JSON.stringify(block), hashFull(block).toString());
cid = await this.blockStorage.add(JSON.stringify(block), blockHash);
logger.info(`Saved block to IPFS - height: ${block.header.height.toString()}, CID: ${cid}`);
} catch {
success = false;
Expand Down
Loading