From 04910831e7c3ecf8b5bdb4faa141f404bde9d3c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Fri, 17 May 2024 11:34:19 -0700 Subject: [PATCH] adjust tests, global registers are always included and never filtered out --- .../execution_state_extract_test.go | 14 ++++++- .../extract_payloads_test.go | 40 ++++++++++++++----- cmd/util/ledger/util/payload_file_test.go | 38 ++++++++++++++++-- 3 files changed, 76 insertions(+), 16 deletions(-) diff --git a/cmd/util/cmd/execution-state-extract/execution_state_extract_test.go b/cmd/util/cmd/execution-state-extract/execution_state_extract_test.go index 26ab069fedf..a57cd966f05 100644 --- a/cmd/util/cmd/execution-state-extract/execution_state_extract_test.go +++ b/cmd/util/cmd/execution-state-extract/execution_state_extract_test.go @@ -406,10 +406,22 @@ func TestExtractPayloadsFromExecutionState(t *testing.T) { // Verify exported payloads. partialState, payloadsFromFile, err := util.ReadPayloadFile(zerolog.Nop(), outputPayloadFileName) require.NoError(t, err) - require.Equal(t, len(selectedKeysValues), len(payloadsFromFile)) require.True(t, partialState) + nonGlobalPayloads := make([]*ledger.Payload, 0, len(selectedKeysValues)) for _, payloadFromFile := range payloadsFromFile { + key, err := payloadFromFile.Key() + require.NoError(t, err) + + owner := key.KeyParts[0].Value + if len(owner) > 0 { + nonGlobalPayloads = append(nonGlobalPayloads, payloadFromFile) + } + } + + require.Equal(t, len(selectedKeysValues), len(nonGlobalPayloads)) + + for _, payloadFromFile := range nonGlobalPayloads { k, err := payloadFromFile.Key() require.NoError(t, err) diff --git a/cmd/util/cmd/extract-payloads-by-address/extract_payloads_test.go b/cmd/util/cmd/extract-payloads-by-address/extract_payloads_test.go index a30574b926a..3c1bb267cc1 100644 --- a/cmd/util/cmd/extract-payloads-by-address/extract_payloads_test.go +++ b/cmd/util/cmd/extract-payloads-by-address/extract_payloads_test.go @@ -1,9 +1,7 @@ package extractpayloads import ( - "bytes" "crypto/rand" - "encoding/hex" "path/filepath" "strings" "testing" @@ -94,10 +92,22 @@ func TestExtractPayloads(t *testing.T) { // Verify exported payloads. partialState, payloadsFromFile, err := util.ReadPayloadFile(zerolog.Nop(), outputFile) require.NoError(t, err) - require.Equal(t, len(selectedKeysValues), len(payloadsFromFile)) require.True(t, partialState) + nonGlobalPayloads := make([]*ledger.Payload, 0, len(selectedKeysValues)) for _, payloadFromFile := range payloadsFromFile { + key, err := payloadFromFile.Key() + require.NoError(t, err) + + owner := key.KeyParts[0].Value + if len(owner) > 0 { + nonGlobalPayloads = append(nonGlobalPayloads, payloadFromFile) + } + } + + require.Equal(t, len(selectedKeysValues), len(nonGlobalPayloads)) + + for _, payloadFromFile := range nonGlobalPayloads { k, err := payloadFromFile.Key() require.NoError(t, err) @@ -108,9 +118,7 @@ func TestExtractPayloads(t *testing.T) { }) }) - t.Run("no payloads", func(t *testing.T) { - - emptyAddress := common.Address{} + t.Run("empty address", func(t *testing.T) { unittest.RunWithTempDir(t, func(datadir string) { @@ -127,9 +135,6 @@ func TestExtractPayloads(t *testing.T) { keys, values := getSampleKeyValues(i) for j, key := range keys { - if bytes.Equal(key.KeyParts[0].Value, emptyAddress[:]) { - continue - } keysValues[key.String()] = keyPair{ key: key, value: values[j], @@ -147,7 +152,7 @@ func TestExtractPayloads(t *testing.T) { Cmd.SetArgs([]string{ "--input-filename", inputFile, "--output-filename", outputFile, - "--addresses", hex.EncodeToString(emptyAddress[:]), + "--addresses", ",", }) err = Cmd.Execute() @@ -156,8 +161,21 @@ func TestExtractPayloads(t *testing.T) { // Verify exported payloads. partialState, payloadsFromFile, err := util.ReadPayloadFile(zerolog.Nop(), outputFile) require.NoError(t, err) - require.Equal(t, 0, len(payloadsFromFile)) require.True(t, partialState) + + var nonGlobalPayloads []*ledger.Payload + for _, payloadFromFile := range payloadsFromFile { + key, err := payloadFromFile.Key() + require.NoError(t, err) + + owner := key.KeyParts[0].Value + if len(owner) > 0 { + nonGlobalPayloads = append(nonGlobalPayloads, payloadFromFile) + } + } + + require.Equal(t, 0, len(nonGlobalPayloads)) + }) }) } diff --git a/cmd/util/ledger/util/payload_file_test.go b/cmd/util/ledger/util/payload_file_test.go index 45f8a5a2f60..26b2092a623 100644 --- a/cmd/util/ledger/util/payload_file_test.go +++ b/cmd/util/ledger/util/payload_file_test.go @@ -142,10 +142,16 @@ func TestPayloadFile(t *testing.T) { keysValues := make(map[string]keyPair) var payloads []*ledger.Payload + var globalRegisterCount int for i := 0; i < size; i++ { keys, values := getSampleKeyValues(i) for j, key := range keys { + + if len(key.KeyParts[0].Value) == 0 { + globalRegisterCount++ + } + keysValues[key.String()] = keyPair{ key: key, value: values[j], @@ -189,7 +195,11 @@ func TestPayloadFile(t *testing.T) { false, // input payloads represent entire state ) require.NoError(t, err) - require.Equal(t, len(selectedKeysValues), numOfPayloadWritten) + require.Equal( + t, + len(selectedKeysValues)+globalRegisterCount, + numOfPayloadWritten, + ) partialState, err := util.IsPayloadFilePartialState(payloadFileName) require.NoError(t, err) @@ -197,10 +207,22 @@ func TestPayloadFile(t *testing.T) { partialState, payloadsFromFile, err := util.ReadPayloadFile(zerolog.Nop(), payloadFileName) require.NoError(t, err) - require.Equal(t, len(selectedKeysValues), len(payloadsFromFile)) require.True(t, partialState) + nonGlobalPayloads := make([]*ledger.Payload, 0, len(selectedKeysValues)) for _, payloadFromFile := range payloadsFromFile { + key, err := payloadFromFile.Key() + require.NoError(t, err) + + owner := key.KeyParts[0].Value + if len(owner) > 0 { + nonGlobalPayloads = append(nonGlobalPayloads, payloadFromFile) + } + } + + require.Equal(t, len(selectedKeysValues), len(nonGlobalPayloads)) + + for _, payloadFromFile := range nonGlobalPayloads { k, err := payloadFromFile.Key() require.NoError(t, err) @@ -213,6 +235,7 @@ func TestPayloadFile(t *testing.T) { }) t.Run("no payloads found with filter", func(t *testing.T) { + emptyAddress := common.Address{} unittest.RunWithTempDir(t, func(datadir string) { @@ -224,13 +247,20 @@ func TestPayloadFile(t *testing.T) { keysValues := make(map[string]keyPair) var payloads []*ledger.Payload + var globalRegisterCount int + for i := 0; i < size; i++ { keys, values := getSampleKeyValues(i) for j, key := range keys { + if len(key.KeyParts[0].Value) == 0 { + globalRegisterCount++ + } + if bytes.Equal(key.KeyParts[0].Value, emptyAddress[:]) { continue } + keysValues[key.String()] = keyPair{ key: key, value: values[j], @@ -250,7 +280,7 @@ func TestPayloadFile(t *testing.T) { false, ) require.NoError(t, err) - require.Equal(t, 0, numOfPayloadWritten) + require.Equal(t, globalRegisterCount, numOfPayloadWritten) partialState, err := util.IsPayloadFilePartialState(payloadFileName) require.NoError(t, err) @@ -258,7 +288,7 @@ func TestPayloadFile(t *testing.T) { partialState, payloadsFromFile, err := util.ReadPayloadFile(zerolog.Nop(), payloadFileName) require.NoError(t, err) - require.Equal(t, 0, len(payloadsFromFile)) + require.Equal(t, globalRegisterCount, len(payloadsFromFile)) require.True(t, partialState) }) })