Skip to content

Commit

Permalink
Improve Synced Speed
Browse files Browse the repository at this point in the history
  • Loading branch information
chives-network committed Jun 23, 2024
1 parent c791766 commit 7be8489
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
2 changes: 0 additions & 2 deletions expressApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ cron.schedule('*/1 * * * *', () => {
console.log('schedule syncingBlock Task Begin !!!');
syncing.syncingBlock(10);
syncing.syncingBlockMinedTime(10);
syncing.syncingTx(10);
syncing.syncingChunksPromiseAll(2);
});
cron.schedule('*/2 * * * *', () => {
console.log('schedule syncingTx Task Begin !!!');
Expand Down
25 changes: 20 additions & 5 deletions src/syncing.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@
try {
const result = [];
for (const TxList of getTxsNotSyncingList) {
const TxInfor = await syncingTxById(TxList.id, TxList.block_height);
const TxInfor = await syncingTxById(TxList.id, TxList.block_height, TxList.timestamp);
log("syncingTx TxInfor: ", TxList.id, TxInfor?.id)
result.push(TxInfor)
}
Expand All @@ -453,7 +453,7 @@
const result = await Promise.all(
getTxsNotSyncingList && getTxsNotSyncingList.map(async (TxList) => {
try {
const TxInfor = await syncingTxById(TxList.id, TxList.block_height);
const TxInfor = await syncingTxById(TxList.id, TxList.block_height, TxList.timestamp);
log("syncingTxPromiseAll TxInfor: ", TxList.id, TxInfor?.id);
return TxInfor;
}
Expand All @@ -474,7 +474,7 @@
async function getTxsNotSyncing(TxCount) {
const { NodeApi, DataDir, arweave, db } = await initChivesLightNode()
return new Promise((resolve, reject) => {
db.all("SELECT id, block_height from tx where from_address is null order by block_height asc limit " + TxCount + " offset 0", (err, result) => {
db.all("SELECT id, block_height, timestamp from tx where from_address is null order by block_height asc limit " + TxCount + " offset 0", (err, result) => {
if (err) {
reject(err);
} else {
Expand Down Expand Up @@ -568,7 +568,7 @@
});
}

async function syncingTxById(TxId, Height) {
async function syncingTxById(TxId, Height, timestamp = 0) {
const { NodeApi, DataDir, arweave, db } = await initChivesLightNode()
// @ts-ignore
let TxInfor = null
Expand Down Expand Up @@ -689,7 +689,13 @@
log("TxInfor from_address: ", from_address)

//Insert Address
const BlockInfor = await getBlockInforByHeightFromDb(Height);
let BlockInfor = {}
if(timestamp == 0) {
BlockInfor = await getBlockInforByHeightFromDb(Height);
}
else {
BlockInfor = {height: Height, timestamp: timestamp }
}
const insertAddress = db.prepare('INSERT OR IGNORE INTO address (id, lastblock, timestamp, balance, txs, profile, referee, last_tx_action) VALUES (?, ?, ?, ?, ?, ?, ?, ?)');
insertAddress.run(from_address, BlockInfor.height, BlockInfor.timestamp, 0, 0, "", "", "");
insertAddress.finalize();
Expand Down Expand Up @@ -1456,6 +1462,10 @@
}).then(res=>res.data).catch(() => {});
log("syncingBlockByHeight Get Block From Remote Node",BlockInfor?.reward_addr, currentHeight)
}

if(BlockInfor == null) {
return
}

// Begin a transaction
//db.exec('BEGIN TRANSACTION');
Expand Down Expand Up @@ -1486,6 +1496,11 @@
})
insertTx.finalize();

//Update Tx Detail
await Promise.all(Txs.map(async (Tx) => {
await syncingTxById(Tx, BlockInfor.height, BlockInfor.timestamp);
}));

//Write Block File
writeFile("/blocks/" + enableBlockHeightDir(currentHeight), BlockInfor.height + ".json", JSON.stringify(BlockInfor), "syncingBlockByHeight")

Expand Down

0 comments on commit 7be8489

Please sign in to comment.