Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion data/transactions/logic/opcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,17 @@ type linearCost struct {
chunkSize int
}

// divideCeilUnsafely provides `math.Ceil` semantics using integer division. The technique avoids slower floating point operations as suggested in https://stackoverflow.com/a/2745086.
// The method does _not_ check for divide-by-zero.
func divideCeilUnsafely(numerator int, denominator int) int {
return (numerator + denominator - 1) / denominator
}

func (lc *linearCost) compute(stack []stackValue) int {
cost := lc.baseCost
if lc.chunkCost != 0 && lc.chunkSize != 0 {
cost += lc.chunkCost * (len(stack[len(stack)-1].Bytes) + lc.chunkSize - 1) / lc.chunkSize
// Uses divideCeilUnsafely rather than (len/size) to match how Ethereum discretizes hashing costs.
cost += divideCeilUnsafely(lc.chunkCost*len(stack[len(stack)-1].Bytes), lc.chunkSize)
}
return cost
}
Expand Down