Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

primitives: Add subsidy calcs. #2920

Merged
merged 2 commits into from
Apr 25, 2022
Merged

Conversation

davecgh
Copy link
Member

@davecgh davecgh commented Apr 9, 2022

This implements a subsidy cache with funcs to calculate subsidies for proof-of-work, votes, and the treasury and includes a full suite of tests and benchmarks.

It consists of ports of the related code in blockchain/standalone with the following modifications:

  • Removes the subsidy split parameters from SubsidyParams since the chaincfg implementations are deprecated
    • Uses hard-coded constants for the PoW, PoS, and treasury splits both prior to and after the activation of DCP0010
  • Removes the version 1 methods and renames the version 2 variants to take their place:
    • CalcWorkSubsidyV2 -> CalcWorkSubsidy
    • CalcStakeVoteSubsidyV2 -> CalcStakeVoteSubsidy
  • Consolidates the tests specific to the decentralized treasury agenda into their counterparts
  • Includes additional maintainer notes with the expected way to update the code without needing a major module bump should it ever be necessary

The following benchmarks show the performance of the most relevant methods:

CalcSubsidyCacheSparse   26023311   45.99 ns/op   0 B/op   0 allocs/op
CalcWorkSubsidy          21051818   52.12 ns/op   0 B/op   0 allocs/op
CalcStakeVoteSubsidy     22216708   53.44 ns/op   0 B/op   0 allocs/op
CalcTreasurySubsidy      22610749   49.78 ns/op   0 B/op   0 allocs/op

This is work towards #2786.

@davecgh davecgh added this to the 1.8.0 milestone Apr 9, 2022
@davecgh davecgh force-pushed the primitives_subsidy branch 3 times, most recently from c38a6f7 to 3182c0d Compare April 11, 2022 22:44
Copy link
Member

@rstaudt2 rstaudt2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Test coverage looks great as well:

go test -run="TestSubsidyCacheCalcs|TestTotalSubsidy|TestTotalSubsidyDCP0010|TestCalcBlockSubsidySparseCaching" -coverprofile=cov.out &&
go tool cover -func=cov.out

github.com/decred/dcrd/internal/staging/primitives/subsidy.go:155:		NewSubsidyCache		100.0%
github.com/decred/dcrd/internal/staging/primitives/subsidy.go:174:		Len			100.0%
github.com/decred/dcrd/internal/staging/primitives/subsidy.go:175:		Less			100.0%
github.com/decred/dcrd/internal/staging/primitives/subsidy.go:176:		Swap			100.0%
github.com/decred/dcrd/internal/staging/primitives/subsidy.go:191:		CalcBlockSubsidy	100.0%
github.com/decred/dcrd/internal/staging/primitives/subsidy.go:289:		calcWorkSubsidy		100.0%
github.com/decred/dcrd/internal/staging/primitives/subsidy.go:332:		CalcWorkSubsidy		100.0%
github.com/decred/dcrd/internal/staging/primitives/subsidy.go:365:		calcStakeVoteSubsidy	100.0%
github.com/decred/dcrd/internal/staging/primitives/subsidy.go:400:		CalcStakeVoteSubsidy	100.0%
github.com/decred/dcrd/internal/staging/primitives/subsidy.go:427:		CalcTreasurySubsidy	100.0%

internal/staging/primitives/subsidy.go Outdated Show resolved Hide resolved
internal/staging/primitives/subsidy.go Outdated Show resolved Hide resolved
@davecgh davecgh force-pushed the primitives_subsidy branch 2 times, most recently from 54433fa to 41230c6 Compare April 12, 2022 21:11
This implements a subsidy cache with funcs to calculate subsidies for
proof-of-work, votes, and the treasury and includes a full suite of
tests.

It consists of ports of the related code in blockchain/standalone with
the following modifications:

- Removes the subsidy split parameters from SubsidyParams since the
  chaincfg implementations are deprecated
  - Uses hard-coded constants for the PoW, PoS, and treasury splits both
    prior to and after the activation of DCP0010
- Removes the version 1 methods and renames the version 2 variants to
  take their place:
  - CalcWorkSubsidyV2 -> CalcWorkSubsidy
  - CalcStakeVoteSubsidyV2 -> CalcStakeVoteSubsidy
- Consolidates the tests specific to the decentralized treasury agenda
  into their counterparts
- Includes additional maintainer notes with the expected way to update
  the code without needing a major module bump should it ever be
  necessary
CalcSubsidyCacheSparse   26023311   45.99 ns/op   0 B/op   0 allocs/op
CalcWorkSubsidy          21051818   52.12 ns/op   0 B/op   0 allocs/op
CalcStakeVoteSubsidy     22216708   53.44 ns/op   0 B/op   0 allocs/op
CalcTreasurySubsidy      22610749   49.78 ns/op   0 B/op   0 allocs/op
@davecgh davecgh merged commit 7124399 into decred:master Apr 25, 2022
@davecgh davecgh deleted the primitives_subsidy branch April 25, 2022 23:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants