log_processor.go 2.72 KB
Newer Older
1
package processors
2 3 4 5 6

import (
	"context"
	"fmt"

7 8 9 10
	"github.com/ethereum/go-ethereum/common"
	ethTypes "github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/crypto"

11
	"github.com/ethereum-optimism/optimism/op-service/eth"
12
	"github.com/ethereum-optimism/optimism/op-supervisor/supervisor/types"
13 14 15
)

type LogStorage interface {
Axel Kingsley's avatar
Axel Kingsley committed
16
	SealBlock(chain types.ChainID, block eth.BlockRef) error
17 18 19 20
	AddLog(chain types.ChainID, logHash common.Hash, parentBlock eth.BlockID, logIdx uint32, execMsg *types.ExecutingMessage) error
}

type ChainsDBClientForLogProcessor interface {
Axel Kingsley's avatar
Axel Kingsley committed
21
	SealBlock(chain types.ChainID, block eth.BlockRef) error
22
	AddLog(chain types.ChainID, logHash common.Hash, parentBlock eth.BlockID, logIdx uint32, execMsg *types.ExecutingMessage) error
23 24
}

25
type logProcessor struct {
26
	chain        types.ChainID
27
	logStore     LogStorage
28
	eventDecoder EventDecoderFn
29 30
}

31
func NewLogProcessor(chain types.ChainID, logStore LogStorage) LogProcessor {
32 33 34
	return &logProcessor{
		chain:        chain,
		logStore:     logStore,
35
		eventDecoder: DecodeExecutingMessageLog,
36
	}
37 38
}

39 40
// ProcessLogs processes logs from a block and stores them in the log storage
// for any logs that are related to executing messages, they are decoded and stored
Axel Kingsley's avatar
Axel Kingsley committed
41
func (p *logProcessor) ProcessLogs(_ context.Context, block eth.BlockRef, rcpts ethTypes.Receipts) error {
42 43
	for _, rcpt := range rcpts {
		for _, l := range rcpt.Logs {
44 45
			// log hash represents the hash of *this* log as a potentially initiating message
			logHash := logToLogHash(l)
46 47 48 49
			// The log may be an executing message emitted by the CrossL2Inbox
			execMsg, err := p.eventDecoder(l)
			if err != nil {
				return fmt.Errorf("invalid log %d from block %s: %w", l.Index, block.ID(), err)
50 51 52
			}
			// executing messages have multiple entries in the database
			// they should start with the initiating message and then include the execution
53 54
			if err := p.logStore.AddLog(p.chain, logHash, block.ParentID(), uint32(l.Index), execMsg); err != nil {
				return fmt.Errorf("failed to add log %d from block %s: %w", l.Index, block.ID(), err)
55 56 57
			}
		}
	}
58
	if err := p.logStore.SealBlock(p.chain, block); err != nil {
59 60
		return fmt.Errorf("failed to seal block %s: %w", block.ID(), err)
	}
61 62 63
	return nil
}

64 65 66 67 68
// logToLogHash transforms a log into a hash that represents the log.
// it is the concatenation of the log's address and the hash of the log's payload,
// which is then hashed again. This is the hash that is stored in the log storage.
// The address is hashed into the payload hash to save space in the log storage,
// and because they represent paired data.
69
func logToLogHash(l *ethTypes.Log) common.Hash {
70 71
	payloadHash := crypto.Keccak256Hash(types.LogToMessagePayload(l))
	return types.PayloadHashToLogHash(payloadHash, l.Address)
72
}