Skip to content

Commit

Permalink
add inline docs
Browse files Browse the repository at this point in the history
Signed-off-by: Francisco de Borja Aranda Castillejo <borja.aranda@smartcontract.com>
  • Loading branch information
Francisco de Borja Aranda Castillejo committed Apr 30, 2024
1 parent 6f2ab98 commit 6a9f80f
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/timelock/timelock.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,15 @@ func (tw *Worker) Listen(ctx context.Context) error {
return nil
}

// setupFilterQuery returns an ethereum.FilterQuery initialized to watch the Timelock contract.
func (tw *Worker) setupFilterQuery() ethereum.FilterQuery {
return ethereum.FilterQuery{
Addresses: tw.address,
FromBlock: tw.fromBlock,
}
}

// subscribeNewLogs subscribes to a Timelock contract and emit logs through the channel it returns.
func (tw *Worker) subscribeNewLogs(ctx context.Context) (<-chan types.Log, error) {
query := tw.setupFilterQuery()
logCh := make(chan types.Log)
Expand All @@ -182,6 +184,7 @@ func (tw *Worker) subscribeNewLogs(ctx context.Context) (<-chan types.Log, error
wg.Add(1)
go func() {
defer wg.Done()
defer close(logCh)
defer sub.Unsubscribe()
for {
select {
Expand Down Expand Up @@ -224,6 +227,8 @@ func (tw *Worker) subscribeNewLogs(ctx context.Context) (<-chan types.Log, error
return logCh, nil
}

// retrieveHistoricalLogs returns a types.Log channel and retrieves all the historical events of a given contract.
// Once all the logs have been sent into the channel the function returns and the channel is closed.
func (tw *Worker) retrieveHistoricalLogs(ctx context.Context) (<-chan types.Log, error) {
query := tw.setupFilterQuery()
logCh := make(chan types.Log)
Expand Down Expand Up @@ -255,6 +260,8 @@ func (tw *Worker) retrieveHistoricalLogs(ctx context.Context) (<-chan types.Log,
return logCh, nil
}

// processLogs is implemented as a fan-in for all the logs channels, merging all the data and handling logs sequentially.
// This function is thread safe.
func (tw *Worker) processLogs(ctx context.Context, oldLog, newLog <-chan types.Log) {
// This is the goroutine watching over the subscribed and historical logs.
wg.Add(1)
Expand All @@ -281,6 +288,9 @@ func (tw *Worker) processLogs(ctx context.Context, oldLog, newLog <-chan types.L
}()
}

// handleLog handles the logic of parsing every event, its type and actions associated to each one.
// CallScheduled events have to be added to the scheduler.
// CallExecuted and CallCanceled signals an event that has to be removed from the scheduler.
func (tw *Worker) handleLog(ctx context.Context, log types.Log) error {
// Ignore logs with no topics.
if len(log.Topics) == 0 {
Expand Down Expand Up @@ -339,6 +349,7 @@ func (tw *Worker) handleLog(ctx context.Context, log types.Log) error {
return nil
}

// startLog prints the timelock-worker configuration.
func (tw *Worker) startLog() {
tw.logger.Info().Msgf("timelock-worker started")
tw.logger.Info().Msgf("\tTimelock contract address: %v", tw.address[0])
Expand Down

0 comments on commit 6a9f80f

Please sign in to comment.