github.com/dim4egster/coreth@v0.10.2/precompile/utils.go (about) 1 // (c) 2019-2020, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package precompile 5 6 import ( 7 "fmt" 8 "regexp" 9 10 "github.com/dim4egster/coreth/vmerrs" 11 "github.com/ethereum/go-ethereum/crypto" 12 ) 13 14 var functionSignatureRegex = regexp.MustCompile(`[\w]+\(((([\w]+)?)|((([\w]+),)+([\w]+)))\)`) 15 16 // CalculateFunctionSelector returns the 4 byte function selector that results from [functionSignature] 17 // Ex. the function setBalance(addr address, balance uint256) should be passed in as the string: 18 // "setBalance(address,uint256)" 19 func CalculateFunctionSelector(functionSignature string) []byte { 20 if !functionSignatureRegex.MatchString(functionSignature) { 21 panic(fmt.Errorf("invalid function signature: %q", functionSignature)) 22 } 23 hash := crypto.Keccak256([]byte(functionSignature)) 24 return hash[:4] 25 } 26 27 // deductGas checks if [suppliedGas] is sufficient against [requiredGas] and deducts [requiredGas] from [suppliedGas]. 28 //nolint:unused,deadcode 29 func deductGas(suppliedGas uint64, requiredGas uint64) (uint64, error) { 30 if suppliedGas < requiredGas { 31 return 0, vmerrs.ErrOutOfGas 32 } 33 return suppliedGas - requiredGas, nil 34 }