Skip to content

Commit

Permalink
Fix migration hanging
Browse files Browse the repository at this point in the history
  • Loading branch information
jarmg committed Jul 4, 2019
1 parent 13378ef commit 43a2752
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 58 deletions.
8 changes: 7 additions & 1 deletion accounts/abi/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ package abi
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"

"github.com/ethereum/go-ethereum/log"
)

// The ABI holds information about a contract's context and available
Expand All @@ -32,6 +35,8 @@ type ABI struct {
Events map[string]Event
}

var ErrEmptyOutput = errors.New("abi: unmarshalling empty output")

// JSON returns a parsed ABI interface and error if it failed.
func JSON(reader io.Reader) (ABI, error) {
dec := json.NewDecoder(reader)
Expand Down Expand Up @@ -74,7 +79,8 @@ func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) {
// Unpack output in v according to the abi specification
func (abi ABI) Unpack(v interface{}, name string, output []byte) (err error) {
if len(output) == 0 {
return fmt.Errorf("abi: unmarshalling empty output")
log.Trace("Returning empty output error from abi unpacking")
return ErrEmptyOutput
}
// since there can't be naming collisions with contracts and events,
// we need to decide whether we're calling a method or an event
Expand Down
2 changes: 0 additions & 2 deletions core/gasprice_minimum.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ func (gpm *GasPriceMinimum) GetGasPriceMinimum(currency *common.Address, state *
return FallbackGasPriceMinimum, err
}

log.Info("========== getting gasprice with singleton ============")
_, err = userspace_communication.MakeStaticCall(
*gasPriceMinimumAddress,
gasPriceMinimumABI,
Expand All @@ -159,7 +158,6 @@ func (gpm *GasPriceMinimum) GetGasPriceMinimum(currency *common.Address, state *
header,
state,
)
log.Info("========== returning from singleton function call ============", "gasprice min", gasPriceMinimum)
return gasPriceMinimum, err
}

Expand Down
9 changes: 5 additions & 4 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,17 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *commo
// about the transaction and calling mechanisms.
vmenv := vm.NewEVM(context, statedb, config, cfg)

addressLookupEMV:= vm.NewEVM(context, statedb, config, cfg)
addressLookupEMV := vm.NewEVM(context, statedb, config, cfg)
//addressLookupEMV.DontMeterGas = true
infraAddress, err := params.GetRegisteredAddress(params.GovernanceRegistryId, addressLookupEMV)
//addressLookupEMV.DontMeterGas = false

if err != nil && err != params.ErrSmartContractNotDeployed {
return nil, 0, err
} else if err == params.ErrSmartContractNotDeployed {
if err == params.ErrSmartContractNotDeployed || err == params.ErrRegistryContractNotDeployed {
infraAddress = nil
} else if err != nil {
return nil, 0, err
}

// Apply the transaction to the current state (included in the env)
_, gas, failed, err := ApplyMessage(vmenv, msg, gp, gcWl, gasPriceMinimum, infraFraction, infraAddress)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,8 @@ func (evm *EVM) TobinTransfer(db StateDB, sender, recipient common.Address, gas
reserveAddress, err := params.GetRegisteredAddress(params.ReserveRegistryId, evm)
evm.DontMeterGas = false

if err != nil && err != params.ErrSmartContractNotDeployed {
if err != nil && err != params.ErrSmartContractNotDeployed && err != params.ErrRegistryContractNotDeployed {
log.Trace("Error in tobin transfer", "error", err)
return gas, err
}

Expand Down
7 changes: 3 additions & 4 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import (
"github.com/ethereum/go-ethereum/eth/filters"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/userspace_communication"
"github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/miner"
Expand All @@ -51,6 +50,7 @@ import (
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/userspace_communication"
)

type LesServer interface {
Expand Down Expand Up @@ -186,9 +186,8 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
// Note that this should NOT be used when executing smart contract calls done via end user transactions.
eth.iEvmH = core.NewInternalEVMHandler(eth.blockchain)

//jarmg - this should become the only signature
userspace_communication.SetInternalEVMHandler(eth.blockchain)

//jarmg - this should become the only signature
userspace_communication.SetInternalEVMHandler(eth.blockchain)

// Object used to retrieve and cache registered addresses from the Registry smart contract.
eth.regAdd = core.NewRegisteredAddresses(eth.iEvmH)
Expand Down
42 changes: 8 additions & 34 deletions params/dynamic_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
)

const (
Expand Down Expand Up @@ -35,7 +36,8 @@ var (
getAddressForFuncABI, _ = abi.JSON(strings.NewReader(getAddressForABI))

// ErrSmartContractNotDeployed is returned when the RegisteredAddresses mapping does not contain the specified contract
ErrSmartContractNotDeployed = errors.New("registered contract not deployed")
ErrSmartContractNotDeployed = errors.New("registered contract not deployed")
ErrRegistryContractNotDeployed = errors.New("contract registry not deployed")

regAddrCache = make(map[string]*regAddrCacheEntry)
regAddrCacheMu sync.RWMutex
Expand Down Expand Up @@ -63,46 +65,18 @@ func GetRegisteredAddress(registryId string, evm EVM) (*common.Address, error) {
return nil, ErrSmartContractNotDeployed
}

registryStorageHash := evm.GetStateDB().GetStorageRoot(registrySmartContractAddress)
registryCodeHash := evm.GetStateDB().GetCodeHash(registrySmartContractAddress)
var contractAddress common.Address
_, err := evm.StaticCallFromSystem(registrySmartContractAddress, getAddressForFuncABI, "getAddressFor", []interface{}{registryId}, &contractAddress, 20000)

regAddrCacheMu.RLock()
if regAddrCacheEntry := regAddrCache[registryId]; regAddrCacheEntry != nil &&
regAddrCacheEntry.registryStorageHash == registryStorageHash &&
regAddrCacheEntry.registryCodeHash == registryCodeHash {
regAddrCacheMu.RUnlock()
return regAddrCacheEntry.address, nil
}
regAddrCacheMu.RUnlock()

// Fetch the address and update the cache
var contractAddress common.Address
regAddrCacheMu.Lock()
defer regAddrCacheMu.Unlock()

// Check to see if the cache just got inserted from another thread.
if regAddrCacheEntry := regAddrCache[registryId]; regAddrCacheEntry != nil &&
regAddrCacheEntry.registryStorageHash == registryStorageHash &&
regAddrCacheEntry.registryCodeHash == registryCodeHash {
return regAddrCacheEntry.address, nil
}

if _, err := evm.StaticCallFromSystem(registrySmartContractAddress, getAddressForFuncABI, "getAddressFor", []interface{}{registryId}, &contractAddress, 20000); err != nil {
if err == abi.ErrEmptyOutput {
return nil, ErrRegistryContractNotDeployed
} else if err != nil {
return nil, err
}

if _, ok := regAddrCache[registryId]; !ok {
regAddrCache[registryId] = new(regAddrCacheEntry)
}

// The contract has not been registered yet
if contractAddress == common.ZeroAddress {
return nil, ErrSmartContractNotDeployed
}

regAddrCache[registryId].address = &contractAddress
regAddrCache[registryId].registryStorageHash = registryStorageHash
regAddrCache[registryId].registryCodeHash = registryCodeHash

return &contractAddress, nil
}
22 changes: 10 additions & 12 deletions userspace_communication/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the celo library. If not, see <http://www.gnu.org/licenses/>.


package userspace_communication

import (
Expand All @@ -31,8 +30,8 @@ import (
)

var (
emptyMessage = types.NewMessage(common.HexToAddress("0x0"), nil, 0, common.Big0, 0, common.Big0, nil, nil, []byte{}, false)
IevmHSingleton *InternalEVMHandler
emptyMessage = types.NewMessage(common.HexToAddress("0x0"), nil, 0, common.Big0, 0, common.Big0, nil, nil, []byte{}, false)
IevmHSingleton *InternalEVMHandler
)

// ChainContext supports retrieving chain data and consensus parameters
Expand Down Expand Up @@ -119,7 +118,7 @@ func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int)

// An EVM handler to make calls to smart contracts from within geth
type InternalEVMHandler struct {
chain ChainContext
chain ChainContext
}

func MakeStaticCall(scAddress common.Address, abi abi.ABI, funcName string, args []interface{}, returnObj interface{}, gas uint64, header *types.Header, state *state.StateDB) (uint64, error) {
Expand All @@ -129,7 +128,6 @@ func MakeStaticCall(scAddress common.Address, abi abi.ABI, funcName string, args
return makeCall(abiStaticCall, header, state)
}


func MakeCall(scAddress common.Address, abi abi.ABI, funcName string, args []interface{}, returnObj interface{}, gas uint64, value *big.Int, header *types.Header, state *state.StateDB) (uint64, error) {
abiCall := func(evm *vm.EVM) (uint64, error) {
gasLeft, err := evm.CallFromSystem(scAddress, abi, funcName, args, returnObj, gas, value)
Expand Down Expand Up @@ -174,11 +172,11 @@ func makeCall(call func(evm *vm.EVM) (uint64, error), header *types.Header, stat
}

func SetInternalEVMHandler(chain ChainContext) {
if IevmHSingleton == nil {
log.Trace("Setting the InternalEVMHandler Singleton")
iEvmH := InternalEVMHandler {
chain: chain,
}
IevmHSingleton = &iEvmH
}
if IevmHSingleton == nil {
log.Trace("Setting the InternalEVMHandler Singleton")
iEvmH := InternalEVMHandler{
chain: chain,
}
IevmHSingleton = &iEvmH
}
}

0 comments on commit 43a2752

Please sign in to comment.