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.

90 lines
2.2 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.NewEventSwitch()
  32. _, err := eventSwitch.Start()
  33. if err != nil {
  34. t.Errorf("Failed to start eventSwitch: %v", err)
  35. }
  36. eventID := types.EventStringLogEvent(account2.Address.Postfix(20))
  37. doneChan := make(chan struct{}, 1)
  38. eventSwitch.AddListenerForEvent("test", eventID, func(event types.EventData) {
  39. logEvent := event.(types.EventDataLog)
  40. // No need to test address as this event would not happen if it wasn't correct
  41. if !reflect.DeepEqual(logEvent.Topics, expectedTopics) {
  42. t.Errorf("Event topics are wrong. Got: %v. Expected: %v", logEvent.Topics, expectedTopics)
  43. }
  44. if !bytes.Equal(logEvent.Data, expectedData) {
  45. t.Errorf("Event data is wrong. Got: %s. Expected: %s", logEvent.Data, expectedData)
  46. }
  47. if logEvent.Height != expectedHeight {
  48. t.Errorf("Event block height is wrong. Got: %d. Expected: %d", logEvent.Height, expectedHeight)
  49. }
  50. doneChan <- struct{}{}
  51. })
  52. ourVm.SetFireable(eventSwitch)
  53. var gas int64 = 100000
  54. mstore8 := byte(MSTORE8)
  55. push1 := byte(PUSH1)
  56. log4 := byte(LOG4)
  57. stop := byte(STOP)
  58. code := []byte{
  59. push1, 16, // data value
  60. push1, 0, // memory slot
  61. mstore8,
  62. push1, 4, // topic 4
  63. push1, 3, // topic 3
  64. push1, 2, // topic 2
  65. push1, 1, // topic 1
  66. push1, 1, // size of data
  67. push1, 0, // data starts at this offset
  68. log4,
  69. stop,
  70. }
  71. _, err = ourVm.Call(account1, account2, code, []byte{}, 0, &gas)
  72. <-doneChan
  73. if err != nil {
  74. t.Fatal(err)
  75. }
  76. }