-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ledger: introduce expired stake cache (#6014)
- Loading branch information
1 parent
0c02f9b
commit f851f50
Showing
5 changed files
with
182 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (C) 2019-2024 Algorand, Inc. | ||
// This file is part of go-algorand | ||
// | ||
// go-algorand is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as | ||
// published by the Free Software Foundation, either version 3 of the | ||
// License, or (at your option) any later version. | ||
// | ||
// go-algorand is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
package ledger | ||
|
||
import ( | ||
"github.com/algorand/go-algorand/data/basics" | ||
"github.com/algorand/go-deadlock" | ||
) | ||
|
||
type expiredCirculationCache struct { | ||
cur map[expiredCirculationKey]basics.MicroAlgos | ||
prev map[expiredCirculationKey]basics.MicroAlgos | ||
|
||
maxSize int | ||
mu deadlock.RWMutex | ||
} | ||
|
||
type expiredCirculationKey struct { | ||
rnd basics.Round | ||
voteRnd basics.Round | ||
} | ||
|
||
func makeExpiredCirculationCache(maxSize int) *expiredCirculationCache { | ||
return &expiredCirculationCache{ | ||
cur: make(map[expiredCirculationKey]basics.MicroAlgos), | ||
maxSize: maxSize, | ||
} | ||
} | ||
|
||
func (c *expiredCirculationCache) get(rnd basics.Round, voteRnd basics.Round) (basics.MicroAlgos, bool) { | ||
c.mu.RLock() | ||
defer c.mu.RUnlock() | ||
if stake, ok := c.cur[expiredCirculationKey{rnd: rnd, voteRnd: voteRnd}]; ok { | ||
return stake, true | ||
} | ||
if stake, ok := c.prev[expiredCirculationKey{rnd: rnd, voteRnd: voteRnd}]; ok { | ||
return stake, true | ||
} | ||
|
||
return basics.MicroAlgos{}, false | ||
} | ||
|
||
func (c *expiredCirculationCache) put(rnd basics.Round, voteRnd basics.Round, expiredStake basics.MicroAlgos) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
if len(c.cur) >= c.maxSize { | ||
c.prev = c.cur | ||
c.cur = make(map[expiredCirculationKey]basics.MicroAlgos) | ||
|
||
} | ||
c.cur[expiredCirculationKey{rnd: rnd, voteRnd: voteRnd}] = expiredStake | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (C) 2019-2024 Algorand, Inc. | ||
// This file is part of go-algorand | ||
// | ||
// go-algorand is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as | ||
// published by the Free Software Foundation, either version 3 of the | ||
// License, or (at your option) any later version. | ||
// | ||
// go-algorand is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
package ledger | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/algorand/go-algorand/data/basics" | ||
"github.com/algorand/go-algorand/test/partitiontest" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAcctOnline_ExpiredCirculationCacheBasic(t *testing.T) { | ||
partitiontest.PartitionTest(t) | ||
t.Parallel() | ||
|
||
cache := makeExpiredCirculationCache(1) | ||
|
||
expStake1 := basics.MicroAlgos{Raw: 123} | ||
cache.put(1, 2, expStake1) | ||
stake, ok := cache.get(1, 2) | ||
require.True(t, ok) | ||
require.Equal(t, expStake1, stake) | ||
|
||
stake, ok = cache.get(3, 4) | ||
require.False(t, ok) | ||
require.Equal(t, basics.MicroAlgos{}, stake) | ||
|
||
expStake2 := basics.MicroAlgos{Raw: 345} | ||
cache.put(3, 4, expStake2) | ||
|
||
stake, ok = cache.get(3, 4) | ||
require.True(t, ok) | ||
require.Equal(t, expStake2, stake) | ||
|
||
// ensure the old entry is still there | ||
stake, ok = cache.get(1, 2) | ||
require.True(t, ok) | ||
require.Equal(t, expStake1, stake) | ||
|
||
// add one more, should evict the first and keep the second | ||
expStake3 := basics.MicroAlgos{Raw: 567} | ||
cache.put(5, 6, expStake3) | ||
stake, ok = cache.get(5, 6) | ||
require.True(t, ok) | ||
require.Equal(t, expStake3, stake) | ||
|
||
stake, ok = cache.get(3, 4) | ||
require.True(t, ok) | ||
require.Equal(t, expStake2, stake) | ||
|
||
stake, ok = cache.get(1, 2) | ||
require.False(t, ok) | ||
require.Equal(t, basics.MicroAlgos{}, stake) | ||
} |