-
Notifications
You must be signed in to change notification settings - Fork 28
Add Stellar Service plumbing and GetLedgerEntries,GetLatestLedger #1990
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
Open
ilija42
wants to merge
6
commits into
main
Choose a base branch
from
add-stellar-service
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b52b8c1
Add Stellar Service plumbing and GetLedgerEntries,GetLatestLedger
ilija42 39d2e77
Add err handling in proto helper
ilija42 b29dd60
Add Stellar service mocks
ilija42 f8b2942
lint
ilija42 6f5ad9c
Merge branch 'main' into add-stellar-service
ilija42 a220ef4
Merge branch 'main' into add-stellar-service
yashnevatia 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| //go:generate go run ./generate | ||
| package stellar |
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,11 @@ | ||
| package main | ||
|
|
||
| import "github.com/smartcontractkit/chainlink-protos/cre/go/installer/pkg" | ||
|
|
||
| func main() { | ||
| gen := &pkg.ProtocGen{Plugins: []pkg.Plugin{pkg.GoPlugin, {Name: "go-grpc"}}} | ||
| gen.AddSourceDirectories("../..", ".") | ||
| if err := gen.GenerateFile("stellar.proto", "."); err != nil { | ||
| panic(err) | ||
| } | ||
| } |
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,205 @@ | ||
| package stellar | ||
|
|
||
| import ( | ||
| "encoding/base64" | ||
| "encoding/hex" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/smartcontractkit/chainlink-common/pkg/types/chains/stellar" | ||
| ) | ||
|
|
||
| // xdrToBytes base64-decodes a domain XDR string to raw binary. | ||
| func xdrToBytes(x stellar.XDR) ([]byte, error) { | ||
| b, err := base64.StdEncoding.DecodeString(string(x)) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("invalid base64 XDR %q: %w", x, err) | ||
| } | ||
| return b, nil | ||
| } | ||
|
|
||
| // bytesToXDR base64-encodes raw binary XDR to the domain type. | ||
| func bytesToXDR(b []byte) stellar.XDR { | ||
| return stellar.XDR(base64.StdEncoding.EncodeToString(b)) | ||
| } | ||
|
|
||
| // hashToBytes hex-decodes a domain hash string to raw bytes. | ||
| func hashToBytes(h string) ([]byte, error) { | ||
| b, err := hex.DecodeString(h) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("invalid hex hash %q: %w", h, err) | ||
| } | ||
| return b, nil | ||
| } | ||
|
|
||
| // bytesToHash hex-encodes raw hash bytes to a string. | ||
| func bytesToHash(b []byte) string { | ||
| return hex.EncodeToString(b) | ||
| } | ||
|
|
||
| // ---- GetLedgerEntries ---- | ||
|
|
||
| // ConvertGetLedgerEntriesRequestToProto converts a domain GetLedgerEntriesRequest to its proto representation. | ||
| func ConvertGetLedgerEntriesRequestToProto(req stellar.GetLedgerEntriesRequest) (*GetLedgerEntriesRequest, error) { | ||
| keys := make([][]byte, len(req.Keys)) | ||
| var errs []error | ||
| for i, k := range req.Keys { | ||
| b, err := xdrToBytes(k) | ||
| if err != nil { | ||
| errs = append(errs, fmt.Errorf("key[%d]: %w", i, err)) | ||
| continue | ||
| } | ||
| keys[i] = b | ||
| } | ||
| if len(errs) > 0 { | ||
| return nil, errors.Join(errs...) | ||
| } | ||
| return &GetLedgerEntriesRequest{Keys: keys}, nil | ||
| } | ||
|
|
||
| // ConvertGetLedgerEntriesRequestFromProto converts a proto GetLedgerEntriesRequest to the domain type. | ||
| func ConvertGetLedgerEntriesRequestFromProto(p *GetLedgerEntriesRequest) (stellar.GetLedgerEntriesRequest, error) { | ||
| if p == nil { | ||
| return stellar.GetLedgerEntriesRequest{}, fmt.Errorf("get ledger entries request is nil") | ||
| } | ||
| if len(p.GetKeys()) == 0 { | ||
| return stellar.GetLedgerEntriesRequest{}, fmt.Errorf("ledger entry keys are empty") | ||
| } | ||
| rawKeys := p.GetKeys() | ||
| keys := make([]stellar.XDR, len(rawKeys)) | ||
| for i, k := range rawKeys { | ||
| keys[i] = bytesToXDR(k) | ||
| } | ||
| return stellar.GetLedgerEntriesRequest{Keys: keys}, nil | ||
| } | ||
|
|
||
| // ConvertLedgerEntryResultToProto converts a domain LedgerEntryResult to its proto representation. | ||
| func ConvertLedgerEntryResultToProto(r stellar.LedgerEntryResult) (*LedgerEntryResult, error) { | ||
| keyXDR, err := xdrToBytes(r.KeyXDR) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("key_xdr: %w", err) | ||
| } | ||
| dataXDR, err := xdrToBytes(r.DataXDR) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("data_xdr: %w", err) | ||
| } | ||
| extXDR, err := xdrToBytes(r.ExtensionXDR) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("extension_xdr: %w", err) | ||
| } | ||
| pr := &LedgerEntryResult{ | ||
| KeyXdr: keyXDR, | ||
| DataXdr: dataXDR, | ||
| LastModifiedLedger: r.LastModifiedLedger, | ||
| ExtensionXdr: extXDR, | ||
| } | ||
| if r.LiveUntilLedgerSeq != nil { | ||
| pr.HasLiveUntilLedgerSeq = true | ||
| pr.LiveUntilLedgerSeq = *r.LiveUntilLedgerSeq | ||
| } | ||
| return pr, nil | ||
| } | ||
|
|
||
| // ConvertLedgerEntryResultFromProto converts a proto LedgerEntryResult to the domain type. | ||
| func ConvertLedgerEntryResultFromProto(p *LedgerEntryResult) (stellar.LedgerEntryResult, error) { | ||
| if p == nil { | ||
| return stellar.LedgerEntryResult{}, fmt.Errorf("ledger entry result is nil") | ||
| } | ||
| r := stellar.LedgerEntryResult{ | ||
| KeyXDR: bytesToXDR(p.GetKeyXdr()), | ||
| DataXDR: bytesToXDR(p.GetDataXdr()), | ||
| LastModifiedLedger: p.GetLastModifiedLedger(), | ||
| ExtensionXDR: bytesToXDR(p.GetExtensionXdr()), | ||
| } | ||
| if p.GetHasLiveUntilLedgerSeq() { | ||
| v := p.GetLiveUntilLedgerSeq() | ||
| r.LiveUntilLedgerSeq = &v | ||
| } | ||
| return r, nil | ||
| } | ||
|
|
||
| // ConvertGetLedgerEntriesResponseToProto converts a domain GetLedgerEntriesResponse to its proto representation. | ||
| func ConvertGetLedgerEntriesResponseToProto(resp stellar.GetLedgerEntriesResponse) (*GetLedgerEntriesResponse, error) { | ||
| entries := make([]*LedgerEntryResult, 0, len(resp.Entries)) | ||
| var errs []error | ||
| for i, e := range resp.Entries { | ||
| protoEntry, err := ConvertLedgerEntryResultToProto(e) | ||
| if err != nil { | ||
| errs = append(errs, fmt.Errorf("entry[%d]: %w", i, err)) | ||
| continue | ||
| } | ||
| entries = append(entries, protoEntry) | ||
| } | ||
| if len(errs) > 0 { | ||
| return nil, errors.Join(errs...) | ||
| } | ||
| return &GetLedgerEntriesResponse{ | ||
| Entries: entries, | ||
| LatestLedger: resp.LatestLedger, | ||
| }, nil | ||
| } | ||
|
|
||
| // ConvertGetLedgerEntriesResponseFromProto converts a proto GetLedgerEntriesResponse to the domain type. | ||
| func ConvertGetLedgerEntriesResponseFromProto(p *GetLedgerEntriesResponse) (stellar.GetLedgerEntriesResponse, error) { | ||
| if p == nil { | ||
| return stellar.GetLedgerEntriesResponse{}, fmt.Errorf("get ledger entries response is nil") | ||
| } | ||
| entries := make([]stellar.LedgerEntryResult, 0, len(p.GetEntries())) | ||
| var errs []error | ||
| for i, pe := range p.GetEntries() { | ||
| e, err := ConvertLedgerEntryResultFromProto(pe) | ||
| if err != nil { | ||
| errs = append(errs, fmt.Errorf("entry[%d]: %w", i, err)) | ||
| continue | ||
| } | ||
| entries = append(entries, e) | ||
| } | ||
| if len(errs) > 0 { | ||
| return stellar.GetLedgerEntriesResponse{}, errors.Join(errs...) | ||
| } | ||
| return stellar.GetLedgerEntriesResponse{ | ||
| Entries: entries, | ||
| LatestLedger: p.GetLatestLedger(), | ||
| }, nil | ||
| } | ||
|
|
||
| // ---- GetLatestLedger ---- | ||
|
|
||
| // ConvertGetLatestLedgerResponseToProto converts a domain GetLatestLedgerResponse to its proto representation. | ||
| func ConvertGetLatestLedgerResponseToProto(resp stellar.GetLatestLedgerResponse) (*GetLatestLedgerResponse, error) { | ||
| hash, err := hashToBytes(string(resp.Hash)) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("hash: %w", err) | ||
| } | ||
| headerXDR, err := xdrToBytes(resp.LedgerHeaderXDR) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("ledger_header_xdr: %w", err) | ||
| } | ||
| metaXDR, err := xdrToBytes(resp.LedgerMetadataXDR) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("ledger_metadata_xdr: %w", err) | ||
| } | ||
| return &GetLatestLedgerResponse{ | ||
| Hash: hash, | ||
| ProtocolVersion: resp.ProtocolVersion, | ||
| Sequence: resp.Sequence, | ||
| LedgerCloseTime: resp.LedgerCloseTime, | ||
| LedgerHeaderXdr: headerXDR, | ||
| LedgerMetadataXdr: metaXDR, | ||
| }, nil | ||
| } | ||
|
|
||
| // ConvertGetLatestLedgerResponseFromProto converts a proto GetLatestLedgerResponse to the domain type. | ||
| func ConvertGetLatestLedgerResponseFromProto(p *GetLatestLedgerResponse) (stellar.GetLatestLedgerResponse, error) { | ||
| if p == nil { | ||
| return stellar.GetLatestLedgerResponse{}, fmt.Errorf("get latest ledger response is nil") | ||
| } | ||
| return stellar.GetLatestLedgerResponse{ | ||
| Hash: stellar.LedgerHash(bytesToHash(p.GetHash())), | ||
| ProtocolVersion: p.GetProtocolVersion(), | ||
| Sequence: p.GetSequence(), | ||
| LedgerCloseTime: p.GetLedgerCloseTime(), | ||
| LedgerHeaderXDR: bytesToXDR(p.GetLedgerHeaderXdr()), | ||
| LedgerMetadataXDR: bytesToXDR(p.GetLedgerMetadataXdr()), | ||
| }, nil | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be cleaner to define these in
package stellar? e.g.XDR) ToBytes() ([]byte, error)andstellar.ParseXDR([]byte)🤷