github.com/ava-labs/subnet-evm@v0.6.4/stateupgrade/state_upgrade.go (about)

     1  // (c) 2023 Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package stateupgrade
     5  
     6  import (
     7  	"math/big"
     8  
     9  	"github.com/ava-labs/subnet-evm/params"
    10  	"github.com/ethereum/go-ethereum/common"
    11  )
    12  
    13  // Configure applies the state upgrade to the state.
    14  func Configure(stateUpgrade *params.StateUpgrade, chainConfig ChainContext, state StateDB, blockContext BlockContext) error {
    15  	isEIP158 := chainConfig.IsEIP158(blockContext.Number())
    16  	for account, upgrade := range stateUpgrade.StateUpgradeAccounts {
    17  		if err := upgradeAccount(account, upgrade, state, isEIP158); err != nil {
    18  			return err
    19  		}
    20  	}
    21  	return nil
    22  }
    23  
    24  // upgradeAccount applies the state upgrade to the given account.
    25  func upgradeAccount(account common.Address, upgrade params.StateUpgradeAccount, state StateDB, isEIP158 bool) error {
    26  	// Create the account if it does not exist
    27  	if !state.Exist(account) {
    28  		state.CreateAccount(account)
    29  	}
    30  
    31  	if upgrade.BalanceChange != nil {
    32  		state.AddBalance(account, (*big.Int)(upgrade.BalanceChange))
    33  	}
    34  	if len(upgrade.Code) != 0 {
    35  		// if the nonce is 0, set the nonce to 1 as we would when deploying a contract at
    36  		// the address.
    37  		if isEIP158 && state.GetNonce(account) == 0 {
    38  			state.SetNonce(account, 1)
    39  		}
    40  		state.SetCode(account, upgrade.Code)
    41  	}
    42  	for key, value := range upgrade.Storage {
    43  		state.SetState(account, key, value)
    44  	}
    45  	return nil
    46  }