You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.1 KiB

  1. package vm
  2. import (
  3. "bytes"
  4. "reflect"
  5. "testing"
  6. . "github.com/tendermint/tendermint/common"
  7. "github.com/tendermint/tendermint/events"
  8. "github.com/tendermint/tendermint/types"
  9. . "github.com/tendermint/tendermint/vm"
  10. )
  11. var expectedData = []byte{0x10}
  12. var expectedHeight int64 = 0
  13. var expectedTopics = []Word256{
  14. Int64ToWord256(1),
  15. Int64ToWord256(2),
  16. Int64ToWord256(3),
  17. Int64ToWord256(4)}
  18. // Tests logs and events.
  19. func TestLog4(t *testing.T) {
  20. st := newAppState()
  21. // Create accounts
  22. account1 := &Account{
  23. Address: LeftPadWord256(makeBytes(20)),
  24. }
  25. account2 := &Account{
  26. Address: LeftPadWord256(makeBytes(20)),
  27. }
  28. st.accounts[account1.Address.String()] = account1
  29. st.accounts[account2.Address.String()] = account2
  30. ourVm := NewVM(st, newParams(), Zero256, nil)
  31. eventSwitch := &events.EventSwitch{}
  32. eventSwitch.Start()
  33. eventId := types.EventStringLogEvent(account2.Address.Postfix(20))
  34. doneChan := make(chan struct{}, 1)
  35. eventSwitch.AddListenerForEvent("test", eventId, func(event interface{}) {
  36. logEvent := event.(*Log)
  37. // No need to test address as this event would not happen if it wasn't correct
  38. if !reflect.DeepEqual(logEvent.Topics, expectedTopics) {
  39. t.Errorf("Event topics are wrong. Got: %v. Expected: %v", logEvent.Topics, expectedTopics)
  40. }
  41. if !bytes.Equal(logEvent.Data, expectedData) {
  42. t.Errorf("Event data is wrong. Got: %s. Expected: %s", logEvent.Data, expectedData)
  43. }
  44. if logEvent.Height != expectedHeight {
  45. t.Errorf("Event block height is wrong. Got: %d. Expected: %d", logEvent.Height, expectedHeight)
  46. }
  47. doneChan <- struct{}{}
  48. })
  49. ourVm.SetFireable(eventSwitch)
  50. var gas int64 = 100000
  51. mstore8 := byte(MSTORE8)
  52. push1 := byte(PUSH1)
  53. log4 := byte(LOG4)
  54. stop := byte(STOP)
  55. code := []byte{
  56. push1, 16, // data value
  57. push1, 0, // memory slot
  58. mstore8,
  59. push1, 4, // topic 4
  60. push1, 3, // topic 3
  61. push1, 2, // topic 2
  62. push1, 1, // topic 1
  63. push1, 1, // size of data
  64. push1, 0, // data starts at this offset
  65. log4,
  66. stop,
  67. }
  68. _, err := ourVm.Call(account1, account2, code, []byte{}, 0, &gas)
  69. <-doneChan
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. }