From 45e6adbe46e1bd406d1bc8e252bd1223440e285e Mon Sep 17 00:00:00 2001 From: Brannon King Date: Thu, 12 Aug 2021 11:20:00 -0400 Subject: [PATCH] made claims never expire --- claimtrie/node/manager.go | 12 +++------- claimtrie/node/manager_test.go | 42 ++++++++++++++++++++++++++++++---- claimtrie/node/node.go | 17 ++++++++++---- claimtrie/param/general.go | 4 ++++ 4 files changed, 56 insertions(+), 19 deletions(-) diff --git a/claimtrie/node/manager.go b/claimtrie/node/manager.go index 605b7123..d098005c 100644 --- a/claimtrie/node/manager.go +++ b/claimtrie/node/manager.go @@ -54,12 +54,6 @@ func (nm *BaseManager) NodeAt(height int32, name []byte) (*Node, error) { return n, nil } -// Node returns a node at the current height. -// The returned node may have pending changes. -func (nm *BaseManager) node(name []byte) (*Node, error) { - return nm.NodeAt(nm.height, name) -} - // newNodeFromChanges returns a new Node constructed from the changes. // The changes must preserve their order received. func (nm *BaseManager) newNodeFromChanges(changes []change.Change, height int32) (*Node, error) { @@ -356,17 +350,17 @@ func (nm *BaseManager) IterateNames(predicate func(name []byte) bool) { func (nm *BaseManager) Hash(name []byte) (*chainhash.Hash, int32) { - n, err := nm.node(name) + n, err := nm.NodeAt(nm.height, name) if err != nil || n == nil { return nil, 0 } if len(n.Claims) > 0 { if n.BestClaim != nil && n.BestClaim.Status == Activated { h := calculateNodeHash(n.BestClaim.OutPoint, n.TakenOverAt) - return h, n.NextUpdate() + return h, n.NextUpdate(nm.height) } } - return nil, n.NextUpdate() + return nil, n.NextUpdate(nm.height) } func (nm *BaseManager) Flush() error { diff --git a/claimtrie/node/manager_test.go b/claimtrie/node/manager_test.go index 705080c8..83c4d470 100644 --- a/claimtrie/node/manager_test.go +++ b/claimtrie/node/manager_test.go @@ -67,25 +67,25 @@ func TestSimpleAddClaim(t *testing.T) { _, err = m.IncrementHeightTo(12) r.NoError(err) - n1, err := m.node(name1) + n1, err := m.NodeAt(m.height, name1) r.NoError(err) r.Equal(1, len(n1.Claims)) r.NotNil(n1.Claims.find(byOut(*out1))) - n2, err := m.node(name2) + n2, err := m.NodeAt(m.height, name2) r.NoError(err) r.Equal(1, len(n2.Claims)) r.NotNil(n2.Claims.find(byOut(*out2))) err = m.DecrementHeightTo([][]byte{name2}, 11) r.NoError(err) - n2, err = m.node(name2) + n2, err = m.NodeAt(m.height, name2) r.NoError(err) r.Nil(n2) err = m.DecrementHeightTo([][]byte{name1}, 1) r.NoError(err) - n2, err = m.node(name1) + n2, err = m.NodeAt(m.height, name1) r.NoError(err) r.Nil(n2) } @@ -131,7 +131,7 @@ func TestSupportAmounts(t *testing.T) { _, err = m.IncrementHeightTo(20) r.NoError(err) - n1, err := m.node(name1) + n1, err := m.NodeAt(m.height, name1) r.NoError(err) r.Equal(2, len(n1.Claims)) r.Equal(int64(5), n1.BestClaim.Amount+n1.SupportSums[n1.BestClaim.ClaimID.Key()]) @@ -248,3 +248,35 @@ func TestCollectChildren(t *testing.T) { r.Len(c[7].SpentChildren, 0) } + +func TestEndOfExpiration(t *testing.T) { + r := require.New(t) + + param.SetNetwork(wire.TestNet) + repo, err := noderepo.NewPebble(t.TempDir()) + r.NoError(err) + + m, err := NewBaseManager(repo) + r.NoError(err) + defer m.Close() + + et := param.ActiveParams.ExtendedClaimExpirationForkHeight + gf := param.ActiveParams.GrandForkHeight + + chg := change.NewChange(change.AddClaim).SetName(name1).SetOutPoint(out1).SetHeight(et).SetAmount(2) + chg.ClaimID = change.NewClaimID(*out1) + m.AppendChange(chg) + + _, err = m.IncrementHeightTo(et) + r.NoError(err) + n, err := m.NodeAt(m.height, name1) + r.NoError(err) + r.Equal(m.height + param.ActiveParams.ExtendedClaimExpirationTime, n.NextUpdate(m.height)) + + _, err = m.IncrementHeightTo(gf) + r.NoError(err) + + n, err = m.NodeAt(m.height, name1) + r.NoError(err) + r.Equal(int32(0), n.NextUpdate(m.height)) +} \ No newline at end of file diff --git a/claimtrie/node/node.go b/claimtrie/node/node.go index f9a466be..0197b98b 100644 --- a/claimtrie/node/node.go +++ b/claimtrie/node/node.go @@ -114,7 +114,10 @@ func (n *Node) AdjustTo(height, maxHeight int32, name []byte) *Node { changed := n.handleExpiredAndActivated(height) > 0 n.updateTakeoverHeight(height, name, changed) if maxHeight > height { - for h := n.NextUpdate(); h <= maxHeight; h = n.NextUpdate() { + for h := n.NextUpdate(height); h <= maxHeight; h = n.NextUpdate(height) { + if h <= 0 { + break + } changed = n.handleExpiredAndActivated(h) > 0 n.updateTakeoverHeight(h, name, changed) height = h @@ -167,7 +170,7 @@ func (n *Node) handleExpiredAndActivated(height int32) int { sums[c.ClaimID.Key()] += c.Amount } } - if c.ExpireAt() <= height || c.Status == Deactivated { + if c.Status == Deactivated || (height < param.ActiveParams.GrandForkHeight && c.ExpireAt() <= height) { if i < len(items)-1 { items[i] = items[len(items)-1] i-- @@ -188,12 +191,12 @@ func (n *Node) handleExpiredAndActivated(height int32) int { // NextUpdate returns the nearest height in the future that the node should // be refreshed due to changes of claims or supports. -func (n Node) NextUpdate() int32 { +func (n Node) NextUpdate(height int32) int32 { next := int32(math.MaxInt32) for _, c := range n.Claims { - if c.ExpireAt() < next { + if height < param.ActiveParams.GrandForkHeight && c.ExpireAt() < next { next = c.ExpireAt() } // if we're not active, we need to go to activeAt unless we're still invisible there @@ -209,7 +212,7 @@ func (n Node) NextUpdate() int32 { } for _, s := range n.Supports { - if s.ExpireAt() < next { + if height < param.ActiveParams.GrandForkHeight && s.ExpireAt() < next { next = s.ExpireAt() } if s.Status == Accepted { @@ -223,6 +226,10 @@ func (n Node) NextUpdate() int32 { } } + if next == int32(math.MaxInt32) || next <= height { + return 0 + } + return next } diff --git a/claimtrie/param/general.go b/claimtrie/param/general.go index 92ff06fe..0e54d50d 100644 --- a/claimtrie/param/general.go +++ b/claimtrie/param/general.go @@ -16,6 +16,7 @@ type ClaimTrieParams struct { NormalizedNameForkHeight int32 AllClaimsInMerkleForkHeight int32 + GrandForkHeight int32 } var ( @@ -32,6 +33,7 @@ var ( MaxRemovalWorkaroundHeight: 658300, NormalizedNameForkHeight: 539940, // targeting 21 March 2019}, https://lbry.com/news/hf1903 AllClaimsInMerkleForkHeight: 658309, // targeting 30 Oct 2019}, https://lbry.com/news/hf1910 + GrandForkHeight: 1200000, } TestNet = ClaimTrieParams{ @@ -45,6 +47,7 @@ var ( MaxRemovalWorkaroundHeight: 1, // if you get a hash mismatch, come back to this NormalizedNameForkHeight: 993380, AllClaimsInMerkleForkHeight: 1198559, + GrandForkHeight: 1200000, } Regtest = ClaimTrieParams{ @@ -58,6 +61,7 @@ var ( MaxRemovalWorkaroundHeight: -1, NormalizedNameForkHeight: 250, AllClaimsInMerkleForkHeight: 349, + GrandForkHeight: 850, } )