Skip to content

Commit 786e48d

Browse files
committed
state upgrade with nonce
1 parent 220454e commit 786e48d

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

params/extras/state_upgrade.go

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type StateUpgradeAccount struct {
2828
Code hexutil.Bytes `json:"code,omitempty"`
2929
Storage map[common.Hash]common.Hash `json:"storage,omitempty"`
3030
BalanceChange *math.HexOrDecimal256 `json:"balanceChange,omitempty"`
31+
Nonce *uint64 `json:"nonce,omitempty"`
3132
}
3233

3334
func (s *StateUpgrade) Equal(other *StateUpgrade) bool {

params/extras/state_upgrade_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ func TestUnmarshalStateUpgradeJSON(t *testing.T) {
160160
"blockTimestamp": 1677608400,
161161
"accounts": {
162162
"0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC": {
163-
"balanceChange": "100"
163+
"balanceChange": "100",
164+
"nonce": 42
164165
}
165166
}
166167
}
@@ -175,6 +176,7 @@ func TestUnmarshalStateUpgradeJSON(t *testing.T) {
175176
StateUpgradeAccounts: map[common.Address]StateUpgradeAccount{
176177
common.HexToAddress("0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC"): {
177178
BalanceChange: (*math.HexOrDecimal256)(big.NewInt(100)),
179+
Nonce: utils.NewUint64(42),
178180
},
179181
},
180182
},

plugin/evm/vm_upgrade_bytes_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,23 @@ func TestVMStateUpgrade(t *testing.T) {
257257
Code: upgradedCode,
258258
}
259259

260+
// Create a new account just for testing nonce setting
261+
nonceTestAccount := common.Address{99}
262+
explicitNonce := uint64(10)
263+
nonceTestAccountUpgrade := &params.StateUpgradeAccount{
264+
BalanceChange: (*math.HexOrDecimal256)(big.NewInt(50)),
265+
Nonce: &explicitNonce,
266+
// No code for this account - should still set the nonce directly
267+
}
268+
260269
upgradeTimestamp := upgrade.InitiallyActiveTime.Add(10 * time.Hour)
261270
upgradeBytesJSON := fmt.Sprintf(
262271
`{
263272
"stateUpgrades": [
264273
{
265274
"blockTimestamp": %d,
266275
"accounts": {
276+
"%s": %s,
267277
"%s": %s,
268278
"%s": %s
269279
}
@@ -275,6 +285,8 @@ func TestVMStateUpgrade(t *testing.T) {
275285
mustMarshal(t, genesisAccountUpgrade),
276286
newAccount.Hex(),
277287
mustMarshal(t, newAccountUpgrade),
288+
nonceTestAccount.Hex(),
289+
mustMarshal(t, nonceTestAccountUpgrade),
278290
)
279291
require.Contains(t, upgradeBytesJSON, upgradedCodeStr)
280292

@@ -327,6 +339,12 @@ func TestVMStateUpgrade(t *testing.T) {
327339
require.Equal(t, state.GetCodeHash(newAccount), crypto.Keccak256Hash(upgradedCode))
328340
require.Equal(t, state.GetNonce(newAccount), uint64(1)) // Nonce should be set to 1 when code is set if nonce was 0
329341
require.Equal(t, state.GetState(newAccount, storageKey), newAccountUpgrade.Storage[storageKey])
342+
343+
// Test the nonce-specific account
344+
expectedNonceTestAccountBalance := uint256.MustFromBig((*big.Int)(nonceTestAccountUpgrade.BalanceChange))
345+
require.Equal(t, state.GetNonce(nonceTestAccount), explicitNonce)
346+
require.Equal(t, state.GetBalance(nonceTestAccount), expectedNonceTestAccountBalance)
347+
require.Empty(t, state.GetCode(nonceTestAccount)) // No code should be set
330348
}
331349

332350
func TestVMEupgradeActivatesCancun(t *testing.T) {

stateupgrade/state_upgrade.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,19 @@ func upgradeAccount(account common.Address, upgrade extras.StateUpgradeAccount,
3333
balanceChange, _ := uint256.FromBig((*big.Int)(upgrade.BalanceChange))
3434
state.AddBalance(account, balanceChange)
3535
}
36-
if len(upgrade.Code) != 0 {
37-
// if the nonce is 0, set the nonce to 1 as we would when deploying a contract at
38-
// the address.
36+
37+
// Set nonce if explicitly provided
38+
if upgrade.Nonce != nil {
39+
state.SetNonce(account, *upgrade.Nonce)
40+
} else if len(upgrade.Code) != 0 {
41+
// If no explicit nonce is provided but code is being set, set the nonce to
42+
// 1 as we would when deploying a contract at the address.
3943
if isEIP158 && state.GetNonce(account) == 0 {
4044
state.SetNonce(account, 1)
4145
}
46+
}
47+
48+
if len(upgrade.Code) != 0 {
4249
state.SetCode(account, upgrade.Code)
4350
}
4451
for key, value := range upgrade.Storage {

0 commit comments

Comments
 (0)