Skip to content

Commit

Permalink
Merge pull request #2 from ergoplatform/tolerate-invalid-trees
Browse files Browse the repository at this point in the history
Tolerate invalid trees
  • Loading branch information
reqlez authored Nov 9, 2023
2 parents d5ae595 + 9dcc271 commit 9b8d7d6
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 90 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ lazy val commonSettings = Seq(
scalacOptions ++= commonScalacOptions,
scalaVersion := "2.12.15",
organization := "org.ergoplatform",
version := "9.17.5",
version := "9.17.9",
resolvers += Resolver.sonatypeRepo("public"),
resolvers += Resolver.sonatypeRepo("snapshots"),
libraryDependencies ++= dependencies.Testing ++ dependencies.CompilerPlugins,
Expand Down
34 changes: 17 additions & 17 deletions modules/chain-grabber/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@
<appender-ref ref="STDOUT"/>
</appender>

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>${logback.file.level:-TRACE}</level>
</filter>
<file>explorer.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>explorer.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
<!-- <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">-->
<!-- <filter class="ch.qos.logback.classic.filter.ThresholdFilter">-->
<!-- <level>${logback.file.level:-TRACE}</level>-->
<!-- </filter>-->
<!-- <file>explorer.log</file>-->
<!-- <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">-->
<!-- &lt;!&ndash; daily rollover &ndash;&gt;-->
<!-- <fileNamePattern>explorer.%d{yyyy-MM-dd}.log.gz</fileNamePattern>-->

<!-- keep 30 days' worth of history capped at 2GB total size -->
<maxHistory>30</maxHistory>
<totalSizeCap>2GB</totalSizeCap>
</rollingPolicy>
<!-- &lt;!&ndash; keep 30 days' worth of history capped at 2GB total size &ndash;&gt;-->
<!-- <maxHistory>30</maxHistory>-->
<!-- <totalSizeCap>2GB</totalSizeCap>-->
<!-- </rollingPolicy>-->

<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level %logger{50} - %msg %n</pattern>
</encoder>
</appender>
<!-- <encoder>-->
<!-- <pattern>%d{HH:mm:ss.SSS} %-5level %logger{50} - %msg %n</pattern>-->
<!-- </encoder>-->
<!-- </appender>-->

<logger name="org.ergoplatform.explorer" level="TRACE"/>
<logger name="org.http4s" level="INFO"/>
<root level="TRACE">
<appender-ref ref="ASYNCSTDOUT"/>
<appender-ref ref="FILE"/>
<!-- <appender-ref ref="FILE"/>-->
</root>

</configuration>
34 changes: 17 additions & 17 deletions modules/explorer-api/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@
<appender-ref ref="STDOUT"/>
</appender>

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>${logback.file.level:-TRACE}</level>
</filter>
<file>explorer.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>explorer.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
<!-- <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">-->
<!-- <filter class="ch.qos.logback.classic.filter.ThresholdFilter">-->
<!-- <level>${logback.file.level:-TRACE}</level>-->
<!-- </filter>-->
<!-- <file>explorer.log</file>-->
<!-- <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">-->
<!-- &lt;!&ndash; daily rollover &ndash;&gt;-->
<!-- <fileNamePattern>explorer.%d{yyyy-MM-dd}.log.gz</fileNamePattern>-->

<!-- keep 30 days' worth of history capped at 2GB total size -->
<maxHistory>30</maxHistory>
<totalSizeCap>2GB</totalSizeCap>
</rollingPolicy>
<!-- &lt;!&ndash; keep 30 days' worth of history capped at 2GB total size &ndash;&gt;-->
<!-- <maxHistory>30</maxHistory>-->
<!-- <totalSizeCap>2GB</totalSizeCap>-->
<!-- </rollingPolicy>-->

<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level %logger{50} - %msg %n</pattern>
</encoder>
</appender>
<!-- <encoder>-->
<!-- <pattern>%d{HH:mm:ss.SSS} %-5level %logger{50} - %msg %n</pattern>-->
<!-- </encoder>-->
<!-- </appender>-->

<logger name="org.ergoplatform.explorer" level="TRACE"/>
<logger name="org.http4s" level="INFO"/>
<root level="TRACE">
<appender-ref ref="ASYNCSTDOUT"/>
<appender-ref ref="FILE"/>
<!-- <appender-ref ref="FILE"/>-->
</root>

</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -15,57 +15,15 @@ final class Emission(settings: MonetarySettings, reemission: ReemissionSettings)
getReemission(defaultReward)
}.sum / constants.Eip27ResidualEmission

def issuedCoinsAfterHeight(h: Long): Long =
if (h < settings.fixedRatePeriod) {
settings.fixedRate * h
} else if (!reemission.applyReemissionRules || h < reemission.activationHeight) {
val fixedRateEmission: Long = settings.fixedRate * (settings.fixedRatePeriod - 1)
val currentEpoch = epoch(h)
val completeNonFixedRateEpochsEmission: Long = (1 to currentEpoch.toInt).map { e =>
math.max(settings.fixedRate - settings.oneEpochReduction * e, 0) * settings.epochLength
}.sum
val heightInThisEpoch = (h - settings.fixedRatePeriod) % settings.epochLength + 1
val rateThisEpoch = math.max(settings.fixedRate - settings.oneEpochReduction * (currentEpoch + 1), 0)
val incompleteEpochEmission = heightInThisEpoch * rateThisEpoch

completeNonFixedRateEpochsEmission + fixedRateEmission + incompleteEpochEmission
} else {
val emissionBeforeEip27 = issuedCoinsAfterHeight(reemission.activationHeight - 1)
val firstEpochAfterActivation =
(reemission.activationHeight - settings.fixedRatePeriod) / settings.epochLength + 1
val firstReductionAfterActivation = firstEpochAfterActivation * settings.epochLength
val currentEpoch = epoch(h)
val defaultRewardPerBlockInCurrentEpoch =
math.max(settings.fixedRate - settings.oneEpochReduction * currentEpoch, 0)
val adjustedReward = defaultRewardPerBlockInCurrentEpoch - getReemission(
defaultRewardPerBlockInCurrentEpoch
)
if (h < firstReductionAfterActivation) {
val blocksSinceActivation = h - reemission.activationHeight
val accumulatedEmissionSinceActivation = blocksSinceActivation * adjustedReward
emissionBeforeEip27 + accumulatedEmissionSinceActivation
} else {
val accumulatedEmissionSinceActivationBeforeFirstReduction =
(firstReductionAfterActivation - reemission.activationHeight) * adjustedReward
if (h < reemission.reemissionStartHeight) {
val accumulatedEmissionSinceFirstReduction =
(firstEpochAfterActivation to currentEpoch.toInt).map(e => getEpochEmissionAfterEip27(e)).sum
val heightInThisEpoch = (h - settings.fixedRatePeriod) % settings.epochLength + 1
val rateThisEpoch = math.max(settings.fixedRate - settings.oneEpochReduction * (currentEpoch + 1), 0)
val rateThisEpochWithReemission = rateThisEpoch - getReemission(rateThisEpoch)
val incompleteEpochEmission = heightInThisEpoch * rateThisEpochWithReemission
emissionBeforeEip27 + accumulatedEmissionSinceActivationBeforeFirstReduction + accumulatedEmissionSinceFirstReduction + incompleteEpochEmission
} else {
val lastEmissionEpoch =
(reemission.reemissionStartHeight - settings.fixedRatePeriod) / settings.epochLength + 1
val accumulatedEmissionSinceFirstReductionUntilReemission =
(firstEpochAfterActivation to lastEmissionEpoch).map(e => getEpochEmissionAfterEip27(e)).sum
val reemissionTail =
math.min(h - reemission.reemissionStartHeight, reemissionLen) * constants.Eip27ResidualEmission
emissionBeforeEip27 + accumulatedEmissionSinceActivationBeforeFirstReduction + accumulatedEmissionSinceFirstReductionUntilReemission + reemissionTail
}
}
def issuedCoinsAfterHeight(h: Long): Long = {
var acc: Long = settings.fixedRate * settings.fixedRatePeriod
var i: Long = settings.fixedRatePeriod + 1
while (i <= h) {
acc += emissionAt(i)
i += 1
}
acc
}

def emissionAt(h: Long): Long = {
val defaultReward = math.max(settings.fixedRate - settings.oneEpochReduction * epoch(h), 0)
Expand All @@ -86,8 +44,10 @@ final class Emission(settings: MonetarySettings, reemission: ReemissionSettings)
1 + (h - settings.fixedRatePeriod) / settings.epochLength

private def getEpochEmissionAfterEip27(e: Int): Long = {
val defaultRewardInEpoch = math.max(settings.fixedRate - settings.oneEpochReduction * e, 0) * settings.epochLength
defaultRewardInEpoch - getReemission(defaultRewardInEpoch) * settings.epochLength
val defaultReward = math.max(settings.fixedRate - settings.oneEpochReduction * e, 0)
val defaultRewardInEpoch = defaultReward * settings.epochLength
val reemissionInEpoch = getReemission(defaultReward) * settings.epochLength
defaultRewardInEpoch - reemissionInEpoch
}

private def getReemission(reward: Long): Long =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ object sigma {
private val constantSerializer: ConstantSerializer = ConstantSerializer(DeserializationSigmaBuilder)

@inline def deserializeErgoTree[F[_]: Applicative: Throws](raw: HexString): F[Values.ErgoTree] =
Base16.decode(raw.unwrapped).map(treeSerializer.deserializeErgoTree).fold(_.raise, _.pure)
Base16
.decode(raw.unwrapped)
.map(treeSerializer.deserializeErgoTree)
.fold(_ => Values.ErgoTree.fromProposition(FalseLeaf.toSigmaProp).pure, _.pure)

@inline def extractErgoTreeConstants[F[_]: Applicative: Throws](
raw: HexString
Expand Down Expand Up @@ -68,7 +71,9 @@ object sigma {
@inline def addressToErgoTreeHex(address: Address)(implicit enc: ErgoAddressEncoder): HexString =
addressToErgoTree(address) |> (tree => HexString.fromStringUnsafe(Base16.encode(tree.bytes)))

@inline def addressToErgoTreeNewtype(address: Address)(implicit enc: ErgoAddressEncoder): org.ergoplatform.explorer.ErgoTree =
@inline def addressToErgoTreeNewtype(address: Address)(implicit
enc: ErgoAddressEncoder
): org.ergoplatform.explorer.ErgoTree =
addressToErgoTreeHex(address) |> (tree => org.ergoplatform.explorer.ErgoTree(tree))

@inline def hexStringToBytes[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.scalatest.matchers.should
class EmissionSpec extends AnyFlatSpec with should.Matchers {
val emission = new Emission(MonetarySettings(), ReemissionSettings())
"Emission.issuedCoinsAfterHeight" should "compute correct total supply" in {
emission.issuedCoinsAfterHeight(6647136L) shouldBe 102624741000000000L
println((1L to 1119273L).map(h => emission.emissionAt(h)).sum)
println(emission.issuedCoinsAfterHeight(8000000))
}
}

0 comments on commit 9b8d7d6

Please sign in to comment.