Skip to content

Commit ae73d4e

Browse files
authored
Fix emission (ergoplatform#255)
* Optimize emission calculator. * Disable file logging by default.
1 parent b6bef0b commit ae73d4e

File tree

5 files changed

+49
-88
lines changed

5 files changed

+49
-88
lines changed

build.sbt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ lazy val commonSettings = Seq(
22
scalacOptions ++= commonScalacOptions,
33
scalaVersion := "2.12.15",
44
organization := "org.ergoplatform",
5-
version := "9.17.5",
5+
version := "9.17.8",
66
resolvers += Resolver.sonatypeRepo("public"),
77
resolvers += Resolver.sonatypeRepo("snapshots"),
88
libraryDependencies ++= dependencies.Testing ++ dependencies.CompilerPlugins,

modules/chain-grabber/src/main/resources/logback.xml

+17-17
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@
1010
<appender-ref ref="STDOUT"/>
1111
</appender>
1212

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

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

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

3232
<logger name="org.ergoplatform.explorer" level="TRACE"/>
3333
<logger name="org.http4s" level="INFO"/>
3434
<root level="TRACE">
3535
<appender-ref ref="ASYNCSTDOUT"/>
36-
<appender-ref ref="FILE"/>
36+
<!-- <appender-ref ref="FILE"/>-->
3737
</root>
3838

3939
</configuration>

modules/explorer-api/src/main/resources/logback.xml

+17-17
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@
1010
<appender-ref ref="STDOUT"/>
1111
</appender>
1212

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

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

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

3232
<logger name="org.ergoplatform.explorer" level="TRACE"/>
3333
<logger name="org.http4s" level="INFO"/>
3434
<root level="TRACE">
3535
<appender-ref ref="ASYNCSTDOUT"/>
36-
<appender-ref ref="FILE"/>
36+
<!-- <appender-ref ref="FILE"/>-->
3737
</root>
3838

3939
</configuration>

modules/explorer-core/src/main/scala/org/ergoplatform/explorer/protocol/Emission.scala

+12-52
Original file line numberDiff line numberDiff line change
@@ -15,57 +15,15 @@ final class Emission(settings: MonetarySettings, reemission: ReemissionSettings)
1515
getReemission(defaultReward)
1616
}.sum / constants.Eip27ResidualEmission
1717

18-
def issuedCoinsAfterHeight(h: Long): Long =
19-
if (h < settings.fixedRatePeriod) {
20-
settings.fixedRate * h
21-
} else if (!reemission.applyReemissionRules || h < reemission.activationHeight) {
22-
val fixedRateEmission: Long = settings.fixedRate * (settings.fixedRatePeriod - 1)
23-
val currentEpoch = epoch(h)
24-
val completeNonFixedRateEpochsEmission: Long = (1 to currentEpoch.toInt).map { e =>
25-
math.max(settings.fixedRate - settings.oneEpochReduction * e, 0) * settings.epochLength
26-
}.sum
27-
val heightInThisEpoch = (h - settings.fixedRatePeriod) % settings.epochLength + 1
28-
val rateThisEpoch = math.max(settings.fixedRate - settings.oneEpochReduction * (currentEpoch + 1), 0)
29-
val incompleteEpochEmission = heightInThisEpoch * rateThisEpoch
30-
31-
completeNonFixedRateEpochsEmission + fixedRateEmission + incompleteEpochEmission
32-
} else {
33-
val emissionBeforeEip27 = issuedCoinsAfterHeight(reemission.activationHeight - 1)
34-
val firstEpochAfterActivation =
35-
(reemission.activationHeight - settings.fixedRatePeriod) / settings.epochLength + 1
36-
val firstReductionAfterActivation = firstEpochAfterActivation * settings.epochLength
37-
val currentEpoch = epoch(h)
38-
val defaultRewardPerBlockInCurrentEpoch =
39-
math.max(settings.fixedRate - settings.oneEpochReduction * currentEpoch, 0)
40-
val adjustedReward = defaultRewardPerBlockInCurrentEpoch - getReemission(
41-
defaultRewardPerBlockInCurrentEpoch
42-
)
43-
if (h < firstReductionAfterActivation) {
44-
val blocksSinceActivation = h - reemission.activationHeight
45-
val accumulatedEmissionSinceActivation = blocksSinceActivation * adjustedReward
46-
emissionBeforeEip27 + accumulatedEmissionSinceActivation
47-
} else {
48-
val accumulatedEmissionSinceActivationBeforeFirstReduction =
49-
(firstReductionAfterActivation - reemission.activationHeight) * adjustedReward
50-
if (h < reemission.reemissionStartHeight) {
51-
val accumulatedEmissionSinceFirstReduction =
52-
(firstEpochAfterActivation to currentEpoch.toInt).map(e => getEpochEmissionAfterEip27(e)).sum
53-
val heightInThisEpoch = (h - settings.fixedRatePeriod) % settings.epochLength + 1
54-
val rateThisEpoch = math.max(settings.fixedRate - settings.oneEpochReduction * (currentEpoch + 1), 0)
55-
val rateThisEpochWithReemission = rateThisEpoch - getReemission(rateThisEpoch)
56-
val incompleteEpochEmission = heightInThisEpoch * rateThisEpochWithReemission
57-
emissionBeforeEip27 + accumulatedEmissionSinceActivationBeforeFirstReduction + accumulatedEmissionSinceFirstReduction + incompleteEpochEmission
58-
} else {
59-
val lastEmissionEpoch =
60-
(reemission.reemissionStartHeight - settings.fixedRatePeriod) / settings.epochLength + 1
61-
val accumulatedEmissionSinceFirstReductionUntilReemission =
62-
(firstEpochAfterActivation to lastEmissionEpoch).map(e => getEpochEmissionAfterEip27(e)).sum
63-
val reemissionTail =
64-
math.min(h - reemission.reemissionStartHeight, reemissionLen) * constants.Eip27ResidualEmission
65-
emissionBeforeEip27 + accumulatedEmissionSinceActivationBeforeFirstReduction + accumulatedEmissionSinceFirstReductionUntilReemission + reemissionTail
66-
}
67-
}
18+
def issuedCoinsAfterHeight(h: Long): Long = {
19+
var acc: Long = settings.fixedRate * settings.fixedRatePeriod
20+
var i: Long = settings.fixedRatePeriod + 1
21+
while (i <= h) {
22+
acc += emissionAt(i)
23+
i += 1
6824
}
25+
acc
26+
}
6927

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

8846
private def getEpochEmissionAfterEip27(e: Int): Long = {
89-
val defaultRewardInEpoch = math.max(settings.fixedRate - settings.oneEpochReduction * e, 0) * settings.epochLength
90-
defaultRewardInEpoch - getReemission(defaultRewardInEpoch) * settings.epochLength
47+
val defaultReward = math.max(settings.fixedRate - settings.oneEpochReduction * e, 0)
48+
val defaultRewardInEpoch = defaultReward * settings.epochLength
49+
val reemissionInEpoch = getReemission(defaultReward) * settings.epochLength
50+
defaultRewardInEpoch - reemissionInEpoch
9151
}
9252

9353
private def getReemission(reward: Long): Long =

modules/explorer-core/src/test/scala/org/ergoplatform/explorer/protocol/EmissionSpec.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.scalatest.matchers.should
77
class EmissionSpec extends AnyFlatSpec with should.Matchers {
88
val emission = new Emission(MonetarySettings(), ReemissionSettings())
99
"Emission.issuedCoinsAfterHeight" should "compute correct total supply" in {
10-
emission.issuedCoinsAfterHeight(6647136L) shouldBe 102624741000000000L
10+
println((1L to 1119273L).map(h => emission.emissionAt(h)).sum)
11+
println(emission.issuedCoinsAfterHeight(8000000))
1112
}
1213
}

0 commit comments

Comments
 (0)