Skip to content

Commit

Permalink
rename InitialValidFromBlockNumber to InitialBlockNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
samsondav committed Jun 7, 2023
1 parent 347629d commit e1edcaa
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 23 deletions.
10 changes: 7 additions & 3 deletions core/services/ocr2/plugins/mercury/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ import (
)

type PluginConfig struct {
RawServerURL string `json:"serverURL" toml:"serverURL"`
ServerPubKey utils.PlainHexBytes `json:"serverPubKey" toml:"serverPubKey"`
InitialValidFromBlockNumber int64 `json:"initialValidFromBlockNumber" toml:"initialValidFromBlockNumber"`
RawServerURL string `json:"serverURL" toml:"serverURL"`
ServerPubKey utils.PlainHexBytes `json:"serverPubKey" toml:"serverPubKey"`
// InitialBlockNumber allows to set a custom "validFromBlockNumber" for
// the first ever report in the case of a brand new feed, where the mercury
// server does not have any previous reports. For a brand new feed, this
// effectively sets the "first" validFromBlockNumber.
InitialBlockNumber int64 `json:"initialBlockNumber" toml:"initialBlockNumber"`
}

func ValidatePluginConfig(config PluginConfig) (merr error) {
Expand Down
16 changes: 8 additions & 8 deletions core/services/ocr2/plugins/mercury/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestIntegration_Mercury(t *testing.T) {
const n = 4 // number of nodes
const fromBlock = 1 // cannot use zero, start from block 1
const multiplier = 100000000
initialValidFromBlockNumber := int64(rand.Int31n(10))
initialBlockNumber := int64(rand.Int31n(10))
testStartTimeStamp := uint32(time.Now().Unix())

// test vars
Expand Down Expand Up @@ -121,8 +121,8 @@ func TestIntegration_Mercury(t *testing.T) {
genesisData := core.GenesisAlloc{steve.From: {Balance: assets.Ether(1000).ToInt()}}
backend := cltest.NewSimulatedBackend(t, genesisData, uint32(ethconfig.Defaults.Miner.GasCeil))
backend.Commit() // ensure starting block number at least 1
// Ensure initialValidFromBlockNumber is at or below current block number
for i := 1; i < int(initialValidFromBlockNumber); i++ {
// Ensure initialBlockNumber is at or below current block number
for i := 1; i < int(initialBlockNumber); i++ {
backend.Commit()
}
stopMining := cltest.Mine(backend, 1*time.Second) // Should be greater than deltaRound since we cannot access old blocks on simulated blockchain
Expand Down Expand Up @@ -227,7 +227,7 @@ func TestIntegration_Mercury(t *testing.T) {
feed.id,
chainID,
fromBlock,
initialValidFromBlockNumber,
initialBlockNumber,
)
}
}
Expand Down Expand Up @@ -342,7 +342,7 @@ func TestIntegration_Mercury(t *testing.T) {
assert.GreaterOrEqual(t, currentBlock.Time(), reportElems["currentBlockTimestamp"].(uint64))
assert.NotEqual(t, common.Hash{}, common.Hash(reportElems["currentBlockHash"].([32]uint8)))
assert.LessOrEqual(t, int(reportElems["validFromBlockNum"].(uint64)), int(reportElems["currentBlockNum"].(uint64)))
assert.LessOrEqual(t, initialValidFromBlockNumber, int64(reportElems["validFromBlockNum"].(uint64)))
assert.LessOrEqual(t, initialBlockNumber, int64(reportElems["validFromBlockNum"].(uint64)))

t.Logf("oracle %x reported for feed %s (0x%x)", req.pk, feed.name, feed.id)

Expand Down Expand Up @@ -605,7 +605,7 @@ func addMercuryJob(
feedID [32]byte,
chainID *big.Int,
fromBlock int,
initialValidFromBlockNumber int64,
initialBlockNumber int64,
) {
node.AddJob(t, fmt.Sprintf(`
type = "offchainreporting2"
Expand Down Expand Up @@ -649,7 +649,7 @@ observationSource = """
[pluginConfig]
serverURL = "%[8]s"
serverPubKey = "%[9]x"
initialValidFromBlockNumber = %[15]d
initialBlockNumber = %[15]d
[relayConfig]
chainID = %[12]d
Expand All @@ -669,6 +669,6 @@ fromBlock = %[13]d
chainID,
fromBlock,
feedName,
initialValidFromBlockNumber,
initialBlockNumber,
))
}
2 changes: 1 addition & 1 deletion core/services/relay/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (r *Relayer) NewMercuryProvider(rargs relaytypes.RelayArgs, pargs relaytype
if err != nil {
return nil, err
}
transmitter := mercury.NewTransmitter(r.lggr, configWatcher.ContractConfigTracker(), client, privKey.PublicKey, *relayConfig.FeedID, mercuryConfig.InitialValidFromBlockNumber)
transmitter := mercury.NewTransmitter(r.lggr, configWatcher.ContractConfigTracker(), client, privKey.PublicKey, *relayConfig.FeedID, mercuryConfig.InitialBlockNumber)

return NewMercuryProvider(configWatcher, transmitter, reportCodec, r.lggr), nil
}
Expand Down
18 changes: 9 additions & 9 deletions core/services/relay/evm/mercury/transmitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ var _ Transmitter = &mercuryTransmitter{}

type mercuryTransmitter struct {
utils.StartStopOnce
lggr logger.Logger
rpcClient wsrpc.Client
cfgTracker ConfigTracker
initialValidFromBlockNumber int64
lggr logger.Logger
rpcClient wsrpc.Client
cfgTracker ConfigTracker
initialBlockNumber int64

feedID [32]byte
fromAccount string
Expand Down Expand Up @@ -78,13 +78,13 @@ func getPayloadTypes() abi.Arguments {
})
}

func NewTransmitter(lggr logger.Logger, cfgTracker ConfigTracker, rpcClient wsrpc.Client, fromAccount ed25519.PublicKey, feedID [32]byte, initialValidFromBlockNumber int64) *mercuryTransmitter {
func NewTransmitter(lggr logger.Logger, cfgTracker ConfigTracker, rpcClient wsrpc.Client, fromAccount ed25519.PublicKey, feedID [32]byte, initialBlockNumber int64) *mercuryTransmitter {
return &mercuryTransmitter{
utils.StartStopOnce{},
lggr.Named("MercuryTransmitter").With("feedID", fmt.Sprintf("%x", feedID[:])),
rpcClient,
cfgTracker,
initialValidFromBlockNumber,
initialBlockNumber,
feedID,
fmt.Sprintf("%x", fromAccount),
make(chan (struct{})),
Expand Down Expand Up @@ -259,14 +259,14 @@ func (mt *mercuryTransmitter) FetchInitialMaxFinalizedBlockNumber(ctx context.Co
return 0, err
}
if resp.Report == nil {
maxFinalizedBlockNumber := mt.initialValidFromBlockNumber - 1
mt.lggr.Infof("FetchInitialMaxFinalizedBlockNumber returned empty LatestReport; this is a new feed so maxFinalizedBlockNumber=%d (initialValidFromBlockNumber=%d)", maxFinalizedBlockNumber, mt.initialValidFromBlockNumber)
maxFinalizedBlockNumber := mt.initialBlockNumber - 1
mt.lggr.Infof("FetchInitialMaxFinalizedBlockNumber returned empty LatestReport; this is a new feed so maxFinalizedBlockNumber=%d (initialBlockNumber=%d)", maxFinalizedBlockNumber, mt.initialBlockNumber)
// NOTE: It's important to return -1 if the server is missing any past
// report (brand new feed) since we will add 1 to the
// maxFinalizedBlockNumber to get the first validFromBlockNum, which
// ought to be zero.
//
// If "initialValidFromBlockNumber" is unset, this will give a starting block of zero.
// If "initialBlockNumber" is unset, this will give a starting block of zero.
return maxFinalizedBlockNumber, nil
} else if !bytes.Equal(resp.Report.FeedId, mt.feedID[:]) {
return 0, fmt.Errorf("FetchInitialMaxFinalizedBlockNumber failed; mismatched feed IDs, expected: 0x%x, got: 0x%x", mt.feedID, resp.Report.FeedId)
Expand Down
4 changes: 2 additions & 2 deletions core/services/relay/evm/mercury/transmitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func Test_MercuryTransmitter_FetchInitialMaxFinalizedBlockNumber(t *testing.T) {
assert.Equal(t, 42, int(bn))
})
t.Run("successful query returning nil report (new feed)", func(t *testing.T) {
t.Run("when initialValidFromBlockNumber is unset (0)", func(t *testing.T) {
t.Run("when initialBlockNumber is unset (0)", func(t *testing.T) {
c := MockWSRPCClient{
latestReport: func(ctx context.Context, in *pb.LatestReportRequest) (out *pb.LatestReportResponse, err error) {
out = new(pb.LatestReportResponse)
Expand All @@ -107,7 +107,7 @@ func Test_MercuryTransmitter_FetchInitialMaxFinalizedBlockNumber(t *testing.T) {

assert.Equal(t, -1, int(bn))
})
t.Run("when initialValidFromBlockNumber is set to some non-zero value", func(t *testing.T) {
t.Run("when initialBlockNumber is set to some non-zero value", func(t *testing.T) {
c := MockWSRPCClient{
latestReport: func(ctx context.Context, in *pb.LatestReportRequest) (out *pb.LatestReportResponse, err error) {
out = new(pb.LatestReportResponse)
Expand Down

0 comments on commit e1edcaa

Please sign in to comment.