Skip to content

Commit

Permalink
Ensure length for deposit proofs.
Browse files Browse the repository at this point in the history
Add a length check for deposit proofs, and fix the many bad tests where
an incorrect proof length was used.

Fixes ##54
  • Loading branch information
mcdee committed Jul 4, 2023
1 parent cbaf4f9 commit 2ba62f6
Show file tree
Hide file tree
Showing 52 changed files with 906 additions and 1,736 deletions.
34 changes: 17 additions & 17 deletions api/v1/bellatrix/blindedbeaconblock_test.go

Large diffs are not rendered by default.

60 changes: 30 additions & 30 deletions api/v1/bellatrix/blindedbeaconblockbody_test.go

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions api/v1/bellatrix/signedblindedbeaconblock_test.go

Large diffs are not rendered by default.

60 changes: 30 additions & 30 deletions api/v1/capella/blindedbeaconblockbody_test.go

Large diffs are not rendered by default.

70 changes: 35 additions & 35 deletions api/v1/deneb/blindedbeaconblockbody_test.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions api/v1/deneb/blindedblobsidecar_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type blindedBlobSidecarJSON struct {
KzgProof deneb.KzgProof `json:"kzg_proof"`
}

// MarshalJSON implements json.Marshaler.
func (b *BlindedBlobSidecar) MarshalJSON() ([]byte, error) {
return json.Marshal(&blindedBlobSidecarJSON{
BlockRoot: b.BlockRoot,
Expand All @@ -48,6 +49,7 @@ func (b *BlindedBlobSidecar) MarshalJSON() ([]byte, error) {
})
}

// UnmarshalJSON implements json.Unmarshaler.
func (b *BlindedBlobSidecar) UnmarshalJSON(input []byte) error {
raw, err := codecs.RawJSON(&blindedBlobSidecarJSON{}, input)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions api/v1/payloadattributesevent.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type PayloadAttributesData struct {
V2 *PayloadAttributesV2
}

// PayloadAttributes represents the payload attributes.
// PayloadAttributesV1 represents the payload attributes.
type PayloadAttributesV1 struct {
// Timestamp is the timestamp of the payload.
Timestamp uint64
Expand Down Expand Up @@ -93,7 +93,7 @@ type payloadAttributesV2JSON struct {
Withdrawals []*capella.Withdrawal `json:"withdrawals"`
}

// MarshalJSON implements json.Marshaler.
// UnmarshalJSON implements json.Unmarshaler.
func (p *PayloadAttributesV1) UnmarshalJSON(input []byte) error {
var payloadAttributes payloadAttributesV1JSON
if err := json.Unmarshal(input, &payloadAttributes); err != nil {
Expand Down Expand Up @@ -140,7 +140,7 @@ func (p *PayloadAttributesV1) unpack(data *payloadAttributesV1JSON) error {
return nil
}

// MarshalJSON implements json.Marshaler.
// UnmarshalJSON implements json.Unmarshaler.
func (p *PayloadAttributesV2) UnmarshalJSON(input []byte) error {
var payloadAttributes payloadAttributesV2JSON
if err := json.Unmarshal(input, &payloadAttributes); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion codecs/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/pkg/errors"
)

// UnmarshalRawMessage generates raw JSON for a struct,
// RawJSON generates raw JSON for a struct,
// ensuring that all values are present.
func RawJSON(b any, input []byte) (map[string]json.RawMessage, error) {
// Make generic map from input.
Expand Down
34 changes: 17 additions & 17 deletions spec/altair/beaconblock_test.go

Large diffs are not rendered by default.

56 changes: 28 additions & 28 deletions spec/altair/beaconblockbody_test.go

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions spec/altair/signedbeaconblock_test.go

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions spec/bellatrix/beaconblock_test.go

Large diffs are not rendered by default.

60 changes: 30 additions & 30 deletions spec/bellatrix/beaconblockbody_test.go

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions spec/bellatrix/signedbeaconblock_test.go

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions spec/capella/beaconblock_test.go

Large diffs are not rendered by default.

60 changes: 30 additions & 30 deletions spec/capella/beaconblockbody_test.go

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions spec/capella/signedbeaconblock_test.go

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions spec/deneb/beaconblock_test.go

Large diffs are not rendered by default.

70 changes: 35 additions & 35 deletions spec/deneb/beaconblockbody_test.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions spec/deneb/blobidentifier_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ type blobIdentifierJSON struct {
Index string `json:"index"`
}

// MarshalJSON implements json.Marshaler.
func (b *BlobIdentifier) MarshalJSON() ([]byte, error) {
return json.Marshal(&blobIdentifierJSON{
BlockRoot: b.BlockRoot,
Index: fmt.Sprintf("%d", b.Index),
})
}

// UnmarshalJSON implements json.Unmarshaler.
func (b *BlobIdentifier) UnmarshalJSON(input []byte) error {
raw, err := codecs.RawJSON(&blobIdentifierJSON{}, input)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions spec/deneb/blobsidecar_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type blobSidecarJSON struct {
KzgProof KzgProof `json:"kzg_proof"`
}

// MarshalJSON implements json.Marshaler.
func (b *BlobSidecar) MarshalJSON() ([]byte, error) {
return json.Marshal(&blobSidecarJSON{
BlockRoot: b.BlockRoot,
Expand All @@ -47,6 +48,7 @@ func (b *BlobSidecar) MarshalJSON() ([]byte, error) {
})
}

// UnmarshalJSON implements json.Unmarshaler.
func (b *BlobSidecar) UnmarshalJSON(input []byte) error {
raw, err := codecs.RawJSON(&blobSidecarJSON{}, input)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions spec/deneb/consensusspec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package deneb_test

import (
"bytes"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -254,3 +255,17 @@ func TestConsensusSpec(t *testing.T) {
}))
}
}

func testYAMLFormat(input []byte) string {
val := make(map[string]any)
if err := yaml.UnmarshalWithOptions(input, &val, yaml.UseOrderedMap()); err != nil {
panic(err)
}

res, err := yaml.MarshalWithOptions(val, yaml.Flow(true))
if err != nil {
panic(err)
}

return string(bytes.ToLower(bytes.ReplaceAll(res, []byte(`"`), []byte(`'`))))
}
17 changes: 1 addition & 16 deletions spec/deneb/executionpayload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
package deneb_test

import (
"bytes"
"encoding/json"
"testing"

"github.com/attestantio/go-eth2-client/spec/deneb"
"github.com/goccy/go-yaml"
require "github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"
require "github.com/stretchr/testify/require"
)

func TestExecutionPayloadJSON(t *testing.T) {
Expand Down Expand Up @@ -384,20 +383,6 @@ func TestExecutionPayloadJSON(t *testing.T) {
}
}

func testYAMLFormat(input []byte) string {
val := make(map[string]any)
if err := yaml.UnmarshalWithOptions(input, &val, yaml.UseOrderedMap()); err != nil {
panic(err)
}

res, err := yaml.MarshalWithOptions(val, yaml.Flow(true))
if err != nil {
panic(err)
}

return string(bytes.ToLower(bytes.ReplaceAll(res, []byte(`"`), []byte(`'`))))
}

func TestExecutionPayloadYAML(t *testing.T) {
tests := []struct {
name string
Expand Down
16 changes: 8 additions & 8 deletions spec/deneb/signedbeaconblock_test.go

Large diffs are not rendered by default.

51 changes: 1 addition & 50 deletions spec/phase0/aggregateandproof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@ package phase0_test
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"

"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/goccy/go-yaml"
"github.com/golang/snappy"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAggregateAndProofJSON(t *testing.T) {
Expand Down Expand Up @@ -150,48 +146,3 @@ func TestAggregateAndProofYAML(t *testing.T) {
})
}
}

func TestAggregateAndProofSpec(t *testing.T) {
if os.Getenv("ETH2_SPEC_TESTS_DIR") == "" {
t.Skip("ETH2_SPEC_TESTS_DIR not suppplied, not running spec tests")
}
baseDir := filepath.Join(os.Getenv("ETH2_SPEC_TESTS_DIR"), "tests", "mainnet", "phase0", "ssz_static", "AggregateAndProof", "ssz_random")
require.NoError(t, filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error {
if path == baseDir {
// Only interested in subdirectories.
return nil
}
require.NoError(t, err)
if info.IsDir() {
t.Run(info.Name(), func(t *testing.T) {
specYAML, err := os.ReadFile(filepath.Join(path, "value.yaml"))
require.NoError(t, err)
var res phase0.AggregateAndProof
require.NoError(t, yaml.Unmarshal(specYAML, &res))

compressedSpecSSZ, err := os.ReadFile(filepath.Join(path, "serialized.ssz_snappy"))
require.NoError(t, err)
var specSSZ []byte
specSSZ, err = snappy.Decode(specSSZ, compressedSpecSSZ)
require.NoError(t, err)

unmarshalled := &phase0.AggregateAndProof{}
require.NoError(t, unmarshalled.UnmarshalSSZ(specSSZ))
remarshalled, err := unmarshalled.MarshalSSZ()
require.NoError(t, err)
require.Equal(t, specSSZ, remarshalled)

ssz, err := res.MarshalSSZ()
require.NoError(t, err)
require.Equal(t, specSSZ, ssz)

root, err := res.HashTreeRoot()
require.NoError(t, err)
rootsYAML, err := os.ReadFile(filepath.Join(path, "roots.yaml"))
require.NoError(t, err)
require.Equal(t, string(rootsYAML), fmt.Sprintf("{root: '%#x'}\n", root))
})
}
return nil
}))
}
51 changes: 1 addition & 50 deletions spec/phase0/attestation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@ package phase0_test
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"

"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/goccy/go-yaml"
"github.com/golang/snappy"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAttestationJSON(t *testing.T) {
Expand Down Expand Up @@ -145,48 +141,3 @@ func TestAttestationYAML(t *testing.T) {
})
}
}

func TestAttestationSpec(t *testing.T) {
if os.Getenv("ETH2_SPEC_TESTS_DIR") == "" {
t.Skip("ETH2_SPEC_TESTS_DIR not suppplied, not running spec tests")
}
baseDir := filepath.Join(os.Getenv("ETH2_SPEC_TESTS_DIR"), "tests", "mainnet", "phase0", "ssz_static", "Attestation", "ssz_random")
require.NoError(t, filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error {
if path == baseDir {
// Only interested in subdirectories.
return nil
}
require.NoError(t, err)
if info.IsDir() {
t.Run(info.Name(), func(t *testing.T) {
specYAML, err := os.ReadFile(filepath.Join(path, "value.yaml"))
require.NoError(t, err)
var res phase0.Attestation
require.NoError(t, yaml.Unmarshal(specYAML, &res))

compressedSpecSSZ, err := os.ReadFile(filepath.Join(path, "serialized.ssz_snappy"))
require.NoError(t, err)
var specSSZ []byte
specSSZ, err = snappy.Decode(specSSZ, compressedSpecSSZ)
require.NoError(t, err)

unmarshalled := &phase0.Attestation{}
require.NoError(t, unmarshalled.UnmarshalSSZ(specSSZ))
remarshalled, err := unmarshalled.MarshalSSZ()
require.NoError(t, err)
require.Equal(t, specSSZ, remarshalled)

ssz, err := res.MarshalSSZ()
require.NoError(t, err)
require.Equal(t, specSSZ, ssz)

root, err := res.HashTreeRoot()
require.NoError(t, err)
rootsYAML, err := os.ReadFile(filepath.Join(path, "roots.yaml"))
require.NoError(t, err)
require.Equal(t, string(rootsYAML), fmt.Sprintf("{root: '%#x'}\n", root))
})
}
return nil
}))
}
51 changes: 1 addition & 50 deletions spec/phase0/attestationdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@ package phase0_test
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"

"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/goccy/go-yaml"
"github.com/golang/snappy"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAttestationDataJSON(t *testing.T) {
Expand Down Expand Up @@ -200,48 +196,3 @@ func TestAttestationDataYAML(t *testing.T) {
})
}
}

func TestAttestationDataSpec(t *testing.T) {
if os.Getenv("ETH2_SPEC_TESTS_DIR") == "" {
t.Skip("ETH2_SPEC_TESTS_DIR not suppplied, not running spec tests")
}
baseDir := filepath.Join(os.Getenv("ETH2_SPEC_TESTS_DIR"), "tests", "mainnet", "phase0", "ssz_static", "AttestationData", "ssz_random")
require.NoError(t, filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error {
if path == baseDir {
// Only interested in subdirectories.
return nil
}
require.NoError(t, err)
if info.IsDir() {
t.Run(info.Name(), func(t *testing.T) {
specYAML, err := os.ReadFile(filepath.Join(path, "value.yaml"))
require.NoError(t, err)
var res phase0.AttestationData
require.NoError(t, yaml.Unmarshal(specYAML, &res))

compressedSpecSSZ, err := os.ReadFile(filepath.Join(path, "serialized.ssz_snappy"))
require.NoError(t, err)
var specSSZ []byte
specSSZ, err = snappy.Decode(specSSZ, compressedSpecSSZ)
require.NoError(t, err)

unmarshalled := &phase0.AttestationData{}
require.NoError(t, unmarshalled.UnmarshalSSZ(specSSZ))
remarshalled, err := unmarshalled.MarshalSSZ()
require.NoError(t, err)
require.Equal(t, specSSZ, remarshalled)

ssz, err := res.MarshalSSZ()
require.NoError(t, err)
require.Equal(t, specSSZ, ssz)

root, err := res.HashTreeRoot()
require.NoError(t, err)
rootsYAML, err := os.ReadFile(filepath.Join(path, "roots.yaml"))
require.NoError(t, err)
require.Equal(t, string(rootsYAML), fmt.Sprintf("{root: '%#x'}\n", root))
})
}
return nil
}))
}
Loading

0 comments on commit 2ba62f6

Please sign in to comment.