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.

99 lines
2.6 KiB

  1. package vm
  2. import (
  3. "crypto/rand"
  4. "encoding/hex"
  5. "fmt"
  6. "strings"
  7. "testing"
  8. "time"
  9. . "github.com/tendermint/tendermint/common"
  10. . "github.com/tendermint/tendermint/vm"
  11. )
  12. func newAppState() *FakeAppState {
  13. return &FakeAppState{
  14. accounts: make(map[string]*Account),
  15. storage: make(map[string]Word256),
  16. logs: nil,
  17. }
  18. }
  19. func newParams() Params {
  20. return Params{
  21. BlockHeight: 0,
  22. BlockHash: Zero256,
  23. BlockTime: 0,
  24. GasLimit: 0,
  25. }
  26. }
  27. func makeBytes(n int) []byte {
  28. b := make([]byte, n)
  29. rand.Read(b)
  30. return b
  31. }
  32. func TestVM(t *testing.T) {
  33. ourVm := NewVM(newAppState(), newParams(), Zero256)
  34. // Create accounts
  35. account1 := &Account{
  36. Address: Uint64ToWord256(100),
  37. }
  38. account2 := &Account{
  39. Address: Uint64ToWord256(101),
  40. }
  41. var gas uint64 = 1000
  42. N := []byte{0xff, 0xff}
  43. // Loop N times
  44. code := []byte{0x60, 0x00, 0x60, 0x20, 0x52, 0x5B, byte(0x60 + len(N) - 1)}
  45. for i := 0; i < len(N); i++ {
  46. code = append(code, N[i])
  47. }
  48. code = append(code, []byte{0x60, 0x20, 0x51, 0x12, 0x15, 0x60, byte(0x1b + len(N)), 0x57, 0x60, 0x01, 0x60, 0x20, 0x51, 0x01, 0x60, 0x20, 0x52, 0x60, 0x05, 0x56, 0x5B}...)
  49. start := time.Now()
  50. output, err := ourVm.Call(account1, account2, code, []byte{}, 0, &gas)
  51. fmt.Printf("Output: %v Error: %v\n", output, err)
  52. fmt.Println("Call took:", time.Since(start))
  53. }
  54. func TestSubcurrency(t *testing.T) {
  55. st := newAppState()
  56. // Create accounts
  57. account1 := &Account{
  58. Address: RightPadWord256(makeBytes(20)),
  59. }
  60. account2 := &Account{
  61. Address: RightPadWord256(makeBytes(20)),
  62. }
  63. st.accounts[account1.Address.String()] = account1
  64. st.accounts[account2.Address.String()] = account2
  65. ourVm := NewVM(st, newParams(), Zero256)
  66. var gas uint64 = 1000
  67. code_parts := []string{"620f42403355",
  68. "7c0100000000000000000000000000000000000000000000000000000000",
  69. "600035046315cf268481141561004657",
  70. "6004356040526040515460605260206060f35b63693200ce81141561008757",
  71. "60043560805260243560a052335460c0523360e05260a05160c05112151561008657",
  72. "60a05160c0510360e0515560a0516080515401608051555b5b505b6000f3"}
  73. code, _ := hex.DecodeString(strings.Join(code_parts, ""))
  74. fmt.Printf("Code: %x\n", code)
  75. data, _ := hex.DecodeString("693200CE0000000000000000000000004B4363CDE27C2EB05E66357DB05BC5C88F850C1A0000000000000000000000000000000000000000000000000000000000000005")
  76. output, err := ourVm.Call(account1, account2, code, data, 0, &gas)
  77. fmt.Printf("Output: %v Error: %v\n", output, err)
  78. }
  79. /*
  80. // infinite loop
  81. code := []byte{0x5B, 0x60, 0x00, 0x56}
  82. // mstore
  83. code := []byte{0x60, 0x00, 0x60, 0x20}
  84. // mstore, mload
  85. code := []byte{0x60, 0x01, 0x60, 0x20, 0x52, 0x60, 0x20, 0x51}
  86. */