forked from 0xPolygonHermez/cdk-erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nonce-cache): read from nonce cache when we flush tx pool or ret…
…urn 0 by sender feat(nonce-cache): rm print lines feat(nonce-cache): rm print lines 2 feat(nonce-cache): rm print lines 3 feat(nonce-cache): rm print lines 4
- Loading branch information
1 parent
e09961a
commit 1c0acc3
Showing
8 changed files
with
241 additions
and
72 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,60 @@ | ||
package nonce_cache | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/ledgerwatch/erigon-lib/common" | ||
) | ||
|
||
const MaxNonceCacheSize = 1000 | ||
|
||
type NonceCache struct { | ||
highestNoncesBySender map[common.Address]uint64 | ||
mu sync.RWMutex | ||
maxSize uint64 | ||
} | ||
|
||
func NewNonceCache(cacheSize uint64) *NonceCache { | ||
return &NonceCache{ | ||
highestNoncesBySender: make(map[common.Address]uint64), | ||
mu: sync.RWMutex{}, | ||
maxSize: cacheSize, | ||
} | ||
} | ||
|
||
func (nc *NonceCache) GetHighestNonceForSender(sender common.Address) (uint64, bool) { | ||
nc.mu.RLock() | ||
defer nc.mu.RUnlock() | ||
nonce, ok := nc.highestNoncesBySender[sender] | ||
return nonce, ok | ||
} | ||
|
||
func (nc *NonceCache) TrySetHighestNonceForSender(sender common.Address, nonce uint64) bool { | ||
nc.mu.Lock() | ||
defer nc.mu.Unlock() | ||
|
||
if nc.highestNoncesBySender[sender] < nonce { | ||
nc.highestNoncesBySender[sender] = nonce | ||
if uint64(len(nc.highestNoncesBySender)) > nc.maxSize { | ||
var oldestSender common.Address | ||
var oldestNonce uint64 | ||
for s, n := range nc.highestNoncesBySender { | ||
if oldestNonce == 0 || n < oldestNonce { | ||
oldestSender = s | ||
oldestNonce = n | ||
} | ||
} | ||
delete(nc.highestNoncesBySender, oldestSender) | ||
} | ||
|
||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
func (nc *NonceCache) CacheSize() int { | ||
nc.mu.RLock() | ||
defer nc.mu.RUnlock() | ||
return len(nc.highestNoncesBySender) | ||
} |
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,79 @@ | ||
package nonce_cache | ||
|
||
import ( | ||
"github.com/ledgerwatch/erigon-lib/common" | ||
"testing" | ||
) | ||
|
||
const testCacheSize = 4 | ||
|
||
func Test_NonceCacheSize(t *testing.T) { | ||
scenarios := map[string]struct { | ||
setupActions []func(*NonceCache) | ||
expectedSize int | ||
expectedExists map[string]bool | ||
}{ | ||
"Initial state with 4 entries": { | ||
setupActions: []func(*NonceCache){ | ||
func(nc *NonceCache) { nc.TrySetHighestNonceForSender(common.HexToAddress("0x1"), 1) }, | ||
func(nc *NonceCache) { nc.TrySetHighestNonceForSender(common.HexToAddress("0x2"), 2) }, | ||
func(nc *NonceCache) { nc.TrySetHighestNonceForSender(common.HexToAddress("0x3"), 3) }, | ||
func(nc *NonceCache) { nc.TrySetHighestNonceForSender(common.HexToAddress("0x4"), 4) }, | ||
}, | ||
expectedSize: 4, | ||
expectedExists: map[string]bool{ | ||
"0x1": true, "0x2": true, "0x3": true, "0x4": true, | ||
}, | ||
}, | ||
"Evict when adding 5th entry": { | ||
setupActions: []func(*NonceCache){ | ||
func(nc *NonceCache) { nc.TrySetHighestNonceForSender(common.HexToAddress("0x1"), 1) }, | ||
func(nc *NonceCache) { nc.TrySetHighestNonceForSender(common.HexToAddress("0x2"), 2) }, | ||
func(nc *NonceCache) { nc.TrySetHighestNonceForSender(common.HexToAddress("0x3"), 3) }, | ||
func(nc *NonceCache) { nc.TrySetHighestNonceForSender(common.HexToAddress("0x4"), 4) }, | ||
func(nc *NonceCache) { nc.TrySetHighestNonceForSender(common.HexToAddress("0x5"), 5) }, | ||
}, | ||
expectedSize: testCacheSize, | ||
expectedExists: map[string]bool{ | ||
"0x1": false, "0x2": true, "0x3": true, "0x4": true, "0x5": true, | ||
}, | ||
}, | ||
"Try write loads of entries": { | ||
setupActions: []func(*NonceCache){ | ||
func(nc *NonceCache) { nc.TrySetHighestNonceForSender(common.HexToAddress("0x1"), 1) }, | ||
func(nc *NonceCache) { nc.TrySetHighestNonceForSender(common.HexToAddress("0x2"), 2) }, | ||
func(nc *NonceCache) { nc.TrySetHighestNonceForSender(common.HexToAddress("0x3"), 3) }, | ||
func(nc *NonceCache) { nc.TrySetHighestNonceForSender(common.HexToAddress("0x4"), 4) }, | ||
func(nc *NonceCache) { nc.TrySetHighestNonceForSender(common.HexToAddress("0x5"), 5) }, | ||
func(nc *NonceCache) { nc.TrySetHighestNonceForSender(common.HexToAddress("0x6"), 6) }, | ||
func(nc *NonceCache) { nc.TrySetHighestNonceForSender(common.HexToAddress("0x7"), 7) }, | ||
func(nc *NonceCache) { nc.TrySetHighestNonceForSender(common.HexToAddress("0x8"), 8) }, | ||
}, | ||
expectedSize: testCacheSize, | ||
expectedExists: map[string]bool{ | ||
"0x1": false, "0x2": false, "0x3": false, "0x4": false, "0x5": true, "0x6": true, "0x7": true, "0x8": true, | ||
}, | ||
}, | ||
} | ||
|
||
for name, scenario := range scenarios { | ||
t.Run(name, func(t *testing.T) { | ||
cache := NewNonceCache(testCacheSize) | ||
|
||
for _, action := range scenario.setupActions { | ||
action(cache) | ||
} | ||
|
||
if size := cache.CacheSize(); size != scenario.expectedSize { | ||
t.Errorf("Expected cache size %d, got %d", scenario.expectedSize, size) | ||
} | ||
|
||
for addr, shouldExist := range scenario.expectedExists { | ||
_, exists := cache.GetHighestNonceForSender(common.HexToAddress(addr)) | ||
if exists != shouldExist { | ||
t.Errorf("Expected sender %s to exist: %t, got %t", addr, shouldExist, exists) | ||
} | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.