From e702303e16d02dd3f7eaa161480672682c2e2d2d Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Fri, 17 Apr 2015 20:51:01 -0700 Subject: [PATCH] little fixes to vm and endianess --- common/int.go | 12 ++++++++++-- common/word.go | 14 ++++---------- state/tx_cache.go | 2 +- vm/stack.go | 8 +++++--- vm/test/fake_app_state.go | 2 +- vm/vm.go | 19 +++++++++++-------- 6 files changed, 32 insertions(+), 25 deletions(-) diff --git a/common/int.go b/common/int.go index f1c376d75..c6e85dc62 100644 --- a/common/int.go +++ b/common/int.go @@ -22,10 +22,18 @@ func (p Uint64Slice) Search(x uint64) int { return SearchUint64s(p, x) } //----------------------------------------------------------------------------- -func PutUint64(dest []byte, i uint64) { +func PutUint64LE(dest []byte, i uint64) { binary.LittleEndian.PutUint64(dest, i) } -func GetUint64(src []byte) uint64 { +func GetUint64LE(src []byte) uint64 { return binary.LittleEndian.Uint64(src) } + +func PutUint64BE(dest []byte, i uint64) { + binary.BigEndian.PutUint64(dest, i) +} + +func GetUint64BE(src []byte) uint64 { + return binary.BigEndian.Uint64(src) +} diff --git a/common/word.go b/common/word.go index 1b62f0640..264a7dcc1 100644 --- a/common/word.go +++ b/common/word.go @@ -2,7 +2,6 @@ package common import ( "bytes" - "encoding/binary" "sort" ) @@ -30,12 +29,9 @@ func (w Word256) Compare(other Word256) int { } func Uint64ToWord256(i uint64) Word256 { - word := Word256{} buf := [8]byte{} - PutUint64(buf[:], i) - word[24], word[25], word[26], word[27] = buf[7], buf[6], buf[5], buf[4] - word[28], word[29], word[30], word[31] = buf[3], buf[2], buf[1], buf[0] - return word + PutUint64BE(buf[:], i) + return LeftPadWord256(buf[:]) } func RightPadWord256(bz []byte) (word Word256) { @@ -49,10 +45,8 @@ func LeftPadWord256(bz []byte) (word Word256) { } func Uint64FromWord256(word Word256) uint64 { - buf := [8]byte{} - buf[0], buf[1], buf[2], buf[3] = word[31], word[30], word[29], word[28] - buf[4], buf[5], buf[6], buf[7] = word[27], word[26], word[25], word[24] - return binary.LittleEndian.Uint64(buf[:]) + buf := word.Postfix(8) + return GetUint64BE(buf) } //------------------------------------- diff --git a/state/tx_cache.go b/state/tx_cache.go index d181e0472..568639d0b 100644 --- a/state/tx_cache.go +++ b/state/tx_cache.go @@ -147,7 +147,7 @@ func (cache *TxCache) AddLog(log *vm.Log) { func NewContractAddress(caller []byte, nonce uint64) []byte { temp := make([]byte, 32+8) copy(temp, caller) - PutUint64(temp[32:], nonce) + PutUint64LE(temp[32:], nonce) return sha3.Sha3(temp)[:20] } diff --git a/vm/stack.go b/vm/stack.go index 17938d2c5..500ff4d15 100644 --- a/vm/stack.go +++ b/vm/stack.go @@ -106,11 +106,13 @@ func (st *Stack) Peek() Word256 { return st.data[st.ptr-1] } -func (st *Stack) Print() { +func (st *Stack) Print(n int) { fmt.Println("### stack ###") if st.ptr > 0 { - for i, val := range st.data { - fmt.Printf("%-3d %v\n", i, val) + nn := MinInt(n, st.ptr) + for j, i := 0, st.ptr-1; i > st.ptr-1-nn; i-- { + fmt.Printf("%-3d %X\n", j, st.data[i]) + j += 1 } } else { fmt.Println("-- empty --") diff --git a/vm/test/fake_app_state.go b/vm/test/fake_app_state.go index 456e3b3c9..a6e29e926 100644 --- a/vm/test/fake_app_state.go +++ b/vm/test/fake_app_state.go @@ -89,6 +89,6 @@ func createAddress(creator *Account) Word256 { creator.Nonce += 1 temp := make([]byte, 32+8) copy(temp, creator.Address[:]) - PutUint64(temp[32:], nonce) + PutUint64LE(temp[32:], nonce) return LeftPadWord256(sha3.Sha3(temp)[:20]) } diff --git a/vm/vm.go b/vm/vm.go index e24da3a06..f9ad0b1c7 100644 --- a/vm/vm.go +++ b/vm/vm.go @@ -254,6 +254,7 @@ func (vm *VM) call(caller, callee *Account, code, input []byte, value uint64, ga yb := new(big.Int).SetBytes(y[:]) pow := new(big.Int).Exp(xb, yb, big.NewInt(0)) res := LeftPadWord256(U256(pow).Bytes()) + stack.Push(res) dbg.Printf(" %v ** %v = %v (%X)\n", xb, yb, pow, res) case SIGNEXTEND: // 0x0B @@ -402,8 +403,8 @@ func (vm *VM) call(caller, callee *Account, code, input []byte, value uint64, ga dbg.Printf(" => (%v) %X\n", size, data) case ADDRESS: // 0x30 - stack.Push(flipWord(callee.Address)) - dbg.Printf(" => %X\n", flipWord(callee.Address)) + stack.Push(callee.Address) + dbg.Printf(" => %X\n", callee.Address) case BALANCE: // 0x31 addr := stack.Pop() @@ -419,12 +420,12 @@ func (vm *VM) call(caller, callee *Account, code, input []byte, value uint64, ga dbg.Printf(" => %v (%X)\n", balance, addr) case ORIGIN: // 0x32 - stack.Push(flipWord(vm.origin)) - dbg.Printf(" => %X\n", flipWord(vm.origin)) + stack.Push(vm.origin) + dbg.Printf(" => %X\n", vm.origin) case CALLER: // 0x33 - stack.Push(flipWord(caller.Address)) - dbg.Printf(" => %X\n", flipWord(caller.Address)) + stack.Push(caller.Address) + dbg.Printf(" => %X\n", caller.Address) case CALLVALUE: // 0x34 stack.Push64(value) @@ -621,6 +622,7 @@ func (vm *VM) call(caller, callee *Account, code, input []byte, value uint64, ga stack.Push(res) pc += a dbg.Printf(" => 0x%X\n", res) + stack.Print(10) case DUP1, DUP2, DUP3, DUP4, DUP5, DUP6, DUP7, DUP8, DUP9, DUP10, DUP11, DUP12, DUP13, DUP14, DUP15, DUP16: n := int(op - DUP1 + 1) @@ -630,7 +632,8 @@ func (vm *VM) call(caller, callee *Account, code, input []byte, value uint64, ga case SWAP1, SWAP2, SWAP3, SWAP4, SWAP5, SWAP6, SWAP7, SWAP8, SWAP9, SWAP10, SWAP11, SWAP12, SWAP13, SWAP14, SWAP15, SWAP16: n := int(op - SWAP1 + 2) stack.Swap(n) - dbg.Printf(" => [%d]\n", n) + dbg.Printf(" => [%d] %X\n", n, stack.Peek()) + stack.Print(10) case LOG0, LOG1, LOG2, LOG3, LOG4: n := int(op - LOG0) @@ -674,7 +677,7 @@ func (vm *VM) call(caller, callee *Account, code, input []byte, value uint64, ga stack.Push(Zero256) } else { newAccount.Code = ret // Set the code - stack.Push(flipWord(newAccount.Address)) + stack.Push(newAccount.Address) } case CALL, CALLCODE: // 0xF1, 0xF2