github.com/Blockdaemon/celo-blockchain@v0.0.0-20200129231733-e667f6b08419/core/vm/dynamic_params.go (about)

     1  package vm
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/ethereum/go-ethereum/accounts/abi"
     7  	"github.com/ethereum/go-ethereum/common"
     8  	"github.com/ethereum/go-ethereum/contract_comm/errors"
     9  	"github.com/ethereum/go-ethereum/log"
    10  	"github.com/ethereum/go-ethereum/params"
    11  )
    12  
    13  const (
    14  	// This is taken from celo-monorepo/packages/protocol/build/<env>/contracts/Registry.json
    15  	getAddressForABI = `[{"constant": true,
    16                                "inputs": [
    17                                     {
    18                                         "name": "identifier",
    19                                         "type": "bytes32"
    20                                     }
    21                                ],
    22                                "name": "getAddressFor",
    23                                "outputs": [
    24                                     {
    25                                         "name": "",
    26                                         "type": "address"
    27                                     }
    28                                ],
    29                                "payable": false,
    30                                "stateMutability": "view",
    31                                "type": "function"
    32                               }]`
    33  )
    34  
    35  var getAddressForFuncABI, _ = abi.JSON(strings.NewReader(getAddressForABI))
    36  
    37  // TODO(kevjue) - Re-Enable caching of the retrieved registered address
    38  // See this commit for the removed code for caching:  https://github.com/celo-org/geth/commit/43a275273c480d307a3d2b3c55ca3b3ee31ec7dd.
    39  func GetRegisteredAddressWithEvm(registryId [32]byte, evm *EVM) (*common.Address, error) {
    40  	evm.DontMeterGas = true
    41  	defer func() { evm.DontMeterGas = false }()
    42  
    43  	if evm.GetStateDB().GetCodeSize(params.RegistrySmartContractAddress) == 0 {
    44  		return nil, errors.ErrRegistryContractNotDeployed
    45  	}
    46  
    47  	var contractAddress common.Address
    48  	_, err := evm.StaticCallFromSystem(params.RegistrySmartContractAddress, getAddressForFuncABI, "getAddressFor", []interface{}{registryId}, &contractAddress, params.MaxGasForGetAddressFor)
    49  
    50  	// TODO(asa): Why was this change necessary?
    51  	if err == abi.ErrEmptyOutput || err == errExecutionReverted {
    52  		log.Trace("Registry contract not deployed")
    53  		return nil, errors.ErrRegistryContractNotDeployed
    54  	} else if err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	if contractAddress == common.ZeroAddress {
    59  		return nil, errors.ErrSmartContractNotDeployed
    60  	}
    61  
    62  	return &contractAddress, nil
    63  }