From 20ee1ae65e57ef7d60404277162c84c572c6bb10 Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 21 Jul 2014 20:38:43 +0200 Subject: [PATCH] Refactored CALLDATALOAD to use big int * Added BigMin --- ethchain/vm.go | 14 +++++++++----- ethutil/big.go | 19 +++++++++++-------- ethutil/common.go | 12 +++++++----- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/ethchain/vm.go b/ethchain/vm.go index 34ecd95b71..a9bed1eca9 100644 --- a/ethchain/vm.go +++ b/ethchain/vm.go @@ -503,13 +503,17 @@ func (vm *Vm) RunClosure(closure *Closure) (ret []byte, err error) { vm.Printf(" => %v", vm.vars.Value) case CALLDATALOAD: require(1) - offset := stack.Pop().Int64() + var ( + offset = stack.Pop() + data = make([]byte, 32) + lenData = big.NewInt(int64(len(closure.Args))) + ) - data := make([]byte, 32) - if big.NewInt(int64(len(closure.Args))).Cmp(big.NewInt(offset)) >= 0 { - l := int64(math.Min(float64(offset+32), float64(len(closure.Args)))) + if lenData.Cmp(offset) >= 0 { + length := new(big.Int).Add(offset, ethutil.Big32) + length = ethutil.BigMin(length, lenData) - copy(data, closure.Args[offset:l]) + copy(data, closure.Args[offset.Int64():length.Int64()]) } vm.Printf(" => 0x%x", data) diff --git a/ethutil/big.go b/ethutil/big.go index 7af6f74145..ec263b8188 100644 --- a/ethutil/big.go +++ b/ethutil/big.go @@ -4,14 +4,6 @@ import ( "math/big" ) -var BigInt0 *big.Int = big.NewInt(0) - -// True -var BigTrue *big.Int = big.NewInt(1) - -// False -var BigFalse *big.Int = big.NewInt(0) - // Big pow // // Returns the power of two big integers @@ -73,3 +65,14 @@ func BigMax(x, y *big.Int) *big.Int { return x } + +// Big min +// +// Returns the minimum size big integer +func BigMin(x, y *big.Int) *big.Int { + if x.Cmp(y) >= 0 { + return y + } + + return x +} diff --git a/ethutil/common.go b/ethutil/common.go index 2fd031a20a..cbd94e7add 100644 --- a/ethutil/common.go +++ b/ethutil/common.go @@ -58,9 +58,11 @@ func CurrencyToString(num *big.Int) string { // Common big integers often used var ( - Big1 = big.NewInt(1) - Big2 = big.NewInt(2) - Big0 = big.NewInt(0) - Big32 = big.NewInt(32) - Big256 = big.NewInt(0xff) + Big1 = big.NewInt(1) + Big2 = big.NewInt(2) + Big0 = big.NewInt(0) + BigTrue = Big1 + BigFalse = Big0 + Big32 = big.NewInt(32) + Big256 = big.NewInt(0xff) )