github.com/ethereum/go-ethereum@v1.14.3/core/state/transient_storage.go (about)

     1  // Copyright 2022 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package state
    18  
    19  import (
    20  	"fmt"
    21  	"slices"
    22  	"strings"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  )
    26  
    27  // transientStorage is a representation of EIP-1153 "Transient Storage".
    28  type transientStorage map[common.Address]Storage
    29  
    30  // newTransientStorage creates a new instance of a transientStorage.
    31  func newTransientStorage() transientStorage {
    32  	return make(transientStorage)
    33  }
    34  
    35  // Set sets the transient-storage `value` for `key` at the given `addr`.
    36  func (t transientStorage) Set(addr common.Address, key, value common.Hash) {
    37  	if value == (common.Hash{}) { // this is a 'delete'
    38  		if _, ok := t[addr]; ok {
    39  			delete(t[addr], key)
    40  			if len(t[addr]) == 0 {
    41  				delete(t, addr)
    42  			}
    43  		}
    44  	} else {
    45  		if _, ok := t[addr]; !ok {
    46  			t[addr] = make(Storage)
    47  		}
    48  		t[addr][key] = value
    49  	}
    50  }
    51  
    52  // Get gets the transient storage for `key` at the given `addr`.
    53  func (t transientStorage) Get(addr common.Address, key common.Hash) common.Hash {
    54  	val, ok := t[addr]
    55  	if !ok {
    56  		return common.Hash{}
    57  	}
    58  	return val[key]
    59  }
    60  
    61  // Copy does a deep copy of the transientStorage
    62  func (t transientStorage) Copy() transientStorage {
    63  	storage := make(transientStorage)
    64  	for key, value := range t {
    65  		storage[key] = value.Copy()
    66  	}
    67  	return storage
    68  }
    69  
    70  // PrettyPrint prints the contents of the access list in a human-readable form
    71  func (t transientStorage) PrettyPrint() string {
    72  	out := new(strings.Builder)
    73  	var sortedAddrs []common.Address
    74  	for addr := range t {
    75  		sortedAddrs = append(sortedAddrs, addr)
    76  		slices.SortFunc(sortedAddrs, common.Address.Cmp)
    77  	}
    78  
    79  	for _, addr := range sortedAddrs {
    80  		fmt.Fprintf(out, "%#x:", addr)
    81  		var sortedKeys []common.Hash
    82  		storage := t[addr]
    83  		for key := range storage {
    84  			sortedKeys = append(sortedKeys, key)
    85  		}
    86  		slices.SortFunc(sortedKeys, common.Hash.Cmp)
    87  		for _, key := range sortedKeys {
    88  			fmt.Fprintf(out, "  %X : %X\n", key, storage[key])
    89  		}
    90  	}
    91  	return out.String()
    92  }