github.com/ethereum-optimism/optimism@v1.7.2/op-node/p2p/store/serialize_test.go (about) 1 package store 2 3 import ( 4 "encoding/json" 5 "strconv" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestRoundtripScoresV0(t *testing.T) { 12 scores := scoreRecord{ 13 PeerScores: PeerScores{Gossip: GossipScores{Total: 1234.52382}}, 14 LastUpdate: 1923841, 15 } 16 data, err := serializeScoresV0(scores) 17 require.NoError(t, err) 18 19 result, err := deserializeScoresV0(data) 20 require.NoError(t, err) 21 require.Equal(t, scores, result) 22 } 23 24 // TestParseHistoricSerializations checks that existing data can still be deserialized 25 // Adding new fields should not require bumping the version. Removing fields may require bumping. 26 // Scores should always default to 0. 27 // A new entry should be added to this test each time any fields are changed to ensure it can always be deserialized 28 func TestParseHistoricSerializationsV0(t *testing.T) { 29 tests := []struct { 30 data string 31 expected scoreRecord 32 }{ 33 { 34 data: `{"peerScores":{"gossip":{"total":1234.52382,"blocks":{"timeInMesh":1234,"firstMessageDeliveries":12,"meshMessageDeliveries":34,"invalidMessageDeliveries":56},"IPColocationFactor":12.34,"behavioralPenalty":56.78},"reqRespSync":123456},"lastUpdate":1923841}`, 35 expected: scoreRecord{ 36 PeerScores: PeerScores{ 37 Gossip: GossipScores{ 38 Total: 1234.52382, 39 Blocks: TopicScores{ 40 TimeInMesh: 1234, 41 FirstMessageDeliveries: 12, 42 MeshMessageDeliveries: 34, 43 InvalidMessageDeliveries: 56, 44 }, 45 IPColocationFactor: 12.34, 46 BehavioralPenalty: 56.78, 47 }, 48 ReqResp: ReqRespScores{}, 49 }, 50 LastUpdate: 1923841, 51 }, 52 }, 53 { 54 data: `{"peerScores":{"gossip":{"total":1234.52382,"blocks":{"timeInMesh":1234,"firstMessageDeliveries":12,"meshMessageDeliveries":34,"invalidMessageDeliveries":56},"IPColocationFactor":12.34,"behavioralPenalty":56.78},"reqResp":{"validResponses":99,"errorResponses":88,"rejectedPayloads":77}},"lastUpdate":1923841}`, 55 expected: scoreRecord{ 56 PeerScores: PeerScores{ 57 Gossip: GossipScores{ 58 Total: 1234.52382, 59 Blocks: TopicScores{ 60 TimeInMesh: 1234, 61 FirstMessageDeliveries: 12, 62 MeshMessageDeliveries: 34, 63 InvalidMessageDeliveries: 56, 64 }, 65 IPColocationFactor: 12.34, 66 BehavioralPenalty: 56.78, 67 }, 68 ReqResp: ReqRespScores{ 69 ValidResponses: 99, 70 ErrorResponses: 88, 71 RejectedPayloads: 77, 72 }, 73 }, 74 LastUpdate: 1923841, 75 }, 76 }, 77 } 78 for idx, test := range tests { 79 test := test 80 out, _ := json.Marshal(&test.expected) 81 t.Log(string(out)) 82 t.Run(strconv.Itoa(idx), func(t *testing.T) { 83 result, err := deserializeScoresV0([]byte(test.data)) 84 require.NoError(t, err) 85 require.Equal(t, test.expected, result) 86 }) 87 } 88 }