|
| 1 | +package usigverifierprecompilefix |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "slices" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + usigverifierprecompile "github.com/pushchain/push-chain-node/precompiles/usigverifier" |
| 13 | +) |
| 14 | + |
| 15 | +const currentAddr = usigverifierprecompile.USigVerifierPrecompileAddress |
| 16 | + |
| 17 | +// baseline mirrors the non-verifier entries of a real chain's ActiveStaticPrecompiles. |
| 18 | +var baseline = []string{ |
| 19 | + "0x00000000000000000000000000000000000000CB", |
| 20 | + "0x0000000000000000000000000000000000000100", |
| 21 | + "0x0000000000000000000000000000000000000400", |
| 22 | + "0x0000000000000000000000000000000000000800", |
| 23 | + "0x0000000000000000000000000000000000000801", |
| 24 | + "0x0000000000000000000000000000000000000802", |
| 25 | + "0x0000000000000000000000000000000000000803", |
| 26 | + "0x0000000000000000000000000000000000000804", |
| 27 | + "0x0000000000000000000000000000000000000805", |
| 28 | +} |
| 29 | + |
| 30 | +func withLegacy() []string { |
| 31 | + out := append([]string{LegacyUSigVerifierAddress}, baseline...) |
| 32 | + slices.Sort(out) |
| 33 | + return out |
| 34 | +} |
| 35 | + |
| 36 | +func TestSyncActiveStaticPrecompiles_ReplacesLegacyAddress(t *testing.T) { |
| 37 | + got, removed, added := syncActiveStaticPrecompiles(withLegacy()) |
| 38 | + |
| 39 | + require.True(t, removed, "legacy address should have been removed") |
| 40 | + require.True(t, added, "current address should have been added") |
| 41 | + |
| 42 | + require.NotContains(t, got, LegacyUSigVerifierAddress) |
| 43 | + require.Contains(t, got, currentAddr) |
| 44 | + |
| 45 | + // Everything else survives untouched, and the list stays sorted so that |
| 46 | + // x/vm's ValidatePrecompiles accepts it. |
| 47 | + for _, addr := range baseline { |
| 48 | + require.Contains(t, got, addr) |
| 49 | + } |
| 50 | + require.Len(t, got, len(baseline)+1) |
| 51 | + require.True(t, slices.IsSorted(got), "precompile list must stay sorted: %v", got) |
| 52 | +} |
| 53 | + |
| 54 | +func TestSyncActiveStaticPrecompiles_Idempotent(t *testing.T) { |
| 55 | + first, _, _ := syncActiveStaticPrecompiles(withLegacy()) |
| 56 | + |
| 57 | + second, removed, added := syncActiveStaticPrecompiles(first) |
| 58 | + require.False(t, removed, "second run should find nothing to remove") |
| 59 | + require.False(t, added, "second run should find nothing to add") |
| 60 | + require.Equal(t, first, second) |
| 61 | +} |
| 62 | + |
| 63 | +func TestSyncActiveStaticPrecompiles_AddsCurrentWhenBothMissing(t *testing.T) { |
| 64 | + got, removed, added := syncActiveStaticPrecompiles(slices.Clone(baseline)) |
| 65 | + |
| 66 | + require.False(t, removed) |
| 67 | + require.True(t, added) |
| 68 | + require.Contains(t, got, currentAddr) |
| 69 | + require.Len(t, got, len(baseline)+1) |
| 70 | +} |
| 71 | + |
| 72 | +func TestSyncActiveStaticPrecompiles_RemovesLegacyWhenCurrentPresent(t *testing.T) { |
| 73 | + in := append(withLegacy(), currentAddr) |
| 74 | + slices.Sort(in) |
| 75 | + |
| 76 | + got, removed, added := syncActiveStaticPrecompiles(in) |
| 77 | + |
| 78 | + require.True(t, removed) |
| 79 | + require.False(t, added) |
| 80 | + require.NotContains(t, got, LegacyUSigVerifierAddress) |
| 81 | + require.Contains(t, got, currentAddr) |
| 82 | + require.Len(t, got, len(baseline)+1) |
| 83 | +} |
| 84 | + |
| 85 | +func TestSyncActiveStaticPrecompiles_MatchesLegacyCaseInsensitively(t *testing.T) { |
| 86 | + in := append([]string{strings.ToUpper(LegacyUSigVerifierAddress[2:])}, baseline...) |
| 87 | + in[0] = "0x" + in[0] |
| 88 | + |
| 89 | + got, removed, _ := syncActiveStaticPrecompiles(in) |
| 90 | + |
| 91 | + require.True(t, removed) |
| 92 | + for _, addr := range got { |
| 93 | + require.False(t, strings.EqualFold(addr, LegacyUSigVerifierAddress)) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// TestGenesisScriptsActivateCurrentVerifier guards the genesis half of the same |
| 98 | +// fix: a fresh chain must activate the address the verifier is registered at and |
| 99 | +// must not declare the legacy one, which nothing implements. |
| 100 | +func TestGenesisScriptsActivateCurrentVerifier(t *testing.T) { |
| 101 | + repoRoot := filepath.Join("..", "..", "..") |
| 102 | + |
| 103 | + scripts := []string{ |
| 104 | + "scripts/test_node.sh", |
| 105 | + "local-native/scripts/setup-genesis-auto.sh", |
| 106 | + "local-multi-validator/scripts/setup-genesis-auto.sh", |
| 107 | + "testnet/core/setup/setup_genesis_validator.sh", |
| 108 | + } |
| 109 | + |
| 110 | + for _, script := range scripts { |
| 111 | + t.Run(script, func(t *testing.T) { |
| 112 | + raw, err := os.ReadFile(filepath.Join(repoRoot, script)) |
| 113 | + require.NoError(t, err) |
| 114 | + |
| 115 | + var line string |
| 116 | + for _, l := range strings.Split(string(raw), "\n") { |
| 117 | + if strings.Contains(l, "active_static_precompiles") { |
| 118 | + line = l |
| 119 | + break |
| 120 | + } |
| 121 | + } |
| 122 | + require.NotEmpty(t, line, "no active_static_precompiles assignment found") |
| 123 | + |
| 124 | + require.NotContains(t, strings.ToLower(line), strings.ToLower(LegacyUSigVerifierAddress), |
| 125 | + "genesis must not declare the legacy verifier address, nothing is registered at it") |
| 126 | + require.Contains(t, strings.ToLower(line), strings.ToLower(currentAddr), |
| 127 | + "genesis must activate the verifier address the node registers") |
| 128 | + }) |
| 129 | + } |
| 130 | +} |
0 commit comments