From 7d492948ce505a65c2d2e49466fccc40c811f9e1 Mon Sep 17 00:00:00 2001 From: Jeffrey Picard Date: Tue, 8 Mar 2022 18:29:24 +0000 Subject: [PATCH] reorganize bisect function so we lock it properly --- db/db_resolve.go | 6 +----- db/db_stack/db_stack.go | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/db/db_resolve.go b/db/db_resolve.go index cb98115..3c509d1 100644 --- a/db/db_resolve.go +++ b/db/db_resolve.go @@ -37,11 +37,7 @@ func PrepareResolveResult( return nil, err } - var txCounts []interface{} - txCounts = db.TxCounts.GetSlice() - txCounts = txCounts[:db.TxCounts.Len()] - height := BisectRight(txCounts, txNum) - createdHeight := BisectRight(txCounts, rootTxNum) + height, createdHeight := db.TxCounts.TxCountsBisectRight(txNum, rootTxNum, BisectRight) lastTakeoverHeight := controllingClaim.Height expirationHeight := GetExpirationHeight(height) diff --git a/db/db_stack/db_stack.go b/db/db_stack/db_stack.go index 1eb3abe..1f79882 100644 --- a/db/db_stack/db_stack.go +++ b/db/db_stack/db_stack.go @@ -1,6 +1,8 @@ package db_stack -import "sync" +import ( + "sync" +) type SliceBackedStack struct { slice []interface{} @@ -77,3 +79,18 @@ func (s *SliceBackedStack) GetSlice() []interface{} { // This is not thread safe so I won't bother with locking return s.slice } + +// This function is dangerous because it assumes underlying types +func (s *SliceBackedStack) TxCountsBisectRight( + txNum, rootTxNum uint32, + bisectFunc func([]interface{}, uint32) uint32, +) (uint32, uint32) { + s.mut.RLock() + defer s.mut.RUnlock() + + txCounts := s.slice[:s.Len()] + height := bisectFunc(txCounts, txNum) + createdHeight := bisectFunc(txCounts, rootTxNum) + + return height, createdHeight +}