/// <summary> /// Provides the cost of gas for a given size of memory in words. /// </summary> /// <param name="wordCount">The target amount of words of memory.</param> /// <returns>Returns the cost of gas for the given size of memory.</returns> public static BigInteger GetMemoryAllocationCost(EthereumRelease currentVersion, BigInteger wordCount) { // Calculate the cost of gas for the given amount of words of memory. BigInteger cost = (wordCount * GAS_MEMORY_BASE) + (BigInteger.Pow(wordCount, 2) / 512); return(cost); }
/// <summary> /// Obtains the base gas cost of executing the given opcode on the given Ethereum release version. /// </summary> /// <param name="currentVersion">The release of Ethereum to assume when obtaining the base gas cost for the given opcode.</param> /// <param name="opcode">The opcode to obtain the base gas cost for.</param> /// <returns>Returns the base gas cost for the given instruction on the given Ethereum release.</returns> public static uint?GetInstructionBaseGasCost(EthereumRelease currentVersion, InstructionOpcode opcode) { // Obtain the cost for this version. It is null if it does not exist yet, or was never declared. uint cost = 0; if (_baseGasLookup[currentVersion].TryGetValue(opcode, out cost)) { return(cost); } else { return(null); } }
/// <summary> /// Sets the Ethereum release to be used in the configuration. /// </summary> /// <param name="releaseVersion">The release version to set this configuration to.</param> public void SetEthereumReleaseBlockStarts(EthereumRelease releaseVersion) { // Obtain all release options. EthereumRelease[] options = (EthereumRelease[])Enum.GetValues(typeof(EthereumRelease)); // Loop through every option, if we are at this version or below, set the block number to 0 so we'll always be considered in this version, set all other block numbers to max so we never reach it. foreach (EthereumRelease option in options) { if (option <= releaseVersion) { EthereumReleaseBlockStart[option] = 0; } else { EthereumReleaseBlockStart[option] = EVMDefinitions.UINT256_MAX_VALUE; } } // TODO: Temporary cheap fix. Replace this. EthereumReleaseBlockStart[EthereumRelease.DAO] = EVMDefinitions.UINT256_MAX_VALUE; }
/// <summary> /// Given a state, updates the current configuration's Ethereum release to accomodate for the current block number. /// </summary> /// <param name="state">The state which we wish to based our versioning off of.</param> public void UpdateEthereumRelease(State state) { // Declare our release type EthereumRelease releaseType = EthereumRelease.Frontier; // Obtain all release options. EthereumRelease[] options = (EthereumRelease[])Enum.GetValues(typeof(EthereumRelease)); // Loop through every option, if we passed the declared block number, and this version is higher, set the version. foreach (EthereumRelease option in options) { if (EthereumReleaseBlockStart.TryGetValue(option, out var blockNum)) { if (state?.CurrentBlock?.Header?.BlockNumber >= blockNum && option > releaseType) { releaseType = option; } } } // Set our release type Version = releaseType; }
/// <summary> /// Provides the cost of gas for a given /// </summary> /// <param name="currentVersion">The Ethereum release currently being executed.</param> /// <param name="size">The size</param> /// <returns></returns> public static BigInteger GetMemoryCopyCost(EthereumRelease currentVersion, BigInteger size) { return(EVMDefinitions.GetWordCount(size) * GAS_MEMORY_COPY); }
public OpcodeBaseGasCostAttribute(EthereumRelease version, uint baseGasCost) { Version = version; BaseGasCost = baseGasCost; }
/// <summary> /// Obtains a release version's starting block number. /// </summary> /// <param name="releaseVersion">The release version to obtain the starting block number for (inclusive).</param> /// <returns>Returns the block number which begins the release version provided.</returns> public BigInteger GetReleaseStartBlockNumber(EthereumRelease releaseVersion) { return(EthereumReleaseBlockStart[releaseVersion]); }