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

Add beacon block reward endpoint #162

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

MisiakGeo
Copy link

@MisiakGeo MisiakGeo commented Sep 20, 2024

Add Block Rewards.
Endpoint /eth/v1/beacon/rewards/blocks is now available to fetch data regarding the block rewards on a slot.

To test it you can run the following:

package main

import (
	"context"
	"errors"
	"fmt"
	"os"
	"time"

	"github.com/attestantio/go-eth2-client/api"
	"github.com/attestantio/go-eth2-client/http"
	"github.com/davecgh/go-spew/spew"
	"github.com/rs/zerolog"
)

func main() {
	// Provide a cancellable context to the creation function.
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	var err error
	client, err := http.New(ctx,
		// WithAddress supplies the address of the beacon node, as a URL.
		http.WithAddress(os.Getenv("BEACON_NODE_ADDRESS")),
		// LogLevel supplies the level of logging to carry out.
		http.WithLogLevel(zerolog.Disabled),
	)
	if err != nil {
		panic(err)
	}

	fmt.Printf("Connected to %s\n", client.Name())

	httpClient := client.(*http.Service)
	reward, err := httpClient.BeaconBlockReward(ctx, &api.BeaconBlockRewardOpts{
		Common: api.CommonOpts{
			Timeout: 2 * time.Minute,
		},
		Block: "head",
	})
	if err != nil {
		var apiErr *api.Error
		if errors.As(err, &apiErr) {
			switch apiErr.StatusCode {
			case 400:
				// Bad Request
			case 404:
				// No block found.
			case 503:
				// Node is syncing.
			case 500:
				// Internal Server Error.
			}
		}
		spew.Dump(err)
		panic(err)
	}
	spew.Dump(reward.Data)

	// Cancelling the context passed to New() frees up resources held by the
	// client, closes connections, clears handlers, etc.
	cancel()
}

One problem I had, was when testing it against QuickNode, some time an error appeared Context Deadline Exceeded, due to the fact that the rewards endpoint with curl takes about 1 min to complete.

@MisiakGeo
Copy link
Author

Ignore the events.go (It was fixed by @mcdee). It is still working perfectly without errors, but urlForCall function is considered better.

Copy link
Contributor

@mcdee mcdee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR, a few comments inline. Also, please could you add in api/v1/beaconblockreward_yaml.go and api/v1/beaconblockreward_test.go files to add YAML support and tests for the spec, respectively? The tests in particular are important to keep up coverage rates for the library.

apiv1 "github.com/attestantio/go-eth2-client/api/v1"
)

// BeaconBlockHeader provides the block header given the opts.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please could you fix this comment to match the function.

"github.com/pkg/errors"
)

// BeaconBlockReward Rewards info for a single block
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please can you fix comments so that they end in a period? Thanks.

// BeaconBlockReward Rewards info for a single block
type BeaconBlockReward struct {
// Proposer of the block, the proposer index who receives these rewards
ProposerIndex uint64
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be of type phase0.ValidatorIndex

// Proposer of the block, the proposer index who receives these rewards
ProposerIndex uint64
// Total block reward in gwei, equal to attestations + sync_aggregate + proposer_slashings + attester_slashings
Total uint64
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and other gwei values should be of type phase0.Gwei

@@ -21,6 +21,7 @@ import (
"math/rand"
"net"
"net/http"
"net/url"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please can you revert the changes to this file, as they have been addressed elsewhere.

@MisiakGeo
Copy link
Author

@mcdee Thank you very for the comments. I will take a look. A comment from my side. YAML files can be found in api/v1/daneb/*_yaml.go . Should I place them in api/v1?

@mcdee
Copy link
Contributor

mcdee commented Sep 21, 2024

Sorry, after taking another look it appears that the rewards object is not a spec object, so it doesn't need the YAML piece.

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

Successfully merging this pull request may close these issues.

3 participants