little fixes to vm and endianess

This commit is contained in:
Ethan Buchman 2015-04-17 20:51:01 -07:00
parent a28c1c83fc
commit e702303e16
6 changed files with 32 additions and 25 deletions

View File

@ -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) binary.LittleEndian.PutUint64(dest, i)
} }
func GetUint64(src []byte) uint64 { func GetUint64LE(src []byte) uint64 {
return binary.LittleEndian.Uint64(src) 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)
}

View File

@ -2,7 +2,6 @@ package common
import ( import (
"bytes" "bytes"
"encoding/binary"
"sort" "sort"
) )
@ -30,12 +29,9 @@ func (w Word256) Compare(other Word256) int {
} }
func Uint64ToWord256(i uint64) Word256 { func Uint64ToWord256(i uint64) Word256 {
word := Word256{}
buf := [8]byte{} buf := [8]byte{}
PutUint64(buf[:], i) PutUint64BE(buf[:], i)
word[24], word[25], word[26], word[27] = buf[7], buf[6], buf[5], buf[4] return LeftPadWord256(buf[:])
word[28], word[29], word[30], word[31] = buf[3], buf[2], buf[1], buf[0]
return word
} }
func RightPadWord256(bz []byte) (word Word256) { func RightPadWord256(bz []byte) (word Word256) {
@ -49,10 +45,8 @@ func LeftPadWord256(bz []byte) (word Word256) {
} }
func Uint64FromWord256(word Word256) uint64 { func Uint64FromWord256(word Word256) uint64 {
buf := [8]byte{} buf := word.Postfix(8)
buf[0], buf[1], buf[2], buf[3] = word[31], word[30], word[29], word[28] return GetUint64BE(buf)
buf[4], buf[5], buf[6], buf[7] = word[27], word[26], word[25], word[24]
return binary.LittleEndian.Uint64(buf[:])
} }
//------------------------------------- //-------------------------------------

View File

@ -147,7 +147,7 @@ func (cache *TxCache) AddLog(log *vm.Log) {
func NewContractAddress(caller []byte, nonce uint64) []byte { func NewContractAddress(caller []byte, nonce uint64) []byte {
temp := make([]byte, 32+8) temp := make([]byte, 32+8)
copy(temp, caller) copy(temp, caller)
PutUint64(temp[32:], nonce) PutUint64LE(temp[32:], nonce)
return sha3.Sha3(temp)[:20] return sha3.Sha3(temp)[:20]
} }

View File

@ -106,11 +106,13 @@ func (st *Stack) Peek() Word256 {
return st.data[st.ptr-1] return st.data[st.ptr-1]
} }
func (st *Stack) Print() { func (st *Stack) Print(n int) {
fmt.Println("### stack ###") fmt.Println("### stack ###")
if st.ptr > 0 { if st.ptr > 0 {
for i, val := range st.data { nn := MinInt(n, st.ptr)
fmt.Printf("%-3d %v\n", i, val) 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 { } else {
fmt.Println("-- empty --") fmt.Println("-- empty --")

View File

@ -89,6 +89,6 @@ func createAddress(creator *Account) Word256 {
creator.Nonce += 1 creator.Nonce += 1
temp := make([]byte, 32+8) temp := make([]byte, 32+8)
copy(temp, creator.Address[:]) copy(temp, creator.Address[:])
PutUint64(temp[32:], nonce) PutUint64LE(temp[32:], nonce)
return LeftPadWord256(sha3.Sha3(temp)[:20]) return LeftPadWord256(sha3.Sha3(temp)[:20])
} }

View File

@ -254,6 +254,7 @@ func (vm *VM) call(caller, callee *Account, code, input []byte, value uint64, ga
yb := new(big.Int).SetBytes(y[:]) yb := new(big.Int).SetBytes(y[:])
pow := new(big.Int).Exp(xb, yb, big.NewInt(0)) pow := new(big.Int).Exp(xb, yb, big.NewInt(0))
res := LeftPadWord256(U256(pow).Bytes()) res := LeftPadWord256(U256(pow).Bytes())
stack.Push(res)
dbg.Printf(" %v ** %v = %v (%X)\n", xb, yb, pow, res) dbg.Printf(" %v ** %v = %v (%X)\n", xb, yb, pow, res)
case SIGNEXTEND: // 0x0B 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) dbg.Printf(" => (%v) %X\n", size, data)
case ADDRESS: // 0x30 case ADDRESS: // 0x30
stack.Push(flipWord(callee.Address)) stack.Push(callee.Address)
dbg.Printf(" => %X\n", flipWord(callee.Address)) dbg.Printf(" => %X\n", callee.Address)
case BALANCE: // 0x31 case BALANCE: // 0x31
addr := stack.Pop() 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) dbg.Printf(" => %v (%X)\n", balance, addr)
case ORIGIN: // 0x32 case ORIGIN: // 0x32
stack.Push(flipWord(vm.origin)) stack.Push(vm.origin)
dbg.Printf(" => %X\n", flipWord(vm.origin)) dbg.Printf(" => %X\n", vm.origin)
case CALLER: // 0x33 case CALLER: // 0x33
stack.Push(flipWord(caller.Address)) stack.Push(caller.Address)
dbg.Printf(" => %X\n", flipWord(caller.Address)) dbg.Printf(" => %X\n", caller.Address)
case CALLVALUE: // 0x34 case CALLVALUE: // 0x34
stack.Push64(value) stack.Push64(value)
@ -621,6 +622,7 @@ func (vm *VM) call(caller, callee *Account, code, input []byte, value uint64, ga
stack.Push(res) stack.Push(res)
pc += a pc += a
dbg.Printf(" => 0x%X\n", res) 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: case DUP1, DUP2, DUP3, DUP4, DUP5, DUP6, DUP7, DUP8, DUP9, DUP10, DUP11, DUP12, DUP13, DUP14, DUP15, DUP16:
n := int(op - DUP1 + 1) 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: case SWAP1, SWAP2, SWAP3, SWAP4, SWAP5, SWAP6, SWAP7, SWAP8, SWAP9, SWAP10, SWAP11, SWAP12, SWAP13, SWAP14, SWAP15, SWAP16:
n := int(op - SWAP1 + 2) n := int(op - SWAP1 + 2)
stack.Swap(n) 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: case LOG0, LOG1, LOG2, LOG3, LOG4:
n := int(op - LOG0) n := int(op - LOG0)
@ -674,7 +677,7 @@ func (vm *VM) call(caller, callee *Account, code, input []byte, value uint64, ga
stack.Push(Zero256) stack.Push(Zero256)
} else { } else {
newAccount.Code = ret // Set the code newAccount.Code = ret // Set the code
stack.Push(flipWord(newAccount.Address)) stack.Push(newAccount.Address)
} }
case CALL, CALLCODE: // 0xF1, 0xF2 case CALL, CALLCODE: // 0xF1, 0xF2