Skip to content

Commit

Permalink
chore: code cleanup and refactoring (#82)
Browse files Browse the repository at this point in the history
* refactor(timer): redundant logic for block queues and query height

* fix: select statement blocking

* fix: Mintscan URL
  • Loading branch information
joelsmith-2019 authored Apr 16, 2024
1 parent f81b17c commit 9f4cf29
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 38 deletions.
2 changes: 1 addition & 1 deletion integration/deployed_relayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func TestNobleBurnToEthDeployed(t *testing.T) {

rpcResponse, err := cc.RPCClient.BroadcastTxSync(context.Background(), txBytes)
require.Nil(t, err)
fmt.Printf("Deposit for Burn broadcasted: https://testnet.mintscan.io/noble-testnet/txs/%s\n", rpcResponse.Hash.String())
fmt.Printf("Deposit for Burn broadcasted: https://mintscan.io/noble-testnet/txs/%s\n", rpcResponse.Hash.String())

fmt.Println("Waiting for circle to approve and destination wallet to receive funds...")
var newEthBalance uint64
Expand Down
68 changes: 31 additions & 37 deletions noble/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,25 @@ func (n *Noble) StartListener(

// listen for new blocks
go func() {
first := make(chan struct{}, 1)
first <- struct{}{}
// inner function to queue blocks
queueBlocks := func() {
chainTip = n.LatestBlock()
if chainTip >= currentBlock {
for i := currentBlock; i <= chainTip; i++ {
blockQueue <- i
}
currentBlock = chainTip + 1
}
}

// initial queue
queueBlocks()

for {
timer := time.NewTimer(6 * time.Second)
select {
case <-first:
timer.Stop()
chainTip = n.LatestBlock()
if chainTip >= currentBlock {
for i := currentBlock; i <= chainTip; i++ {
blockQueue <- i
}
currentBlock = chainTip + 1
}
case <-timer.C:
chainTip = n.LatestBlock()
if chainTip >= currentBlock {
for i := currentBlock; i <= chainTip; i++ {
blockQueue <- i
}
currentBlock = chainTip + 1
}
queueBlocks()
case <-ctx.Done():
timer.Stop()
return
Expand All @@ -92,8 +89,7 @@ func (n *Noble) StartListener(
select {
case <-ctx.Done():
return
default:
block := <-blockQueue
case block := <-blockQueue:
res, err := n.cc.RPCClient.TxSearch(ctx, fmt.Sprintf("tx.height=%d", block), false, nil, nil, "")
if err != nil || res == nil {
logger.Debug(fmt.Sprintf("Unable to query Noble block %d. Will retry.", block), "error:", err)
Expand Down Expand Up @@ -177,30 +173,28 @@ func (n *Noble) TrackLatestBlockHeight(ctx context.Context, logger log.Logger, m

d := fmt.Sprint(n.Domain())

// first time
res, err := n.cc.RPCClient.Status(ctx)
if err != nil {
logger.Error("Unable to query Nobles latest height", "err", err)
}
n.SetLatestBlock(uint64(res.SyncInfo.LatestBlockHeight))
if m != nil {
m.SetLatestHeight(n.Name(), d, res.SyncInfo.LatestBlockHeight)
// inner function to update block height
updateBlockHeight := func() {
res, err := n.cc.RPCClient.Status(ctx)
if err != nil {
logger.Error("Unable to query Nobles latest height", "err", err)
} else {
n.SetLatestBlock(uint64(res.SyncInfo.LatestBlockHeight))
if m != nil {
m.SetLatestHeight(n.Name(), d, res.SyncInfo.LatestBlockHeight)
}
}
}

// initial call
updateBlockHeight()

// then start loop on a timer
for {
timer := time.NewTimer(6 * time.Second)
select {
case <-timer.C:
res, err := n.cc.RPCClient.Status(ctx)
if err != nil {
logger.Error("Unable to query Nobles latest height", "err", err)
continue
}
n.SetLatestBlock(uint64(res.SyncInfo.LatestBlockHeight))
if m != nil {
m.SetLatestHeight(n.Name(), d, res.SyncInfo.LatestBlockHeight)
}
updateBlockHeight()
case <-ctx.Done():
timer.Stop()
return
Expand Down

0 comments on commit 9f4cf29

Please sign in to comment.