Skip to content

Commit baa8c80

Browse files
committed
state upgrade with nonce
1 parent 8b0ef32 commit baa8c80

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

params/state_upgrade.go

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

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

params/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
@@ -259,13 +259,23 @@ func TestVMStateUpgrade(t *testing.T) {
259259
Code: upgradedCode,
260260
}
261261

262+
// Create a new account just for testing nonce setting
263+
nonceTestAccount := common.Address{99}
264+
explicitNonce := uint64(10)
265+
nonceTestAccountUpgrade := &params.StateUpgradeAccount{
266+
BalanceChange: (*math.HexOrDecimal256)(big.NewInt(50)),
267+
Nonce: &explicitNonce,
268+
// No code for this account - should still set the nonce directly
269+
}
270+
262271
upgradeTimestamp := upgrade.InitiallyActiveTime.Add(10 * time.Hour)
263272
upgradeBytesJSON := fmt.Sprintf(
264273
`{
265274
"stateUpgrades": [
266275
{
267276
"blockTimestamp": %d,
268277
"accounts": {
278+
"%s": %s,
269279
"%s": %s,
270280
"%s": %s
271281
}
@@ -277,6 +287,8 @@ func TestVMStateUpgrade(t *testing.T) {
277287
mustMarshal(t, genesisAccountUpgrade),
278288
newAccount.Hex(),
279289
mustMarshal(t, newAccountUpgrade),
290+
nonceTestAccount.Hex(),
291+
mustMarshal(t, nonceTestAccountUpgrade),
280292
)
281293
require.Contains(t, upgradeBytesJSON, upgradedCodeStr)
282294

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

334352
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 params.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)