Skip to content

Commit

Permalink
Use Blocks from Block Sync in Block Manager (cosmos#1056)
Browse files Browse the repository at this point in the history
<!--
Please read and fill out this form before submitting your PR.

Please make sure you have reviewed our contributors guide before
submitting your
first PR.
-->

## Overview
Resolves: cosmos#1053, resolves: cosmos#1060, resolve cosmos#1061

<!-- 
Please provide an explanation of the PR, including the appropriate
context,
background, goal, and rationale. If there is an issue with this
information,
please provide a tl;dr and link the issue. 
-->

## Checklist

<!-- 
Please complete the checklist to ensure that the PR is ready to be
reviewed.

IMPORTANT:
PRs should be left in Draft until the below checklist is completed.
-->

- [x] New and updated code has appropriate documentation
- [x] New and updated code has new and/or updated testing
- [ ] Required CI checks are passing
- [ ] Visual proof for any user facing features like CLI or
documentation updates
- [x] Linked issues closed with keywords

---------

Co-authored-by: Ganesha Upadhyaya <[email protected]>
  • Loading branch information
Manav-Aggarwal and Ganesha Upadhyaya authored Aug 14, 2023
1 parent ad8190c commit 63d462c
Show file tree
Hide file tree
Showing 11 changed files with 435 additions and 130 deletions.
60 changes: 60 additions & 0 deletions block/block_cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package block

import (
"sync"

"github.com/rollkit/rollkit/types"
)

type BlockCache struct {
blocks map[uint64]*types.Block
hashes map[string]bool
hardConfirmations map[string]bool
mtx *sync.RWMutex
}

func NewBlockCache() *BlockCache {
return &BlockCache{
blocks: make(map[uint64]*types.Block),
hashes: make(map[string]bool),
hardConfirmations: make(map[string]bool),
mtx: new(sync.RWMutex),
}
}

func (bc *BlockCache) getBlock(height uint64) (*types.Block, bool) {
bc.mtx.Lock()
defer bc.mtx.Unlock()
block, ok := bc.blocks[height]
return block, ok
}

func (bc *BlockCache) setBlock(height uint64, block *types.Block) {
bc.mtx.Lock()
defer bc.mtx.Unlock()
bc.blocks[height] = block
}

func (bc *BlockCache) deleteBlock(height uint64) {
bc.mtx.Lock()
defer bc.mtx.Unlock()
delete(bc.blocks, height)
}

func (bc *BlockCache) isSeen(hash string) bool {
bc.mtx.Lock()
defer bc.mtx.Unlock()
return bc.hashes[hash]
}

func (bc *BlockCache) setSeen(hash string) {
bc.mtx.Lock()
defer bc.mtx.Unlock()
bc.hashes[hash] = true
}

func (bc *BlockCache) setHardConfirmed(hash string) {
bc.mtx.Lock()
defer bc.mtx.Unlock()
bc.hardConfirmations[hash] = true
}
Loading

0 comments on commit 63d462c

Please sign in to comment.