From dec3c7baee8b680827a1a92785203dc3d2b7c68a Mon Sep 17 00:00:00 2001 From: andri lim Date: Sat, 26 Oct 2019 08:24:43 +0700 Subject: [PATCH] fixes 32bit problem --- nimbus/vm/interpreter/gas_costs.nim | 40 +++++++++---------- nimbus/vm/interpreter/utils/utils_numeric.nim | 15 ++++--- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/nimbus/vm/interpreter/gas_costs.nim b/nimbus/vm/interpreter/gas_costs.nim index 90de3b2bf..7b8b09b89 100644 --- a/nimbus/vm/interpreter/gas_costs.nim +++ b/nimbus/vm/interpreter/gas_costs.nim @@ -69,13 +69,13 @@ type c_isNewAccount*: bool c_gasBalance*: GasInt c_contractGas*: Uint256 - c_currentMemSize*: Natural - c_memOffset*: Natural - c_memLength*: Natural + c_currentMemSize*: GasNatural + c_memOffset*: GasNatural + c_memLength*: GasNatural of Create: - cr_currentMemSize*: Natural - cr_memOffset*: Natural - cr_memLength*: Natural + cr_currentMemSize*: GasNatural + cr_memOffset*: GasNatural + cr_memLength*: GasNatural of SelfDestruct: sd_condition*: bool else: @@ -99,7 +99,7 @@ type of GckDynamic: d_handler*: proc(value: Uint256): GasInt {.nimcall, gcsafe.} of GckMemExpansion: - m_handler*: proc(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall, gcsafe.} + m_handler*: proc(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall, gcsafe.} of GckComplex: c_handler*: proc(value: Uint256, gasParams: GasParams): GasResult {.nimcall, gcsafe.} # We use gasCost/gasRefund for: @@ -118,7 +118,7 @@ template gasCosts(fork: Fork, prefix, ResultGasCostsName: untyped) = # ############### Helper functions ############################## - func `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength: Natural): GasInt {.inline.} = + func `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength: GasNatural): GasInt {.inline.} = # Input: size (in bytes) # Yellow Paper: @@ -185,23 +185,23 @@ template gasCosts(fork: Fork, prefix, ResultGasCostsName: untyped) = gasParams.cr_memOffset, gasParams.cr_memLength) - func `prefix gasSha3`(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall.} = + func `prefix gasSha3`(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall.} = result = `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength) result += static(FeeSchedule[GasSha3]) + static(FeeSchedule[GasSha3Word]) * (memLength).wordCount - func `prefix gasCopy`(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall.} = + func `prefix gasCopy`(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall.} = result = static(FeeSchedule[GasVeryLow]) + static(FeeSchedule[GasCopy]) * memLength.wordCount result += `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength) - func `prefix gasExtCodeCopy`(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall.} = + func `prefix gasExtCodeCopy`(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall.} = result = static(FeeSchedule[GasExtCode]) + static(FeeSchedule[GasCopy]) * memLength.wordCount result += `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength) - func `prefix gasLoadStore`(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall.} = + func `prefix gasLoadStore`(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall.} = result = static(FeeSchedule[GasVeryLow]) result += `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength) @@ -223,34 +223,34 @@ template gasCosts(fork: Fork, prefix, ResultGasCostsName: untyped) = if value.isZero and not gasParams.s_isStorageEmpty: result.gasRefund = static(FeeSchedule[RefundSclear]) - func `prefix gasLog0`(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall.} = + func `prefix gasLog0`(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall.} = result = `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength) result += static(FeeSchedule[GasLog]) + static(FeeSchedule[GasLogData]) * memLength - func `prefix gasLog1`(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall.} = + func `prefix gasLog1`(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall.} = result = `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength) result += static(FeeSchedule[GasLog]) + static(FeeSchedule[GasLogData]) * memLength + static(FeeSchedule[GasLogTopic]) - func `prefix gasLog2`(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall.} = + func `prefix gasLog2`(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall.} = result = `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength) result += static(FeeSchedule[GasLog]) + static(FeeSchedule[GasLogData]) * memLength + static(2 * FeeSchedule[GasLogTopic]) - func `prefix gasLog3`(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall.} = + func `prefix gasLog3`(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall.} = result = `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength) result += static(FeeSchedule[GasLog]) + static(FeeSchedule[GasLogData]) * memLength + static(3 * FeeSchedule[GasLogTopic]) - func `prefix gasLog4`(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall.} = + func `prefix gasLog4`(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall.} = result = `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength) result += static(FeeSchedule[GasLog]) + @@ -343,7 +343,7 @@ template gasCosts(fork: Fork, prefix, ResultGasCostsName: untyped) = if not value.isZero and gasParams.kind in {Call, CallCode}: result.gasRefund += static(FeeSchedule[GasCallStipend]) - func `prefix gasHalt`(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall.} = + func `prefix gasHalt`(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall.} = `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength) func `prefix gasSelfDestruct`(value: Uint256, gasParams: Gasparams): GasResult {.nimcall.} = @@ -352,7 +352,7 @@ template gasCosts(fork: Fork, prefix, ResultGasCostsName: untyped) = if gasParams.sd_condition: result.gasCost += static(FeeSchedule[GasNewAccount]) - func `prefix gasCreate2`(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall.} = + func `prefix gasCreate2`(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall.} = result = static(FeeSchedule[GasSha3Word]) * (memLength).wordCount # ################################################################################################### @@ -369,7 +369,7 @@ template gasCosts(fork: Fork, prefix, ResultGasCostsName: untyped) = func dynamic(handler: proc(value: Uint256): GasInt {.nimcall, gcsafe.}): GasCost = GasCost(kind: GckDynamic, d_handler: handler) - func memExpansion(handler: proc(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall, gcsafe.}): GasCost = + func memExpansion(handler: proc(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall, gcsafe.}): GasCost = GasCost(kind: GckMemExpansion, m_handler: handler) func complex(handler: proc(value: Uint256, gasParams: GasParams): GasResult {.nimcall, gcsafe.}): GasCost = diff --git a/nimbus/vm/interpreter/utils/utils_numeric.nim b/nimbus/vm/interpreter/utils/utils_numeric.nim index ee0ad996e..f0c2b215f 100644 --- a/nimbus/vm/interpreter/utils/utils_numeric.nim +++ b/nimbus/vm/interpreter/utils/utils_numeric.nim @@ -10,6 +10,11 @@ import eth/common/eth_types, eth/rlp, ../../../constants +type + # cannot use range for unknown reason + # Nim bug? + GasNatural* = int64 # range[0'i64..high(int64)] + # some methods based on py-evm utils/numeric func log2*[bits: static int](value: StUint[bits]): Natural {.inline.}= @@ -18,7 +23,7 @@ func log2*[bits: static int](value: StUint[bits]): Natural {.inline.}= func log256*(value: UInt256): Natural {.inline.}= value.log2 shr 3 # div 8 (= log2(256), Logb x = Loga x/Loga b) -func ceil32*(value: Natural): Natural {.inline.}= +func ceil32*(value: GasNatural): GasNatural {.inline.}= # Round input to the nearest bigger multiple of 32 result = value @@ -27,7 +32,7 @@ func ceil32*(value: Natural): Natural {.inline.}= if remainder != 0: return value + 32 - remainder -func wordCount*(length: Natural): Natural {.inline.}= +func wordCount*(length: GasNatural): GasNatural {.inline.}= # Returns the number of EVM words corresponding to a specific size. # EVM words is rounded up length.ceil32 shr 5 # equivalent to `div 32` (32 = 2^5) @@ -68,7 +73,7 @@ proc rangeToPadded*[T: StUint](x: openarray[byte], first, last: int, toLen = 0): if toLen > hi-lo+1: var temp: array[N, byte] - temp[0..hi-lo] = x.toOpenArray(lo, hi) + temp[0..hi-lo] = x.toOpenArray(lo, hi) result = T.fromBytesBE( temp, allowPadding = false @@ -78,7 +83,7 @@ proc rangeToPadded*[T: StUint](x: openarray[byte], first, last: int, toLen = 0): x.toOpenArray(lo, hi), allowPadding = true ) - + proc rangeToPadded2*[T: StUint](x: openarray[byte], first, last: int, toLen = 0): T = ## Convert take a slice of a sequence of bytes interpret it as the big endian ## representation of an Uint256. Use padding for sequence shorter than 32 bytes @@ -92,7 +97,7 @@ proc rangeToPadded2*[T: StUint](x: openarray[byte], first, last: int, toLen = 0) return # 0 var temp: array[N, byte] - temp[0..hi-lo] = x.toOpenArray(lo, hi) + temp[0..hi-lo] = x.toOpenArray(lo, hi) result = T.fromBytesBE( temp.toOpenArray(0, toLen-1), allowPadding = true