github.com/Night-mk/quorum@v21.1.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  	"github.com/ethereum/go-ethereum/private"
    12  	"github.com/ethereum/go-ethereum/private/engine"
    13  )
    14  
    15  func setState(privateState *state.StateDB, accounts map[string]extension.AccountWithMetadata, privacyMetaData *state.PrivacyMetadata, managedParties []string) bool {
    16  	log.Debug("Extension: set private state explicitly from state dump")
    17  	for key, value := range accounts {
    18  		stateDump := value.State
    19  
    20  		contractAddress := common.HexToAddress(key)
    21  
    22  		newBalance, errBalanceSet := new(big.Int).SetString(stateDump.Balance, 10)
    23  		if !errBalanceSet {
    24  			log.Error("could not set address balance", "address", key, "balance", stateDump.Balance)
    25  			return false
    26  		}
    27  		privateState.SetBalance(contractAddress, newBalance)
    28  		privateState.SetNonce(contractAddress, stateDump.Nonce)
    29  		privateState.SetCode(contractAddress, common.Hex2Bytes(stateDump.Code))
    30  		for keyStore, valueStore := range stateDump.Storage {
    31  			privateState.SetState(contractAddress, keyStore, common.HexToHash(valueStore))
    32  		}
    33  		if privacyMetaData.PrivacyFlag != engine.PrivacyFlagStandardPrivate {
    34  			privateState.SetPrivacyMetadata(contractAddress, privacyMetaData)
    35  		}
    36  		if managedParties != nil {
    37  			privateState.SetManagedParties(contractAddress, managedParties)
    38  		}
    39  	}
    40  	return true
    41  }
    42  
    43  // updates the privacy metadata
    44  func setPrivacyMetadata(privateState *state.StateDB, address common.Address, hash string) {
    45  	privacyMetaData, err := privateState.GetPrivacyMetadata(address)
    46  	if err != nil || privacyMetaData.PrivacyFlag.IsStandardPrivate() {
    47  		return
    48  	}
    49  
    50  	ptmHash, err := common.Base64ToEncryptedPayloadHash(hash)
    51  	if err != nil {
    52  		log.Error("setting privacy metadata failed", "err", err)
    53  		return
    54  	}
    55  	pm := state.NewStatePrivacyMetadata(ptmHash, privacyMetaData.PrivacyFlag)
    56  	privateState.SetPrivacyMetadata(address, pm)
    57  }
    58  
    59  func setManagedParties(ptm private.PrivateTransactionManager, privateState *state.StateDB, address common.Address, hash string) {
    60  	existingManagedParties, err := privateState.GetManagedParties(address)
    61  	if err != nil {
    62  		return
    63  	}
    64  
    65  	ptmHash, err := common.Base64ToEncryptedPayloadHash(hash)
    66  	if err != nil {
    67  		log.Error("setting privacy metadata failed", "err", err)
    68  		return
    69  	}
    70  
    71  	_, managedParties, _, _, err := ptm.Receive(ptmHash)
    72  	newManagedParties := common.AppendSkipDuplicates(existingManagedParties, managedParties...)
    73  	privateState.SetManagedParties(address, newManagedParties)
    74  }
    75  
    76  func logContainsExtensionTopic(receivedLog *types.Log) bool {
    77  	if len(receivedLog.Topics) != 1 {
    78  		return false
    79  	}
    80  	return receivedLog.Topics[0].String() == extension.StateSharedTopicHash
    81  }
    82  
    83  // validateAccountsExist checks that all the accounts in the expected list are
    84  // present in the state map, and that no  other accounts exist in the state map
    85  // that are unexpected
    86  func validateAccountsExist(expectedAccounts []common.Address, actualAccounts map[string]extension.AccountWithMetadata) bool {
    87  	if len(expectedAccounts) != len(actualAccounts) {
    88  		return false
    89  	}
    90  	for _, account := range expectedAccounts {
    91  		_, exists := actualAccounts[account.String()]
    92  		if !exists {
    93  			return false
    94  		}
    95  	}
    96  	return true
    97  }