From 13ff98416f394f54b0da7bfa63a45fc3bcda6095 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Thu, 26 Nov 2020 23:36:38 +0100 Subject: [PATCH] Fix cs.tipsets being unbounded Signed-off-by: Jakub Sztandera --- chain/store/store.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/chain/store/store.go b/chain/store/store.go index e7a269602..2ca09dfe9 100644 --- a/chain/store/store.go +++ b/chain/store/store.go @@ -733,11 +733,25 @@ func (cs *ChainStore) AddToTipSetTracker(b *types.BlockHeader) error { } } } + // This function is called 5 times per epoch on average + // It is also called with tipsets that are done with initial validation + // so they cannot be from the future. + // We are guaranteed not to use tipsets older than 900 epochs (fork limit) + // This means that we ideally want to keep only most recent 900 epochs in here + // Golang's map iteration starts at a random point in a map. + // With 5 tries per epoch, and 900 entries to keep, on average we will have + // ~136 garbage entires in the `cs.tipsets` map. (solve for 1-(1-x/(900+x))^5 == 0.5) + // Seems good enough to me + + for height := range cs.tipsets { + if height < b.Height-build.Finality { + delete(cs.tipsets, height) + } + break + } cs.tipsets[b.Height] = append(tss, b.Cid()) - // TODO: do we want to look for slashable submissions here? might as well... - return nil }