This repository has been archived by the owner on Oct 18, 2023. It is now read-only.
forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rpcdaemon: subscriptions,
newHeads
(erigontech#1359)
* fix `make grpc` on new checkouts * update proto files * add some stub * prototype with fake events * notifying about events * pass events * events are being sent * transfer headers to filters * create the “filters” struct * implement new heads * PoC of New Heads subscription * fix keep alive * fixups for the client * add “type” to the event * support header event type on client * better stage refactor * fixup for the eth backend * fixups * fix tests * fix tests * fix linters * address comments * remove unused log
- Loading branch information
Showing
24 changed files
with
563 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,3 +55,4 @@ root_*.txt | |
|
||
__pycache__ | ||
docker-compose.dev.yml | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package filters | ||
|
||
import ( | ||
"crypto/rand" | ||
"encoding/json" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"github.com/ledgerwatch/turbo-geth/core/types" | ||
"github.com/ledgerwatch/turbo-geth/ethdb" | ||
"github.com/ledgerwatch/turbo-geth/ethdb/remote" | ||
"github.com/ledgerwatch/turbo-geth/ethdb/remote/remotedbserver" | ||
"github.com/ledgerwatch/turbo-geth/log" | ||
) | ||
|
||
type Filters struct { | ||
mu sync.RWMutex | ||
|
||
headsSubs map[string]chan *types.Header | ||
} | ||
|
||
func New(ethBackend ethdb.Backend) *Filters { | ||
log.Info("rpc filters: subscribing to tg events") | ||
|
||
ff := &Filters{headsSubs: make(map[string]chan *types.Header)} | ||
|
||
go func() { | ||
var err error | ||
for i := 0; i < 10; i++ { | ||
err = ethBackend.Subscribe(ff.OnNewEvent) | ||
if err != nil { | ||
log.Warn("rpc filters: error subscribing to events", "err", err) | ||
time.Sleep(time.Second) | ||
} | ||
} | ||
}() | ||
|
||
return ff | ||
} | ||
|
||
func (ff *Filters) SubscribeNewHeads(out chan *types.Header) string { | ||
ff.mu.Lock() | ||
defer ff.mu.Unlock() | ||
id := generateSubscriptionID() | ||
ff.headsSubs[id] = out | ||
return id | ||
} | ||
|
||
func (ff *Filters) Unsubscribe(id string) { | ||
ff.mu.Lock() | ||
defer ff.mu.Unlock() | ||
delete(ff.headsSubs, id) | ||
} | ||
|
||
func (ff *Filters) OnNewEvent(event *remote.SubscribeReply) { | ||
ff.mu.RLock() | ||
defer ff.mu.RUnlock() | ||
|
||
if remotedbserver.RpcEventType(event.Type) != remotedbserver.EventTypeHeader { | ||
log.Warn("rpc filters: unsupported event type", "type", event.Type) | ||
return | ||
} | ||
|
||
payload := event.Data | ||
var header types.Header | ||
err := json.Unmarshal(payload, &header) | ||
if err != nil { | ||
// ignoring what we can't unmarshal | ||
log.Warn("rpc filters, unprocessable payload", "err", err) | ||
} else { | ||
for _, v := range ff.headsSubs { | ||
v <- &header | ||
} | ||
} | ||
} | ||
|
||
func generateSubscriptionID() string { | ||
var id [32]byte | ||
|
||
_, err := rand.Read(id[:]) | ||
if err != nil { | ||
log.Crit("rpc filters: error creating random id", "err", err) | ||
} | ||
|
||
return fmt.Sprintf("%x", id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package stagedsync | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/ledgerwatch/turbo-geth/core/rawdb" | ||
"github.com/ledgerwatch/turbo-geth/log" | ||
) | ||
|
||
func NotifyRpcDaemon(from, to uint64, notifier ChainEventNotifier, db rawdb.DatabaseReader) error { | ||
if notifier == nil { | ||
log.Warn("rpc notifier is not set, rpc daemon won't be updated about headers") | ||
return nil | ||
} | ||
for i := from; i <= to; i++ { | ||
hash, err := rawdb.ReadCanonicalHash(db, i) | ||
if err != nil { | ||
return err | ||
} | ||
header := rawdb.ReadHeader(db, hash, i) | ||
if header == nil { | ||
return fmt.Errorf("could not find canonical header for hash: %x number: %d", hash, i) | ||
} | ||
notifier.OnNewHeader(header) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.