github.com/RobustRoundRobin/quorum@v20.10.0+incompatible/extension/privacyExtension/state_set_utilities.go (about)

     1  package privacyExtension
     2  
     3  import (
     4  	"math/big"
     5  
     6  	"github.com/ethereum/go-ethereum/common"
     7  	"github.com/ethereum/go-ethereum/core/state"
     8  	"github.com/ethereum/go-ethereum/core/types"
     9  	extension "github.com/ethereum/go-ethereum/extension/extensionContracts"
    10  	"github.com/ethereum/go-ethereum/log"
    11  )
    12  
    13  func setState(privateState *state.StateDB, accounts map[string]extension.AccountWithMetadata, privacyMetaData *state.PrivacyMetadata) bool {
    14  	log.Debug("Extension: set private state explicitly from state dump")
    15  	for key, value := range accounts {
    16  		stateDump := value.State
    17  
    18  		contractAddress := common.HexToAddress(key)
    19  
    20  		newBalance, errBalanceSet := new(big.Int).SetString(stateDump.Balance, 10)
    21  		if !errBalanceSet {
    22  			log.Error("could not set address balance", "address", key, "balance", stateDump.Balance)
    23  			return false
    24  		}
    25  		privateState.SetBalance(contractAddress, newBalance)
    26  		privateState.SetNonce(contractAddress, stateDump.Nonce)
    27  		privateState.SetCode(contractAddress, common.Hex2Bytes(stateDump.Code))
    28  		for keyStore, valueStore := range stateDump.Storage {
    29  			privateState.SetState(contractAddress, keyStore, common.HexToHash(valueStore))
    30  		}
    31  		privateState.SetStatePrivacyMetadata(contractAddress, privacyMetaData)
    32  	}
    33  	return true
    34  }
    35  
    36  // updates the privacy metadata
    37  func setPrivacyMetadata(privateState *state.StateDB, address common.Address, hash string) {
    38  	privacyMetaData, err := privateState.GetStatePrivacyMetadata(address)
    39  	if err != nil || privacyMetaData.PrivacyFlag.IsStandardPrivate() {
    40  		return
    41  	}
    42  
    43  	ptmHash, err := common.Base64ToEncryptedPayloadHash(hash)
    44  	if err != nil {
    45  		log.Error("setting privacy metadata failed", "err", err)
    46  		return
    47  	}
    48  	pm := state.NewStatePrivacyMetadata(ptmHash, privacyMetaData.PrivacyFlag)
    49  	privateState.SetStatePrivacyMetadata(address, pm)
    50  }
    51  
    52  func logContainsExtensionTopic(receivedLog *types.Log) bool {
    53  	if len(receivedLog.Topics) != 1 {
    54  		return false
    55  	}
    56  	return receivedLog.Topics[0].String() == extension.StateSharedTopicHash
    57  }
    58  
    59  // validateAccountsExist checks that all the accounts in the expected list are
    60  // present in the state map, and that no  other accounts exist in the state map
    61  // that are unexpected
    62  func validateAccountsExist(expectedAccounts []common.Address, actualAccounts map[string]extension.AccountWithMetadata) bool {
    63  	if len(expectedAccounts) != len(actualAccounts) {
    64  		return false
    65  	}
    66  	for _, account := range expectedAccounts {
    67  		_, exists := actualAccounts[account.String()]
    68  		if !exists {
    69  			return false
    70  		}
    71  	}
    72  	return true
    73  }