diff --git a/README.md b/README.md index 841e152..f1eda00 100644 --- a/README.md +++ b/README.md @@ -65,12 +65,15 @@ spectest subcommand =================== The `generated` directory is a good place to stick generated tests as it is already in the .gitignore for the project. Assuming the tarball in the example above has been downloaded to the repo directory, running the following command there will generate spectests for all prysm types described in the test fixture yaml config: -``` -go run ./cmd/ssz spectest --release-uri=file://$PWD/mainnet-v1.3.0-rc.2.tar.gz --config=$PWD/specs/testdata/prysm.yaml --output=$PWD/generated +```bash +go run ./cmd/ssz spectest --release-uri=file://$PWD/mainnet-v1.3.0-rc.2.tar.gz --config=$PWD/specs/testdata/prysm.yaml --output=$PWD/spectests/v1alpha1 + +go run ./cmd/ssz spectest --release-uri=file://$PWD/mainnet-v1.3.0-rc.2.tar.gz --config=$PWD/specs/testdata/engine-prysm.yaml --output=$PWD/spectests/engine ``` Run the spectest like normal go tests: ``` -go test ./generated -ok github.com/OffchainLabs/methodical-ssz/generated 1.003s +go test ./spectests/... +ok github.com/OffchainLabs/methodical-ssz/spectests/engine 0.110s +ok github.com/OffchainLabs/methodical-ssz/spectests/v1alpha1 1.112s ``` diff --git a/cmd/ssz/generate.go b/cmd/ssz/generate.go index cdcbccb..c1caf2d 100644 --- a/cmd/ssz/generate.go +++ b/cmd/ssz/generate.go @@ -9,6 +9,7 @@ import ( "github.com/OffchainLabs/methodical-ssz/sszgen" "github.com/OffchainLabs/methodical-ssz/sszgen/backend" + "github.com/pkg/errors" "github.com/urfave/cli/v2" ) @@ -41,8 +42,11 @@ var generate = &cli.Command{ Action: func(c *cli.Context) error { sourcePackage = c.Args().Get(0) if sourcePackage == "" { - cli.ShowCommandHelp(c, "generate") - return fmt.Errorf("error: mising required argument") + missingErr := fmt.Errorf("error: missing required argument") + if err := cli.ShowCommandHelp(c, "generate"); err != nil { + return errors.Wrapf(missingErr, "unable to show help text, ShowCommandHelp error=%s", err.Error()) + } + return missingErr } var err error var fields []string diff --git a/go.mod b/go.mod index 3a4c944..9f87f60 100644 --- a/go.mod +++ b/go.mod @@ -30,18 +30,19 @@ require ( github.com/klauspost/cpuid/v2 v2.2.1 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect github.com/minio/sha256-simd v1.0.0 // indirect - github.com/mitchellh/mapstructure v1.4.1 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/prometheus/client_golang v1.14.0 // indirect github.com/prometheus/client_model v0.3.0 // indirect github.com/prometheus/common v0.39.0 // indirect github.com/prometheus/procfs v0.9.0 // indirect github.com/prysmaticlabs/gohashtree v0.0.2-alpha // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect + github.com/stretchr/testify v1.8.2 // indirect github.com/thomaso-mirodin/intmath v0.0.0-20160323211736-5dc6d854e46e // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect golang.org/x/crypto v0.5.0 // indirect golang.org/x/mod v0.8.0 // indirect - golang.org/x/sys v0.5.0 // indirect + golang.org/x/sys v0.6.0 // indirect golang.org/x/text v0.7.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index ed3d4bc..377ca12 100644 --- a/go.sum +++ b/go.sum @@ -52,8 +52,8 @@ github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zk github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= github.com/minio/sha256-simd v1.0.0 h1:v1ta+49hkWZyvaKwrQB8elexRqm6Y0aMLjCNsrYxo6g= github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM= -github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag= -github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw= github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= @@ -88,8 +88,13 @@ github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVs github.com/spf13/afero v1.2.2 h1:5jhuqJyZCZf2JRofRvN/nIFgIWNzPa3/Vz8mYylgbWc= github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d h1:vfofYNRScrDdvS342BElfbETmL1Aiz3i2t0zfRj16Hs= github.com/thomaso-mirodin/intmath v0.0.0-20160323211736-5dc6d854e46e h1:cR8/SYRgyQCt5cNCMniB/ZScMkhI9nk8U5C7SbISXjo= github.com/thomaso-mirodin/intmath v0.0.0-20160323211736-5dc6d854e46e/go.mod h1:Tu4lItkATkonrYuvtVjG0/rhy15qrNGNTjPdaphtZ/8= @@ -110,8 +115,8 @@ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= diff --git a/specs/testdata/engine-prysm.yaml b/specs/testdata/engine-prysm.yaml new file mode 100644 index 0000000..9d0b0db --- /dev/null +++ b/specs/testdata/engine-prysm.yaml @@ -0,0 +1,14 @@ +package: github.com/prysmaticlabs/prysm/v3/proto/engine/v1 +preset: mainnet +defs: + - fork: bellatrix + types: + - name: ExecutionPayload + - name: ExecutionPayloadHeader + - fork: capella + types: + - name: ExecutionPayload + type_name: ExecutionPayloadCapella + - name: ExecutionPayloadHeader + type_name: ExecutionPayloadHeaderCapella + - name: Withdrawal diff --git a/sszgen/backend/container_test.go b/sszgen/backend/container_test.go index 94f4082..c245dad 100644 --- a/sszgen/backend/container_test.go +++ b/sszgen/backend/container_test.go @@ -22,32 +22,4 @@ func normalizeFixtureString(raw string) (string, error) { return normalizeFixtureBytes([]byte(raw)) } -var testFixStatus types.ValRep = &types.ValueContainer{Name: "Status", Package: "github.com/prysmaticlabs/prysm/v3/proto/beacon/p2p/v1", Contents: []types.ContainerField{{Key: "ForkDigest", Value: &types.ValueVector{Size: 4, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "FinalizedRoot", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "FinalizedEpoch", Value: &types.ValueOverlay{Name: "Epoch", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "HeadRoot", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "HeadSlot", Value: &types.ValueOverlay{Name: "Slot", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}}} - -var testFixBeaconBlocksByRangeRequest types.ValRep = &types.ValueContainer{Name: "BeaconBlocksByRangeRequest", Package: "github.com/prysmaticlabs/prysm/v3/proto/beacon/p2p/v1", Contents: []types.ContainerField{{Key: "StartSlot", Value: &types.ValueOverlay{Name: "Slot", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "Count", Value: &types.ValueUint{Name: "uint64", Size: 64}}, {Key: "Step", Value: &types.ValueUint{Name: "uint64", Size: 64}}}} - -var testFixENRForkID types.ValRep = &types.ValueContainer{Name: "ENRForkID", Package: "github.com/prysmaticlabs/prysm/v3/proto/beacon/p2p/v1", Contents: []types.ContainerField{{Key: "CurrentForkDigest", Value: &types.ValueVector{Size: 4, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "NextForkVersion", Value: &types.ValueVector{Size: 4, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "NextForkEpoch", Value: &types.ValueOverlay{Name: "Epoch", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}}} - -var testFixMetaDataV0 types.ValRep = &types.ValueContainer{Name: "MetaDataV0", Package: "github.com/prysmaticlabs/prysm/v3/proto/beacon/p2p/v1", Contents: []types.ContainerField{{Key: "SeqNumber", Value: &types.ValueUint{Name: "uint64", Size: 64}}, {Key: "Attnets", Value: &types.ValueOverlay{Name: "Bitvector64", Package: "github.com/prysmaticlabs/go-bitfield", Underlying: &types.ValueVector{Size: 8, ElementValue: &types.ValueByte{Name: "byte"}}}}}} - -var testFixMetaDataV1 types.ValRep = &types.ValueContainer{Name: "MetaDataV1", Package: "github.com/prysmaticlabs/prysm/v3/proto/beacon/p2p/v1", Contents: []types.ContainerField{{Key: "SeqNumber", Value: &types.ValueUint{Name: "uint64", Size: 64}}, {Key: "Attnets", Value: &types.ValueOverlay{Name: "Bitvector64", Package: "github.com/prysmaticlabs/go-bitfield", Underlying: &types.ValueVector{Size: 8, ElementValue: &types.ValueByte{Name: "byte"}}}}, {Key: "Syncnets", Value: &types.ValueOverlay{Name: "Bitvector512", Package: "github.com/prysmaticlabs/go-bitfield", Underlying: &types.ValueVector{Size: 64, ElementValue: &types.ValueByte{Name: "byte"}}}}}} - var testFixBeaconState types.ValRep = &types.ValueContainer{Name: "BeaconState", Package: "github.com/prysmaticlabs/prysm/v3/proto/beacon/p2p/v1", Contents: []types.ContainerField{{Key: "GenesisTime", Value: &types.ValueUint{Name: "uint64", Size: 64}}, {Key: "GenesisValidatorsRoot", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "Slot", Value: &types.ValueOverlay{Name: "Slot", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "Fork", Value: &types.ValuePointer{Referent: &types.ValueContainer{Name: "Fork", Package: "github.com/prysmaticlabs/prysm/v3/proto/beacon/p2p/v1", Contents: []types.ContainerField{{Key: "PreviousVersion", Value: &types.ValueVector{Size: 4, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "CurrentVersion", Value: &types.ValueVector{Size: 4, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "Epoch", Value: &types.ValueOverlay{Name: "Epoch", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}}}}}, {Key: "LatestBlockHeader", Value: &types.ValuePointer{Referent: &types.ValueContainer{Name: "BeaconBlockHeader", Package: "github.com/prysmaticlabs/prysm/v3/proto/eth/v1alpha1", Contents: []types.ContainerField{{Key: "Slot", Value: &types.ValueOverlay{Name: "Slot", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "ProposerIndex", Value: &types.ValueOverlay{Name: "ValidatorIndex", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "ParentRoot", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "StateRoot", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "BodyRoot", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}}}}, {Key: "BlockRoots", Value: &types.ValueVector{Size: 8192, ElementValue: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}, {Key: "StateRoots", Value: &types.ValueVector{Size: 8192, ElementValue: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}, {Key: "HistoricalRoots", Value: &types.ValueList{MaxSize: 16777216, ElementValue: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}, {Key: "Eth1Data", Value: &types.ValuePointer{Referent: &types.ValueContainer{Name: "Eth1Data", Package: "github.com/prysmaticlabs/prysm/v3/proto/eth/v1alpha1", Contents: []types.ContainerField{{Key: "DepositRoot", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "DepositCount", Value: &types.ValueUint{Name: "uint64", Size: 64}}, {Key: "BlockHash", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}}}}, {Key: "Eth1DataVotes", Value: &types.ValueList{MaxSize: 2048, ElementValue: &types.ValuePointer{Referent: &types.ValueContainer{Name: "Eth1Data", Package: "github.com/prysmaticlabs/prysm/v3/proto/eth/v1alpha1", Contents: []types.ContainerField{{Key: "DepositRoot", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "DepositCount", Value: &types.ValueUint{Name: "uint64", Size: 64}}, {Key: "BlockHash", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}}}}}, {Key: "Eth1DepositIndex", Value: &types.ValueUint{Name: "uint64", Size: 64}}, {Key: "Validators", Value: &types.ValueList{MaxSize: 1099511627776, ElementValue: &types.ValuePointer{Referent: &types.ValueContainer{Name: "Validator", Package: "github.com/prysmaticlabs/prysm/v3/proto/eth/v1alpha1", Contents: []types.ContainerField{{Key: "PublicKey", Value: &types.ValueVector{Size: 48, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "WithdrawalCredentials", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "EffectiveBalance", Value: &types.ValueUint{Name: "uint64", Size: 64}}, {Key: "Slashed", Value: &types.ValueBool{Name: "bool"}}, {Key: "ActivationEligibilityEpoch", Value: &types.ValueOverlay{Name: "Epoch", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "ActivationEpoch", Value: &types.ValueOverlay{Name: "Epoch", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "ExitEpoch", Value: &types.ValueOverlay{Name: "Epoch", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "WithdrawableEpoch", Value: &types.ValueOverlay{Name: "Epoch", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}}}}}}, {Key: "Balances", Value: &types.ValueList{MaxSize: 1099511627776, ElementValue: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "RandaoMixes", Value: &types.ValueVector{Size: 65536, ElementValue: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}, {Key: "Slashings", Value: &types.ValueVector{Size: 8192, ElementValue: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "PreviousEpochAttestations", Value: &types.ValueList{MaxSize: 4096, ElementValue: &types.ValuePointer{Referent: &types.ValueContainer{Name: "PendingAttestation", Package: "github.com/prysmaticlabs/prysm/v3/proto/beacon/p2p/v1", Contents: []types.ContainerField{{Key: "AggregationBits", Value: &types.ValueOverlay{Name: "Bitlist", Package: "github.com/prysmaticlabs/go-bitfield", Underlying: &types.ValueList{MaxSize: 2048, ElementValue: &types.ValueByte{Name: "byte"}}}}, {Key: "Data", Value: &types.ValuePointer{Referent: &types.ValueContainer{Name: "AttestationData", Package: "github.com/prysmaticlabs/prysm/v3/proto/eth/v1alpha1", Contents: []types.ContainerField{{Key: "Slot", Value: &types.ValueOverlay{Name: "Slot", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "CommitteeIndex", Value: &types.ValueOverlay{Name: "CommitteeIndex", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "BeaconBlockRoot", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "Source", Value: &types.ValuePointer{Referent: &types.ValueContainer{Name: "Checkpoint", Package: "github.com/prysmaticlabs/prysm/v3/proto/eth/v1alpha1", Contents: []types.ContainerField{{Key: "Epoch", Value: &types.ValueOverlay{Name: "Epoch", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "Root", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}}}}, {Key: "Target", Value: &types.ValuePointer{Referent: &types.ValueContainer{Name: "Checkpoint", Package: "github.com/prysmaticlabs/prysm/v3/proto/eth/v1alpha1", Contents: []types.ContainerField{{Key: "Epoch", Value: &types.ValueOverlay{Name: "Epoch", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "Root", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}}}}}}}}, {Key: "InclusionDelay", Value: &types.ValueOverlay{Name: "Slot", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "ProposerIndex", Value: &types.ValueOverlay{Name: "ValidatorIndex", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}}}}}}, {Key: "CurrentEpochAttestations", Value: &types.ValueList{MaxSize: 4096, ElementValue: &types.ValuePointer{Referent: &types.ValueContainer{Name: "PendingAttestation", Package: "github.com/prysmaticlabs/prysm/v3/proto/beacon/p2p/v1", Contents: []types.ContainerField{{Key: "AggregationBits", Value: &types.ValueOverlay{Name: "Bitlist", Package: "github.com/prysmaticlabs/go-bitfield", Underlying: &types.ValueList{MaxSize: 2048, ElementValue: &types.ValueByte{Name: "byte"}}}}, {Key: "Data", Value: &types.ValuePointer{Referent: &types.ValueContainer{Name: "AttestationData", Package: "github.com/prysmaticlabs/prysm/v3/proto/eth/v1alpha1", Contents: []types.ContainerField{{Key: "Slot", Value: &types.ValueOverlay{Name: "Slot", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "CommitteeIndex", Value: &types.ValueOverlay{Name: "CommitteeIndex", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "BeaconBlockRoot", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "Source", Value: &types.ValuePointer{Referent: &types.ValueContainer{Name: "Checkpoint", Package: "github.com/prysmaticlabs/prysm/v3/proto/eth/v1alpha1", Contents: []types.ContainerField{{Key: "Epoch", Value: &types.ValueOverlay{Name: "Epoch", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "Root", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}}}}, {Key: "Target", Value: &types.ValuePointer{Referent: &types.ValueContainer{Name: "Checkpoint", Package: "github.com/prysmaticlabs/prysm/v3/proto/eth/v1alpha1", Contents: []types.ContainerField{{Key: "Epoch", Value: &types.ValueOverlay{Name: "Epoch", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "Root", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}}}}}}}}, {Key: "InclusionDelay", Value: &types.ValueOverlay{Name: "Slot", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "ProposerIndex", Value: &types.ValueOverlay{Name: "ValidatorIndex", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}}}}}}, {Key: "JustificationBits", Value: &types.ValueOverlay{Name: "Bitvector4", Package: "github.com/prysmaticlabs/go-bitfield", Underlying: &types.ValueVector{Size: 1, ElementValue: &types.ValueByte{Name: "byte"}}}}, {Key: "PreviousJustifiedCheckpoint", Value: &types.ValuePointer{Referent: &types.ValueContainer{Name: "Checkpoint", Package: "github.com/prysmaticlabs/prysm/v3/proto/eth/v1alpha1", Contents: []types.ContainerField{{Key: "Epoch", Value: &types.ValueOverlay{Name: "Epoch", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "Root", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}}}}, {Key: "CurrentJustifiedCheckpoint", Value: &types.ValuePointer{Referent: &types.ValueContainer{Name: "Checkpoint", Package: "github.com/prysmaticlabs/prysm/v3/proto/eth/v1alpha1", Contents: []types.ContainerField{{Key: "Epoch", Value: &types.ValueOverlay{Name: "Epoch", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "Root", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}}}}, {Key: "FinalizedCheckpoint", Value: &types.ValuePointer{Referent: &types.ValueContainer{Name: "Checkpoint", Package: "github.com/prysmaticlabs/prysm/v3/proto/eth/v1alpha1", Contents: []types.ContainerField{{Key: "Epoch", Value: &types.ValueOverlay{Name: "Epoch", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "Root", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}}}}}} - -var testFixBeaconStateAltair types.ValRep = &types.ValueContainer{Name: "BeaconStateAltair", Package: "github.com/prysmaticlabs/prysm/v3/proto/beacon/p2p/v1", Contents: []types.ContainerField{{Key: "GenesisTime", Value: &types.ValueUint{Name: "uint64", Size: 64}}, {Key: "GenesisValidatorsRoot", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "Slot", Value: &types.ValueOverlay{Name: "Slot", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "Fork", Value: &types.ValuePointer{Referent: &types.ValueContainer{Name: "Fork", Package: "github.com/prysmaticlabs/prysm/v3/proto/beacon/p2p/v1", Contents: []types.ContainerField{{Key: "PreviousVersion", Value: &types.ValueVector{Size: 4, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "CurrentVersion", Value: &types.ValueVector{Size: 4, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "Epoch", Value: &types.ValueOverlay{Name: "Epoch", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}}}}}, {Key: "LatestBlockHeader", Value: &types.ValuePointer{Referent: &types.ValueContainer{Name: "BeaconBlockHeader", Package: "github.com/prysmaticlabs/prysm/v3/proto/eth/v1alpha1", Contents: []types.ContainerField{{Key: "Slot", Value: &types.ValueOverlay{Name: "Slot", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "ProposerIndex", Value: &types.ValueOverlay{Name: "ValidatorIndex", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "ParentRoot", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "StateRoot", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "BodyRoot", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}}}}, {Key: "BlockRoots", Value: &types.ValueVector{Size: 8192, ElementValue: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}, {Key: "StateRoots", Value: &types.ValueVector{Size: 8192, ElementValue: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}, {Key: "HistoricalRoots", Value: &types.ValueList{MaxSize: 16777216, ElementValue: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}, {Key: "Eth1Data", Value: &types.ValuePointer{Referent: &types.ValueContainer{Name: "Eth1Data", Package: "github.com/prysmaticlabs/prysm/v3/proto/eth/v1alpha1", Contents: []types.ContainerField{{Key: "DepositRoot", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "DepositCount", Value: &types.ValueUint{Name: "uint64", Size: 64}}, {Key: "BlockHash", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}}}}, {Key: "Eth1DataVotes", Value: &types.ValueList{MaxSize: 2048, ElementValue: &types.ValuePointer{Referent: &types.ValueContainer{Name: "Eth1Data", Package: "github.com/prysmaticlabs/prysm/v3/proto/eth/v1alpha1", Contents: []types.ContainerField{{Key: "DepositRoot", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "DepositCount", Value: &types.ValueUint{Name: "uint64", Size: 64}}, {Key: "BlockHash", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}}}}}, {Key: "Eth1DepositIndex", Value: &types.ValueUint{Name: "uint64", Size: 64}}, {Key: "Validators", Value: &types.ValueList{MaxSize: 1099511627776, ElementValue: &types.ValuePointer{Referent: &types.ValueContainer{Name: "Validator", Package: "github.com/prysmaticlabs/prysm/v3/proto/eth/v1alpha1", Contents: []types.ContainerField{{Key: "PublicKey", Value: &types.ValueVector{Size: 48, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "WithdrawalCredentials", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "EffectiveBalance", Value: &types.ValueUint{Name: "uint64", Size: 64}}, {Key: "Slashed", Value: &types.ValueBool{Name: "bool"}}, {Key: "ActivationEligibilityEpoch", Value: &types.ValueOverlay{Name: "Epoch", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "ActivationEpoch", Value: &types.ValueOverlay{Name: "Epoch", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "ExitEpoch", Value: &types.ValueOverlay{Name: "Epoch", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "WithdrawableEpoch", Value: &types.ValueOverlay{Name: "Epoch", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}}}}}}, {Key: "Balances", Value: &types.ValueList{MaxSize: 1099511627776, ElementValue: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "RandaoMixes", Value: &types.ValueVector{Size: 65536, ElementValue: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}, {Key: "Slashings", Value: &types.ValueVector{Size: 8192, ElementValue: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "PreviousEpochParticipation", Value: &types.ValueList{MaxSize: 1099511627776, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "CurrentEpochParticipation", Value: &types.ValueList{MaxSize: 1099511627776, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "JustificationBits", Value: &types.ValueOverlay{Name: "Bitvector4", Package: "github.com/prysmaticlabs/go-bitfield", Underlying: &types.ValueVector{Size: 1, ElementValue: &types.ValueByte{Name: "byte"}}}}, {Key: "PreviousJustifiedCheckpoint", Value: &types.ValuePointer{Referent: &types.ValueContainer{Name: "Checkpoint", Package: "github.com/prysmaticlabs/prysm/v3/proto/eth/v1alpha1", Contents: []types.ContainerField{{Key: "Epoch", Value: &types.ValueOverlay{Name: "Epoch", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "Root", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}}}}, {Key: "CurrentJustifiedCheckpoint", Value: &types.ValuePointer{Referent: &types.ValueContainer{Name: "Checkpoint", Package: "github.com/prysmaticlabs/prysm/v3/proto/eth/v1alpha1", Contents: []types.ContainerField{{Key: "Epoch", Value: &types.ValueOverlay{Name: "Epoch", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "Root", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}}}}, {Key: "FinalizedCheckpoint", Value: &types.ValuePointer{Referent: &types.ValueContainer{Name: "Checkpoint", Package: "github.com/prysmaticlabs/prysm/v3/proto/eth/v1alpha1", Contents: []types.ContainerField{{Key: "Epoch", Value: &types.ValueOverlay{Name: "Epoch", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "Root", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}}}}, {Key: "InactivityScores", Value: &types.ValueList{MaxSize: 1099511627776, ElementValue: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "CurrentSyncCommittee", Value: &types.ValuePointer{Referent: &types.ValueContainer{Name: "SyncCommittee", Package: "github.com/prysmaticlabs/prysm/v3/proto/beacon/p2p/v1", Contents: []types.ContainerField{{Key: "Pubkeys", Value: &types.ValueVector{Size: 512, ElementValue: &types.ValueVector{Size: 48, ElementValue: &types.ValueByte{Name: "byte"}}}}, {Key: "AggregatePubkey", Value: &types.ValueVector{Size: 48, ElementValue: &types.ValueByte{Name: "byte"}}}}}}}, {Key: "NextSyncCommittee", Value: &types.ValuePointer{Referent: &types.ValueContainer{Name: "SyncCommittee", Package: "github.com/prysmaticlabs/prysm/v3/proto/beacon/p2p/v1", Contents: []types.ContainerField{{Key: "Pubkeys", Value: &types.ValueVector{Size: 512, ElementValue: &types.ValueVector{Size: 48, ElementValue: &types.ValueByte{Name: "byte"}}}}, {Key: "AggregatePubkey", Value: &types.ValueVector{Size: 48, ElementValue: &types.ValueByte{Name: "byte"}}}}}}}}} - -var testFixFork types.ValRep = &types.ValueContainer{Name: "Fork", Package: "github.com/prysmaticlabs/prysm/v3/proto/beacon/p2p/v1", Contents: []types.ContainerField{{Key: "PreviousVersion", Value: &types.ValueVector{Size: 4, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "CurrentVersion", Value: &types.ValueVector{Size: 4, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "Epoch", Value: &types.ValueOverlay{Name: "Epoch", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}}} - -var testFixPendingAttestation types.ValRep = &types.ValueContainer{Name: "PendingAttestation", Package: "github.com/prysmaticlabs/prysm/v3/proto/beacon/p2p/v1", Contents: []types.ContainerField{{Key: "AggregationBits", Value: &types.ValueOverlay{Name: "Bitlist", Package: "github.com/prysmaticlabs/go-bitfield", Underlying: &types.ValueList{MaxSize: 2048, ElementValue: &types.ValueByte{Name: "byte"}}}}, {Key: "Data", Value: &types.ValuePointer{Referent: &types.ValueContainer{Name: "AttestationData", Package: "github.com/prysmaticlabs/prysm/v3/proto/eth/v1alpha1", Contents: []types.ContainerField{{Key: "Slot", Value: &types.ValueOverlay{Name: "Slot", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "CommitteeIndex", Value: &types.ValueOverlay{Name: "CommitteeIndex", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "BeaconBlockRoot", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "Source", Value: &types.ValuePointer{Referent: &types.ValueContainer{Name: "Checkpoint", Package: "github.com/prysmaticlabs/prysm/v3/proto/eth/v1alpha1", Contents: []types.ContainerField{{Key: "Epoch", Value: &types.ValueOverlay{Name: "Epoch", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "Root", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}}}}, {Key: "Target", Value: &types.ValuePointer{Referent: &types.ValueContainer{Name: "Checkpoint", Package: "github.com/prysmaticlabs/prysm/v3/proto/eth/v1alpha1", Contents: []types.ContainerField{{Key: "Epoch", Value: &types.ValueOverlay{Name: "Epoch", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "Root", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}}}}}}}}, {Key: "InclusionDelay", Value: &types.ValueOverlay{Name: "Slot", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}, {Key: "ProposerIndex", Value: &types.ValueOverlay{Name: "ValidatorIndex", Package: "github.com/prysmaticlabs/eth2-types", Underlying: &types.ValueUint{Name: "uint64", Size: 64}}}}} - -var testFixHistoricalBatch types.ValRep = &types.ValueContainer{Name: "HistoricalBatch", Package: "github.com/prysmaticlabs/prysm/v3/proto/beacon/p2p/v1", Contents: []types.ContainerField{{Key: "BlockRoots", Value: &types.ValueVector{Size: 8192, ElementValue: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}, {Key: "StateRoots", Value: &types.ValueVector{Size: 8192, ElementValue: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}}} - -var testFixSigningData types.ValRep = &types.ValueContainer{Name: "SigningData", Package: "github.com/prysmaticlabs/prysm/v3/proto/beacon/p2p/v1", Contents: []types.ContainerField{{Key: "ObjectRoot", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "Domain", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}} - -var testFixForkData types.ValRep = &types.ValueContainer{Name: "ForkData", Package: "github.com/prysmaticlabs/prysm/v3/proto/beacon/p2p/v1", Contents: []types.ContainerField{{Key: "CurrentVersion", Value: &types.ValueVector{Size: 4, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "GenesisValidatorsRoot", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}}} - -var testFixDepositMessage types.ValRep = &types.ValueContainer{Name: "DepositMessage", Package: "github.com/prysmaticlabs/prysm/v3/proto/beacon/p2p/v1", Contents: []types.ContainerField{{Key: "PublicKey", Value: &types.ValueVector{Size: 48, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "WithdrawalCredentials", Value: &types.ValueVector{Size: 32, ElementValue: &types.ValueByte{Name: "byte"}}}, {Key: "Amount", Value: &types.ValueUint{Name: "uint64", Size: 64}}}} - -var testFixSyncCommittee types.ValRep = &types.ValueContainer{Name: "SyncCommittee", Package: "github.com/prysmaticlabs/prysm/v3/proto/beacon/p2p/v1", Contents: []types.ContainerField{{Key: "Pubkeys", Value: &types.ValueVector{Size: 512, ElementValue: &types.ValueVector{Size: 48, ElementValue: &types.ValueByte{Name: "byte"}}}}, {Key: "AggregatePubkey", Value: &types.ValueVector{Size: 48, ElementValue: &types.ValueByte{Name: "byte"}}}}} - -var testFixSyncAggregatorSelectionData types.ValRep = &types.ValueContainer{Name: "SyncAggregatorSelectionData", Package: "github.com/prysmaticlabs/prysm/v3/proto/beacon/p2p/v1", Contents: []types.ContainerField{{Key: "Slot", Value: &types.ValueUint{Name: "uint64", Size: 64}}, {Key: "SubcommitteeIndex", Value: &types.ValueUint{Name: "uint64", Size: 64}}}} diff --git a/sszgen/backend/genunmarshal.go b/sszgen/backend/genunmarshal.go index 7da33e3..821abe0 100644 --- a/sszgen/backend/genunmarshal.go +++ b/sszgen/backend/genunmarshal.go @@ -45,25 +45,8 @@ func GenerateUnmarshalSSZ(g *generateContainer) (*generatedCode, error) { sliceName := fmt.Sprintf("s%d", i) mv := mg.generateUnmarshalValue(fieldName, sliceName) if mv != "" { - //unmarshalBlocks = append(unmarshalBlocks, fmt.Sprintf("\t%s = %s", fieldName, mv)) unmarshalBlocks = append(unmarshalBlocks, mv) } - - /* - if !c.Value.IsVariableSized() { - continue - } - _, ok := mg.(variableUnmarshaller) - if !ok { - continue - } - vm := mg.(variableUnmarshaller) - vmc := vm.generateVariableUnmarshalValue(fieldName) - if vmc != "" { - unmarshalVariableBlocks = append(unmarshalVariableBlocks, fmt.Sprintf("\n\t// Field %d: %s", i, c.Key)) - unmarshalVariableBlocks = append(unmarshalVariableBlocks, "\t" + vmc) - } - */ } sliceDeclarations := strings.Join([]string{ums.fixedSlices(), "", ums.variableSlices(g.fixedOffset())}, "\n") diff --git a/sszgen/backend/list.go b/sszgen/backend/list.go index b5407dc..fba1f4b 100644 --- a/sszgen/backend/list.go +++ b/sszgen/backend/list.go @@ -73,14 +73,14 @@ func (g *generateList) generateHTRPutter(fieldName string) string { switch v := vr.(type) { case *types.ValueByte: t := `if len(%s) > %d { - return ssz.ErrBytesLength + return ssz.ErrIncorrectListSize } subIndx := hh.Index() - hh.PutBytes(%s)` + hh.AppendBytes32(%s)` putBytes := fmt.Sprintf(t, fieldName, g.valRep.MaxSize, fieldName) mtmpl := `numItems := uint64(len(%s)) - hh.MerkleizeWithMixin(subIndx, numItems, (%d*%d + 31)/32)` - res := "\n{\n" + putBytes + "\n" + fmt.Sprintf(mtmpl, fieldName, g.valRep.MaxSize, v.FixedSize()) + "\n}\n" + hh.MerkleizeWithMixin(subIndx, numItems, (%d + 31)/32)` + res := "\n{\n" + putBytes + "\n" + fmt.Sprintf(mtmpl, fieldName, g.valRep.MaxSize) + "\n}\n" return res case *types.ValueVector: gv := &generateVector{valRep: v, targetPackage: g.targetPackage} @@ -105,6 +105,11 @@ func (g *generateList) generateHTRPutter(fieldName string) string { lpe.AppendCall = gc.generateHTRPutter(nestedFieldName) lpe.Merkleize = fmt.Sprintf("hh.MerkleizeWithMixin(subIndx, uint64(len(%s)), %d)", fieldName, g.valRep.MaxSize) return renderHtrListPutter(lpe) + case *types.ValueList: + gc := newValueGenerator(interfaces.SszLightHasher, v, g.targetPackage, g.importNamer) + lpe.AppendCall = gc.generateHTRPutter(nestedFieldName) + lpe.Merkleize = fmt.Sprintf("hh.MerkleizeWithMixin(subIndx, uint64(len(%s)), %d)", fieldName, g.valRep.MaxSize) + return renderHtrListPutter(lpe) default: panic(fmt.Sprintf("unsupported type combination - list of %v", v)) } @@ -310,17 +315,6 @@ func (g *generateList) variableSizeSSZ(fieldName string) string { return buf.String() } -var generateVariableMarshalValueTmpl = `if len({{ .FieldName }}) > {{ .MaxSize }} { - return nil, ssz.ErrListTooBig -} - -for _, o := range {{ .FieldName }} { - if len(o) != {{ .ElementSize }} { - return nil, ssz.ErrBytesLength - } - dst = append(dst, o) -}` - var tmplVariableOffsetManagement = `{ offset = 4 * len({{.FieldName}}) for _, {{.NestedFieldName}} := range {{.FieldName}} { diff --git a/sszgen/backend/overlay.go b/sszgen/backend/overlay.go index a574017..1f7d713 100644 --- a/sszgen/backend/overlay.go +++ b/sszgen/backend/overlay.go @@ -61,7 +61,17 @@ func (g *generateOverlay) generateFixedMarshalValue(fieldName string) string { } func (g *generateOverlay) variableSizeSSZ(fieldname string) string { - return "" + l, ok := g.Underlying.(*types.ValueList) + if !ok { + panic("unhandled case, non-list variable sized overlay") + } + if l.ElementValue.IsVariableSized() { + panic("unhandled case, overlay of list of variable-sized values") + } + if l.ElementValue.FixedSize() == 1 { + return fmt.Sprintf("len(%s)", fieldname) + } + return fmt.Sprintf("len(%s) * %d", fieldname, l.ElementValue.FixedSize()) } func (g *generateOverlay) generateHTRPutter(fieldName string) string { diff --git a/sszgen/backend/render.go b/sszgen/backend/render.go index c42a6b2..f6e2288 100644 --- a/sszgen/backend/render.go +++ b/sszgen/backend/render.go @@ -143,10 +143,6 @@ type variableMarshaller interface { generateVariableMarshalValue(string) string } -type variableUnmarshaller interface { - generateVariableUnmarshalValue(string) string -} - type coercer interface { coerce() func(string) string } diff --git a/sszgen/backend/render_test.go b/sszgen/backend/render_test.go index 28a7949..3f056fd 100644 --- a/sszgen/backend/render_test.go +++ b/sszgen/backend/render_test.go @@ -49,7 +49,7 @@ func TestGenerator_GenerateBeaconState(t *testing.T) { g := &Generator{ packagePath: "github.com/prysmaticlabs/prysm/v3/proto/beacon/p2p/v1", } - g.Generate(testFixBeaconState) + require.NoError(t, g.Generate(testFixBeaconState)) rendered, err := g.Render() require.NoError(t, err) actual := string(rendered) diff --git a/sszgen/node.go b/sszgen/node.go index c11ac9d..97932db 100644 --- a/sszgen/node.go +++ b/sszgen/node.go @@ -26,6 +26,8 @@ type FieldDef struct { typ types.Type tag string pkg *types.Package + // declared as a slice pointer for unambiguous zero-value + dims *[]*SSZDimension } func newStructDef(fs *token.FileSet, imp types.Importer, typ *types.Named, packageName string) *TypeDef { diff --git a/sszgen/parser.go b/sszgen/parser.go index c1b4a69..bc91adb 100644 --- a/sszgen/parser.go +++ b/sszgen/parser.go @@ -15,7 +15,6 @@ import ( type GoPathScoper struct { packagePath string - fieldNames []string pkg *types.Package } diff --git a/sszgen/representer.go b/sszgen/representer.go index 566cd7a..ba5b878 100644 --- a/sszgen/representer.go +++ b/sszgen/representer.go @@ -91,7 +91,7 @@ func (p *FieldParser) expand(f *FieldDef) (gentypes.ValRep, error) { } return &container, nil case *types.Named: - exp, err := p.expand(&FieldDef{name: ty.Obj().Name(), tag: f.tag, typ: ty.Underlying(), pkg: f.pkg}) + exp, err := p.expand(&FieldDef{name: ty.Obj().Name(), tag: f.tag, typ: ty.Underlying(), pkg: f.pkg, dims: f.dims}) switch ty.Underlying().(type) { case *types.Struct: return exp, err @@ -114,6 +114,10 @@ func (p *FieldParser) expand(f *FieldDef) (gentypes.ValRep, error) { } func (p *FieldParser) expandArrayHead(f *FieldDef) (gentypes.ValRep, error) { + // when recursing through a nested vector/list, receive dimensions parsed through parents + if f.dims != nil { + return p.expandArray(*f.dims, f) + } dims, err := extractSSZDimensions(fmt.Sprintf("`%v`", f.tag)) if err != nil { return nil, errors.Wrapf(err, "name=%s, package=%s, tag=%s", f.name, f.pkg.Path(), f.tag) @@ -142,17 +146,14 @@ func (p *FieldParser) expandArray(dims []*SSZDimension, f *FieldDef) (gentypes.V return nil, fmt.Errorf("invalid typ in expand array: %v with name: %v ", f.typ, f.name) } - // Only expand the inner array if it is not a named type - if _, ok := elem.(*types.Named); !ok && len(dims) > 1 { - elv, err = p.expandArray(dims[1:], &FieldDef{name: f.name, typ: elem.Underlying(), pkg: f.pkg}) - if err != nil { - return nil, err - } - } else { - elv, err = p.expand(&FieldDef{name: f.name, tag: f.tag, typ: elem, pkg: f.pkg}) - if err != nil { - return nil, err - } + var dimTail *[]*SSZDimension + if len(dims) > 1 { + dt := dims[1:] + dimTail = &dt + } + elv, err = p.expand(&FieldDef{name: f.name, tag: f.tag, typ: elem, pkg: f.pkg, dims: dimTail}) + if err != nil { + return nil, err } if d.IsVector() { diff --git a/sszgen/types/container_test.go b/sszgen/types/container_test.go index 4c82da0..c231a08 100644 --- a/sszgen/types/container_test.go +++ b/sszgen/types/container_test.go @@ -6,17 +6,6 @@ import ( "github.com/prysmaticlabs/prysm/v3/testing/require" ) -var testFixderp ValRep = &ValueContainer{ - Name: "derp", - Package: "derp", - Contents: []ContainerField{ - { - Key: "sup", - Value: nil, - }, - }, -} - func TestBeaconStateSizeSSZ(t *testing.T) { require.Equal(t, 84, testFixStatus.FixedSize()) require.Equal(t, false, testFixStatus.IsVariableSized()) diff --git a/sszgen/typeutil.go b/sszgen/typeutil.go index ea7be78..4a0e779 100644 --- a/sszgen/typeutil.go +++ b/sszgen/typeutil.go @@ -58,92 +58,6 @@ func lookupType(scope *types.Scope, name string) (*types.Named, types.Object, er return typ.Type().(*types.Named), obj, nil } -func isPointer(typ types.Type) bool { - _, ok := typ.(*types.Pointer) - return ok -} - -func underlyingSlice(typ types.Type) *types.Slice { - for { - switch t := typ.(type) { - case *types.Named: - typ = typ.Underlying() - case *types.Slice: - return t - default: - return nil - } - } -} - -func underlyingMap(typ types.Type) *types.Map { - for { - switch t := typ.(type) { - case *types.Named: - typ = typ.Underlying() - case *types.Map: - return t - default: - return nil - } - } -} - -func ensureNilCheckable(typ types.Type) types.Type { - orig := typ - named := false - for { - switch typ.(type) { - case *types.Named: - typ = typ.Underlying() - named = true - case *types.Slice, *types.Map: - if named { - // Named slices, maps, etc. are special because they can have a custom - // decoder function that prevents the JSON null value. Wrap them with a - // pointer to allow null always so required/optional works as expected. - return types.NewPointer(orig) - } - return orig - case *types.Pointer, *types.Interface: - return orig - default: - return types.NewPointer(orig) - } - } -} - -// checkConvertible determines whether values of type from can be converted to type to. It -// returns nil if convertible and a descriptive error otherwise. -// See package documentation for this definition of 'convertible'. -func checkConvertible(from, to types.Type) error { - if types.ConvertibleTo(from, to) { - return nil - } - // Slices. - sfrom := underlyingSlice(from) - sto := underlyingSlice(to) - if sfrom != nil && sto != nil { - if !types.ConvertibleTo(sfrom.Elem(), sto.Elem()) { - return fmt.Errorf("slice element type %s is not convertible to %s", sfrom.Elem(), sto.Elem()) - } - return nil - } - // Maps. - mfrom := underlyingMap(from) - mto := underlyingMap(to) - if mfrom != nil && mto != nil { - if !types.ConvertibleTo(mfrom.Key(), mto.Key()) { - return fmt.Errorf("map key type %s is not convertible to %s", mfrom.Key(), mto.Key()) - } - if !types.ConvertibleTo(mfrom.Elem(), mto.Elem()) { - return fmt.Errorf("map element type %s is not convertible to %s", mfrom.Elem(), mto.Elem()) - } - return nil - } - return fmt.Errorf("type %s is not convertible to %s", from, to) -} - // fileScope tracks imports and other names at file scope. type fileScope struct { imports []*types.Package @@ -248,29 +162,3 @@ func (s *fileScope) packageName(path string) string { } return name } - -// funcScope tracks used identifiers in a function. It can create new identifiers that do -// not clash with the parent scope. -type funcScope struct { - used map[string]bool - parent *fileScope -} - -func newFuncScope(parent *fileScope) *funcScope { - return &funcScope{make(map[string]bool), parent} -} - -// newIdent creates a new identifier that doesn't clash with any name -// in the scope or its parent file scope. -func (s *funcScope) newIdent(base string) string { - for i := 0; ; i++ { - name := base - if i > 0 { - name += strconv.Itoa(i - 1) - } - if !s.parent.isNameTaken(name) && !s.used[name] { - s.used[name] = true - return name - } - } -}