|
| 1 | +package integrationtest |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + evmtypes "github.com/cosmos/evm/x/vm/types" |
| 7 | + "github.com/ethereum/go-ethereum/common" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + utils "github.com/pushchain/push-chain-node/test/utils" |
| 11 | + uregistrytypes "github.com/pushchain/push-chain-node/x/uregistry/types" |
| 12 | +) |
| 13 | + |
| 14 | +// The read-state upgrade must reserve every system-contract slot that is still |
| 15 | +// empty, and leave the ones already deployed exactly as they are. |
| 16 | +// |
| 17 | +// UNIVERSAL_CALLBACK (0x…C2) was added to SYSTEM_CONTRACTS after donut launched, so |
| 18 | +// on that chain its proxy, admin and implementation are all bare — verified against |
| 19 | +// the live testnet. x/ucallback only accepts ReadRequested logs from that exact |
| 20 | +// address, so if the upgrade does not reserve it the module is inert. |
| 21 | +func TestReadStateUpgrade_ReservesMissingSystemContracts(t *testing.T) { |
| 22 | + chainApp, ctx, _ := utils.SetAppWithValidators(t) |
| 23 | + k := chainApp.UregistryKeeper |
| 24 | + |
| 25 | + code := func(addr string) []byte { |
| 26 | + a := common.HexToAddress(addr) |
| 27 | + acct := chainApp.EVMKeeper.GetAccountOrEmpty(ctx, a) |
| 28 | + return chainApp.EVMKeeper.GetCode(ctx, common.BytesToHash(acct.CodeHash)) |
| 29 | + } |
| 30 | + |
| 31 | + // Simulate a chain that launched before the slot existed: clear the triple. |
| 32 | + cb := uregistrytypes.SYSTEM_CONTRACTS["UNIVERSAL_CALLBACK"] |
| 33 | + for _, a := range []string{cb.Address, cb.ProxyAdmin, cb.Implementation} { |
| 34 | + // Point the account back at the empty-code sentinel; that is exactly the |
| 35 | + // state the reservation guard treats as "not deployed". |
| 36 | + chainApp.EVMKeeper.SetCodeHash(ctx, |
| 37 | + common.HexToAddress(a).Bytes(), evmtypes.EmptyCodeHash) |
| 38 | + } |
| 39 | + require.Empty(t, code(cb.Address), "precondition: the slot must start bare") |
| 40 | + |
| 41 | + // A slot that IS deployed must be left untouched. |
| 42 | + core := uregistrytypes.SYSTEM_CONTRACTS["UNIVERSAL_CORE"] |
| 43 | + coreBefore := code(core.Address) |
| 44 | + require.NotEmpty(t, coreBefore, "precondition: UNIVERSAL_CORE is deployed at genesis") |
| 45 | + |
| 46 | + require.NoError(t, k.DeployMissingSystemContracts(ctx)) |
| 47 | + |
| 48 | + require.NotEmpty(t, code(cb.Address), "the callback proxy must be reserved") |
| 49 | + require.NotEmpty(t, code(cb.ProxyAdmin), "its ProxyAdmin must be reserved") |
| 50 | + require.NotEmpty(t, code(cb.Implementation), "its implementation must be reserved") |
| 51 | + require.Equal(t, coreBefore, code(core.Address), |
| 52 | + "an already-deployed contract must not be redeployed or overwritten") |
| 53 | +} |
| 54 | + |
| 55 | +// Running the reservation twice must change nothing. The upgrade runs once, but the |
| 56 | +// same guard protects chains where the slot is already present, and a second pass is |
| 57 | +// the cheapest way to prove the guard actually holds. |
| 58 | +func TestReadStateUpgrade_ReservationIsIdempotent(t *testing.T) { |
| 59 | + chainApp, ctx, _ := utils.SetAppWithValidators(t) |
| 60 | + k := chainApp.UregistryKeeper |
| 61 | + |
| 62 | + code := func(addr string) []byte { |
| 63 | + a := common.HexToAddress(addr) |
| 64 | + acct := chainApp.EVMKeeper.GetAccountOrEmpty(ctx, a) |
| 65 | + return chainApp.EVMKeeper.GetCode(ctx, common.BytesToHash(acct.CodeHash)) |
| 66 | + } |
| 67 | + |
| 68 | + require.NoError(t, k.DeployMissingSystemContracts(ctx)) |
| 69 | + |
| 70 | + before := map[string][]byte{} |
| 71 | + for name, c := range uregistrytypes.SYSTEM_CONTRACTS { |
| 72 | + before[name] = code(c.Address) |
| 73 | + require.NotEmpty(t, before[name], "%s must be reserved after the first pass", name) |
| 74 | + } |
| 75 | + |
| 76 | + require.NoError(t, k.DeployMissingSystemContracts(ctx)) |
| 77 | + |
| 78 | + for name, c := range uregistrytypes.SYSTEM_CONTRACTS { |
| 79 | + require.Equal(t, before[name], code(c.Address), "%s changed on the second pass", name) |
| 80 | + } |
| 81 | +} |
0 commit comments