From 3579a3fdb9cb72fa4da1515e9f516a3d1e6e9007 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Fri, 17 Apr 2015 17:43:45 -0700 Subject: [PATCH] tiny fixes --- merkle/iavl_tree.go | 4 ++-- rpc/core/txs.go | 5 ++--- vm/vm.go | 45 +++++++++++++++++++++++++++++++-------------- 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/merkle/iavl_tree.go b/merkle/iavl_tree.go index da474318..725d9a6f 100644 --- a/merkle/iavl_tree.go +++ b/merkle/iavl_tree.go @@ -235,9 +235,9 @@ func (ndb *nodeDB) SaveNode(t *IAVLTree, node *IAVLNode) { if node.persisted { panic("Shouldn't be calling save on an already persisted node.") } - if _, ok := ndb.cache[string(node.hash)]; ok { + /*if _, ok := ndb.cache[string(node.hash)]; ok { panic("Shouldn't be calling save on an already cached node.") - } + }*/ // Save node bytes to db buf := bytes.NewBuffer(nil) _, _, err := node.writeToCountHashes(t, buf) diff --git a/rpc/core/txs.go b/rpc/core/txs.go index 628bc658..84606ccf 100644 --- a/rpc/core/txs.go +++ b/rpc/core/txs.go @@ -12,7 +12,7 @@ import ( func toVMAccount(acc *account.Account) *vm.Account { return &vm.Account{ - Address: RightPadWord256(acc.Address), + Address: LeftPadWord256(acc.Address), Balance: acc.Balance, Code: acc.Code, // This is crazy. Nonce: uint64(acc.Sequence), @@ -26,9 +26,8 @@ func toVMAccount(acc *account.Account) *vm.Account { // Run a contract's code on an isolated and unpersisted state // Cannot be used to create new contracts func Call(address, data []byte) (*ctypes.ResponseCall, error) { - st := consensusState.GetState() // performs a copy - cache := mempoolReactor.Mempool.GetCache() + cache := state.NewBlockCache(st) outAcc := cache.GetAccount(address) if outAcc == nil { return nil, fmt.Errorf("Account %x does not exist", address) diff --git a/vm/vm.go b/vm/vm.go index ba16d846..19344591 100644 --- a/vm/vm.go +++ b/vm/vm.go @@ -147,7 +147,7 @@ func (vm *VM) call(caller, callee *Account, code, input []byte, value uint64, ga xb := new(big.Int).SetBytes(flip(x[:])) yb := new(big.Int).SetBytes(flip(y[:])) prod := new(big.Int).Mul(xb, yb) - stack.Push(RightPadWord256(flip(prod.Bytes()))) + stack.Push(RightPadWord256(flip(rightMostBytes(prod.Bytes(), 32)))) dbg.Printf(" %v * %v = %v\n", xb, yb, prod) case SUB: // 0x03 @@ -304,13 +304,24 @@ func (vm *VM) call(caller, callee *Account, code, input []byte, value uint64, ga dbg.Printf(" %v == 0 = %v\n", x, x == 0) case AND: // 0x16 - x, y := stack.Pop64(), stack.Pop64() - stack.Push64(x & y) - dbg.Printf(" %v & %v = %v\n", x, y, x&y) + //x, y := stack.Pop64(), stack.Pop64() + //stack.Push64(x & y) + x, y := stack.Pop(), stack.Pop() + xb := new(big.Int).SetBytes(flip(x[:])) + yb := new(big.Int).SetBytes(flip(y[:])) + res := new(big.Int).And(xb, yb) + stack.Push(RightPadWord256(flip(res.Bytes()))) + dbg.Printf(" %v & %v = %v\n", xb, yb, res) case OR: // 0x17 - x, y := stack.Pop64(), stack.Pop64() - stack.Push64(x | y) - dbg.Printf(" %v | %v = %v\n", x, y, x|y) + //x, y := stack.Pop64(), stack.Pop64() + //stack.Push64(x | y) + //dbg.Printf(" %v | %v = %v\n", x, y, x|y) + x, y := stack.Pop(), stack.Pop() + xb := new(big.Int).SetBytes(flip(x[:])) + yb := new(big.Int).SetBytes(flip(y[:])) + res := new(big.Int).Or(xb, yb) + stack.Push(RightPadWord256(flip(res.Bytes()))) + dbg.Printf(" %v & %v = %v\n", xb, yb, res) case XOR: // 0x18 x, y := stack.Pop64(), stack.Pop64() @@ -345,8 +356,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(callee.Address) - dbg.Printf(" => %X\n", callee.Address) + stack.Push(flipWord(callee.Address)) + dbg.Printf(" => %X\n", flipWord(callee.Address)) case BALANCE: // 0x31 addr := stack.Pop() @@ -362,12 +373,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(vm.origin) - dbg.Printf(" => %X\n", vm.origin) + stack.Push(flipWord(vm.origin)) + dbg.Printf(" => %X\n", flipWord(vm.origin)) case CALLER: // 0x33 - stack.Push(caller.Address) - dbg.Printf(" => %X\n", caller.Address) + stack.Push(flipWord(caller.Address)) + dbg.Printf(" => %X\n", flipWord(caller.Address)) case CALLVALUE: // 0x34 stack.Push64(value) @@ -616,7 +627,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(newAccount.Address) + stack.Push(flipWord(newAccount.Address)) } case CALL, CALLCODE: // 0xF1, 0xF2 @@ -732,6 +743,12 @@ func subslice(data []byte, offset, length uint64, flip_ bool) (ret []byte, ok bo return } +func rightMostBytes(data []byte, n int) []byte { + size := MinInt(len(data), n) + offset := len(data) - size + return data[offset:] +} + func codeGetOp(code []byte, n uint64) OpCode { if uint64(len(code)) <= n { return OpCode(0) // stop