Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can we actually see the event from Beacon #161

Closed
MisiakGeo opened this issue Sep 19, 2024 · 6 comments
Closed

How can we actually see the event from Beacon #161

MisiakGeo opened this issue Sep 19, 2024 · 6 comments

Comments

@MisiakGeo
Copy link

Hello, I have this piece of code. I trying to see block events from beacon chain. The test case does not show exactly how can I see and navigate within the event json.

	doneChn := make(chan struct{})
	errChn := make(chan error)
	//eventChn := make(chan *apiv1.Event)
	go func() {
		spew.Dump("Start listening for blocks")

		for {
			err = client.(eth2Client.EventsProvider).Events(ctx, []string{"block"}, func(event *apiv1.Event) {
				spew.Dump(event)
			})
			if err != nil {
				errChn <- err
			}

			select {
			case err := <-errChn:
				spew.Dump(err)
			case <-doneChn:
				// case incoming := <-eventChn:
				// 	spew.Dump("New event arrived: ")
				// 	spew.Dump(incoming)
				// Update global slot to be available to all services  (TODO)
			}
		}
	}()

Is there a way to fetch the event data?
@mcdee
Copy link
Contributor

mcdee commented Sep 19, 2024

Where you have spew.Dump(event) you need to cast the event data, e.g. data := event.Data.(*api.BlockEvent) then you can access the individual fields.

@MisiakGeo
Copy link
Author

MisiakGeo commented Sep 19, 2024 via email

@mcdee
Copy link
Contributor

mcdee commented Sep 19, 2024

You don't need that for loop. I'm not 100% sure what you are trying to achieve, but here is a full example:

package main

import (
        "context"
        "fmt"
        "os"
        "os/signal"
        "syscall"
        "time"

        consensusclient "github.com/attestantio/go-eth2-client"
        apiv1 "github.com/attestantio/go-eth2-client/api/v1"
        "github.com/attestantio/go-eth2-client/http"
        "github.com/rs/zerolog"
)

func main() {
        ctx, cancel := context.WithCancel(context.Background())

        client, err := http.New(ctx,
                http.WithLogLevel(zerolog.Disabled),
                http.WithAddress("http://localhost:5052"),
        )
        if err != nil {
                panic(err)
        }

        blockHandlers := []func(context.Context, *apiv1.BlockEvent){
                handler1,
                handler2,
        }

        handler := func(event *apiv1.Event) {
                for _, blockHandler := range blockHandlers {
                        go blockHandler(ctx, event.Data.(*apiv1.BlockEvent))
                }
        }

        if err := client.(consensusclient.EventsProvider).Events(ctx, []string{"block"}, handler); err != nil {
                panic(err)
        }

        // Wait for a signal or error.
        sigCh := make(chan os.Signal, 1)
        signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
        for {
                sig := <-sigCh
                if sig == syscall.SIGINT || sig == syscall.SIGTERM || sig == os.Interrupt || sig == os.Kill {
                        break
                }
        }

        // Shut down the connection cleanly.
        cancel()
        time.Sleep(100 * time.Millisecond)
}

func handler1(ctx context.Context, block *apiv1.BlockEvent) {
        fmt.Fprintf(os.Stdout, "Handler 1 received event for slot %d\n", block.Slot)
}

func handler2(ctx context.Context, block *apiv1.BlockEvent) {
        fmt.Fprintf(os.Stdout, "Handler 2 received event for slot %d\n", block.Slot)
}

You would need to decide how to create your list of handlers (and they could just be a list of channels if you prefer) but something close the above is likely what you're looking for.

@MisiakGeo
Copy link
Author

Thabk you very much. I do want to create a listener to listen for live block events and slot numbers.

@MisiakGeo
Copy link
Author

@mcdee Thank you very much. Indeed the code is working. There was an issue with events.go where when I used a quicknode url with token in it https://methodical-billowing-dew.quiknode.pro/<token>, the line where the callURL was created callURL := s.base.ResolveReference(reference).String(), results in a url without the token https://methodical-billowing-dew.quiknode.pro, thus the call to the url was silent failed, due to absent of error returned in .Dial . I resolved it using this line instead callURL := fmt.Sprintf("%s/%s", s.base.String(), reference)

@mcdee
Copy link
Contributor

mcdee commented Sep 20, 2024

Thank you for letting me know of this issue, it has been fixed in the latest master.

@mcdee mcdee closed this as completed Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants