Repository navigation
core/services/ocr3_1/plugins/queue: ReportingPlugin implementation #24102
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
Changes from all commits
8f1afd8
a2b6326
8e88824
1867569
7482bef
21514f3
711d054
90768bd
b0d2926
0a0595c
51c55f9
e7bc3da
0fcadd7
60e3b4d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| package queue | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "google.golang.org/protobuf/encoding/protowire" | ||
| "google.golang.org/protobuf/proto" | ||
|
|
||
| "github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3_1types" | ||
|
|
||
| "github.com/smartcontractkit/chainlink-common/pkg/capabilities/pb" | ||
| ) | ||
|
|
||
| // blobCache is a short-lived cache for coordinating concurrent fetches of event blobs. | ||
| type blobCache struct { | ||
| fetcher ocr3_1types.BlobFetcher | ||
|
|
||
| mu sync.RWMutex | ||
| cache map[string]func() ([]byte, error) // blob handle -> blob | ||
| } | ||
|
|
||
| func (c *blobCache) fetchBlob(ctx context.Context, handle []byte) ([]byte, error) { | ||
| c.mu.RLock() | ||
| if fn, ok := c.cache[string(handle)]; ok { | ||
| c.mu.RUnlock() | ||
| return fn() | ||
| } | ||
| c.mu.RUnlock() | ||
|
|
||
| c.mu.Lock() | ||
| if fn, ok := c.cache[string(handle)]; ok { | ||
| c.mu.Unlock() | ||
| return fn() | ||
| } | ||
| fn := sync.OnceValues(func() ([]byte, error) { | ||
| var blobHandle ocr3_1types.BlobHandle | ||
| if err := blobHandle.UnmarshalBinary(handle); err != nil { | ||
| return nil, fmt.Errorf("failed to unmarshal blob handle: %w", err) | ||
| } | ||
| return c.fetcher.FetchBlob(ctx, blobHandle) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will cache all errors. Sometimes FetchBlob will fail because of latency or network issues which we'd want to retry the next time instead of immediately returning the cached error
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that is the case for these particular usages. We just have short bursts of spawn/sink parallel execution that is simpler to self-organize around synchronization rather than do one fetch of each blob up front before giving out access. i.e. there is no next time in this cases - we just fetch once and then discard it. Unless you are saying we should retry within the same round? |
||
| }) | ||
| c.cache[string(handle)] = fn | ||
| c.mu.Unlock() | ||
| return fn() | ||
| } | ||
|
|
||
| // fetchCoordinatedEventFromBlob fetches and extracts the idx-th event from the blob, without unmarshaling any other events. | ||
| func fetchCoordinatedEventFromBlob(ctx context.Context, fetcher *blobCache, handle []byte, idx int) (*pb.CoordinatedEvent, []byte, error) { | ||
| bb, err := fetcher.fetchBlob(ctx, handle) | ||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("failed to fetch blob: %w", err) | ||
| } | ||
|
|
||
| coordEvent, eb, err := extractEvent(bb, idx) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| return coordEvent, eb, nil | ||
| } | ||
|
|
||
| const coordinatedEventsFieldNumber = 1 // pb.CoordinatedEventList.Events | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it better to use something like this to get the field number? ageFieldNumber := (&person.Person{}).
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a test set up to ensure it doesn't drift. I suppose we could derive it directly, though we have to give up having a |
||
|
|
||
| // extractEvent extracts the idx-th event from the blob, without unmarshaling any other events. | ||
| func extractEvent(b []byte, idx int) (*pb.CoordinatedEvent, []byte, error) { | ||
| i := 0 | ||
| for len(b) > 0 { | ||
| num, typ, n := protowire.ConsumeTag(b) | ||
| if n < 0 { | ||
| return nil, nil, protowire.ParseError(n) | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whitespace after the closing } (when it's not followed by another }) would make this a lot easier to read. This issues is in many functions in the PR |
||
| b = b[n:] | ||
| if num == coordinatedEventsFieldNumber { | ||
|
jmank88 marked this conversation as resolved.
Resolved
|
||
| if typ != protowire.BytesType { | ||
| return nil, nil, fmt.Errorf("expected bytes type for field %d, got %d", coordinatedEventsFieldNumber, typ) | ||
| } | ||
| v, m := protowire.ConsumeBytes(b) | ||
| if m < 0 { | ||
| return nil, nil, protowire.ParseError(m) | ||
| } | ||
| if i == idx { | ||
| var event pb.CoordinatedEvent | ||
| if err := proto.Unmarshal(v, &event); err != nil { | ||
| return nil, v, fmt.Errorf("failed to unmarshal event: %w", err) | ||
| } | ||
| return &event, v, nil | ||
| } | ||
| // advance | ||
| i++ | ||
| b = b[m:] | ||
| continue | ||
| } else { | ||
| n := protowire.ConsumeFieldValue(num, typ, b) | ||
| if n < 0 { | ||
| return nil, nil, protowire.ParseError(n) | ||
| } | ||
| b = b[n:] | ||
| } | ||
| } | ||
| return nil, nil, fmt.Errorf("index %d out of range", idx) | ||
| } | ||
|
|
||
| // broadcastAsBlob broadcasts a blob with as many coordinated events as possible, returning the blob handle and count of events included. | ||
| func broadcastAsBlob(ctx context.Context, broadcaster ocr3_1types.BlobBroadcastFetcher, events []*pb.CoordinatedEvent, | ||
| seqNr uint64, estimatedRoundInterval time.Duration) (blobHandle []byte, n int, err error) { | ||
| protoOpts := proto.MarshalOptions{Deterministic: true} | ||
| for ; protoOpts.Size(&pb.CoordinatedEventList{Events: events}) > maxBlobPayloadBytes; events = events[:len(events)-1] { | ||
| //TODO opt: binary search for largest fit instead.... | ||
| } | ||
| n = len(events) | ||
| var maxDeadline time.Time | ||
| for _, event := range events { | ||
| if t := event.Deadline.AsTime(); t.After(maxDeadline) { | ||
| maxDeadline = t | ||
| } | ||
| } | ||
| roundDur := estimatedRoundInterval //TODO OK? pad? | ||
| //TODO pad extra time and/or rounds? | ||
| expSeqNr := seqNr + uint64(time.Until(maxDeadline)/roundDur) | ||
|
|
||
| data, err := protoOpts.Marshal(&pb.CoordinatedEventList{Events: events}) | ||
| if err != nil { | ||
| return nil, -1, fmt.Errorf("failed to marshal blob: %w", err) | ||
| } | ||
| bh, err := broadcaster.BroadcastBlob(ctx, data, ocr3_1types.BlobExpirationHintSequenceNumber{SeqNr: expSeqNr}) | ||
| if err != nil { | ||
| return nil, -1, fmt.Errorf("failed to broadcast blob: %w", err) | ||
| } | ||
| blobHandle, err = bh.MarshalBinary() | ||
| return | ||
| } | ||
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.
Doesn't the BlobFetcher already keep everything locally once it's fetched?
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.
I tried to look for an implementation to reassure me that it would be cached efficiently, but IIUC we would at least be paying extra synchronization and allocation overhead.
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.
libocr caches on their end but you'd still be paying the network cost to fetch the blob from cache so the cache here is helpful