From e5528358106f7cfa3d252fe22aaf24cd4b5f1d36 Mon Sep 17 00:00:00 2001 From: ALPAC-4 Date: Tue, 17 Dec 2024 16:47:26 +0900 Subject: [PATCH] refactor: remove `this.-` and replace it to `ClassName.-` expression for static variables --- src/db/controller/channel.ts | 28 ++++++++++---------- src/db/controller/client.ts | 8 +++--- src/db/controller/connection.ts | 18 ++++++++----- src/db/controller/packet.ts | 46 ++++++++++++++++----------------- src/db/controller/packetFee.ts | 4 +-- src/db/controller/syncInfo.ts | 46 +++++++++++++++++++++------------ src/msgs/updateClient.ts | 3 ++- 7 files changed, 85 insertions(+), 68 deletions(-) diff --git a/src/db/controller/channel.ts b/src/db/controller/channel.ts index a9a9a28..d32c2d7 100644 --- a/src/db/controller/channel.ts +++ b/src/db/controller/channel.ts @@ -87,7 +87,7 @@ export class ChannelController { return select( DB, - this.tableName, + ChannelController.tableName, wheres, { id: 'ASC' }, limit @@ -103,7 +103,7 @@ export class ChannelController { del( DB, - this.tableName, + ChannelController.tableName, events.map((v) => ({ id: v.id as number })) ) } @@ -111,7 +111,7 @@ export class ChannelController { public static updateInProgress(id?: number, inProgress = true) { update( DB, - this.tableName, + ChannelController.tableName, { in_progress: inProgress ? Bool.TRUE : Bool.FALSE }, [{ id }] ) @@ -119,7 +119,7 @@ export class ChannelController { public static resetPacketInProgress(db?: Database) { db = db ?? DB - update(db, this.tableName, { + update(db, ChannelController.tableName, { in_progress: Bool.FALSE, }) } @@ -152,7 +152,7 @@ export class ChannelController { } return () => { - insert(DB, this.tableName, channelOnOpen) // store INIT state to the dst chain + insert(DB, ChannelController.tableName, channelOnOpen) // store INIT state to the dst chain } } @@ -184,7 +184,7 @@ export class ChannelController { } return () => { - del(DB, this.tableName, [ + del(DB, ChannelController.tableName, [ { state: ChannelState.INIT, counterparty_chain_id: connection.counterparty_chain_id, @@ -192,7 +192,7 @@ export class ChannelController { counterparty_channel_id: channelOnOpen.channel_id, }, ]) // remove INIT from the dst chain - insert(DB, this.tableName, channelOnOpen) // store TRYOPEN state to the src chain + insert(DB, ChannelController.tableName, channelOnOpen) // store TRYOPEN state to the src chain } } @@ -224,7 +224,7 @@ export class ChannelController { } return () => { - del(DB, this.tableName, [ + del(DB, ChannelController.tableName, [ { state: ChannelState.TRYOPEN, counterparty_chain_id: channelOnOpen.chain_id, @@ -232,7 +232,7 @@ export class ChannelController { counterparty_channel_id: channelOnOpen.channel_id, }, ]) // remove TRYOPEN from src chain - del(DB, this.tableName, [ + del(DB, ChannelController.tableName, [ { state: ChannelState.INIT, counterparty_chain_id: chainId, @@ -240,7 +240,7 @@ export class ChannelController { counterparty_channel_id: channelOnOpen.counterparty_channel_id, }, ]) // remove INIT from dst chain - insert(DB, this.tableName, channelOnOpen) // store ACK state to the dst chain + insert(DB, ChannelController.tableName, channelOnOpen) // store ACK state to the dst chain } } @@ -256,7 +256,7 @@ export class ChannelController { event.channelOpenCloseInfo.dstConnectionId ) return () => { - del(DB, this.tableName, [ + del(DB, ChannelController.tableName, [ { state: ChannelState.TRYOPEN, counterparty_chain_id: chainId, @@ -264,7 +264,7 @@ export class ChannelController { counterparty_channel_id: event.channelOpenCloseInfo.dstChannelId, }, ]) // remove TRYOPEN from src chain - del(DB, this.tableName, [ + del(DB, ChannelController.tableName, [ { state: ChannelState.ACK, counterparty_chain_id: connection.counterparty_chain_id, @@ -302,7 +302,7 @@ export class ChannelController { } return () => { - insert(DB, this.tableName, channelOnOpen) + insert(DB, ChannelController.tableName, channelOnOpen) // Mark all packets as timed out PacketController.updateTimeout( @@ -318,7 +318,7 @@ export class ChannelController { event: ChannelOpenCloseEvent ): () => void { return () => { - del(DB, this.tableName, [ + del(DB, ChannelController.tableName, [ { state: ChannelState.CLOSE, chain_id: chainId, diff --git a/src/db/controller/client.ts b/src/db/controller/client.ts index e985827..0aa5756 100644 --- a/src/db/controller/client.ts +++ b/src/db/controller/client.ts @@ -27,7 +27,7 @@ export class ClientController { last_update_time: 0, } - insert(DB, this.tableName, client) + insert(DB, ClientController.tableName, client) return client } @@ -68,7 +68,7 @@ export class ClientController { update( DB, - this.tableName, + ClientController.tableName, { revision_height: client.revision_height, last_update_time: client.last_update_time, @@ -88,7 +88,7 @@ export class ClientController { clientId: string ): Promise { // get client - const client = selectOne(DB, this.tableName, [ + const client = selectOne(DB, ClientController.tableName, [ { chain_id: chainId, client_id: clientId, @@ -104,7 +104,7 @@ export class ClientController { ): ClientTable[] { const clients = select( DB, - this.tableName, + ClientController.tableName, counterpartyChainIds.map((counterpartyChainId) => ({ chain_id: chainId, counterparty_chain_id: counterpartyChainId, diff --git a/src/db/controller/connection.ts b/src/db/controller/connection.ts index 9eb9c36..73e9803 100644 --- a/src/db/controller/connection.ts +++ b/src/db/controller/connection.ts @@ -25,7 +25,7 @@ export class ConnectionController { counterparty_client_id: connectionInfo.connection.counterparty.client_id, } - insert(DB, this.tableName, connection) + insert(DB, ConnectionController.tableName, connection) return connection } @@ -37,12 +37,16 @@ export class ConnectionController { chainId: string, connectionId: string ): Promise { - const connection = selectOne(DB, this.tableName, [ - { - chain_id: chainId, - connection_id: connectionId, - }, - ]) + const connection = selectOne( + DB, + ConnectionController.tableName, + [ + { + chain_id: chainId, + connection_id: connectionId, + }, + ] + ) return connection ?? this.addConnection(rest, chainId, connectionId) } diff --git a/src/db/controller/packet.ts b/src/db/controller/packet.ts index e0eaa86..fb4b71e 100644 --- a/src/db/controller/packet.ts +++ b/src/db/controller/packet.ts @@ -104,7 +104,7 @@ export class PacketController { res.push( ...select( DB, - this.tableNamePacketSend, + PacketController.tableNamePacketSend, wheres, { sequence: 'ASC' }, limit - res.length @@ -159,7 +159,7 @@ export class PacketController { return select( DB, - this.tableNamePacketTimeout, + PacketController.tableNamePacketTimeout, wheres, { sequence: 'ASC' }, limit @@ -207,7 +207,7 @@ export class PacketController { return select( DB, - this.tableNamePacketWriteAck, + PacketController.tableNamePacketWriteAck, wheres, { sequence: 'ASC' }, limit @@ -218,7 +218,7 @@ export class PacketController { if (packets.length === 0) return del( DB, - this.tableNamePacketSend, + PacketController.tableNamePacketSend, packets.map((packet) => ({ dst_chain_id: packet.dst_chain_id, dst_connection_id: packet.dst_connection_id, @@ -232,7 +232,7 @@ export class PacketController { if (packets.length === 0) return del( DB, - this.tableNamePacketTimeout, + PacketController.tableNamePacketTimeout, packets.map((packet) => ({ src_chain_id: packet.src_chain_id, src_connection_id: packet.src_connection_id, @@ -246,7 +246,7 @@ export class PacketController { if (packets.length === 0) return del( DB, - this.tableNamePacketWriteAck, + PacketController.tableNamePacketWriteAck, packets.map((packet) => ({ src_chain_id: packet.src_chain_id, src_connection_id: packet.src_connection_id, @@ -262,7 +262,7 @@ export class PacketController { ) { update( DB, - this.tableNamePacketSend, + PacketController.tableNamePacketSend, { in_progress: inProgress ? Bool.TRUE : Bool.FALSE }, [ { @@ -281,7 +281,7 @@ export class PacketController { ) { update( DB, - this.tableNamePacketTimeout, + PacketController.tableNamePacketTimeout, { in_progress: inProgress ? Bool.TRUE : Bool.FALSE }, [ { @@ -300,7 +300,7 @@ export class PacketController { ) { update( DB, - this.tableNamePacketWriteAck, + PacketController.tableNamePacketWriteAck, { in_progress: inProgress ? Bool.TRUE : Bool.FALSE }, [ { @@ -315,13 +315,13 @@ export class PacketController { public static resetPacketInProgress(db?: Database) { db = db ?? DB - update(db, this.tableNamePacketSend, { + update(db, PacketController.tableNamePacketSend, { in_progress: Bool.FALSE, }) - update(db, this.tableNamePacketTimeout, { + update(db, PacketController.tableNamePacketTimeout, { in_progress: Bool.FALSE, }) - update(db, this.tableNamePacketWriteAck, { + update(db, PacketController.tableNamePacketWriteAck, { in_progress: Bool.FALSE, }) } @@ -382,14 +382,14 @@ export class PacketController { } return () => { - insert(DB, this.tableNamePacketSend, packetSend) - insert(DB, this.tableNamePacketTimeout, packetTimeout) + insert(DB, PacketController.tableNamePacketSend, packetSend) + insert(DB, PacketController.tableNamePacketTimeout, packetTimeout) // if channel is ordered channel, update in progress for higher sequence if (packetSend.is_ordered === Bool.TRUE) { update( DB, - this.tableNamePacketSend, + PacketController.tableNamePacketSend, { in_progress: Bool.FALSE }, [ { @@ -438,7 +438,7 @@ export class PacketController { } return () => { // remove pakcet send - del(DB, this.tableNamePacketSend, [ + del(DB, PacketController.tableNamePacketSend, [ { dst_chain_id: chainId, dst_channel_id: event.packetInfo.dstChannel, @@ -460,13 +460,13 @@ export class PacketController { FeeType.TIMEOUT ) - insert(DB, this.tableNamePacketWriteAck, packetWriteAck) + insert(DB, PacketController.tableNamePacketWriteAck, packetWriteAck) // if channel is ordered channel, update in progress for higher sequence if (packetWriteAck.is_ordered === Bool.TRUE) { update( DB, - this.tableNamePacketSend, + PacketController.tableNamePacketSend, { in_progress: Bool.FALSE }, [ { @@ -493,7 +493,7 @@ export class PacketController { return () => { // remove pakcet send - del(DB, this.tableNamePacketSend, [ + del(DB, PacketController.tableNamePacketSend, [ { dst_chain_id: connection.counterparty_chain_id, dst_connection_id: connection.counterparty_connection_id, @@ -503,7 +503,7 @@ export class PacketController { ]) // remove packet timeout - del(DB, this.tableNamePacketSend, [ + del(DB, PacketController.tableNamePacketSend, [ { src_chain_id: chainId, src_connection_id: event.packetInfo.connectionId, @@ -513,7 +513,7 @@ export class PacketController { ]) // remove packet write ack - del(DB, this.tableNamePacketSend, [ + del(DB, PacketController.tableNamePacketSend, [ { src_chain_id: chainId, src_connection_id: event.packetInfo.connectionId, @@ -557,7 +557,7 @@ export class PacketController { ) return () => { // remove pakcet send - del(DB, this.tableNamePacketSend, [ + del(DB, PacketController.tableNamePacketSend, [ { dst_chain_id: connection.counterparty_chain_id, dst_connection_id: connection.counterparty_connection_id, @@ -567,7 +567,7 @@ export class PacketController { ]) // remove packet timeout - del(DB, this.tableNamePacketSend, [ + del(DB, PacketController.tableNamePacketSend, [ { src_chain_id: chainId, src_connection_id: event.packetInfo.connectionId, diff --git a/src/db/controller/packetFee.ts b/src/db/controller/packetFee.ts index 022a4c1..4383003 100644 --- a/src/db/controller/packetFee.ts +++ b/src/db/controller/packetFee.ts @@ -26,7 +26,7 @@ export class PacketFeeController { denom: coin.denom, amount: Number(coin.amount), } - insert(DB, this.tableName, packetFee) + insert(DB, PacketFeeController.tableName, packetFee) } } } @@ -52,7 +52,7 @@ export class PacketFeeController { sequence: number, type: FeeType ) { - del(DB, this.tableName, [ + del(DB, PacketFeeController.tableName, [ { chain_id: chainId, channel_id: channelId, sequence, fee_type: type }, ]) } diff --git a/src/db/controller/syncInfo.ts b/src/db/controller/syncInfo.ts index b95afff..e2b0f39 100644 --- a/src/db/controller/syncInfo.ts +++ b/src/db/controller/syncInfo.ts @@ -25,7 +25,7 @@ export class SyncInfoController { synced_height: startHeight - 1, } - insert(DB, this.tableName, syncInfo) + insert(DB, SyncInfoController.tableName, syncInfo) syncInfos.unshift(syncInfo) } @@ -41,7 +41,7 @@ export class SyncInfoController { } syncInfos.unshift(newSyncInfo) - insert(DB, this.tableName, newSyncInfo) + insert(DB, SyncInfoController.tableName, newSyncInfo) } // TODO: split sync info when syncInfo.syncedHeight < startHeight < syncInfo.endHeight @@ -53,7 +53,9 @@ export class SyncInfoController { } public static getSyncInfos(chainId: string): SyncInfoTable[] { - return select(DB, this.tableName, [{ chain_id: chainId }]) + return select(DB, SyncInfoController.tableName, [ + { chain_id: chainId }, + ]) } /** @@ -73,28 +75,38 @@ export class SyncInfoController { ): boolean { // check finish if (syncedHeight === endHeight) { - del(DB, this.tableName, [ + del(DB, SyncInfoController.tableName, [ { chain_id: chainId, start_height: startHeight }, ]) - update(DB, this.tableName, { start_height: startHeight }, [ - { - chain_id: chainId, - start_height: endHeight + 1, - }, - ]) + update( + DB, + SyncInfoController.tableName, + { start_height: startHeight }, + [ + { + chain_id: chainId, + start_height: endHeight + 1, + }, + ] + ) - console.log(select(DB, this.tableName)) + console.log(select(DB, SyncInfoController.tableName)) return true } - update(DB, this.tableName, { synced_height: syncedHeight }, [ - { - chain_id: chainId, - start_height: startHeight, - }, - ]) + update( + DB, + SyncInfoController.tableName, + { synced_height: syncedHeight }, + [ + { + chain_id: chainId, + start_height: startHeight, + }, + ] + ) return false } diff --git a/src/msgs/updateClient.ts b/src/msgs/updateClient.ts index 5300943..5091373 100644 --- a/src/msgs/updateClient.ts +++ b/src/msgs/updateClient.ts @@ -116,7 +116,8 @@ async function getValidatorSet( const proposer: Validator | undefined = mappedValidators.find( (val) => - Buffer.from(val.address).toString('hex') === proposerAddress.toLowerCase() + Buffer.from(val.address).toString('hex').toLowerCase() === + proposerAddress.toLowerCase() ) return ValidatorSet.fromPartial({