Skip to content

Commit

Permalink
Implement the eth_getLogs JSON-RPC endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
m-Peter committed Jan 23, 2024
1 parent 1b1548a commit 111a6f0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
14 changes: 3 additions & 11 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,18 +456,10 @@ func (s *BlockChainAPI) GetLogs(
if len(criteria.Topics) > maxTopics {
return nil, errExceedMaxTopics
}
log := &types.Log{
Index: 1,
BlockNumber: 436,
BlockHash: common.HexToHash("0x8216c5785ac562ff41e2dcfdf5785ac562ff41e2dcfdf829c5a142f1fccd7d"),
TxHash: common.HexToHash("0xdf829c5a142f1fccd7d8216c5785ac562ff41e2dcfdf5785ac562ff41e2dcf"),
TxIndex: 0,
Address: common.HexToAddress("0x16c5785ac562ff41e2dcfdf829c5a142f1fccd7d"),
Data: []byte{0, 0, 0},
Topics: []common.Hash{common.HexToHash("0x59ebeb90bc63057b6515673c3ecf9438e5058bca0f92585014eced636878c9a5")},
}

return []*types.Log{log}, nil
logs := s.Store.LogsByTopic(criteria.Topics[0][0].Hex())

return logs, nil
}

// eth_newFilter
Expand Down
3 changes: 3 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ func runIndexer(ctx context.Context, store *storage.Store, logger zerolog.Logger
}
for _, event := range response.Events {
logger.Info().Msgf(" %s", event.Value)
if event.Type == "flow.evm.TransactionExecuted" {
store.StoreLog(ctx, event.Value)
}
}

lastHeight = response.Height
Expand Down
43 changes: 42 additions & 1 deletion storage/store.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package storage

import (
"bytes"
"context"
"encoding/hex"
"sync"

"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
"github.com/onflow/cadence"
)

type Store struct {
mu sync.RWMutex
logsByTopic map[string][]*types.Log
latestHeight uint64
}

Expand All @@ -16,7 +23,9 @@ type Store struct {
// this race condition, we should require an initial value for
// `latestHeight` in `NewStore`.
func NewStore() *Store {
return &Store{}
return &Store{
logsByTopic: make(map[string][]*types.Log),
}
}

func (s *Store) LatestBlockHeight(ctx context.Context) (uint64, error) {
Expand All @@ -26,6 +35,38 @@ func (s *Store) LatestBlockHeight(ctx context.Context) (uint64, error) {
return s.latestHeight, nil
}

func (s *Store) StoreLog(ctx context.Context, event cadence.Event) {
s.mu.Lock()
defer s.mu.Unlock()

logValue := event.GetFieldValues()[9]
logC, ok := logValue.(cadence.String)
if !ok {
return
}
logS := logC.ToGoValue().(string)
if len(logS) == 0 {
return
}
bt, err := hex.DecodeString(logS)
if err != nil {
panic(err)
}
logs := []*types.Log{}
err = rlp.Decode(bytes.NewReader(bt), &logs)
if err != nil {
panic(err)
}
for _, log := range logs {
topic := log.Topics[0].Hex()
s.logsByTopic[topic] = logs
}
}

func (s *Store) LogsByTopic(topic string) []*types.Log {
return s.logsByTopic[topic]
}

func (s *Store) StoreBlockHeight(ctx context.Context, blockHeight uint64) error {
s.mu.Lock()
defer s.mu.Unlock()
Expand Down

0 comments on commit 111a6f0

Please sign in to comment.