Skip to content

Commit

Permalink
feat: initTickBitmap() to init certain worPos value
Browse files Browse the repository at this point in the history
  • Loading branch information
r3v4s committed Dec 16, 2024
1 parent bf5d75a commit 21df7a0
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions pool/tick_bitmap.gno
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func tickBitmapPosition(tick int32) (int16, uint8) {

// tickBitmapFlipTick flips tthe bit corresponding to the given tick
// in the pool's tick bitmap.
func (pool *Pool) tickBitmapFlipTick(
func (p *Pool) tickBitmapFlipTick(
tick int32,
tickSpacing int32,
) {
Expand All @@ -34,12 +34,12 @@ func (pool *Pool) tickBitmapFlipTick(
wordPos, bitPos := tickBitmapPosition(tick / tickSpacing)

mask := new(u256.Uint).Lsh(u256.One(), uint(bitPos))
pool.setTickBitmap(wordPos, new(u256.Uint).Xor(pool.getTickBitmap(wordPos), mask))
p.setTickBitmap(wordPos, new(u256.Uint).Xor(p.getTickBitmap(wordPos), mask))
}

// tickBitmapNextInitializedTickWithInOneWord finds the next initialized tick within
// one word of the bitmap.
func (pool *Pool) tickBitmapNextInitializedTickWithInOneWord(
func (p *Pool) tickBitmapNextInitializedTickWithInOneWord(
tick int32,
tickSpacing int32,
lte bool,
Expand All @@ -51,7 +51,7 @@ func (pool *Pool) tickBitmapNextInitializedTickWithInOneWord(

wordPos, bitPos := getWordAndBitPos(compress, lte)
mask := getMaskBit(uint(bitPos), lte)
masked := new(u256.Uint).And(pool.getTickBitmap(wordPos), mask)
masked := new(u256.Uint).And(p.getTickBitmap(wordPos), mask)
initialized := !(masked.IsZero())

nextTick := getNextTick(lte, initialized, compress, bitPos, tickSpacing, masked)
Expand All @@ -60,21 +60,21 @@ func (pool *Pool) tickBitmapNextInitializedTickWithInOneWord(

// getTickBitmap gets the tick bitmap for the given word position
// if the tick bitmap is not initialized, initialize it to zero
func (pool *Pool) getTickBitmap(wordPos int16) *u256.Uint {
func (p *Pool) getTickBitmap(wordPos int16) *u256.Uint {
wordPosStr := strconv.FormatInt(int64(wordPos), 10)

iBitmap, exist := pool.tickBitmaps.Get(wordPosStr)
iBitmap, exist := p.tickBitmaps.Get(wordPosStr)
if !exist {
pool.tickBitmaps.Set(wordPosStr, u256.Zero())
p.initTickBitmap(wordPosStr)
return u256.Zero()
}

return bitmap.(*u256.Uint)
return iBitmap.(*u256.Uint)
}

// setTickBitmap sets the tick bitmap for the given word position
func (pool *Pool) setTickBitmap(wordPos int16, bitmap *u256.Uint) {
pool.tickBitmaps.Set(strconv.FormatInt(int64(wordPos), 10), bitmap)
func (p *Pool) setTickBitmap(wordPos int16, bitmap *u256.Uint) {
p.tickBitmaps.Set(strconv.FormatInt(int64(wordPos), 10), bitmap)
}

// getWordAndBitPos gets tick's wordPos and bitPos depending on the swap direction
Expand Down Expand Up @@ -137,3 +137,7 @@ func getTickIfNotInitialized(compress, tickSpacing int32, bitPos uint8, lte bool

return (compress + 1 + int32(255-bitPos)) * tickSpacing
}

func (p *Pool) initTickBitmap(wordPos string) {
p.tickBitmaps.Set(wordPos, u256.Zero())
}

0 comments on commit 21df7a0

Please sign in to comment.