-
Notifications
You must be signed in to change notification settings - Fork 14
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
Shard 1426 #128
Open
devendra-shardeum
wants to merge
6
commits into
dev
Choose a base branch
from
shard-1426
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Shard 1426 #128
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
12deda1
added prepared statements manager for standardising how we handle pre…
devendra-shardeum 24500e5
made the prepared statement manager even better
devendra-shardeum 09efac8
added prepared statements for cycles database
devendra-shardeum 5fe4424
added prepared statements for receipts database
devendra-shardeum 51ac271
added prepared statements for originalTxsData database
devendra-shardeum 907f97e
added prepared statements in all the files
devendra-shardeum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,13 @@ import * as Utils from '../Utils' | |
import { globalAccountsMap } from '../GlobalAccount' | ||
import * as NodeList from '../NodeList' | ||
import { currentNetworkMode } from './Cycles' | ||
import { | ||
fetchAccountsByRangeWithAccountOffset, | ||
fetchAccountsByRangeWithoutAccountOffset, | ||
AccountsCopy | ||
} from '../dbstore/accounts'; | ||
import { DeSerializeFromJsonString } from '../utils/serialization' | ||
|
||
|
||
interface WrappedData { | ||
/** Account ID */ | ||
|
@@ -175,90 +182,178 @@ export const validateGlobalAccountReportRequest = ( | |
} | ||
return { success: true } | ||
} | ||
|
||
|
||
/** | ||
* | ||
* This function is contructed to provide data in similar way as the `getAccountDataByRangeSmart` function in the validator | ||
* Provides account data in the range specified by the payload. | ||
* @param payload | ||
* @returns GetAccountDataByRangeSmart | ||
*/ | ||
export const provideAccountDataRequest = async ( | ||
payload: AccountDataRequestSchema | ||
): Promise<GetAccountDataByRangeSmart> => { | ||
const wrappedAccounts: WrappedStateArray = [] | ||
const wrappedAccounts2: WrappedStateArray = [] // We might not need to provide data to this | ||
let lastUpdateNeeded = false | ||
let highestTs = 0 | ||
let delta = 0 | ||
const { accountStart, accountEnd, tsStart, maxRecords, offset, accountOffset } = payload | ||
const tsEnd = Date.now() | ||
// Query from Shardeum->queryAccountsEntryByRanges3 fn | ||
// const query = `SELECT * FROM accountsEntry | ||
// WHERE (timestamp, accountId) >= (${tsStart}, "${accountOffset}") | ||
// AND timestamp < ${tsEnd} | ||
// AND accountId <= "${accountEnd}" AND accountId >= "${accountStart}" | ||
// ORDER BY timestamp, accountId LIMIT ${maxRecords}` | ||
|
||
const safeSkip = Number.isInteger(offset) ? offset : 0 | ||
const safeLimit = Number.isInteger(maxRecords) ? maxRecords : 100 | ||
const sqlPrefix = `SELECT * FROM accounts WHERE ` | ||
const queryString = `accountId BETWEEN ? AND ? AND timestamp BETWEEN ? AND ? ORDER BY timestamp ASC, accountId ASC LIMIT ${safeLimit}` | ||
const offsetCondition = ` OFFSET ${safeSkip}` | ||
let sql = sqlPrefix | ||
let values = [] | ||
if (accountOffset) { | ||
sql += `accountId >= ? AND ` | ||
values.push(accountOffset) | ||
} | ||
sql += queryString | ||
values.push(accountStart, accountEnd, tsStart, tsEnd) | ||
if (!accountOffset) sql += offsetCondition | ||
const wrappedAccounts: WrappedStateArray = []; | ||
const wrappedAccounts2: WrappedStateArray = []; | ||
let lastUpdateNeeded = false; | ||
let highestTs = 0; | ||
let delta = 0; | ||
const { accountStart, accountEnd, tsStart, maxRecords, offset, accountOffset } = payload; | ||
const tsEnd = Date.now(); | ||
|
||
let accounts = await Account.fetchAccountsBySqlQuery(sql, values) | ||
for (const account of accounts) { | ||
wrappedAccounts.push({ | ||
accountId: account.accountId, | ||
stateId: account.hash, | ||
data: account.data, | ||
timestamp: account.timestamp, | ||
}) | ||
} | ||
if (wrappedAccounts.length === 0) { | ||
lastUpdateNeeded = true | ||
} else { | ||
// see if our newest record is new enough | ||
highestTs = 0 | ||
for (const account of wrappedAccounts) { | ||
if (account.timestamp > highestTs) { | ||
highestTs = account.timestamp | ||
} | ||
const safeSkip = Number.isInteger(offset) ? offset : 0; | ||
const safeLimit = Number.isInteger(maxRecords) ? maxRecords : 100; | ||
|
||
try { | ||
let accounts: AccountsCopy[] = []; | ||
if (accountOffset) { | ||
// Fetch accounts with `accountOffset` | ||
accounts = await fetchAccountsByRangeWithAccountOffset( | ||
accountStart, | ||
accountEnd, | ||
tsStart, | ||
tsEnd, | ||
accountOffset, | ||
safeLimit | ||
); | ||
} else { | ||
// Fetch accounts without `accountOffset` | ||
accounts = await fetchAccountsByRangeWithoutAccountOffset( | ||
accountStart, | ||
accountEnd, | ||
tsStart, | ||
tsEnd, | ||
safeLimit, | ||
safeSkip | ||
); | ||
} | ||
delta = tsEnd - highestTs | ||
// Logger.mainLogger.debug('Account Data received', StringUtils.safeStringify(payload)) | ||
// Logger.mainLogger.debug( | ||
// 'delta ' + delta, | ||
// 'tsEnd ' + tsEnd, | ||
// 'highestTs ' + highestTs, | ||
// delta < QUEUE_SIT_TIME * 2 | ||
// ) | ||
if (delta < QUEUE_SIT_TIME * 2) { | ||
const tsStart2 = highestTs | ||
const tsEnd2 = Date.now() | ||
sql = sqlPrefix + queryString + offsetCondition | ||
values = [accountStart, accountEnd, tsStart2, tsEnd2] | ||
accounts = await Account.fetchAccountsBySqlQuery(sql, values) | ||
for (const account of accounts) { | ||
wrappedAccounts2.push({ | ||
accountId: account.accountId, | ||
stateId: account.hash, | ||
data: account.data, | ||
timestamp: account.timestamp, | ||
}) | ||
|
||
for (const account of accounts) { | ||
wrappedAccounts.push({ | ||
accountId: account.accountId, | ||
stateId: account.hash, | ||
data: account.data, | ||
timestamp: account.timestamp, | ||
}); | ||
} | ||
|
||
if (wrappedAccounts.length === 0) { | ||
lastUpdateNeeded = true; | ||
} else { | ||
highestTs = Math.max(...wrappedAccounts.map((a) => a.timestamp)); | ||
delta = tsEnd - highestTs; | ||
|
||
if (delta < QUEUE_SIT_TIME * 2) { | ||
const tsStart2 = highestTs; | ||
const tsEnd2 = Date.now(); | ||
|
||
const accounts2 = await fetchAccountsByRangeWithoutAccountOffset( | ||
accountStart, | ||
accountEnd, | ||
tsStart2, | ||
tsEnd2, | ||
safeLimit, | ||
safeSkip | ||
); | ||
|
||
for (const account of accounts2) { | ||
wrappedAccounts2.push({ | ||
accountId: account.accountId, | ||
stateId: account.hash, | ||
data: account.data, | ||
timestamp: account.timestamp, | ||
}); | ||
} | ||
lastUpdateNeeded = true; | ||
} | ||
lastUpdateNeeded = true | ||
} | ||
} catch (e) { | ||
Logger.mainLogger.error(e); | ||
} | ||
return { wrappedAccounts, lastUpdateNeeded, wrappedAccounts2, highestTs, delta } | ||
} | ||
|
||
return { wrappedAccounts, lastUpdateNeeded, wrappedAccounts2, highestTs, delta }; | ||
}; | ||
|
||
|
||
|
||
// /** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove commented out code if no longer needed |
||
// * | ||
// * This function is contructed to provide data in similar way as the `getAccountDataByRangeSmart` function in the validator | ||
// * @param payload | ||
// * @returns GetAccountDataByRangeSmart | ||
// */ | ||
// export const provideAccountDataRequest = async ( | ||
// payload: AccountDataRequestSchema | ||
// ): Promise<GetAccountDataByRangeSmart> => { | ||
// const wrappedAccounts: WrappedStateArray = [] | ||
// const wrappedAccounts2: WrappedStateArray = [] // We might not need to provide data to this | ||
// let lastUpdateNeeded = false | ||
// let highestTs = 0 | ||
// let delta = 0 | ||
// const { accountStart, accountEnd, tsStart, maxRecords, offset, accountOffset } = payload | ||
// const tsEnd = Date.now() | ||
|
||
// const safeSkip = Number.isInteger(offset) ? offset : 0 | ||
// const safeLimit = Number.isInteger(maxRecords) ? maxRecords : 100 | ||
// const sqlPrefix = `SELECT * FROM accounts WHERE ` | ||
// const queryString = `accountId BETWEEN ? AND ? AND timestamp BETWEEN ? AND ? ORDER BY timestamp ASC, accountId ASC LIMIT ${safeLimit}` | ||
// const offsetCondition = ` OFFSET ${safeSkip}` | ||
// let sql = sqlPrefix | ||
// let values = [] | ||
// if (accountOffset) { | ||
// sql += `accountId >= ? AND ` | ||
// values.push(accountOffset) | ||
// } | ||
// sql += queryString | ||
// values.push(accountStart, accountEnd, tsStart, tsEnd) | ||
// if (!accountOffset) sql += offsetCondition | ||
|
||
// let accounts = await Account.fetchAccountsBySqlQuery(sql, values) | ||
// for (const account of accounts) { | ||
// wrappedAccounts.push({ | ||
// accountId: account.accountId, | ||
// stateId: account.hash, | ||
// data: account.data, | ||
// timestamp: account.timestamp, | ||
// }) | ||
// } | ||
// if (wrappedAccounts.length === 0) { | ||
// lastUpdateNeeded = true | ||
// } else { | ||
// // see if our newest record is new enough | ||
// highestTs = 0 | ||
// for (const account of wrappedAccounts) { | ||
// if (account.timestamp > highestTs) { | ||
// highestTs = account.timestamp | ||
// } | ||
// } | ||
// delta = tsEnd - highestTs | ||
// // Logger.mainLogger.debug('Account Data received', StringUtils.safeStringify(payload)) | ||
// // Logger.mainLogger.debug( | ||
// // 'delta ' + delta, | ||
// // 'tsEnd ' + tsEnd, | ||
// // 'highestTs ' + highestTs, | ||
// // delta < QUEUE_SIT_TIME * 2 | ||
// // ) | ||
// if (delta < QUEUE_SIT_TIME * 2) { | ||
// const tsStart2 = highestTs | ||
// const tsEnd2 = Date.now() | ||
// sql = sqlPrefix + queryString + offsetCondition | ||
// values = [accountStart, accountEnd, tsStart2, tsEnd2] | ||
// accounts = await Account.fetchAccountsBySqlQuery(sql, values) | ||
// for (const account of accounts) { | ||
// wrappedAccounts2.push({ | ||
// accountId: account.accountId, | ||
// stateId: account.hash, | ||
// data: account.data, | ||
// timestamp: account.timestamp, | ||
// }) | ||
// } | ||
// lastUpdateNeeded = true | ||
// } | ||
// } | ||
// return { wrappedAccounts, lastUpdateNeeded, wrappedAccounts2, highestTs, delta } | ||
// } | ||
|
||
|
||
export const provideAccountDataByListRequest = async ( | ||
payload: AccountDataByListRequestSchema | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add more info to this error message than just the error itself