github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/model/flow/service_event_test.go (about) 1 package flow_test 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/fxamacker/cbor/v2" 8 "github.com/google/go-cmp/cmp" 9 "github.com/onflow/crypto" 10 "github.com/stretchr/testify/require" 11 "github.com/vmihailenco/msgpack" 12 "gotest.tools/assert" 13 14 cborcodec "github.com/onflow/flow-go/model/encoding/cbor" 15 "github.com/onflow/flow-go/model/flow" 16 "github.com/onflow/flow-go/utils/unittest" 17 ) 18 19 func TestEncodeDecode(t *testing.T) { 20 21 setup := unittest.EpochSetupFixture() 22 commit := unittest.EpochCommitFixture() 23 versionBeacon := unittest.VersionBeaconFixture() 24 protocolVersionUpgrade := unittest.ProtocolStateVersionUpgradeFixture() 25 26 comparePubKey := cmp.FilterValues(func(a, b crypto.PublicKey) bool { 27 return true 28 }, cmp.Comparer(func(a, b crypto.PublicKey) bool { 29 if a == nil { 30 return b == nil 31 } 32 return a.Equals(b) 33 })) 34 35 t.Run("json", func(t *testing.T) { 36 t.Run("specific event types", func(t *testing.T) { 37 // EpochSetup 38 b, err := json.Marshal(setup) 39 require.NoError(t, err) 40 41 gotSetup := new(flow.EpochSetup) 42 err = json.Unmarshal(b, gotSetup) 43 require.NoError(t, err) 44 assert.DeepEqual(t, setup, gotSetup, comparePubKey) 45 46 // EpochCommit 47 b, err = json.Marshal(commit) 48 require.NoError(t, err) 49 50 gotCommit := new(flow.EpochCommit) 51 err = json.Unmarshal(b, gotCommit) 52 require.NoError(t, err) 53 assert.DeepEqual(t, commit, gotCommit, comparePubKey) 54 55 // VersionBeacon 56 b, err = json.Marshal(versionBeacon) 57 require.NoError(t, err) 58 59 gotVersionBeacon := new(flow.VersionBeacon) 60 err = json.Unmarshal(b, gotVersionBeacon) 61 require.NoError(t, err) 62 assert.DeepEqual(t, versionBeacon, gotVersionBeacon) 63 64 // ProtocolStateVersionUpgrade 65 b, err = json.Marshal(protocolVersionUpgrade) 66 require.NoError(t, err) 67 68 gotProtocolVersionUpgrade := new(flow.ProtocolStateVersionUpgrade) 69 err = json.Unmarshal(b, gotProtocolVersionUpgrade) 70 require.NoError(t, err) 71 assert.DeepEqual(t, protocolVersionUpgrade, gotProtocolVersionUpgrade) 72 }) 73 74 t.Run("generic type", func(t *testing.T) { 75 // EpochSetup 76 b, err := json.Marshal(setup.ServiceEvent()) 77 require.NoError(t, err) 78 79 outer := new(flow.ServiceEvent) 80 err = json.Unmarshal(b, outer) 81 require.NoError(t, err) 82 gotSetup, ok := outer.Event.(*flow.EpochSetup) 83 require.True(t, ok) 84 assert.DeepEqual(t, setup, gotSetup, comparePubKey) 85 86 // EpochCommit 87 t.Logf("- debug: setup.ServiceEvent()=%+v\n", setup.ServiceEvent()) 88 b, err = json.Marshal(commit.ServiceEvent()) 89 require.NoError(t, err) 90 91 outer = new(flow.ServiceEvent) 92 t.Logf("- debug: outer=%+v <-- before .UnmarshalWrapped()\n", outer) 93 err = json.Unmarshal(b, outer) 94 t.Logf("- debug: outer=%+v <-- after .UnmarshalWrapped()\n", outer) 95 require.NoError(t, err) 96 gotCommit, ok := outer.Event.(*flow.EpochCommit) 97 require.True(t, ok) 98 assert.DeepEqual(t, commit, gotCommit, comparePubKey) 99 100 // VersionBeacon 101 t.Logf("- debug: versionBeacon.ServiceEvent()=%+v\n", versionBeacon.ServiceEvent()) 102 b, err = json.Marshal(versionBeacon.ServiceEvent()) 103 require.NoError(t, err) 104 105 outer = new(flow.ServiceEvent) 106 t.Logf("- debug: outer=%+v <-- before .UnmarshalWrapped()\n", outer) 107 err = json.Unmarshal(b, outer) 108 t.Logf("- debug: outer=%+v <-- after .UnmarshalWrapped()\n", outer) 109 require.NoError(t, err) 110 gotVersionTable, ok := outer.Event.(*flow.VersionBeacon) 111 require.True(t, ok) 112 assert.DeepEqual(t, versionBeacon, gotVersionTable) 113 114 // ProtocolStateVersionUpgrade 115 b, err = json.Marshal(protocolVersionUpgrade.ServiceEvent()) 116 require.NoError(t, err) 117 118 outer = new(flow.ServiceEvent) 119 err = json.Unmarshal(b, outer) 120 require.NoError(t, err) 121 gotProtocolVersionUpgrade, ok := outer.Event.(*flow.ProtocolStateVersionUpgrade) 122 require.True(t, ok) 123 assert.DeepEqual(t, protocolVersionUpgrade, gotProtocolVersionUpgrade) 124 }) 125 }) 126 127 t.Run("msgpack", func(t *testing.T) { 128 t.Run("specific event types", func(t *testing.T) { 129 // EpochSetup 130 b, err := msgpack.Marshal(setup) 131 require.NoError(t, err) 132 133 gotSetup := new(flow.EpochSetup) 134 err = msgpack.Unmarshal(b, gotSetup) 135 require.NoError(t, err) 136 assert.DeepEqual(t, setup, gotSetup, comparePubKey) 137 138 // EpochCommit 139 b, err = msgpack.Marshal(commit) 140 require.NoError(t, err) 141 142 gotCommit := new(flow.EpochCommit) 143 err = msgpack.Unmarshal(b, gotCommit) 144 require.NoError(t, err) 145 assert.DeepEqual(t, commit, gotCommit, comparePubKey) 146 147 // VersionBeacon 148 b, err = msgpack.Marshal(versionBeacon) 149 require.NoError(t, err) 150 151 gotVersionTable := new(flow.VersionBeacon) 152 err = msgpack.Unmarshal(b, gotVersionTable) 153 require.NoError(t, err) 154 assert.DeepEqual(t, versionBeacon, gotVersionTable) 155 156 // ProtocolStateVersionUpgrade 157 b, err = msgpack.Marshal(protocolVersionUpgrade) 158 require.NoError(t, err) 159 160 gotProtocolVersionUpgrade := new(flow.ProtocolStateVersionUpgrade) 161 err = msgpack.Unmarshal(b, gotProtocolVersionUpgrade) 162 require.NoError(t, err) 163 assert.DeepEqual(t, protocolVersionUpgrade, gotProtocolVersionUpgrade) 164 }) 165 166 t.Run("generic type", func(t *testing.T) { 167 b, err := msgpack.Marshal(setup.ServiceEvent()) 168 require.NoError(t, err) 169 170 outer := new(flow.ServiceEvent) 171 err = msgpack.Unmarshal(b, outer) 172 require.NoError(t, err) 173 gotSetup, ok := outer.Event.(*flow.EpochSetup) 174 require.True(t, ok) 175 assert.DeepEqual(t, setup, gotSetup, comparePubKey) 176 177 // EpochCommit 178 t.Logf("- debug: setup.ServiceEvent()=%+v\n", setup.ServiceEvent()) 179 b, err = msgpack.Marshal(commit.ServiceEvent()) 180 require.NoError(t, err) 181 182 outer = new(flow.ServiceEvent) 183 t.Logf("- debug: outer=%+v <-- before .UnmarshalWrapped()\n", outer) 184 err = msgpack.Unmarshal(b, outer) 185 t.Logf("- debug: outer=%+v <-- after .UnmarshalWrapped()\n", outer) 186 require.NoError(t, err) 187 gotCommit, ok := outer.Event.(*flow.EpochCommit) 188 require.True(t, ok) 189 assert.DeepEqual(t, commit, gotCommit, comparePubKey) 190 191 // VersionBeacon 192 t.Logf("- debug: versionTable.ServiceEvent()=%+v\n", versionBeacon.ServiceEvent()) 193 b, err = msgpack.Marshal(versionBeacon.ServiceEvent()) 194 require.NoError(t, err) 195 196 outer = new(flow.ServiceEvent) 197 t.Logf("- debug: outer=%+v <-- before .UnmarshalWrapped()\n", outer) 198 err = msgpack.Unmarshal(b, outer) 199 t.Logf("- debug: outer=%+v <-- after .UnmarshalWrapped()\n", outer) 200 require.NoError(t, err) 201 gotVersionTable, ok := outer.Event.(*flow.VersionBeacon) 202 require.True(t, ok) 203 assert.DeepEqual(t, versionBeacon, gotVersionTable, comparePubKey) 204 205 // ProtocolStateVersionUpgrade 206 b, err = msgpack.Marshal(protocolVersionUpgrade.ServiceEvent()) 207 require.NoError(t, err) 208 209 outer = new(flow.ServiceEvent) 210 err = msgpack.Unmarshal(b, outer) 211 require.NoError(t, err) 212 gotProtocolVersionUpgrade, ok := outer.Event.(*flow.ProtocolStateVersionUpgrade) 213 require.True(t, ok) 214 assert.DeepEqual(t, protocolVersionUpgrade, gotProtocolVersionUpgrade) 215 }) 216 }) 217 218 t.Run("cbor", func(t *testing.T) { 219 t.Run("specific event types", func(t *testing.T) { 220 // EpochSetup 221 b, err := cborcodec.EncMode.Marshal(setup) 222 require.NoError(t, err) 223 224 gotSetup := new(flow.EpochSetup) 225 err = cbor.Unmarshal(b, gotSetup) 226 require.NoError(t, err) 227 assert.DeepEqual(t, setup, gotSetup, comparePubKey) 228 229 // EpochCommit 230 b, err = cborcodec.EncMode.Marshal(commit) 231 require.NoError(t, err) 232 233 gotCommit := new(flow.EpochCommit) 234 err = cbor.Unmarshal(b, gotCommit) 235 require.NoError(t, err) 236 assert.DeepEqual(t, commit, gotCommit, comparePubKey) 237 238 // VersionBeacon 239 b, err = cborcodec.EncMode.Marshal(versionBeacon) 240 require.NoError(t, err) 241 242 gotVersionTable := new(flow.VersionBeacon) 243 err = cbor.Unmarshal(b, gotVersionTable) 244 require.NoError(t, err) 245 assert.DeepEqual(t, versionBeacon, gotVersionTable) 246 247 // ProtocolStateVersionUpgrade 248 b, err = cborcodec.EncMode.Marshal(protocolVersionUpgrade) 249 require.NoError(t, err) 250 251 gotProtocolVersionUpgrade := new(flow.ProtocolStateVersionUpgrade) 252 err = cbor.Unmarshal(b, gotProtocolVersionUpgrade) 253 require.NoError(t, err) 254 assert.DeepEqual(t, protocolVersionUpgrade, gotProtocolVersionUpgrade) 255 }) 256 257 t.Run("generic type", func(t *testing.T) { 258 // EpochSetup 259 t.Logf("- debug: setup.ServiceEvent()=%+v\n", setup.ServiceEvent()) 260 b, err := cborcodec.EncMode.Marshal(setup.ServiceEvent()) 261 require.NoError(t, err) 262 263 outer := new(flow.ServiceEvent) 264 t.Logf("- debug: outer=%+v <-- before .UnmarshalWrapped()\n", outer) 265 err = cbor.Unmarshal(b, outer) 266 t.Logf("- debug: outer=%+v <-- after .UnmarshalWrapped()\n", outer) 267 require.NoError(t, err) 268 gotSetup, ok := outer.Event.(*flow.EpochSetup) 269 require.True(t, ok) 270 assert.DeepEqual(t, setup, gotSetup, comparePubKey) 271 272 // EpochCommit 273 b, err = cborcodec.EncMode.Marshal(commit.ServiceEvent()) 274 require.NoError(t, err) 275 276 outer = new(flow.ServiceEvent) 277 err = cbor.Unmarshal(b, outer) 278 require.NoError(t, err) 279 gotCommit, ok := outer.Event.(*flow.EpochCommit) 280 require.True(t, ok) 281 assert.DeepEqual(t, commit, gotCommit, comparePubKey) 282 283 // VersionBeacon 284 t.Logf("- debug: setup.ServiceEvent()=%+v\n", versionBeacon.ServiceEvent()) 285 b, err = cborcodec.EncMode.Marshal(versionBeacon.ServiceEvent()) 286 require.NoError(t, err) 287 288 outer = new(flow.ServiceEvent) 289 err = cbor.Unmarshal(b, outer) 290 require.NoError(t, err) 291 gotVersionTable, ok := outer.Event.(*flow.VersionBeacon) 292 require.True(t, ok) 293 assert.DeepEqual(t, versionBeacon, gotVersionTable) 294 295 // ProtocolStateVersionUpgrade 296 b, err = cborcodec.EncMode.Marshal(protocolVersionUpgrade.ServiceEvent()) 297 require.NoError(t, err) 298 299 outer = new(flow.ServiceEvent) 300 err = cbor.Unmarshal(b, outer) 301 require.NoError(t, err) 302 gotProtocolVersionUpgrade, ok := outer.Event.(*flow.ProtocolStateVersionUpgrade) 303 require.True(t, ok) 304 assert.DeepEqual(t, protocolVersionUpgrade, gotProtocolVersionUpgrade) 305 }) 306 }) 307 }