-
Notifications
You must be signed in to change notification settings - Fork 1
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
median: prefer adapting ChainReader to MedianContract #7
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ package median | |
|
||
import ( | ||
"context" | ||
"math/big" | ||
"time" | ||
|
||
"github.com/smartcontractkit/libocr/offchainreporting2/reportingplugin/median" | ||
ocrtypes "github.com/smartcontractkit/libocr/offchainreporting2plus/types" | ||
|
@@ -26,7 +28,6 @@ func (p *Plugin) NewMedianFactory(ctx context.Context, provider types.MedianProv | |
ctxVals.SetValues(ctx) | ||
lggr := logger.With(p.Logger, ctxVals.Args()...) | ||
factory := median.NumericalMedianFactory{ | ||
ContractTransmitter: provider.MedianContract(), | ||
DataSource: dataSource, | ||
JuelsPerFeeCoinDataSource: juelsPerFeeCoin, | ||
Logger: logger.NewOCRWrapper(lggr, true, func(msg string) { | ||
|
@@ -39,6 +40,11 @@ func (p *Plugin) NewMedianFactory(ctx context.Context, provider types.MedianProv | |
OnchainConfigCodec: provider.OnchainConfigCodec(), | ||
ReportCodec: provider.ReportCodec(), | ||
} | ||
if cr := provider.ChainReader(); cr != nil { | ||
factory.ContractTransmitter = &chainReaderContract{cr, types.BoundContract{Name: "median"}} | ||
} else { | ||
factory.ContractTransmitter = provider.MedianContract() | ||
} | ||
s := &reportingPluginFactoryService{lggr: logger.Named(lggr, "ReportingPluginFactory"), ReportingPluginFactory: factory} | ||
|
||
p.SubService(s) | ||
|
@@ -65,3 +71,45 @@ func (r *reportingPluginFactoryService) Close() error { | |
func (r *reportingPluginFactoryService) HealthReport() map[string]error { | ||
return map[string]error{r.Name(): r.Healthy()} | ||
} | ||
|
||
// chainReaderContract adapts a [types.ChainReader] to [median.MedianContract]. | ||
type chainReaderContract struct { | ||
chainReader types.ChainReader | ||
contract types.BoundContract | ||
} | ||
|
||
type latestTransmissionDetailsResponse struct { | ||
ConfigDigest ocrtypes.ConfigDigest | ||
Epoch uint32 | ||
Round uint8 | ||
LatestAnswer *big.Int | ||
LatestTimestamp time.Time | ||
} | ||
|
||
type latestRoundRequested struct { | ||
ConfigDigest ocrtypes.ConfigDigest | ||
Epoch uint32 | ||
Round uint8 | ||
} | ||
|
||
func (c *chainReaderContract) LatestTransmissionDetails(ctx context.Context) (configDigest ocrtypes.ConfigDigest, epoch uint32, round uint8, latestAnswer *big.Int, latestTimestamp time.Time, err error) { | ||
var resp latestTransmissionDetailsResponse | ||
|
||
err = c.chainReader.GetLatestValue(ctx, c.contract, "LatestTransmissionDetails", nil, &resp) | ||
if err != nil { | ||
return | ||
} | ||
|
||
return resp.ConfigDigest, resp.Epoch, resp.Round, resp.LatestAnswer, resp.LatestTimestamp, nil | ||
} | ||
|
||
func (c *chainReaderContract) LatestRoundRequested(ctx context.Context, lookback time.Duration) (configDigest ocrtypes.ConfigDigest, epoch uint32, round uint8, err error) { | ||
var resp latestRoundRequested | ||
|
||
err = c.chainReader.GetLatestValue(ctx, c.contract, "LatestRoundReported", map[string]any{"lookback": lookback}, &resp) | ||
if err != nil { | ||
return | ||
} | ||
|
||
return resp.ConfigDigest, resp.Epoch, resp.Round, nil | ||
} | ||
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. Actually, come to think of it, if we're going to do it this way then the definition of 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. Why would chainlink-common need to import this? We only want to use it at the outermost client layer. If we used it on the server side, then we would still be using the GRPC services over the wire, and could not phase them out. 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. Yeah, nevermind I think I was confused about the interconnection of all of this. I was thinking the 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. Kind of yeah. I think it's more about the GRPC layer being a separate concern from chain reader being available or not. |
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.
lookback is an obsolete parameter unused anywhere at Chainlink. We can't support it with
GetLatestValue()
without changing the meaning of the method and breaking the chain agnostic nature of it in an awkward way. Only contract method params (or equivalently, log topics) should be passed in params field toGetLatestValue()