diff --git a/sync/cache/cache.go b/sync/cache/cache.go index 0a66e29ab..32bd39068 100644 --- a/sync/cache/cache.go +++ b/sync/cache/cache.go @@ -1,69 +1,54 @@ package cache import ( - lru "github.com/hashicorp/golang-lru" + lru "github.com/hashicorp/golang-lru/v2" "github.com/pactus-project/pactus/types/block" - "github.com/pactus-project/pactus/util" ) -const ( - blockPrefix = 0x01 - certificatePrefix = 0x02 -) - -type key [32]byte - -func blockKey(height uint32) key { - var k key - k[0] = blockPrefix - copy(k[1:], util.Uint32ToSlice(height)) - return k -} - -func certificateKey(height uint32) key { - var k key - k[0] = certificatePrefix - copy(k[1:], util.Uint32ToSlice(height)) - return k -} - type Cache struct { - cache *lru.Cache // it's thread safe + blocks *lru.Cache[uint32, *block.Block] // it's thread safe + certs *lru.Cache[uint32, *block.Certificate] } func NewCache(size int) (*Cache, error) { - c, err := lru.New(size) + b, err := lru.New[uint32, *block.Block](size) if err != nil { return nil, err } + + c, err := lru.New[uint32, *block.Certificate](size) + if err != nil { + return nil, err + } + return &Cache{ - cache: c, + blocks: b, + certs: c, }, nil } func (c *Cache) HasBlockInCache(height uint32) bool { - _, ok := c.cache.Get(blockKey(height)) - return ok + return c.blocks.Contains(height) } func (c *Cache) GetBlock(height uint32) *block.Block { - i, ok := c.cache.Get(blockKey(height)) + block, ok := c.blocks.Get(height) if ok { - return i.(*block.Block) + return block } return nil } func (c *Cache) AddBlock(height uint32, block *block.Block) { - c.cache.Add(blockKey(height), block) + c.blocks.Add(height, block) c.AddCertificate(height-1, block.PrevCertificate()) } func (c *Cache) GetCertificate(height uint32) *block.Certificate { - i, ok := c.cache.Get(certificateKey(height)) + certificate, ok := c.certs.Get(height) if ok { - return i.(*block.Certificate) + return certificate } return nil @@ -71,14 +56,19 @@ func (c *Cache) GetCertificate(height uint32) *block.Certificate { func (c *Cache) AddCertificate(height uint32, cert *block.Certificate) { if cert != nil { - c.cache.Add(certificateKey(height), cert) + c.certs.Add(height, cert) } } +// Len returns the maximum number of items in the blocks and certificates cache. func (c *Cache) Len() int { - return c.cache.Len() + if c.blocks.Len() > c.certs.Len() { + return c.blocks.Len() + } + return c.certs.Len() } func (c *Cache) Clear() { - c.cache.Purge() + c.blocks.Purge() + c.certs.Purge() } diff --git a/sync/cache/cache_test.go b/sync/cache/cache_test.go index 91205c478..4acbf14e7 100644 --- a/sync/cache/cache_test.go +++ b/sync/cache/cache_test.go @@ -7,13 +7,6 @@ import ( "github.com/stretchr/testify/assert" ) -func TestKeys(t *testing.T) { - assert.Equal(t, blockKey(1234), - key{0x1, 0xd2, 0x4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) - assert.Equal(t, certificateKey(1234), - key{0x2, 0xd2, 0x4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) -} - func TestCacheBlocks(t *testing.T) { ts := testsuite.NewTestSuite(t) @@ -51,7 +44,7 @@ func TestClearCache(t *testing.T) { cache.AddBlock(2, b) - assert.Equal(t, cache.Len(), 2) // block + certificate + assert.Equal(t, cache.Len(), 1) cache.Clear() assert.Equal(t, cache.Len(), 0) assert.Nil(t, cache.GetBlock(2))