perf(android): 优化交易记录同步速度
- 减少扫描区块数从 100000 到 20000(只扫描最近约 2 天) - 并行查询 SENT 和 RECEIVED 交易(提速 2倍) - 从约 100 秒减少到约 10-15 秒每个代币 - 总同步时间从 5 分钟减少到 30-45 秒 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
78e105d46d
commit
144d28238e
|
|
@ -3180,9 +3180,9 @@ data class ParticipantStatusInfo(
|
||||||
val currentBlock = currentBlockHex?.removePrefix("0x")?.toLongOrNull(16) ?: 0L
|
val currentBlock = currentBlockHex?.removePrefix("0x")?.toLongOrNull(16) ?: 0L
|
||||||
|
|
||||||
// RPC 节点限制:每次最多查询 10000 个区块
|
// RPC 节点限制:每次最多查询 10000 个区块
|
||||||
// 分批查询最近 100000 个区块
|
// 只扫描最近 20000 个区块(约 2 批,20-30 秒)
|
||||||
val maxBlocksPerRequest = 10000L
|
val maxBlocksPerRequest = 10000L
|
||||||
val totalBlocksToScan = 100000L
|
val totalBlocksToScan = 20000L
|
||||||
val startBlock = if (currentBlock > totalBlocksToScan) {
|
val startBlock = if (currentBlock > totalBlocksToScan) {
|
||||||
currentBlock - totalBlocksToScan
|
currentBlock - totalBlocksToScan
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -3190,7 +3190,7 @@ data class ParticipantStatusInfo(
|
||||||
}
|
}
|
||||||
|
|
||||||
android.util.Log.d("TssRepository", "[SYNC-ERC20] Current block: $currentBlock")
|
android.util.Log.d("TssRepository", "[SYNC-ERC20] Current block: $currentBlock")
|
||||||
android.util.Log.d("TssRepository", "[SYNC-ERC20] Scanning blocks $startBlock to $currentBlock (total: ${currentBlock - startBlock})")
|
android.util.Log.d("TssRepository", "[SYNC-ERC20] Scanning blocks $startBlock to $currentBlock (${currentBlock - startBlock} blocks, ~${(currentBlock - startBlock) / maxBlocksPerRequest} batches)")
|
||||||
|
|
||||||
// Transfer event signature: keccak256("Transfer(address,address,uint256)")
|
// Transfer event signature: keccak256("Transfer(address,address,uint256)")
|
||||||
val transferTopic = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
|
val transferTopic = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
|
||||||
|
|
@ -3212,60 +3212,69 @@ data class ParticipantStatusInfo(
|
||||||
|
|
||||||
android.util.Log.d("TssRepository", "[SYNC-ERC20] Batch #$batchCount: blocks $fromBlock-$toBlock")
|
android.util.Log.d("TssRepository", "[SYNC-ERC20] Batch #$batchCount: blocks $fromBlock-$toBlock")
|
||||||
|
|
||||||
// 查询发送的交易 (from = address)
|
// 并行查询 SENT 和 RECEIVED(提速 2倍)
|
||||||
val sentRequest = """
|
val batchSynced = kotlinx.coroutines.coroutineScope {
|
||||||
{
|
val sentDeferred = async {
|
||||||
"jsonrpc": "2.0",
|
val sentRequest = """
|
||||||
"method": "eth_getLogs",
|
{
|
||||||
"params": [{
|
"jsonrpc": "2.0",
|
||||||
"address": "$contractAddress",
|
"method": "eth_getLogs",
|
||||||
"topics": ["$transferTopic", "$paddedAddress", null],
|
"params": [{
|
||||||
"fromBlock": "$fromBlockHex",
|
"address": "$contractAddress",
|
||||||
"toBlock": "$toBlockHex"
|
"topics": ["$transferTopic", "$paddedAddress", null],
|
||||||
}],
|
"fromBlock": "$fromBlockHex",
|
||||||
"id": 1
|
"toBlock": "$toBlockHex"
|
||||||
|
}],
|
||||||
|
"id": 1
|
||||||
|
}
|
||||||
|
""".trimIndent()
|
||||||
|
|
||||||
|
val sentResponse = client.newCall(
|
||||||
|
okhttp3.Request.Builder()
|
||||||
|
.url(rpcUrl)
|
||||||
|
.post(sentRequest.toRequestBody(jsonMediaType))
|
||||||
|
.build()
|
||||||
|
).execute()
|
||||||
|
|
||||||
|
val sentBody = sentResponse.body?.string()
|
||||||
|
if (sentBody != null) {
|
||||||
|
parseAndSaveTransferLogs(sentBody, shareId, address, tokenType, "SENT")
|
||||||
|
} else 0
|
||||||
}
|
}
|
||||||
""".trimIndent()
|
|
||||||
|
|
||||||
val sentResponse = client.newCall(
|
val receivedDeferred = async {
|
||||||
okhttp3.Request.Builder()
|
val receivedRequest = """
|
||||||
.url(rpcUrl)
|
{
|
||||||
.post(sentRequest.toRequestBody(jsonMediaType))
|
"jsonrpc": "2.0",
|
||||||
.build()
|
"method": "eth_getLogs",
|
||||||
).execute()
|
"params": [{
|
||||||
|
"address": "$contractAddress",
|
||||||
|
"topics": ["$transferTopic", null, "$paddedAddress"],
|
||||||
|
"fromBlock": "$fromBlockHex",
|
||||||
|
"toBlock": "$toBlockHex"
|
||||||
|
}],
|
||||||
|
"id": 2
|
||||||
|
}
|
||||||
|
""".trimIndent()
|
||||||
|
|
||||||
val sentBody = sentResponse.body?.string()
|
val receivedResponse = client.newCall(
|
||||||
if (sentBody != null) {
|
okhttp3.Request.Builder()
|
||||||
totalSynced += parseAndSaveTransferLogs(sentBody, shareId, address, tokenType, "SENT")
|
.url(rpcUrl)
|
||||||
}
|
.post(receivedRequest.toRequestBody(jsonMediaType))
|
||||||
|
.build()
|
||||||
// 查询接收的交易 (to = address)
|
).execute()
|
||||||
val receivedRequest = """
|
|
||||||
{
|
val receivedBody = receivedResponse.body?.string()
|
||||||
"jsonrpc": "2.0",
|
if (receivedBody != null) {
|
||||||
"method": "eth_getLogs",
|
parseAndSaveTransferLogs(receivedBody, shareId, address, tokenType, "RECEIVED")
|
||||||
"params": [{
|
} else 0
|
||||||
"address": "$contractAddress",
|
}
|
||||||
"topics": ["$transferTopic", null, "$paddedAddress"],
|
|
||||||
"fromBlock": "$fromBlockHex",
|
// 等待两个查询都完成
|
||||||
"toBlock": "$toBlockHex"
|
sentDeferred.await() + receivedDeferred.await()
|
||||||
}],
|
|
||||||
"id": 2
|
|
||||||
}
|
|
||||||
""".trimIndent()
|
|
||||||
|
|
||||||
val receivedResponse = client.newCall(
|
|
||||||
okhttp3.Request.Builder()
|
|
||||||
.url(rpcUrl)
|
|
||||||
.post(receivedRequest.toRequestBody(jsonMediaType))
|
|
||||||
.build()
|
|
||||||
).execute()
|
|
||||||
|
|
||||||
val receivedBody = receivedResponse.body?.string()
|
|
||||||
if (receivedBody != null) {
|
|
||||||
totalSynced += parseAndSaveTransferLogs(receivedBody, shareId, address, tokenType, "RECEIVED")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
totalSynced += batchSynced
|
||||||
fromBlock = toBlock + 1
|
fromBlock = toBlock + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue