Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
cometbft/mempool/cache_test.go at main · cometbft/cometbft · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
cometbft
cometbft
Repository navigation
Code
Issues
269
(269)
Pull requests
44
(44)
Actions
Projects
Wiki
Security and quality
12
(12)
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
cometbft
/
mempool
/
cache_test.go
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
113 lines (99 loc) · 3.47 KB
main
Breadcrumbs
cometbft
/
mempool
/
cache_test.go
Copy path
Top
File metadata and controls
Code
Blame
113 lines (99 loc) · 3.47 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package
mempool
import
(
"crypto/rand"
"crypto/sha256"
"fmt"
"testing"
"github.com/cometbft/cometbft/abci/example/kvstore"
abci
"github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/proxy"
"github.com/cometbft/cometbft/types"
"github.com/stretchr/testify/require"
)
func
TestCacheRemove
(
t
*
testing.
T
) {
cache
:=
NewLRUTxCache
(
100
)
numTxs
:=
10
txs
:=
make
([][]
byte
,
numTxs
)
for
i
:=
0
;
i
<
numTxs
;
i
++
{
// probability of collision is 2**-256
txBytes
:=
make
([]
byte
,
32
)
_
,
err
:=
rand
.
Read
(
txBytes
)
require
.
NoError
(
t
,
err
)
txs
[
i
]
=
txBytes
cache
.
Push
(
txBytes
)
// make sure its added to both the linked list and the map
require
.
Equal
(
t
,
i
+
1
,
len
(
cache
.
cacheMap
))
require
.
Equal
(
t
,
i
+
1
,
cache
.
list
.
Len
())
}
for
i
:=
0
;
i
<
numTxs
;
i
++
{
cache
.
Remove
(
txs
[
i
])
// make sure its removed from both the map and the linked list
require
.
Equal
(
t
,
numTxs
-
(
i
+
1
),
len
(
cache
.
cacheMap
))
require
.
Equal
(
t
,
numTxs
-
(
i
+
1
),
cache
.
list
.
Len
())
}
}
func
TestCacheAfterUpdate
(
t
*
testing.
T
) {
app
:=
kvstore
.
NewInMemoryApplication
()
cc
:=
proxy
.
NewLocalClientCreator
(
app
)
mp
,
cleanup
:=
newMempoolWithApp
(
cc
)
defer
cleanup
()
// reAddIndices & txsInCache can have elements > numTxsToCreate
// also assumes max index is 255 for convenience
// txs in cache also checks order of elements
tests
:=
[]
struct
{
numTxsToCreate
int
updateIndices
[]
int
reAddIndices
[]
int
txsInCache
[]
int
}{
{
1
, []
int
{}, []
int
{
1
}, []
int
{
1
,
0
}},
// adding new txs works
{
2
, []
int
{
1
}, []
int
{}, []
int
{
1
,
0
}},
// update doesn't remove tx from cache
{
2
, []
int
{
2
}, []
int
{}, []
int
{
2
,
1
,
0
}},
// update adds new tx to cache
{
2
, []
int
{
1
}, []
int
{
1
}, []
int
{
1
,
0
}},
// re-adding after update doesn't make dupe
}
for
tcIndex
,
tc
:=
range
tests
{
for
i
:=
0
;
i
<
tc
.
numTxsToCreate
;
i
++
{
tx
:=
kvstore
.
NewTx
(
fmt
.
Sprintf
(
"%d"
,
i
),
"value"
)
err
:=
mp
.
CheckTx
(
tx
,
func
(
resp
*
abci.
ResponseCheckTx
) {
require
.
False
(
t
,
resp
.
IsErr
())
},
TxInfo
{})
require
.
NoError
(
t
,
err
)
}
updateTxs
:=
[]types.
Tx
{}
for
_
,
v
:=
range
tc
.
updateIndices
{
tx
:=
kvstore
.
NewTx
(
fmt
.
Sprintf
(
"%d"
,
v
),
"value"
)
updateTxs
=
append
(
updateTxs
,
tx
)
}
err
:=
mp
.
Update
(
int64
(
tcIndex
),
updateTxs
,
abciResponses
(
len
(
updateTxs
),
abci
.
CodeTypeOK
),
nil
,
nil
)
require
.
NoError
(
t
,
err
)
for
_
,
v
:=
range
tc
.
reAddIndices
{
tx
:=
kvstore
.
NewTx
(
fmt
.
Sprintf
(
"%d"
,
v
),
"value"
)
_
=
mp
.
CheckTx
(
tx
,
func
(
resp
*
abci.
ResponseCheckTx
) {
require
.
False
(
t
,
resp
.
IsErr
())
},
TxInfo
{})
}
cache
:=
mp
.
cache
.(
*
LRUTxCache
)
node
:=
cache
.
GetList
().
Front
()
counter
:=
0
for
node
!=
nil
{
require
.
NotEqual
(
t
,
len
(
tc
.
txsInCache
),
counter
,
"cache larger than expected on testcase %d"
,
tcIndex
)
nodeVal
:=
node
.
Value
.(types.
TxKey
)
expTx
:=
kvstore
.
NewTx
(
fmt
.
Sprintf
(
"%d"
,
tc
.
txsInCache
[
len
(
tc
.
txsInCache
)
-
counter
-
1
]),
"value"
)
expectedBz
:=
sha256
.
Sum256
(
expTx
)
// Reference for reading the errors:
// >>> sha256('\x00').hexdigest()
// '6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d'
// >>> sha256('\x01').hexdigest()
// '4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a'
// >>> sha256('\x02').hexdigest()
// 'dbc1b4c900ffe48d575b5da5c638040125f65db0fe3e24494b76ea986457d986'
require
.
EqualValues
(
t
,
expectedBz
,
nodeVal
,
"Equality failed on index %d, tc %d"
,
counter
,
tcIndex
)
counter
++
node
=
node
.
Next
()
}
require
.
Equal
(
t
,
len
(
tc
.
txsInCache
),
counter
,
"cache smaller than expected on testcase %d"
,
tcIndex
)
mp
.
Flush
()
}
}
You can’t perform that action at this time.