Skip to content

Commit

Permalink
adjust tests, global registers are always included and never filtered…
Browse files Browse the repository at this point in the history
… out
  • Loading branch information
turbolent committed May 17, 2024
1 parent a82f463 commit 0491083
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
40 changes: 29 additions & 11 deletions cmd/util/cmd/extract-payloads-by-address/extract_payloads_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package extractpayloads

import (
"bytes"
"crypto/rand"
"encoding/hex"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -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)

Expand All @@ -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) {

Expand All @@ -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],
Expand All @@ -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()
Expand All @@ -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))

})
})
}
Expand Down
38 changes: 34 additions & 4 deletions cmd/util/ledger/util/payload_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -189,18 +195,34 @@ 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)
require.True(t, partialState)

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)

Expand All @@ -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) {
Expand All @@ -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],
Expand All @@ -250,15 +280,15 @@ 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)
require.True(t, partialState)

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)
})
})
Expand Down

0 comments on commit 0491083

Please sign in to comment.