vm: eventable and flip fix on CALL address

This commit is contained in:
Ethan Buchman 2015-04-14 19:25:49 -07:00
parent 860d5478e9
commit b4388ae010
3 changed files with 35 additions and 3 deletions

View File

@ -36,7 +36,7 @@ func makeBytes(n int) []byte {
}
func TestVM(t *testing.T) {
ourVm := NewVM(newAppState(), newParams(), Zero256)
ourVm := NewVM(newAppState(), newParams(), Zero256, nil)
// Create accounts
account1 := &Account{
@ -72,7 +72,7 @@ func TestSubcurrency(t *testing.T) {
st.accounts[account1.Address.String()] = account1
st.accounts[account2.Address.String()] = account2
ourVm := NewVM(st, newParams(), Zero256)
ourVm := NewVM(st, newParams(), Zero256, nil)
var gas uint64 = 1000
code_parts := []string{"620f42403355",

View File

@ -46,3 +46,11 @@ type Params struct {
BlockTime int64
GasLimit uint64
}
type CallData struct {
Caller []byte
Callee []byte
Data []byte
Value uint64
Gas uint64
}

View File

@ -7,6 +7,7 @@ import (
. "github.com/tendermint/tendermint/common"
"github.com/tendermint/tendermint/events"
"github.com/tendermint/tendermint/types"
"github.com/tendermint/tendermint/vm/sha3"
)
@ -44,18 +45,20 @@ type VM struct {
appState AppState
params Params
origin Word256
txid []byte
callDepth int
evsw *events.EventSwitch
}
func NewVM(appState AppState, params Params, origin Word256) *VM {
func NewVM(appState AppState, params Params, origin Word256, txid []byte) *VM {
return &VM{
appState: appState,
params: params,
origin: origin,
callDepth: 0,
txid: txid,
}
}
@ -80,12 +83,31 @@ func (vm *VM) Call(caller, callee *Account, code, input []byte, value uint64, ga
vm.callDepth += 1
output, err = vm.call(caller, callee, code, input, value, gas)
vm.callDepth -= 1
exception := ""
if err != nil {
exception = err.Error()
err := transfer(callee, caller, value)
if err != nil {
panic("Could not return value to caller")
}
}
// if callDepth is 0 the event is fired from ExecTx (along with the Input invent)
// otherwise, we fire from here.
if vm.callDepth != 0 && vm.evsw != nil {
vm.evsw.FireEvent(types.EventStringAccReceive(callee.Address.Prefix(20)), struct {
CallData *CallData
Origin []byte
TxId []byte
Return []byte
Exception string
}{
&CallData{caller.Address.Prefix(20), callee.Address.Prefix(20), input, value, *gas},
vm.origin.Prefix(20),
vm.txid,
output,
exception,
})
}
return
}
@ -636,6 +658,8 @@ func (vm *VM) call(caller, callee *Account, code, input []byte, value uint64, ga
if ok = useGas(gas, GasGetAccount); !ok {
return nil, firstErr(err, ErrInsufficientGas)
}
// :(
addr = RightPadWord256(flip(addr.Prefix(20)))
acc := vm.appState.GetAccount(addr)
if acc == nil {
return nil, firstErr(err, ErrUnknownAddress)