"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// const cHashSetStepSize = 4
// const cHashSetTXStepSize = 2
// const cHashSetDataStepSize = 2
class Deprecated {
    // sentReceipts: Map<string, boolean>
    // sendArchiveData: boolean
    // purgeArchiveData: boolean
    // /** tracks state for repairing partitions. index by cycle counter key to get the repair object, index by parition  */
    // repairTrackingByCycleById: { [cycleKey: string]: { [id: string]: RepairTracker } }
    // /** UpdateRepairData by cycle key */
    // repairUpdateDataByCycle: { [cycleKey: string]: UpdateRepairData[] }
    // applyAllPreparedRepairsRunning: boolean
    // repairStartedMap: Map<string, boolean>
    // repairCompletedMap: Map<string, boolean>
    // dataRepairStack: RepairTracker[]
    constructor(stateManager, profiler, app, logger, storage, p2p, crypto, config) {
        this.crypto = crypto;
        this.app = app;
        this.logger = logger;
        this.config = config;
        this.profiler = profiler;
        this.p2p = p2p;
        this.storage = storage;
        this.stateManager = stateManager;
        this.mainLogger = logger.getLogger('main');
        this.fatalLogger = logger.getLogger('fatal');
        this.shardLogger = logger.getLogger('shardDump');
        this.statsLogger = logger.getLogger('statsDump');
        this.statemanager_fatal = stateManager.statemanager_fatal;
        // this.sentReceipts = new Map()
        // this.sendArchiveData = false
        // this.purgeArchiveData = false
        // this.repairTrackingByCycleById = {}
        // this.repairUpdateDataByCycle = {}
        // this.applyAllPreparedRepairsRunning = false
        // this.repairStartedMap = new Map()
        // this.repairCompletedMap = new Map()
        // this.dataRepairStack = []
    }
    //NOT used but seem possibly usefull...
    purgeTransactionData() {
        const tsStart = 0;
        const tsEnd = 0;
        this.storage.clearAcceptedTX(tsStart, tsEnd);
    }
    purgeStateTableData() {
        // do this by timestamp maybe..
        // this happnes on a slower scale.
        const tsEnd = 0; // todo get newest time to keep
        this.storage.clearAccountStateTableOlderThan(tsEnd);
    }
    /***
     *    ##     ##    ###    ##    ## ########  ##       ######## ########   ######
     *    ##     ##   ## ##   ###   ## ##     ## ##       ##       ##     ## ##    ##
     *    ##     ##  ##   ##  ####  ## ##     ## ##       ##       ##     ## ##
     *    ######### ##     ## ## ## ## ##     ## ##       ######   ########   ######
     *    ##     ## ######### ##  #### ##     ## ##       ##       ##   ##         ##
     *    ##     ## ##     ## ##   ### ##     ## ##       ##       ##    ##  ##    ##
     *    ##     ## ##     ## ##    ## ########  ######## ######## ##     ##  ######
     */
    setupHandlers() {
        // // /get_account_state_hash (Acc_start, Acc_end, Ts_start, Ts_end)
        // // Acc_start - get data for accounts starting with this account id; inclusive
        // // Acc_end - get data for accounts up to this account id; inclusive
        // // Ts_start - get data newer than this timestamp
        // // Ts_end - get data older than this timestamp
        // // Returns a single hash of the data from the Account State Table determined by the input parameters; sort by Tx_ts  then Tx_id before taking the hash
        // // Updated names:  accountStart , accountEnd, tsStart, tsEnd
        // this.p2p.registerInternal(
        //   'get_account_state_hash',
        //   async (
        //     payload: AccountStateHashReq,
        //     respond: (arg0: AccountStateHashResp) => Promise<number>,
        //     _sender: unknown,
        //     _tracker: string,
        //     msgSize: number
        //   ) => {
        //     this.profiler.scopedProfileSectionStart('get_account_state_hash', false, msgSize)
        //     let responseSize = cUninitializedSize
        //     try {
        //       const result = {} as AccountStateHashResp
        //       if (this.softSync_checkInitialFlag && this.initalSyncFinished === false) {
        //         //not ready?
        //         result.ready = false
        //         result.stateHash = this.stateManager.currentCycleShardData.ourNode.id
        //         await respond(result)
        //         return
        //       }
        //       // yikes need to potentially hash only N records at a time and return an array of hashes
        //       const stateHash = await this.stateManager.transactionQueue.getAccountsStateHash(
        //         payload.accountStart,
        //         payload.accountEnd,
        //         payload.tsStart,
        //         payload.tsEnd
        //       )
        //       result.stateHash = stateHash
        //       result.ready = true
        //       responseSize = await respond(result)
        //     } catch (e) {
        //       this.statemanager_fatal('get_account_state_hash', e)
        //     } finally {
        //       this.profiler.scopedProfileSectionEnd('get_account_state_hash', responseSize)
        //     }
        //   }
        // )
        // //    /get_account_state (Acc_start, Acc_end, Ts_start, Ts_end)
        // // Acc_start - get data for accounts starting with this account id; inclusive
        // // Acc_end - get data for accounts up to this account id; inclusive
        // // Ts_start - get data newer than this timestamp
        // // Ts_end - get data older than this timestamp
        // // Returns data from the Account State Table determined by the input parameters; limits result to 1000 records (as configured)
        // // Updated names:  accountStart , accountEnd, tsStart, tsEnd
        // this.p2p.registerInternal(
        //   'get_account_state',
        //   async (
        //     payload: GetAccountStateReq,
        //     respond: (arg0: { accountStates: Shardus.StateTableObject[] }) => Promise<number>,
        //     _sender: unknown,
        //     _tracker: string,
        //     msgSize: number
        //   ) => {
        //     if (this.config.stateManager == null) {
        //       throw new Error('this.config.stateManager == null') //TODO TSConversion  would be nice to eliminate some of these config checks.
        //     }
        //     this.profiler.scopedProfileSectionStart('get_account_state', false, msgSize)
        //     const result = {} as { accountStates: Shardus.StateTableObject[] }
        //     // max records set artificially low for better test coverage
        //     // todo m11: make configs for how many records to query
        //     const accountStates = await this.storage.queryAccountStateTable(
        //       payload.accountStart,
        //       payload.accountEnd,
        //       payload.tsStart,
        //       payload.tsEnd,
        //       this.config.stateManager.stateTableBucketSize
        //     )
        //     result.accountStates = accountStates
        //     const responseSize = await respond(result)
        //     this.profiler.scopedProfileSectionEnd('get_account_state', responseSize)
        //   }
        // )
        // // /get_account_data (Acc_start, Acc_end)
        // // Acc_start - get data for accounts starting with this account id; inclusive
        // // Acc_end - get data for accounts up to this account id; inclusive
        // // Returns data from the application Account Table; limits result to 300 records (as configured);
        // // For applications with multiple “Account” tables the returned data is grouped by table name.
        // // For example: [ {Acc_id, State_after, Acc_data}, { … }, ….. ]
        // // Updated names:  accountStart , accountEnd
        // this.p2p.registerInternal('get_account_data', async (payload: GetAccountDataReq, respond: (arg0: { accountData: Shardus.WrappedData[] | null }) => any) => {
        //     throw new Error('get_account_data endpoint retired')
        //     // let result = {} as {accountData: Shardus.WrappedData[] | null}//TSConversion  This is complicated !! check app for details.
        //     // let accountData = null
        //     // let ourLockID = -1
        //     // try {
        //     //   ourLockID = await this.fifoLock('accountModification')
        //     //   accountData = await this.app.getAccountData(payload.accountStart, payload.accountEnd, payload.maxRecords)
        //     // } finally {
        //     //   this.fifoUnlock('accountModification', ourLockID)
        //     // }
        //     // //PERF Disiable this in production or performance testing.
        //     // this.testAccountDataWrapped(accountData)
        //     // result.accountData = accountData
        //     // await respond(result)
        //   })
        // // After joining the network
        // //   Record Joined timestamp
        // //   Even a syncing node will receive accepted transactions
        // //   Starts receiving accepted transaction and saving them to Accepted Tx Table
        // this.p2p.registerGossipHandler('acceptedTx', async (acceptedTX: AcceptedTx, sender: Shardus.Node, tracker: string) => {
        //   // docs mention putting this in a table but it seems so far that an in memory queue should be ok
        //   // should we filter, or instead rely on gossip in to only give us TXs that matter to us?
        //   this.p2p.sendGossipIn('acceptedTx', acceptedTX, tracker, sender)
        //   let noConsensus = false // this can only be true for a set command which will never come from an endpoint
        //   this.stateManager.transactionQueue.routeAndQueueAcceptedTransaction(acceptedTX, /*sendGossip*/ false, sender, /*globalModification*/ false, noConsensus)
        //   //Note await not needed so beware if you add code below this.
        // })
        // // /get_accepted_transactions (Ts_start, Ts_end)
        // // Ts_start - get data newer than this timestamp
        // // Ts_end - get data older than this timestamp
        // // Returns data from the Accepted Tx Table starting with Ts_start; limits result to 500 records (as configured)
        // // Updated names: tsStart, tsEnd
        // this.p2p.registerInternal('get_accepted_transactions', async (payload: AcceptedTransactionsReq, respond: (arg0: { transactions: Shardus.AcceptedTx[] }) => any) => {
        //   let result = {} as { transactions: Shardus.AcceptedTx[] }
        //   if (!payload.limit) {
        //     payload.limit = 10
        //   }
        //   let transactions = await this.storage.queryAcceptedTransactions(payload.tsStart, payload.tsEnd, payload.limit)
        //   result.transactions = transactions
        //   await respond(result)
        // })
        // this.p2p.registerInternal('get_account_data2', async (payload: GetAccountData2Req, respond: (arg0: { accountData: Shardus.WrappedData[] | null }) => any) => {
        //     let result = {} as { accountData: Shardus.WrappedData[] | null } //TSConversion  This is complicated !!
        //     let accountData = null
        //     let ourLockID = -1
        //     try {
        //       ourLockID = await this.fifoLock('accountModification')
        //       accountData = await this.app.getAccountDataByRange(payload.accountStart, payload.accountEnd, payload.tsStart, payload.tsEnd, payload.maxRecords)
        //     } finally {
        //       this.fifoUnlock('accountModification', ourLockID)
        //     }
        //     //PERF Disiable this in production or performance testing.
        //     this.testAccountDataWrapped(accountData)
        //     result.accountData = accountData
        //     await respond(result)
        //   })
        // // /get_transactions_by_list (Tx_ids)
        // //   Tx_ids - array of transaction ids
        // //   Returns data from the Transactions Table for just the given transaction ids
        // this.p2p.registerInternal('get_transactions_by_list', async (payload: GetTransactionsByListReq, respond: (arg0: Shardus.AcceptedTx[]) => any) => {
        //     let result = [] as AcceptedTx[]
        //     try {
        //       result = await this.storage.queryAcceptedTransactionsByIds(payload.Tx_ids)
        //     } finally {
        //     }
        //     await respond(result)
        //   })
        // this.p2p.registerInternal('get_transactions_by_partition_index', async (payload: TransactionsByPartitionReq, respond: (arg0: TransactionsByPartitionResp) => any) => {
        //     // let result = {}
        //     let passFailList = []
        //     let statesList = []
        //     let acceptedTXs = null
        //     try {
        //       // let partitionId = payload.partitionId
        //       let cycle = payload.cycle
        //       let indicies = payload.tx_indicies
        //       let hash = payload.hash
        //       let partitionId = payload.partitionId
        //       let expectedResults = indicies.length
        //       let returnedResults = 0
        //       let key = 'c' + cycle
        //       let partitionObjectsByHash = this.partitionObjects.recentPartitionObjectsByCycleByHash[key]
        //       if (!partitionObjectsByHash) {
        //         await respond({ success: false })
        //       }
        //       let partitionObject = partitionObjectsByHash[hash]
        //       if (!partitionObject) {
        //         await respond({ success: false })
        //       }
        //       let txIDList = []
        //       for (let index of indicies) {
        //         let txid = partitionObject.Txids[index]
        //         txIDList.push(txid)
        //         let passFail = partitionObject.Status[index]
        //         passFailList.push(passFail)
        //       }
        //       for (let index of indicies) {
        //         let state = partitionObject.States[index]
        //         statesList.push(state)
        //         if (state != null) {
        //           returnedResults++
        //         }
        //       }
        //       if (returnedResults < expectedResults) {
        //         /* prettier-ignore */ if (logFlags.verbose) this.mainLogger.error(`get_transactions_by_partition_index failed! returnedResults < expectedResults send ${returnedResults} < ${expectedResults}`)
        //       }
        //       acceptedTXs = await this.storage.queryAcceptedTransactionsByIds(txIDList)
        //       // /* prettier-ignore */ if (logFlags.verbose) this.mainLogger.error(`get_transactions_by_partition_index failed! returnedResults < expectedResults send2 `)
        //       if (acceptedTXs != null && acceptedTXs.length < expectedResults) {
        //         /* prettier-ignore */ if (logFlags.verbose) this.mainLogger.error(`get_transactions_by_partition_index results ${utils.stringifyReduce(acceptedTXs)} snippets ${utils.stringifyReduce(payload.debugSnippets)} `)
        //         /* prettier-ignore */ if (logFlags.verbose) this.mainLogger.error(`get_transactions_by_partition_index results2:${utils.stringifyReduce(acceptedTXs.map((x: Shardus.AcceptedTx) => x.id))} snippets:${utils.stringifyReduce(payload.debugSnippets)} txid:${utils.stringifyReduce(txIDList)} `)
        //         let acceptedTXsBefore = 0
        //         if (acceptedTXs != null) {
        //           acceptedTXsBefore = acceptedTXs.length
        //         }
        //         // find an log missing results:
        //         // for(let txid of txIDList)
        //         let received: StringBoolObjectMap = {}
        //         for (let acceptedTX of acceptedTXs) {
        //           received[acceptedTX.id] = true
        //         }
        //         let missingTXs: string[] = []
        //         let missingTXHash: StringBoolObjectMap = {}
        //         for (let txid of txIDList) {
        //           if (received[txid] !== true) {
        //             missingTXs.push(txid)
        //             missingTXHash[txid] = true
        //           }
        //         }
        //         let finds = -1
        //         let txTally = this.partitionObjects.getTXList(cycle, partitionId)
        //         let found = []
        //         if (txTally) {
        //           finds = 0
        //           for (let tx of txTally.txs) {
        //             if (missingTXHash[tx.id] === true) {
        //               finds++
        //               acceptedTXs.push(tx)
        //               found.push(tx.id)
        //             }
        //           }
        //         }
        //         /* prettier-ignore */ if (logFlags.verbose) this.mainLogger.error(`get_transactions_by_partition_index failed! returnedResults < expectedResults send3 ${acceptedTXsBefore} < ${expectedResults} findsFixed: ${finds}  missing: ${utils.stringifyReduce(missingTXs)} found: ${utils.stringifyReduce(found)} acceptedTXs.length updated: ${acceptedTXs.length}`)
        //       } else {
        //       }
        //     } catch (ex) {
        //       this.statemanager_fatal(`get_transactions_by_partition_index_ex`, 'get_transactions_by_partition_index failed: ' + ex.name + ': ' + ex.message + ' at ' + ex.stack)
        //     } finally {
        //     }
        //     // TODO fix pass fail sorting.. it is probably all wrong and out of sync, but currently nothing fails.
        //     await respond({ success: true, acceptedTX: acceptedTXs, passFail: passFailList, statesList: statesList })
        //   })
        //   // /get_partition_txids (Partition_id, Cycle_number)
        //   //   Partition_id
        //   //   Cycle_number
        //   //   Returns the partition object which contains the txids along with the status
        //   this.p2p.registerInternal('get_partition_txids', async (payload: GetPartitionTxidsReq, respond: (arg0: {}) => any) => {
        //     let result = {}
        //     try {
        //       let id = payload.Partition_id
        //       let key = 'c' + payload.Cycle_number
        //       let partitionObjects = this.partitionObjects.partitionObjectsByCycle[key]
        //       for (let obj of partitionObjects) {
        //         if (obj.Partition_id === id) {
        //           result = obj
        //         }
        //       }
        //     } finally {
        //     }
        //     await respond(result)
        //   })
    }
}
exports.default = Deprecated;
//# sourceMappingURL=Deprecated.js.map