Skip to content

Commit

Permalink
Merge pull request #59 from FigureTechnologies/pb_bug_fee_adjustment
Browse files Browse the repository at this point in the history
Re-calculate fees within the block range of the pb release of 1.11
  • Loading branch information
rchaing-figure authored Oct 24, 2022
2 parents 7463b03 + 1663b39 commit 8aba27c
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ data class Config (
val hrp: String,
val upload: UploadConfig = UploadConfig.empty(),
val dbConfig: DBConfig,
val apiHost: String
val apiHost: String,
val badBlockRange: List<Long>
)

data class DBConfig(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tech.figure.aggregate.common.models

import com.google.common.io.BaseEncoding
import com.google.gson.Gson
import cosmos.crypto.secp256k1.Keys
import cosmos.tx.v1beta1.TxOuterClass
import tech.figure.aggregate.common.decodeBase64
Expand All @@ -20,6 +21,10 @@ import io.provenance.eventstream.stream.clients.BlockData
import io.provenance.eventstream.stream.models.Event
import io.provenance.hdwallet.bech32.toBech32
import io.provenance.hdwallet.common.hashing.sha256hash160
import tech.figure.aggregate.common.models.fee.Fee
import tech.figure.aggregate.common.models.fee.SignerInfo
import tech.figure.aggregate.common.repeatDecodeBase64
import java.math.BigDecimal
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException
import tendermint.types.Types.Header as GrpcHeader
Expand Down Expand Up @@ -75,7 +80,7 @@ fun Block.txData(index: Int, hrp: String): TxInfo? {

return TxInfo(
this.data?.txs?.get(index)?.hash(),
Fee(amount, denom, signerAddr, feeIncurringAddr)
Fee(amount, denom, SignerInfo(signerAddr, feeIncurringAddr))
)
}
return null
Expand All @@ -88,18 +93,61 @@ fun Block.dateTime() = this.header?.dateTime()
fun BlockHeader.dateTime(): OffsetDateTime? =
runCatching { OffsetDateTime.parse(this.time, DateTimeFormatter.ISO_DATE_TIME) }.getOrNull()

fun BlockResultsResponseResult.txEvents(blockDateTime: OffsetDateTime?, txHash: (Int) -> TxInfo?): List<TxEvent> =
fun BlockResultsResponseResult.txEvents(blockDateTime: OffsetDateTime?, badBlockRange: Pair<Long, Long>, txHash: (Int) -> TxInfo?): List<TxEvent> =
txsResults?.flatMapIndexed { index: Int, tx: BlockResultsResponseResultTxsResults ->
val txHashData = txHash(index)

// 1.11 bug - fees were not properly swept [0] - lower, [1] - higher
val fee = if(height >= badBlockRange.first && height <= badBlockRange.second ) {
tx.toBugFee(txHashData?.fee?.signerInfo)
} else {
txHashData?.fee
}

if(tx.code?.toInt() != 0) {
mutableListOf(toTxEvent(height, blockDateTime, txHashData?.txHash, txHashData?.fee ?: Fee()))
mutableListOf(toTxEvent(height, blockDateTime, txHashData?.txHash, fee ?: Fee()))
} else {
tx.events?.map {
it.toTxEvent(height, blockDateTime, txHashData?.txHash, fee = txHashData?.fee ?: Fee())
it.toTxEvent(height, blockDateTime, txHashData?.txHash, fee = fee ?: Fee())
} ?: emptyList()
}
} ?: emptyList()

fun BlockResultsResponseResultTxsResults.toBugFee(signerInfo: SignerInfo?): Fee {
var msgBaseFeeList = listOf<MsgFeeAttribute>()
this.events?.map { event ->
if(event.type?.repeatDecodeBase64() == "provenance.msgfees.v1.EventMsgFees") {
event.attributes?.toDecodedMap()?.map { attribute ->
msgBaseFeeList = Gson().fromJson(attribute.value?.decodeBase64(), Array<MsgFeeAttribute>::class.java).toList()
}
}
}

val msgBaseFee = if(msgBaseFeeList.isNotEmpty()) msgBaseFeeList[0].total.toAmountDenom().amount.toLong() else 0

// manually calc the proper fees
val gasPrice = 1905
val baseFee = BigDecimal(this.gasUsed!!.toLong() * gasPrice.toLong()).toLong()
val overageFee = BigDecimal(this.gasWanted!!.toLong() * gasPrice.toLong()).toLong() - baseFee
val totalFee = baseFee + msgBaseFee + overageFee

return Fee(fee = totalFee, "nhash", signerInfo)
}

fun String.toAmountDenom(): AmountDenom {
val amount = StringBuilder(this)
val denom = StringBuilder()
for (i in this.length - 1 downTo 0) {
val ch = this[i]
if (!ch.isDigit()) {
amount.deleteCharAt(i)
denom.insert(0, ch)
} else {
break
}
}
return AmountDenom(amount.toString(), denom.toString())
}

fun String.toSignerAddr(hrp: String): List<String> {
val tx = TxOuterClass.Tx.parseFrom(BaseEncoding.base64().decode(this)) ?: return mutableListOf()
Expand All @@ -108,14 +156,22 @@ fun String.toSignerAddr(hrp: String): List<String> {
}
}

fun BlockResultsResponseResult.txErroredEvents(blockDateTime: OffsetDateTime?, txHash: (Int) -> TxInfo?): List<TxError> =
fun BlockResultsResponseResult.txErroredEvents(blockDateTime: OffsetDateTime?, gasPriceUpdateBlockHeight: Long, badBlockRange: Pair<Long, Long>, txHash: (Int) -> TxInfo?): List<TxError> =
txsResults?.filter {
it.code?.toInt() ?: 0 != 0
(it.code?.toInt() ?: 0) != 0
}?.mapIndexed { index: Int, tx: BlockResultsResponseResultTxsResults ->
tx.toBlockError(height, blockDateTime, txHash(index)?.txHash, txHash(index)?.fee ?: Fee())
}?.filterNotNull() ?: emptyList()

fun BlockResultsResponseResultTxsResults.toBlockError(blockHeight: Long, blockDateTime: OffsetDateTime?, txHash: String?, fee: Fee): TxError? =
// 1.11 bug - fees were not properly swept [0] - lower, [1] - higher
val fee = if(height >= badBlockRange.first && height <= badBlockRange.second ) {
tx.toBugFee(txHash(index)?.fee?.signerInfo)
} else {
txHash(index)?.fee
}

tx.toBlockError(height, blockDateTime, txHash(index)?.txHash, fee = fee ?: Fee())
} ?: emptyList()

fun BlockResultsResponseResultTxsResults.toBlockError(blockHeight: Long, blockDateTime: OffsetDateTime?, txHash: String?, fee: Fee): TxError =
TxError(
blockHeight = blockHeight,
blockDateTime = blockDateTime,
Expand Down Expand Up @@ -151,8 +207,8 @@ fun BlockResultsResponseResultEvents.toTxEvent(
fee = fee
)

fun BlockResultsResponse.txEvents(blockDate: OffsetDateTime, txHash: (index: Int) -> TxInfo): List<TxEvent> =
this.result.txEvents(blockDate, txHash)
fun BlockResultsResponse.txEvents(blockDate: OffsetDateTime, badBlockRange: Pair<Long, Long>, txHash: (index: Int) -> TxInfo): List<TxEvent> =
this.result.txEvents(blockDate, badBlockRange, txHash)

fun BlockResultsResponseResult.blockEvents(blockDateTime: OffsetDateTime?): List<BlockEvent> =
beginBlockEvents?.map { e: BlockResultsResponseResultEvents ->
Expand All @@ -164,11 +220,11 @@ fun BlockResultsResponseResult.blockEvents(blockDateTime: OffsetDateTime?): List
)
} ?: emptyList()

fun BlockData.toStreamBlock(hrp: String): StreamBlockImpl {
fun BlockData.toStreamBlock(hrp: String, badBlockRange: Pair<Long, Long>): StreamBlockImpl {
val blockDatetime = block.header?.dateTime()
val blockEvents = blockResult.blockEvents(blockDatetime)
val blockTxResults = blockResult.txsResults
val txEvents = blockResult.txEvents(blockDatetime) { index: Int -> block.txData(index, hrp) }
val txEvents = blockResult.txEvents(blockDatetime, badBlockRange) { index: Int -> block.txData(index, hrp) }
return StreamBlockImpl(block, blockEvents, blockTxResults, txEvents)
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package tech.figure.aggregate.common.models

data class MsgFeeAttribute(
val msgType: String,
val count: Int,
val total: String,
val recipient: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package tech.figure.aggregate.common.models.fee

data class Fee(
val fee: Long? = 0L,
val denom: String? = "",
val signerInfo: SignerInfo? = SignerInfo()
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package tech.figure.aggregate.common.models.fee

data class SignerInfo(
val signerAddrs: List<String>? = mutableListOf(),
val incurrAddr: String? = ""
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tech.figure.aggregate.common.models.tx

import com.squareup.moshi.JsonClass
import tech.figure.aggregate.common.models.Fee
import tech.figure.aggregate.common.models.fee.Fee
import java.time.OffsetDateTime

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tech.figure.aggregate.common.models.tx

import com.squareup.moshi.JsonClass
import tech.figure.aggregate.common.models.Fee
import tech.figure.aggregate.common.models.fee.Fee
import tech.figure.aggregate.common.models.block.EncodedBlockchainEvent
import io.provenance.eventstream.stream.models.Event
import java.time.OffsetDateTime
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package tech.figure.aggregate.common.models.tx

import tech.figure.aggregate.common.models.Fee
import tech.figure.aggregate.common.models.fee.Fee

data class TxInfo(
val txHash: String? = "",
Expand Down
7 changes: 4 additions & 3 deletions src/main/kotlin/tech/figure/aggregate/service/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ fun main(args: Array<String>) {
val blockFlow: Flow<BlockData> = blockDataFlow(netAdapter, moshiDecoderAdapter(), from = options.fromHeight, to = options.toHeight)
if (observe) {
blockFlow
.transform { emit(it.toStreamBlock(config.hrp)) }
.transform { emit(it.toStreamBlock(config.hrp, Pair(config.badBlockRange[0], config.badBlockRange[1]))) }
.onEach {
val text = "Block: ${it.block.header?.height ?: "--"}:${it.block.header?.dateTime()?.toLocalDate()}"
println(green(text))
Expand All @@ -281,7 +281,7 @@ fun main(args: Array<String>) {
}
}
}
}
}
} else {
if (config.upload.extractors.isNotEmpty()) {
log.info("upload: adding extractors")
Expand All @@ -295,7 +295,8 @@ fun main(args: Array<String>) {
aws,
repository,
options,
config.hrp
config.hrp,
Pair(config.badBlockRange[0], config.badBlockRange[1])
)
.addExtractor(config.upload.extractors)
.upload()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class EventStreamUploader(
private val repository: RepositoryBase?,
private val options: BlockStreamOptions,
private val hrp: String,
private val badBlockRange: Pair<Long, Long>,
private val dispatchers: DispatcherProvider = DefaultDispatcherProvider(),
) {

Expand Down Expand Up @@ -151,7 +152,7 @@ class EventStreamUploader(
}

return blockFlow
.transform { emit(it.toStreamBlock(hrp)) }
.transform { emit(it.toStreamBlock(hrp, badBlockRange)) }
.filter { streamBlock ->
!streamBlock.blockResult.isNullOrEmpty().also {
log.info(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TxFees: CSVFileExtractor(
event.blockDateTime,
event.fee.fee,
event.fee.denom,
event.fee.incurrAddr, // wallet addr that is paying the fee
event.fee.signerInfo?.incurrAddr, // wallet addr that is paying the fee
includeHash = true
)
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ aws:

ws_node: ${WS_NODE}
api_host: ${API_HOST}
bad_block_range: ${ONE_DOT_ELEVEN_BAD_BLOCK_RANGE}

dbConfig:
addr: ${DB_URI}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/local.env.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ DB_TYPE=RAVENDB
DB_MAX_CONNECTIONS=100
API_HOST=http://localhost:8081
HRP=tp
ONE_DOT_ELEVEN_BAD_BLOCK_RANGE=8721085,10046412
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ class EventStreamUploaderTests {
aws,
repository,
options,
"tp"
"tp",
Pair(config.badBlockRange[0], config.badBlockRange[1])
)
.addExtractor(config.upload.extractors)
.upload { inspected1 = true }
Expand Down Expand Up @@ -169,7 +170,8 @@ class EventStreamUploaderTests {
aws,
repository,
options,
"tp"
"tp",
Pair(config.badBlockRange[0], config.badBlockRange[1])
)
.addExtractor(config.upload.extractors)
.upload { inspected1 = true }
Expand Down

0 comments on commit 8aba27c

Please sign in to comment.