Browse Source

little fixes to vm and endianess

pull/55/head
Ethan Buchman 10 years ago
parent
commit
e702303e16
6 changed files with 32 additions and 25 deletions
  1. +10
    -2
      common/int.go
  2. +4
    -10
      common/word.go
  3. +1
    -1
      state/tx_cache.go
  4. +5
    -3
      vm/stack.go
  5. +1
    -1
      vm/test/fake_app_state.go
  6. +11
    -8
      vm/vm.go

+ 10
- 2
common/int.go 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)
}
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)
}

+ 4
- 10
common/word.go View File

@ -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)
}
//-------------------------------------


+ 1
- 1
state/tx_cache.go View File

@ -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]
}


+ 5
- 3
vm/stack.go View File

@ -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 --")


+ 1
- 1
vm/test/fake_app_state.go View File

@ -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])
}

+ 11
- 8
vm/vm.go 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[:])
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


Loading…
Cancel
Save