mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-19 06:31:57 +00:00
* tendermint/binary handles fixed-length arrays
* log-event tests
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package vm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
"github.com/tendermint/tendermint/events"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
. "github.com/tendermint/tendermint/vm"
|
||||
)
|
||||
|
||||
var expectedData = []byte{0x10}
|
||||
var expectedHeight int64 = 0
|
||||
var expectedTopics = []Word256{
|
||||
Int64ToWord256(1),
|
||||
Int64ToWord256(2),
|
||||
Int64ToWord256(3),
|
||||
Int64ToWord256(4)}
|
||||
|
||||
// Tests logs and events.
|
||||
func TestLog4(t *testing.T) {
|
||||
|
||||
st := newAppState()
|
||||
// Create accounts
|
||||
account1 := &Account{
|
||||
Address: LeftPadWord256(makeBytes(20)),
|
||||
}
|
||||
account2 := &Account{
|
||||
Address: LeftPadWord256(makeBytes(20)),
|
||||
}
|
||||
st.accounts[account1.Address.String()] = account1
|
||||
st.accounts[account2.Address.String()] = account2
|
||||
|
||||
ourVm := NewVM(st, newParams(), Zero256, nil)
|
||||
|
||||
eventSwitch := &events.EventSwitch{}
|
||||
eventSwitch.Start()
|
||||
eventId := types.EventStringLogEvent(account2.Address.Postfix(20))
|
||||
|
||||
doneChan := make(chan struct{}, 1)
|
||||
|
||||
eventSwitch.AddListenerForEvent("test", eventId, func(event interface{}) {
|
||||
logEvent := event.(*Log)
|
||||
// No need to test address as this event would not happen if it wasn't correct
|
||||
if !reflect.DeepEqual(logEvent.Topics, expectedTopics) {
|
||||
t.Errorf("Event topics are wrong. Got: %v. Expected: %v", logEvent.Topics, expectedTopics)
|
||||
}
|
||||
if !bytes.Equal(logEvent.Data, expectedData) {
|
||||
t.Errorf("Event data is wrong. Got: %s. Expected: %s", logEvent.Data, expectedData)
|
||||
}
|
||||
if logEvent.Height != expectedHeight {
|
||||
t.Errorf("Event block height is wrong. Got: %d. Expected: %d", logEvent.Height, expectedHeight)
|
||||
}
|
||||
doneChan <- struct{}{}
|
||||
})
|
||||
|
||||
ourVm.SetFireable(eventSwitch)
|
||||
|
||||
var gas int64 = 100000
|
||||
|
||||
mstore8 := byte(MSTORE8)
|
||||
push1 := byte(PUSH1)
|
||||
log4 := byte(LOG4)
|
||||
stop := byte(STOP)
|
||||
|
||||
code := []byte{
|
||||
push1, 16, // data value
|
||||
push1, 0, // memory slot
|
||||
mstore8,
|
||||
push1, 4, // topic 4
|
||||
push1, 3, // topic 3
|
||||
push1, 2, // topic 2
|
||||
push1, 1, // topic 1
|
||||
push1, 1, // size of data
|
||||
push1, 0, // data starts at this offset
|
||||
log4,
|
||||
stop,
|
||||
}
|
||||
|
||||
_, err := ourVm.Call(account1, account2, code, []byte{}, 0, &gas)
|
||||
<-doneChan
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -696,7 +696,13 @@ func (vm *VM) call(caller, callee *Account, code, input []byte, value int64, gas
|
||||
vm.params.BlockHeight,
|
||||
}
|
||||
vm.appState.AddLog(log)
|
||||
dbg.Printf(" => %v\n", log)
|
||||
if vm.evc != nil {
|
||||
eventId := types.EventStringLogEvent(callee.Address.Postfix(20))
|
||||
fmt.Printf("eventId: %s\n", eventId)
|
||||
vm.evc.FireEvent(eventId, log)
|
||||
}
|
||||
// Using sol-log for this as well since 'log' will print garbage.
|
||||
dbg.Printf(" => T:%X D:%X\n", log.Topics, log.Data)
|
||||
|
||||
case CREATE: // 0xF0
|
||||
if !HasPermission(vm.appState, callee, ptypes.CreateContract) {
|
||||
|
||||
Reference in New Issue
Block a user