github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/evm/keeper/statedb.go (about)

     1  package keeper
     2  
     3  import (
     4  	"math/big"
     5  
     6  	"github.com/fibonacci-chain/fbc/x/evm/types"
     7  
     8  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     9  
    10  	ethcmn "github.com/ethereum/go-ethereum/common"
    11  )
    12  
    13  // ----------------------------------------------------------------------------
    14  // Setters, only for test use
    15  // ----------------------------------------------------------------------------
    16  
    17  // SetBalance calls CommitStateDB.SetBalance using the passed in context
    18  func (k *Keeper) SetBalance(ctx sdk.Context, addr ethcmn.Address, amount *big.Int) {
    19  	csdb := types.CreateEmptyCommitStateDB(k.GenerateCSDBParams(), ctx)
    20  	csdb.SetBalance(addr, amount)
    21  	csdb.Commit(false)
    22  }
    23  
    24  // SetNonce calls CommitStateDB.SetNonce using the passed in context
    25  func (k *Keeper) SetNonce(ctx sdk.Context, addr ethcmn.Address, nonce uint64) {
    26  	csdb := types.CreateEmptyCommitStateDB(k.GenerateCSDBParams(), ctx)
    27  	csdb.SetNonce(addr, nonce)
    28  	csdb.Commit(false)
    29  }
    30  
    31  // ----------------------------------------------------------------------------
    32  // Getters, for test and query case
    33  // ----------------------------------------------------------------------------
    34  
    35  // GetBalance calls CommitStateDB.GetBalance using the passed in context
    36  func (k *Keeper) GetBalance(ctx sdk.Context, addr ethcmn.Address) *big.Int {
    37  	return types.CreateEmptyCommitStateDB(k.GenerateCSDBParams(), ctx).WithHistoricalTrie().GetBalance(addr)
    38  }
    39  
    40  // GetCode calls CommitStateDB.GetCode using the passed in context
    41  func (k *Keeper) GetCode(ctx sdk.Context, addr ethcmn.Address) []byte {
    42  	return types.CreateEmptyCommitStateDB(k.GenerateCSDBParams(), ctx).WithHistoricalTrie().GetCode(addr)
    43  }
    44  
    45  // GetCodeByHash calls CommitStateDB.GetCode using the passed in context
    46  func (k *Keeper) GetCodeByHash(ctx sdk.Context, hash ethcmn.Hash) []byte {
    47  	return types.CreateEmptyCommitStateDB(k.GenerateCSDBParams(), ctx).WithHistoricalTrie().GetCodeByHash(hash)
    48  }
    49  
    50  // GetState calls CommitStateDB.GetState using the passed in context
    51  func (k *Keeper) GetState(ctx sdk.Context, addr ethcmn.Address, hash ethcmn.Hash) ethcmn.Hash {
    52  	return types.CreateEmptyCommitStateDB(k.GenerateCSDBParams(), ctx).WithHistoricalTrie().GetState(addr, hash)
    53  }
    54  
    55  // GetStateByKey calls CommitStateDB.GetState using the passed in context
    56  func (k *Keeper) GetStateByKey(ctx sdk.Context, addr ethcmn.Address, key ethcmn.Hash) ethcmn.Hash {
    57  	return types.CreateEmptyCommitStateDB(k.GenerateCSDBParams(), ctx).WithHistoricalTrie().GetStateByKey(addr, key)
    58  }
    59  
    60  // ----------------------------------------------------------------------------
    61  // Auxiliary, for test and query case
    62  // ----------------------------------------------------------------------------
    63  
    64  // ForEachStorage calls CommitStateDB.ForEachStorage using passed in context
    65  func (k *Keeper) ForEachStorage(ctx sdk.Context, addr ethcmn.Address, cb func(key, value ethcmn.Hash) bool) error {
    66  	return types.CreateEmptyCommitStateDB(k.GenerateCSDBParams(), ctx).WithHistoricalTrie().ForEachStorage(addr, cb)
    67  }
    68  
    69  // GetOrNewStateObject calls CommitStateDB.GetOrNetStateObject using the passed in context
    70  func (k *Keeper) GetOrNewStateObject(ctx sdk.Context, addr ethcmn.Address) types.StateObject {
    71  	return types.CreateEmptyCommitStateDB(k.GenerateCSDBParams(), ctx).WithHistoricalTrie().GetOrNewStateObject(addr)
    72  }