From 43ddb3ec09f5f444708f54cae925517ed8b4db93 Mon Sep 17 00:00:00 2001 From: Chris Berry Date: Mon, 4 Nov 2024 17:07:26 +0000 Subject: [PATCH] Fix SingleAttestation assignment and add tests --- spec/electra/singleattestation_json.go | 11 +++ spec/electra/singleattestation_test.go | 125 +++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 spec/electra/singleattestation_test.go diff --git a/spec/electra/singleattestation_json.go b/spec/electra/singleattestation_json.go index 0c24bf04..abd7152b 100644 --- a/spec/electra/singleattestation_json.go +++ b/spec/electra/singleattestation_json.go @@ -17,6 +17,7 @@ import ( "encoding/hex" "encoding/json" "fmt" + "strconv" "strings" "github.com/attestantio/go-eth2-client/spec/phase0" @@ -57,9 +58,19 @@ func (a *SingleAttestation) unpack(singleAttestationJSON *singleAttestationJSON) if singleAttestationJSON.CommitteeIndex == "" { return errors.New("committee index missing") } + committeeIndex, err := strconv.ParseUint(singleAttestationJSON.CommitteeIndex, 10, 64) + if err != nil { + return errors.Wrap(err, "invalid value for committee index") + } + a.CommitteeIndex = phase0.CommitteeIndex(committeeIndex) if singleAttestationJSON.AttesterIndex == "" { return errors.New("attester index missing") } + attesterIndex, err := strconv.ParseUint(singleAttestationJSON.AttesterIndex, 10, 64) + if err != nil { + return errors.Wrap(err, "invalid value for attester index") + } + a.AttesterIndex = phase0.ValidatorIndex(attesterIndex) a.Data = singleAttestationJSON.Data if a.Data == nil { return errors.New("data missing") diff --git a/spec/electra/singleattestation_test.go b/spec/electra/singleattestation_test.go new file mode 100644 index 00000000..248209bc --- /dev/null +++ b/spec/electra/singleattestation_test.go @@ -0,0 +1,125 @@ +// Copyright © 2020, 2021 Attestant Limited. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package electra_test + +import ( + "encoding/json" + "github.com/attestantio/go-eth2-client/spec/electra" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestSingleAttestationJSON(t *testing.T) { + tests := []struct { + name string + input []byte + err string + }{ + { + name: "Empty", + err: "unexpected end of JSON input", + }, + { + name: "JSONBad", + input: []byte("[]"), + err: "invalid JSON: json: cannot unmarshal array into Go value of type electra.singleAttestationJSON", + }, + { + name: "CommitteeIndexMissing", + input: []byte(`{"attester_index":"200","data":{"slot":"100","index":"1","beacon_block_root":"0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f","source":{"epoch":"1","root":"0x202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"},"target":{"epoch":"2","root":"0x404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f"}},"signature":"0x606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf"}`), + err: "committee index missing", + }, + { + name: "CommitteeIndexWrongType", + input: []byte(`{"committee_index":true,"attester_index":"200","data":{"slot":"100","index":"1","beacon_block_root":"0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f","source":{"epoch":"1","root":"0x202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"},"target":{"epoch":"2","root":"0x404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f"}},"signature":"0x606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf"}`), + err: "invalid JSON: json: cannot unmarshal bool into Go struct field singleAttestationJSON.committee_index of type string", + }, + { + name: "CommitteeIndexInvalid", + input: []byte(`{"committee_index":"-1","attester_index":"200","data":{"slot":"100","index":"1","beacon_block_root":"0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f","source":{"epoch":"1","root":"0x202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"},"target":{"epoch":"2","root":"0x404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f"}},"signature":"0x606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf"}`), + err: `invalid value for committee index: strconv.ParseUint: parsing "-1": invalid syntax`, + }, + { + name: "AttesterIndexMissing", + input: []byte(`{"committee_index":"1","data":{"slot":"100","index":"1","beacon_block_root":"0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f","source":{"epoch":"1","root":"0x202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"},"target":{"epoch":"2","root":"0x404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f"}},"signature":"0x606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf"}`), + err: "attester index missing", + }, + { + name: "AttesterIndexWrongType", + input: []byte(`{"committee_index":"1","attester_index":200,"data":{"slot":"100","index":"1","beacon_block_root":"0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f","source":{"epoch":"1","root":"0x202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"},"target":{"epoch":"2","root":"0x404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f"}},"signature":"0x606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf"}`), + err: "invalid JSON: json: cannot unmarshal number into Go struct field singleAttestationJSON.attester_index of type string", + }, + { + name: "AttesterIndexInvalid", + input: []byte(`{"committee_index":"1","attester_index":"-1","data":{"slot":"100","index":"1","beacon_block_root":"0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f","source":{"epoch":"1","root":"0x202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"},"target":{"epoch":"2","root":"0x404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f"}},"signature":"0x606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf"}`), + err: `invalid value for attester index: strconv.ParseUint: parsing "-1": invalid syntax`, + }, + { + name: "DataMissing", + input: []byte(`{"committee_index":"1","attester_index":"200","signature":"0x606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf"}`), + err: "data missing", + }, + { + name: "DataInvalid", + input: []byte(`{"committee_index":"1","attester_index":"200","data":true,"signature":"0x606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf"}`), + err: "invalid JSON: invalid JSON: json: cannot unmarshal bool into Go value of type phase0.attestationDataJSON", + }, + { + name: "SignatureMissing", + input: []byte(`{"committee_index":"1","attester_index":"200","data":{"slot":"100","index":"1","beacon_block_root":"0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f","source":{"epoch":"1","root":"0x202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"},"target":{"epoch":"2","root":"0x404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f"}}}`), + err: "signature missing", + }, + { + name: "SignatureWrongType", + input: []byte(`{"committee_index":"1","attester_index":"200","data":{"slot":"100","index":"1","beacon_block_root":"0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f","source":{"epoch":"1","root":"0x202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"},"target":{"epoch":"2","root":"0x404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f"}},"signature":true}`), + err: "invalid JSON: json: cannot unmarshal bool into Go struct field singleAttestationJSON.signature of type string", + }, + { + name: "SignatureInvalid", + input: []byte(`{"committee_index":"1","attester_index":"200","data":{"slot":"100","index":"1","beacon_block_root":"0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f","source":{"epoch":"1","root":"0x202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"},"target":{"epoch":"2","root":"0x404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f"}},"signature":"invalid"}`), + err: "invalid value for signature: encoding/hex: invalid byte: U+0069 'i'", + }, + { + name: "SignatureShort", + input: []byte(`{"committee_index":"1","attester_index":"200","data":{"slot":"100","index":"1","beacon_block_root":"0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f","source":{"epoch":"1","root":"0x202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"},"target":{"epoch":"2","root":"0x404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f"}},"signature":"0x6162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf"}`), + err: "incorrect length for signature", + }, + { + name: "SignatureLong", + input: []byte(`{"committee_index":"1","attester_index":"200","data":{"slot":"100","index":"1","beacon_block_root":"0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f","source":{"epoch":"1","root":"0x202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"},"target":{"epoch":"2","root":"0x404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f"}},"signature":"0x60606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf"}`), + err: "incorrect length for signature", + }, + { + name: "Good", + input: []byte(`{"committee_index":"1","attester_index":"200","data":{"slot":"100","index":"1","beacon_block_root":"0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f","source":{"epoch":"1","root":"0x202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"},"target":{"epoch":"2","root":"0x404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f"}},"signature":"0x606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf"}`), + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + var res electra.SingleAttestation + err := json.Unmarshal(test.input, &res) + if test.err != "" { + require.EqualError(t, err, test.err) + } else { + require.NoError(t, err) + rt, err := json.Marshal(&res) + require.NoError(t, err) + assert.Equal(t, string(test.input), string(rt)) + } + }) + } +}