ssz_static

package
v5.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2024 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunSSZStaticTests

func RunSSZStaticTests(t *testing.T, config, forkOrPhase string, unmarshaller Unmarshaller, customHtr CustomHTRAdder)

RunSSZStaticTests executes "ssz_static" tests for the given fork of phase using the provided unmarshaller to hydrate serialized test data into go struct pointers and also applies any custom HTR methods via the customHTR callback.

Example
package main

import (
	"context"
	"fmt"
	"testing"

	"github.com/pkg/errors"
	fssz "github.com/prysmaticlabs/fastssz"

	state_native "github.com/prysmaticlabs/prysm/v5/beacon-chain/state/state-native"

	ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
	"github.com/prysmaticlabs/prysm/v5/testing/require"

	common "github.com/prysmaticlabs/prysm/v5/testing/spectest/shared/common/ssz_static"
)

func main() {
	// Define an unmarshaller to select the correct go type based on the string
	// name provided in spectests and then populate it with the serialized bytes.
	unmarshaller := func(t *testing.T, serializedBytes []byte, objectName string) (interface{}, error) {
		var obj interface{}
		switch objectName {
		case "Attestation":
			obj = &ethpb.Attestation{}
		case "BeaconState":
			obj = &ethpb.BeaconState{}
		case "Eth1Block":
			// Some types may not apply to prysm, but exist in the spec test folders. It is OK to
			// skip these tests with a valid justification. Otherwise, the test should fail with an
			// unsupported type.
			t.Skip("Unused type")
			return nil, nil
		default:
			return nil, fmt.Errorf("unsupported type: %s", objectName)
		}
		var err error
		if o, ok := obj.(fssz.Unmarshaler); ok {
			err = o.UnmarshalSSZ(serializedBytes)
		} else {
			err = errors.New("could not unmarshal object, not a fastssz compatible object")
		}
		return obj, err
	}

	// Optional: define a method to add custom HTR methods for a given object.
	// This argument may be nil if your test does not require custom HTR methods.
	// Most commonly, this is used when a handwritten HTR method with specialized caching
	// is used and you want to ensure it passes spectests.
	customHTR := func(t *testing.T, htrs []common.HTR, object interface{}) []common.HTR {
		switch object.(type) {
		case *ethpb.BeaconState:
			htrs = append(htrs, func(s interface{}) ([32]byte, error) {
				beaconState, err := state_native.InitializeFromProtoPhase0(s.(*ethpb.BeaconState))
				require.NoError(t, err)
				return beaconState.HashTreeRoot(context.TODO())
			})
		}
		return htrs
	}

	var t *testing.T
	// common.RunSSZStaticTests will run all of the tests found in the spec test folder with the
	// given config and forkOrPhase. It will then use the unmarshaller to hydrate the types and
	// ensure that fastssz generated methods match the expected results. It will also test custom
	// HTR methods if provided.
	common.RunSSZStaticTests(t,
		"mainnet", // Network configuration
		"phase0",  // Fork or phase
		unmarshaller,
		customHTR) // nil customHTR is acceptable.
}
Output:

Types

type CustomHTRAdder

type CustomHTRAdder func(t *testing.T, htrs []HTR, object interface{}) []HTR

CustomHTRAdder adds any custom HTR methods for the given object. This method should return a HTR slice with the custom HTR method applied.

type HTR

type HTR func(interface{}) ([32]byte, error)

HTR is the HashTreeRoot function signature.

type SSZRoots

type SSZRoots struct {
	Root        string `json:"root"`
	SigningRoot string `json:"signing_root"`
}

SSZRoots is the format used to read spectest test data.

type Unmarshaller

type Unmarshaller func(t *testing.T, serializedBytes []byte, objectName string) (interface{}, error)

Unmarshaller determines the correct type per ObjectName and then hydrates the object from the serializedBytes. This method may call t.Skip if the type is not supported.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL