Skip to content

Commit bb52b8b

Browse files
authored
Merge pull request #17 from push-protocol/upgradeable-factory
feat: made Factory contract upgradeable via Transparent Proxy
2 parents e845e54 + d2ed775 commit bb52b8b

File tree

6 files changed

+90
-14
lines changed

6 files changed

+90
-14
lines changed

x/ue/keeper/genesis.go

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,44 @@ import (
1212
"github.com/rollchains/pchain/x/ue/types"
1313
)
1414

15-
func deployFactoryContract(ctx context.Context, evmKeeper types.EVMKeeper) {
15+
func deployFactoryProxy(ctx context.Context, evmKeeper types.EVMKeeper) {
1616
sdkCtx := sdk.UnwrapSDKContext(ctx)
17-
factoryAddress := common.HexToAddress(types.FACTORY_ADDRESS_HEX)
18-
owner := common.HexToAddress(types.FACTORY_OWNER_ADDRESS_HEX)
17+
proxyAddress := common.HexToAddress(types.FACTORY_PROXY_ADDRESS_HEX)
18+
proxyAdminOwner := common.HexToAddress(types.PROXY_ADMIN_ADDRESS_HEX)
19+
factoryImplAddress := common.HexToAddress(types.FACTORY_IMPL_ADDRESS_HEX)
1920

2021
// Compute the code hash from the runtime bytecode
21-
codeHash := crypto.Keccak256(types.FactoryRuntimeBytecode)
22+
codeHash := crypto.Keccak256(types.ProxyRuntimeBytecode)
23+
24+
// Create the EVM account object
25+
evmAccount := statedb.Account{
26+
Nonce: 1, // to prevent tx nonce=0 conflicts
27+
Balance: big.NewInt(0), // zero balance by default
28+
CodeHash: codeHash, // link to deployed code
29+
}
30+
31+
// Set the EVM account with the factory proxy contract
32+
err := evmKeeper.SetAccount(sdkCtx, proxyAddress, evmAccount)
33+
if err != nil {
34+
panic("failed to set factory proxy contract account: " + err.Error())
35+
}
36+
37+
// Store the runtime bytecode linked to the code hash
38+
evmKeeper.SetCode(sdkCtx, codeHash, types.ProxyRuntimeBytecode)
39+
40+
// Update proxyAdmin Slot with the proxyAdmin owner address (left padded to 32 bytes)
41+
evmKeeper.SetState(sdkCtx, proxyAddress, types.PROXY_ADMIN_SLOT, common.LeftPadBytes(proxyAdminOwner.Bytes(), 32))
42+
43+
// Update proxyImplementation Slot with the factory implementation address (left padded to 32 bytes)
44+
evmKeeper.SetState(sdkCtx, proxyAddress, types.PROXY_IMPLEMENTATION_SLOT, common.LeftPadBytes(factoryImplAddress.Bytes(), 32))
45+
}
46+
47+
func deployFactoryImplContract(ctx context.Context, evmKeeper types.EVMKeeper) {
48+
sdkCtx := sdk.UnwrapSDKContext(ctx)
49+
factoryAddress := common.HexToAddress(types.FACTORY_IMPL_ADDRESS_HEX)
50+
51+
// Compute the code hash from the runtime bytecode
52+
codeHash := crypto.Keccak256(types.FactoryImplRuntimeBytecode)
2253

2354
// Create the EVM account object
2455
evmAccount := statedb.Account{
@@ -34,8 +65,44 @@ func deployFactoryContract(ctx context.Context, evmKeeper types.EVMKeeper) {
3465
}
3566

3667
// Store the runtime bytecode linked to the code hash
37-
evmKeeper.SetCode(sdkCtx, codeHash, types.FactoryRuntimeBytecode)
68+
evmKeeper.SetCode(sdkCtx, codeHash, types.FactoryImplRuntimeBytecode)
69+
}
70+
71+
func deployProxyAdminContract(ctx context.Context, evmKeeper types.EVMKeeper) {
72+
sdkCtx := sdk.UnwrapSDKContext(ctx)
73+
proxyAdminAddress := common.HexToAddress(types.PROXY_ADMIN_ADDRESS_HEX)
74+
owner := common.HexToAddress(types.PROXY_ADMIN_OWNER_ADDRESS_HEX)
75+
76+
// Compute the code hash from the runtime bytecode
77+
codeHash := crypto.Keccak256(types.ProxyAdminRuntimeBytecode)
78+
79+
// Create the EVM account object
80+
evmAccount := statedb.Account{
81+
Nonce: 1, // to prevent tx nonce=0 conflicts
82+
Balance: big.NewInt(0), // zero balance by default
83+
CodeHash: codeHash, // link to deployed code
84+
}
85+
86+
// Set the EVM account with the proxy admin contract
87+
err := evmKeeper.SetAccount(sdkCtx, proxyAdminAddress, evmAccount)
88+
if err != nil {
89+
panic("failed to set proxy admin contract account: " + err.Error())
90+
}
91+
92+
// Store the runtime bytecode linked to the code hash
93+
evmKeeper.SetCode(sdkCtx, codeHash, types.ProxyAdminRuntimeBytecode)
3894

3995
// Initialize storage slot 0 (Ownable.owner) with the owner address (left padded to 32 bytes)
40-
evmKeeper.SetState(sdkCtx, factoryAddress, common.Hash{}, common.LeftPadBytes(owner.Bytes(), 32))
96+
evmKeeper.SetState(sdkCtx, proxyAdminAddress, common.Hash{}, common.LeftPadBytes(owner.Bytes(), 32))
97+
}
98+
99+
func deployFactoryEA(ctx context.Context, evmKeeper types.EVMKeeper) {
100+
// Deploy the factory implementation contract
101+
deployFactoryImplContract(ctx, evmKeeper)
102+
103+
// Deploy the proxy admin contract
104+
deployProxyAdminContract(ctx, evmKeeper)
105+
106+
// Deploy the factory proxy contract
107+
deployFactoryProxy(ctx, evmKeeper)
41108
}

x/ue/keeper/keeper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ func (k *Keeper) InitGenesis(ctx context.Context, data *types.GenesisState) erro
7979
return err
8080
}
8181

82-
// deploy factory at 0xEA address
83-
deployFactoryContract(ctx, k.evmKeeper)
82+
// deploy factory proxy at 0xEA address
83+
deployFactoryEA(ctx, k.evmKeeper)
8484

8585
return k.Params.Set(ctx, data.Params)
8686
}

x/ue/keeper/msg_deploy_uea.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (k Keeper) deployUEA(ctx context.Context, evmFrom common.Address, universal
2121
fmt.Println("Base Fee BigInt:", baseFeeBig)
2222

2323
// EVM Call arguments
24-
factoryAddress := common.HexToAddress(types.FACTORY_ADDRESS_HEX)
24+
factoryAddress := common.HexToAddress(types.FACTORY_PROXY_ADDRESS_HEX)
2525

2626
// RPC call verification to verify the gateway interaction tx on source chain
2727
err := k.utvKeeper.VerifyGatewayInteractionTx(ctx, universalAccount.Owner, txHash, universalAccount.Chain)

x/ue/keeper/msg_execute_payload.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (k Keeper) executePayload(ctx context.Context, evmFrom common.Address, univ
2525
return fmt.Errorf("chain %s is not enabled", universalAccount.Chain)
2626
}
2727

28-
factoryAddress := common.HexToAddress(types.FACTORY_ADDRESS_HEX)
28+
factoryAddress := common.HexToAddress(types.FACTORY_PROXY_ADDRESS_HEX)
2929

3030
// Step 1: Compute smart account address
3131
receipt, err := k.CallFactoryToComputeUEAAddress(sdkCtx, evmFrom, factoryAddress, universalAccount)

x/ue/keeper/msg_mint_push.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
func (k Keeper) mintPush(ctx context.Context, evmFrom common.Address, universalAccount *types.UniversalAccount, txHash string) error {
1919
sdkCtx := sdk.UnwrapSDKContext(ctx)
2020

21-
factoryAddress := common.HexToAddress(types.FACTORY_ADDRESS_HEX)
21+
factoryAddress := common.HexToAddress(types.FACTORY_PROXY_ADDRESS_HEX)
2222

2323
// RPC call verification to get amount to be mint
2424
amountOfUsdLocked, usdDecimals, err := k.utvKeeper.VerifyAndGetLockedFunds(ctx, universalAccount.Owner, txHash, universalAccount.Chain)

0 commit comments

Comments
 (0)