-
Notifications
You must be signed in to change notification settings - Fork 0
Enable hash based stellar consensus for latestledger #673
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a345bf1
Add stellar block provider
ilija42 2c452b4
Init block provider
ilija42 ee3fa79
Add consensus to GetLatestLedger
ilija42 e9dd1e7
lint
ilija42 c7cdd0c
Move stellar height provider into its own package
ilija42 fb00d83
Add ledger provider tests and mocks
ilija42 fa3fec0
tidy
ilija42 9643cf9
add a TODO
ilija42 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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
93 changes: 93 additions & 0 deletions
93
chain_capabilities/stellar/height/mocks/ledger_provider.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,104 @@ | ||
| package height | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/smartcontractkit/chainlink-common/pkg/logger" | ||
| "github.com/smartcontractkit/chainlink-common/pkg/services" | ||
| stellartypes "github.com/smartcontractkit/chainlink-common/pkg/types/chains/stellar" | ||
| ) | ||
|
|
||
| // LedgerProvider is the subset of the Stellar chain service used to poll the latest closed ledger sequence. | ||
| type LedgerProvider interface { | ||
| GetLatestLedger(ctx context.Context) (stellartypes.GetLatestLedgerResponse, error) | ||
| } | ||
|
|
||
| // Provider is a service that polls the latest closed Stellar ledger sequence. | ||
| type Provider struct { | ||
| services.Service | ||
| engine *services.Engine | ||
|
|
||
| lggr logger.SugaredLogger | ||
| pollPeriod time.Duration | ||
| service LedgerProvider | ||
|
|
||
| mutex sync.RWMutex | ||
| sequence int64 | ||
| } | ||
|
|
||
| // NewProvider builds a Stellar height provider | ||
| func NewProvider(lggr logger.Logger, pollPeriod time.Duration, service LedgerProvider) (*Provider, error) { | ||
| if pollPeriod <= 0 { | ||
| return nil, fmt.Errorf("height provider poll period must be positive, got %s", pollPeriod) | ||
| } | ||
| if service == nil { | ||
| return nil, fmt.Errorf("height provider requires a non-nil ledger service") | ||
| } | ||
| b := &Provider{ | ||
| pollPeriod: pollPeriod, | ||
| service: service, | ||
| } | ||
| b.Service, b.engine = services.Config{ | ||
| Name: "StellarHeightProvider", | ||
| Start: b.start, | ||
| Close: b.close, | ||
| }.NewServiceEngine(lggr) | ||
| b.lggr = b.engine.SugaredLogger | ||
| return b, nil | ||
| } | ||
|
|
||
| func (b *Provider) start(_ context.Context) error { | ||
| b.engine.Go(b.poll) | ||
| return nil | ||
| } | ||
|
|
||
| func (b *Provider) close() error { return nil } | ||
|
|
||
| func (b *Provider) poll(ctx context.Context) { | ||
| b.pollLedger(ctx) | ||
| ticker := time.NewTicker(b.pollPeriod) | ||
| defer ticker.Stop() | ||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return | ||
| case <-ticker.C: | ||
| b.pollLedger(ctx) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (b *Provider) pollLedger(ctx context.Context) { | ||
| resp, err := b.service.GetLatestLedger(ctx) | ||
| if err != nil { | ||
| b.lggr.Errorw("failed to poll latest ledger", "error", err) | ||
| return | ||
| } | ||
| b.mutex.Lock() | ||
| defer b.mutex.Unlock() | ||
| // Ledger sequence is monotonically increasing; retain the max so a lagging RPC | ||
| // can't lower the agreed height across rounds. | ||
| if seq := int64(resp.Sequence); seq > b.sequence { | ||
| b.sequence = seq | ||
| } | ||
| } | ||
|
|
||
| // GetLatest returns the latest observed ledger sequence. | ||
| func (b *Provider) GetLatest() int64 { return b.get() } | ||
|
|
||
| // GetSafe returns the safe ledger sequence. Stellar ledgers are final on close, so | ||
| // this equals GetLatest. | ||
| func (b *Provider) GetSafe() int64 { return b.get() } | ||
|
|
||
| // GetFinalized returns the finalized ledger sequence. Stellar ledgers are final on | ||
| // close, so this equals GetLatest. | ||
| func (b *Provider) GetFinalized() int64 { return b.get() } | ||
|
|
||
| func (b *Provider) get() int64 { | ||
| b.mutex.RLock() | ||
| defer b.mutex.RUnlock() | ||
| return b.sequence | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.