Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC 12 Implementation: Coordination procedure #3745

Merged
merged 15 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pkg/chain/ethereum/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,19 @@ func (bc *baseChain) GetBlockNumberByTimestamp(
return block.NumberU64(), nil
}

// GetBlockHashByNumber gets the block hash for the given block number.
func (bc *baseChain) GetBlockHashByNumber(blockNumber uint64) (
[32]byte,
error,
) {
block, err := bc.blockByNumber(blockNumber)
if err != nil {
return [32]byte{}, fmt.Errorf("cannot get block: [%v]", err)
}

return block.Hash(), nil
}

// currentBlock fetches the current block.
func (bc *baseChain) currentBlock() (*types.Block, error) {
currentBlockNumber, err := bc.blockCounter.CurrentBlock()
Expand Down
2 changes: 2 additions & 0 deletions pkg/tbtc/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,8 @@ type Chain interface {
// If the aforementioned is not possible, it tries to return the closest
// possible block.
GetBlockNumberByTimestamp(timestamp uint64) (uint64, error)
// GetBlockHashByNumber gets the block hash for the given block number.
GetBlockHashByNumber(blockNumber uint64) ([32]byte, error)

sortition.Chain
GroupSelectionChain
Expand Down
37 changes: 37 additions & 0 deletions pkg/tbtc/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ type localChain struct {
blocksByTimestampMutex sync.Mutex
blocksByTimestamp map[uint64]uint64

blocksHashesByNumberMutex sync.Mutex
blocksHashesByNumber map[uint64][32]byte

pastDepositRevealedEventsMutex sync.Mutex
pastDepositRevealedEvents map[[32]byte][]*DepositRevealedEvent

Expand Down Expand Up @@ -105,6 +108,39 @@ func (lc *localChain) setBlockNumberByTimestamp(timestamp uint64, block uint64)
lc.blocksByTimestamp[timestamp] = block
}

func (lc *localChain) GetBlockHashByNumber(blockNumber uint64) (
[32]byte,
error,
) {
lc.blocksHashesByNumberMutex.Lock()
defer lc.blocksHashesByNumberMutex.Unlock()

blockHash, ok := lc.blocksHashesByNumber[blockNumber]
if !ok {
return [32]byte{}, fmt.Errorf("block not found")
}

return blockHash, nil
}

func (lc *localChain) setBlockHashByNumber(
blockNumber uint64,
blockHashString string,
) {
lc.blocksHashesByNumberMutex.Lock()
defer lc.blocksHashesByNumberMutex.Unlock()

blockHashBytes, err := hex.DecodeString(blockHashString)
if err != nil {
panic(err)
}

var blockHash [32]byte
copy(blockHash[:], blockHashBytes)

lc.blocksHashesByNumber[blockNumber] = blockHash
}

func (lc *localChain) OperatorToStakingProvider() (chain.Address, bool, error) {
panic("unsupported")
}
Expand Down Expand Up @@ -823,6 +859,7 @@ func ConnectWithKey(
),
wallets: make(map[[20]byte]*WalletChainData),
blocksByTimestamp: make(map[uint64]uint64),
blocksHashesByNumber: make(map[uint64][32]byte),
pastDepositRevealedEvents: make(map[[32]byte][]*DepositRevealedEvent),
depositSweepProposalValidations: make(map[[32]byte]bool),
pendingRedemptionRequests: make(map[[32]byte]*RedemptionRequest),
Expand Down
Loading