Skip to content

Commit

Permalink
clean up shadows, logs, typos etc (#10533)
Browse files Browse the repository at this point in the history
* clean up some shadows and logs

* fix typos and adhere to go convention

* fix tests
  • Loading branch information
FelixFan1992 authored Sep 8, 2023
1 parent 3dd2523 commit d13f179
Show file tree
Hide file tree
Showing 19 changed files with 46 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type triggerWrapper = automation_utils_2_1.KeeperRegistryBase21LogTrigger

var ErrABINotParsable = fmt.Errorf("error parsing abi")

// according to the upkeep type of the given id.
// PackTrigger packs the trigger data according to the upkeep type of the given id. it will remove the first 4 bytes of function selector.
func PackTrigger(id *big.Int, trig triggerWrapper) ([]byte, error) {
var trigger []byte
var err error
Expand Down
6 changes: 3 additions & 3 deletions core/services/ocr2/plugins/ocr2keeper/evm21/core/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ func GetTxBlock(ctx context.Context, client client.Client, txHash common.Hash) (
if strings.Contains(err.Error(), "not yet been implemented") {
// workaround for simulated chains
// Exploratory: fix this properly (e.g. in the simulated backend)
receipt, err1 := client.TransactionReceipt(ctx, txHash)
r, err1 := client.TransactionReceipt(ctx, txHash)
if err1 != nil {
return nil, common.Hash{}, err1
}
if receipt.Status != 1 {
if r.Status != 1 {
return nil, common.Hash{}, nil
}
return receipt.BlockNumber, receipt.BlockHash, nil
return r.BlockNumber, r.BlockHash, nil
}
return nil, common.Hash{}, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (e reportEncoder) Encode(results ...ocr2keepers.CheckResult) ([]byte, error
return e.packer.PackReport(report)
}

// Extract the plugin will call this function to accept/transmit reports
// Extract extracts a slice of reported upkeeps (upkeep id, trigger, and work id) from raw bytes. the plugin will call this function to accept/transmit reports.
func (e reportEncoder) Extract(raw []byte) ([]ocr2keepers.ReportedUpkeep, error) {
report, err := e.packer.UnpackReport(raw)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ type Packer interface {
UnpackCheckResult(payload ocr2keepers.UpkeepPayload, raw string) (ocr2keepers.CheckResult, error)
UnpackCheckCallbackResult(callbackResp []byte) (PipelineExecutionState, bool, []byte, uint8, *big.Int, error)
UnpackPerformResult(raw string) (PipelineExecutionState, bool, error)
UnpackUpkeepInfo(id *big.Int, raw string) (UpkeepInfo, error)
UnpackLogTriggerConfig(raw []byte) (automation_utils_2_1.LogTriggerConfig, error)
PackReport(report automation_utils_2_1.KeeperRegistryBase21Report) ([]byte, error)
UnpackReport(raw []byte) (automation_utils_2_1.KeeperRegistryBase21Report, error)
Expand Down
16 changes: 0 additions & 16 deletions core/services/ocr2/plugins/ocr2keeper/evm21/encoding/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,6 @@ func (p *abiPacker) UnpackPerformResult(raw string) (PipelineExecutionState, boo
return NoPipelineError, *abi.ConvertType(out[0], new(bool)).(*bool), nil
}

func (p *abiPacker) UnpackUpkeepInfo(id *big.Int, raw string) (UpkeepInfo, error) {
b, err := hexutil.Decode(raw)
if err != nil {
return UpkeepInfo{}, err
}

out, err := p.abi.Methods["getUpkeep"].Outputs.UnpackValues(b)
if err != nil {
return UpkeepInfo{}, fmt.Errorf("%w: unpack getUpkeep return: %s", err, raw)
}

info := *abi.ConvertType(out[0], new(UpkeepInfo)).(*UpkeepInfo)

return info, nil
}

// UnpackLogTriggerConfig unpacks the log trigger config from the given raw data
func (p *abiPacker) UnpackLogTriggerConfig(raw []byte) (automation_utils_2_1.LogTriggerConfig, error) {
var cfg automation_utils_2_1.LogTriggerConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func NewOptions(finalityDepth int64) LogTriggersOptions {
func (o *LogTriggersOptions) Defaults(finalityDepth int64) {
if o.LookbackBlocks == 0 {
lookbackBlocks := int64(200)
if lookbackBlocks < int64(finalityDepth) {
lookbackBlocks = int64(finalityDepth)
if lookbackBlocks < finalityDepth {
lookbackBlocks = finalityDepth
}
o.LookbackBlocks = lookbackBlocks
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (f upkeepFilter) Clone() upkeepFilter {
}
}

// Select returns a slice of logs which match the upkeep filter.
func (f upkeepFilter) Select(logs ...logpoller.Log) []logpoller.Log {
var selected []logpoller.Log
for _, log := range logs {
Expand All @@ -56,6 +57,7 @@ func (f upkeepFilter) Select(logs ...logpoller.Log) []logpoller.Log {
return selected
}

// match returns a bool indicating if the log's topics data matches selector and indexed topics in upkeep filter.
func (f upkeepFilter) match(log logpoller.Log) bool {
filters := f.topics[1:]
selector := f.selector
Expand All @@ -65,7 +67,7 @@ func (f upkeepFilter) match(log logpoller.Log) bool {
return true
}

for i, f := range filters {
for i, filter := range filters {
// bitwise AND the selector with the index to check
// if the filter is needed
mask := uint8(1 << uint8(i))
Expand All @@ -76,7 +78,7 @@ func (f upkeepFilter) match(log logpoller.Log) bool {
// log doesn't have enough topics
return false
}
if !bytes.Equal(f.Bytes(), log.Topics[i+1]) {
if !bytes.Equal(filter.Bytes(), log.Topics[i+1]) {
return false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewLogEventsPacker(utilsABI abi.ABI) *logEventsPacker {
}

func (p *logEventsPacker) PackLogData(log logpoller.Log) ([]byte, error) {
topics := [][32]byte{}
var topics [][32]byte
for _, topic := range log.GetTopics() {
topics = append(topics, topic)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import (
var (
// LogRetention is the amount of time to retain logs for.
LogRetention = 24 * time.Hour
// When adding a filter in log poller, backfill is done for this number of blocks
// from latest
// LogBackfillBuffer is the number of blocks from the latest block for which backfill is done when adding a filter in log poller
LogBackfillBuffer = 100
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func NewLogRecoverer(lggr logger.Logger, poller logpoller.LogPoller, client clie
return rec
}

func (r *logRecoverer) Start(pctx context.Context) error {
func (r *logRecoverer) Start(_ context.Context) error {
ctx, cancel := context.WithCancel(context.Background())

r.lock.Lock()
Expand Down Expand Up @@ -441,7 +441,7 @@ func (r *logRecoverer) populatePending(f upkeepFilter, filteredLogs []logpoller.
}

// filterFinalizedStates filters out the log upkeeps that have already been completed (performed or ineligible).
func (r *logRecoverer) filterFinalizedStates(f upkeepFilter, logs []logpoller.Log, states []ocr2keepers.UpkeepState) []logpoller.Log {
func (r *logRecoverer) filterFinalizedStates(_ upkeepFilter, logs []logpoller.Log, states []ocr2keepers.UpkeepState) []logpoller.Log {
filtered := make([]logpoller.Log, 0)

for i, log := range logs {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (b *payloadBuilder) BuildPayloads(ctx context.Context, proposals ...ocr2kee
var payload ocr2keepers.UpkeepPayload
if b.upkeepList.IsActive(proposal.UpkeepID.BigInt()) {
b.lggr.Debugf("building payload for coordinated block proposal %+v", proposal)
checkData := []byte{}
var checkData []byte
var err error
switch core.GetUpkeepType(proposal.UpkeepID) {
case ocr2keepers.LogTrigger:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ func TestNewPayloadBuilder(t *testing.T) {
BlockNumber: 1,
BlockHash: [32]byte{1},
},
CheckData: make([]byte, 0),
},
},
},
Expand Down
13 changes: 7 additions & 6 deletions core/services/ocr2/plugins/ocr2keeper/evm21/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (r *EvmRegistry) Name() string {
return r.lggr.Name()
}

func (r *EvmRegistry) Start(ctx context.Context) error {
func (r *EvmRegistry) Start(_ context.Context) error {
return r.sync.StartOnce("AutomationRegistry", func() error {
r.mu.Lock()
defer r.mu.Unlock()
Expand Down Expand Up @@ -484,17 +484,17 @@ func RegistryUpkeepFilterName(addr common.Address) string {
return logpoller.FilterName("KeeperRegistry Events", addr.String())
}

func (r *EvmRegistry) registerEvents(chainID uint64, addr common.Address) error {
// Add log filters for the log poller so that it can poll and find the logs that
// we need
// registerEvents registers upkeep state events from keeper registry on log poller
func (r *EvmRegistry) registerEvents(_ uint64, addr common.Address) error {
// Add log filters for the log poller so that it can poll and find the logs that we need
return r.poller.RegisterFilter(logpoller.Filter{
Name: RegistryUpkeepFilterName(addr),
EventSigs: upkeepStateEvents,
Addresses: []common.Address{addr},
})
}

// Removes an upkeepID from active list and unregisters the log filter for log upkeeps
// removeFromActive removes an upkeepID from active list and unregisters the log filter for log upkeeps
func (r *EvmRegistry) removeFromActive(id *big.Int) {
r.active.Remove(id)

Expand Down Expand Up @@ -565,6 +565,7 @@ func (r *EvmRegistry) getLatestIDsFromContract(ctx context.Context) ([]*big.Int,
return ids, nil
}

// updateTriggerConfig updates the trigger config for an upkeep. it will re-register a filter for this upkeep.
func (r *EvmRegistry) updateTriggerConfig(id *big.Int, cfg []byte, logBlock uint64) error {
uid := &ocr2keepers.UpkeepIdentifier{}
uid.FromBigInt(id)
Expand Down Expand Up @@ -596,7 +597,7 @@ func (r *EvmRegistry) updateTriggerConfig(id *big.Int, cfg []byte, logBlock uint
return nil
}

// updateTriggerConfig gets invoked upon changes in the trigger config of an upkeep.
// fetchTriggerConfig fetches trigger config in raw bytes for an upkeep.
func (r *EvmRegistry) fetchTriggerConfig(id *big.Int) ([]byte, error) {
opts := r.buildCallOpts(r.ctx, nil)
cfg, err := r.registry.GetUpkeepTriggerConfig(opts, id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (r *EvmRegistry) getBlockHash(blockNumber *big.Int) (common.Hash, error) {
}

// verifyCheckBlock checks that the check block and hash are valid, returns the pipeline execution state and retryable
func (r *EvmRegistry) verifyCheckBlock(ctx context.Context, checkBlock, upkeepId *big.Int, checkHash common.Hash) (state encoding.PipelineExecutionState, retryable bool) {
func (r *EvmRegistry) verifyCheckBlock(_ context.Context, checkBlock, upkeepId *big.Int, checkHash common.Hash) (state encoding.PipelineExecutionState, retryable bool) {
var h string
var ok bool
// verify check block number and hash are valid
Expand Down
6 changes: 3 additions & 3 deletions core/services/ocr2/plugins/ocr2keeper/evm21/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
type AutomationServices interface {
Registry() *EvmRegistry
Encoder() ocr2keepers.Encoder
TransmitEventProvider() *transmit.TransmitEventProvider
TransmitEventProvider() *transmit.EventProvider
BlockSubscriber() *BlockSubscriber
PayloadBuilder() ocr2keepers.PayloadBuilder
UpkeepStateStore() upkeepstate.UpkeepStateStore
Expand Down Expand Up @@ -97,7 +97,7 @@ func New(addr common.Address, client evm.Chain, mc *models.MercuryCredentials, k
type automationServices struct {
reg *EvmRegistry
encoder ocr2keepers.Encoder
transmitEventProvider *transmit.TransmitEventProvider
transmitEventProvider *transmit.EventProvider
blockSub *BlockSubscriber
payloadBuilder ocr2keepers.PayloadBuilder
upkeepState upkeepstate.UpkeepStateStore
Expand All @@ -117,7 +117,7 @@ func (f *automationServices) Encoder() ocr2keepers.Encoder {
return f.encoder
}

func (f *automationServices) TransmitEventProvider() *transmit.TransmitEventProvider {
func (f *automationServices) TransmitEventProvider() *transmit.EventProvider {
return f.transmitEventProvider
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/utils"
)

var _ ocr2keepers.TransmitEventProvider = &TransmitEventProvider{}
var _ ocr2keepers.TransmitEventProvider = &EventProvider{}

type logParser func(registry *iregistry21.IKeeperRegistryMaster, log logpoller.Log) (transmitEventLog, error)

type TransmitEventProvider struct {
type EventProvider struct {
sync utils.StartStopOnce
mu sync.RWMutex
runState int
Expand All @@ -40,7 +40,7 @@ type TransmitEventProvider struct {
cache transmitEventCache
}

func TransmitEventProviderFilterName(addr common.Address) string {
func EventProviderFilterName(addr common.Address) string {
return logpoller.FilterName("KeepersRegistry TransmitEventProvider", addr)
}

Expand All @@ -50,15 +50,15 @@ func NewTransmitEventProvider(
registryAddress common.Address,
client evmclient.Client,
lookbackBlocks int64,
) (*TransmitEventProvider, error) {
) (*EventProvider, error) {
var err error

contract, err := iregistry21.NewIKeeperRegistryMaster(registryAddress, client)
if err != nil {
return nil, err
}
err = logPoller.RegisterFilter(logpoller.Filter{
Name: TransmitEventProviderFilterName(contract.Address()),
Name: EventProviderFilterName(contract.Address()),
EventSigs: []common.Hash{
// These are the events that are emitted when a node transmits a report
iregistry21.IKeeperRegistryMasterUpkeepPerformed{}.Topic(), // Happy path: report performed the upkeep
Expand All @@ -74,7 +74,7 @@ func NewTransmitEventProvider(
return nil, err
}

return &TransmitEventProvider{
return &EventProvider{
logger: logger,
logPoller: logPoller,
registryAddress: registryAddress,
Expand All @@ -86,11 +86,11 @@ func NewTransmitEventProvider(
}, nil
}

func (c *TransmitEventProvider) Name() string {
func (c *EventProvider) Name() string {
return c.logger.Name()
}

func (c *TransmitEventProvider) Start(ctx context.Context) error {
func (c *EventProvider) Start(_ context.Context) error {
return c.sync.StartOnce("AutomationTransmitEventProvider", func() error {
c.mu.Lock()
defer c.mu.Unlock()
Expand All @@ -100,7 +100,7 @@ func (c *TransmitEventProvider) Start(ctx context.Context) error {
})
}

func (c *TransmitEventProvider) Close() error {
func (c *EventProvider) Close() error {
return c.sync.StopOnce("AutomationRegistry", func() error {
c.mu.Lock()
defer c.mu.Unlock()
Expand All @@ -111,7 +111,7 @@ func (c *TransmitEventProvider) Close() error {
})
}

func (c *TransmitEventProvider) Ready() error {
func (c *EventProvider) Ready() error {
c.mu.RLock()
defer c.mu.RUnlock()

Expand All @@ -121,7 +121,7 @@ func (c *TransmitEventProvider) Ready() error {
return c.sync.Ready()
}

func (c *TransmitEventProvider) HealthReport() map[string]error {
func (c *EventProvider) HealthReport() map[string]error {
c.mu.RLock()
defer c.mu.RUnlock()

Expand All @@ -131,7 +131,7 @@ func (c *TransmitEventProvider) HealthReport() map[string]error {
return map[string]error{c.Name(): c.sync.Healthy()}
}

func (c *TransmitEventProvider) GetLatestEvents(ctx context.Context) ([]ocr2keepers.TransmitEvent, error) {
func (c *EventProvider) GetLatestEvents(ctx context.Context) ([]ocr2keepers.TransmitEvent, error) {
end, err := c.logPoller.LatestBlock(pg.WithParentCtx(ctx))
if err != nil {
return nil, fmt.Errorf("%w: failed to get latest block from log poller", err)
Expand Down Expand Up @@ -159,8 +159,8 @@ func (c *TransmitEventProvider) GetLatestEvents(ctx context.Context) ([]ocr2keep
}

// processLogs will parse the unseen logs and return the corresponding transmit events.
func (c *TransmitEventProvider) processLogs(latestBlock int64, logs ...logpoller.Log) ([]ocr2keepers.TransmitEvent, error) {
vals := []ocr2keepers.TransmitEvent{}
func (c *EventProvider) processLogs(latestBlock int64, logs ...logpoller.Log) ([]ocr2keepers.TransmitEvent, error) {
var vals []ocr2keepers.TransmitEvent

for _, log := range logs {
k := c.logKey(log)
Expand Down Expand Up @@ -214,7 +214,7 @@ func (c *TransmitEventProvider) processLogs(latestBlock int64, logs ...logpoller
return vals, nil
}

func (c *TransmitEventProvider) logKey(log logpoller.Log) string {
func (c *EventProvider) logKey(log logpoller.Log) string {
logExt := ocr2keepers.LogTriggerExtension{
TxHash: log.TxHash,
Index: uint32(log.LogIndex),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewUpkeepProvider(activeUpkeeps ActiveUpkeepList, bs *BlockSubscriber, lp l
}
}

func (p *upkeepProvider) GetActiveUpkeeps(ctx context.Context) ([]ocr2keepers.UpkeepPayload, error) {
func (p *upkeepProvider) GetActiveUpkeeps(_ context.Context) ([]ocr2keepers.UpkeepPayload, error) {
latestBlock := p.bs.latestBlock.Load()
if latestBlock == nil {
return nil, fmt.Errorf("no latest block found when fetching active upkeeps")
Expand All @@ -35,7 +35,7 @@ func (p *upkeepProvider) GetActiveUpkeeps(ctx context.Context) ([]ocr2keepers.Up
for _, uid := range p.activeUpkeeps.View(ocr2keepers.ConditionTrigger) {
payload, err := core.NewUpkeepPayload(
uid,
ocr2keepers.NewTrigger(ocr2keepers.BlockNumber(latestBlock.Number), latestBlock.Hash),
ocr2keepers.NewTrigger(latestBlock.Number, latestBlock.Hash),
nil,
)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (s *performedEventsScanner) Start(_ context.Context) error {
})
}

// implements io.Closer, does nothing upon close
// Close implements io.Closer and does nothing
func (s *performedEventsScanner) Close() error {
return nil
}
Expand Down
Loading

0 comments on commit d13f179

Please sign in to comment.