-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
## Description - Helps to decrease the number of calls from price-feeder to node chain --- ### Author Checklist _All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues._ I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] added appropriate labels to the PR - [ ] targeted the correct branch (see [PR Targeting](https://github.com/umee-network/umee/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist _All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items._ I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) (cherry picked from commit 6430840) Co-authored-by: Rafael Tenfen <rafaeltenfen.rt@gmail.com>
- Loading branch information
1 parent
90b8ee3
commit cf4ea5c
Showing
4 changed files
with
129 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"sync" | ||
|
||
"github.com/rs/zerolog" | ||
tmrpcclient "github.com/tendermint/tendermint/rpc/client" | ||
tmctypes "github.com/tendermint/tendermint/rpc/core/types" | ||
tmtypes "github.com/tendermint/tendermint/types" | ||
) | ||
|
||
var ( | ||
errParseEventDataNewBlockHeader = errors.New("error parsing EventDataNewBlockHeader") | ||
queryEventNewBlockHeader = tmtypes.QueryForEvent(tmtypes.EventNewBlockHeader) | ||
) | ||
|
||
// ChainHeight is used to cache the chain height of the | ||
// current node which is being updated each time the | ||
// node sends an event of EventNewBlockHeader. | ||
// It starts a goroutine to subscribe to blockchain new block event and update the cached height. | ||
type ChainHeight struct { | ||
Logger zerolog.Logger | ||
|
||
mtx sync.RWMutex | ||
errGetChainHeight error | ||
lastChainHeight int64 | ||
} | ||
|
||
// NewChainHeight returns a new ChainHeight struct that | ||
// starts a new goroutine subscribed to EventNewBlockHeader. | ||
func NewChainHeight( | ||
ctx context.Context, | ||
rpcClient tmrpcclient.Client, | ||
logger zerolog.Logger, | ||
) (*ChainHeight, error) { | ||
if !rpcClient.IsRunning() { | ||
if err := rpcClient.Start(); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
newBlockHeaderSubscription, err := rpcClient.Subscribe( | ||
ctx, tmtypes.EventNewBlockHeader, queryEventNewBlockHeader.String()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
chainHeight := &ChainHeight{ | ||
Logger: logger.With().Str("oracle_client", "chain_height").Logger(), | ||
errGetChainHeight: nil, | ||
lastChainHeight: 0, | ||
} | ||
|
||
go chainHeight.subscribe(ctx, rpcClient, newBlockHeaderSubscription) | ||
|
||
return chainHeight, nil | ||
} | ||
|
||
// updateChainHeight receives the data to be updated thread safe. | ||
func (chainHeight *ChainHeight) updateChainHeight(blockHeight int64, err error) { | ||
chainHeight.mtx.Lock() | ||
defer chainHeight.mtx.Unlock() | ||
|
||
chainHeight.lastChainHeight = blockHeight | ||
chainHeight.errGetChainHeight = err | ||
} | ||
|
||
// subscribe listens to new blocks being made | ||
// and updates the chain height. | ||
func (chainHeight *ChainHeight) subscribe( | ||
ctx context.Context, | ||
eventsClient tmrpcclient.EventsClient, | ||
newBlockHeaderSubscription <-chan tmctypes.ResultEvent, | ||
) { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
err := eventsClient.Unsubscribe(ctx, tmtypes.EventNewBlockHeader, queryEventNewBlockHeader.String()) | ||
if err != nil { | ||
chainHeight.Logger.Err(err) | ||
chainHeight.updateChainHeight(chainHeight.lastChainHeight, err) | ||
} | ||
chainHeight.Logger.Info().Msg("closing the ChainHeight subscription") | ||
return | ||
|
||
case resultEvent := <-newBlockHeaderSubscription: | ||
eventDataNewBlockHeader, ok := resultEvent.Data.(tmtypes.EventDataNewBlockHeader) | ||
if !ok { | ||
chainHeight.Logger.Err(errParseEventDataNewBlockHeader) | ||
chainHeight.updateChainHeight(chainHeight.lastChainHeight, errParseEventDataNewBlockHeader) | ||
continue | ||
} | ||
chainHeight.updateChainHeight(eventDataNewBlockHeader.Header.Height, nil) | ||
} | ||
} | ||
} | ||
|
||
// GetChainHeight returns the last chain height available. | ||
func (chainHeight *ChainHeight) GetChainHeight() (int64, error) { | ||
chainHeight.mtx.RLock() | ||
defer chainHeight.mtx.RUnlock() | ||
|
||
return chainHeight.lastChainHeight, chainHeight.errGetChainHeight | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters