From 63b1b24108374e2a1788b59841a5589c835ca93c Mon Sep 17 00:00:00 2001 From: lukechampine Date: Sun, 20 Oct 2024 21:59:09 -0400 Subject: [PATCH] consensus: Add BlockInterval and MaturityDelay network parameters --- consensus/state.go | 10 ++++++---- consensus/update_test.go | 8 ++++---- consensus/validation_test.go | 8 +++++--- types/multiproof_test.go | 2 +- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/consensus/state.go b/consensus/state.go index b7aa24a1..4fcc42c8 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -50,6 +50,8 @@ type Network struct { InitialCoinbase types.Currency `json:"initialCoinbase"` MinimumCoinbase types.Currency `json:"minimumCoinbase"` InitialTarget types.BlockID `json:"initialTarget"` + BlockInterval time.Duration `json:"blockInterval"` + MaturityDelay uint64 `json:"maturityDelay"` HardforkDevAddr struct { Height uint64 `json:"height"` @@ -225,7 +227,7 @@ func (s State) SufficientlyHeavierThan(t State) bool { // BlockInterval is the expected wall clock time between consecutive blocks. func (s State) BlockInterval() time.Duration { - return 10 * time.Minute + return s.Network.BlockInterval } // BlockReward returns the reward for mining a child block. @@ -240,7 +242,7 @@ func (s State) BlockReward() types.Currency { // MaturityHeight is the height at which various outputs created in the child // block will "mature" (become spendable). func (s State) MaturityHeight() uint64 { - return s.childHeight() + 144 + return s.childHeight() + s.Network.MaturityDelay } // SiafundCount is the number of siafunds in existence. @@ -260,8 +262,8 @@ func (s State) FoundationSubsidy() (sco types.SiacoinOutput, exists bool) { sco.Address = s.FoundationPrimaryAddress subsidyPerBlock := types.Siacoins(30000) - const blocksPerYear = 144 * 365 - const blocksPerMonth = blocksPerYear / 12 + blocksPerYear := uint64(365 * 24 * time.Hour / s.BlockInterval()) + blocksPerMonth := blocksPerYear / 12 hardforkHeight := s.Network.HardforkFoundation.Height if s.childHeight() < hardforkHeight || (s.childHeight()-hardforkHeight)%blocksPerMonth != 0 { sco.Value = types.ZeroCurrency diff --git a/consensus/update_test.go b/consensus/update_test.go index 62aedac1..0d6b3c8d 100644 --- a/consensus/update_test.go +++ b/consensus/update_test.go @@ -818,8 +818,8 @@ func TestApplyRevertBlockV1(t *testing.T) { } } addedSCEs = []types.SiacoinElement{ - {SiacoinOutput: txnB3.FileContracts[0].ValidProofOutputs[1], MaturityHeight: 148}, - {SiacoinOutput: txnB3.FileContracts[0].ValidProofOutputs[0], MaturityHeight: 148}, + {SiacoinOutput: txnB3.FileContracts[0].ValidProofOutputs[1], MaturityHeight: cs.MaturityHeight()}, + {SiacoinOutput: txnB3.FileContracts[0].ValidProofOutputs[0], MaturityHeight: cs.MaturityHeight()}, {SiacoinOutput: b5.MinerPayouts[0], MaturityHeight: cs.MaturityHeight()}, } spentSCEs = nil @@ -1242,8 +1242,8 @@ func TestApplyRevertBlockV2(t *testing.T) { } addedSCEs = []types.SiacoinElement{ - {SiacoinOutput: txnB3.FileContracts[0].RenterOutput, MaturityHeight: 148}, - {SiacoinOutput: txnB3.FileContracts[0].HostOutput, MaturityHeight: 148}, + {SiacoinOutput: txnB3.FileContracts[0].RenterOutput, MaturityHeight: cs.MaturityHeight()}, + {SiacoinOutput: txnB3.FileContracts[0].HostOutput, MaturityHeight: cs.MaturityHeight()}, {SiacoinOutput: b5.MinerPayouts[0], MaturityHeight: cs.MaturityHeight()}, } spentSCEs = nil diff --git a/consensus/validation_test.go b/consensus/validation_test.go index 96185832..b3d76b6b 100644 --- a/consensus/validation_test.go +++ b/consensus/validation_test.go @@ -20,6 +20,8 @@ func testnet() (*Network, types.Block) { InitialCoinbase: types.Siacoins(300000), MinimumCoinbase: types.Siacoins(300000), InitialTarget: types.BlockID{0xFF}, + BlockInterval: 10 * time.Millisecond, + MaturityDelay: 5, } n.HardforkDevAddr.Height = 1 n.HardforkTax.Height = 2 @@ -713,8 +715,6 @@ func TestValidateBlock(t *testing.T) { if err := ValidateBlock(cs, corruptBlock, db.supplementTipBlock(corruptBlock)); err == nil { t.Fatalf("accepted block with %v", test.desc) - } else { - t.Log(test.desc, err) } } } @@ -1626,7 +1626,9 @@ func TestV2ImmatureSiacoinOutput(t *testing.T) { } // check for immature payout error - if err := mineBlock(types.VoidAddress, []types.V2Transaction{txn}); !strings.Contains(err.Error(), "has immature parent") { + if err := mineBlock(types.VoidAddress, []types.V2Transaction{txn}); err == nil { + t.Fatal("expected immature output error, got nil") + } else if !strings.Contains(err.Error(), "has immature parent") { t.Fatalf("expected immature output err, got %v", err) } } diff --git a/types/multiproof_test.go b/types/multiproof_test.go index 207b89a9..d1a8e9cc 100644 --- a/types/multiproof_test.go +++ b/types/multiproof_test.go @@ -16,7 +16,7 @@ import ( // the way they should. This is annoying. func multiproofTxns(numTxns int, numElems int) []types.V2Transaction { // fake accumulator state - cs := (&consensus.Network{InitialTarget: types.BlockID{0: 1}}).GenesisState() + cs := (&consensus.Network{InitialTarget: types.BlockID{0: 1}, BlockInterval: time.Second}).GenesisState() cs.Elements.NumLeaves = 19527 // arbitrary for i := range cs.Elements.Trees { cs.Elements.Trees[i] = frand.Entropy256()