-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplanner_test.go
117 lines (96 loc) · 2.7 KB
/
planner_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package weirollgo
import (
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/umbracle/ethgo"
"github.com/umbracle/ethgo/abi"
"github.com/umbracle/ethgo/testutil"
)
type artifact struct {
Name string `json:"contractName"`
ABI *abi.ABI `json:"abi"`
Bytecode []byte
BytecodeRaw string `json:"bytecode"`
}
func readArtifact(path string) (a *artifact) {
fullPath := filepath.Join("artifacts", path+".sol/"+filepath.Base(path)+".json")
data, err := os.ReadFile(fullPath)
if err != nil {
panic(fmt.Sprintf("BUG: failed to read file %s: %v", fullPath, err))
}
if err := json.Unmarshal(data, &a); err != nil {
panic(fmt.Sprintf("BUG: failed to decode artifact %s: %v", fullPath, err))
}
bytecode, err := hex.DecodeString(a.BytecodeRaw[2:])
if err != nil {
panic(fmt.Sprintf("BUG: failed to decode bytecode %s: %v", fullPath, err))
}
a.Bytecode = bytecode
return
}
type receipt struct {
t *testing.T
raw *ethgo.Receipt
events *Contract
}
func (r *receipt) Expect(log string, val interface{}) {
res, err := abi.ParseLog(r.events.abi.Events[log].Inputs, r.raw.Logs[0])
assert.NoError(r.t, err)
assert.Equal(r.t, res["message"], val)
}
func TestServer(t *testing.T) {
server := testutil.NewTestServer(t, nil)
defer server.Close()
contracts := map[string]*Contract{}
for _, name := range []string{
"test/TestableVM",
"test/Sender",
"Libraries/Events",
"Libraries/Math",
} {
ar := readArtifact(name)
receipt, err := server.SendTxn(ðgo.Transaction{
Input: ar.Bytecode,
})
assert.NoError(t, err)
contracts[ar.Name] = NewContract(receipt.ContractAddress, ar.ABI)
}
math := contracts["Math"]
events := contracts["Events"]
sender := contracts["Sender"]
submit := func(planner *Planner) *receipt {
plan, err := planner.Plan()
assert.NoError(t, err)
vm := contracts["TestableVM"]
input, err := vm.abi.GetMethod("execute").Encode([]interface{}{plan.Commands, plan.State})
assert.NoError(t, err)
raw, err := server.SendTxn(ðgo.Transaction{
To: &vm.addr,
Input: input,
})
assert.NoError(t, err)
return &receipt{raw: raw, t: t, events: contracts["Events"]}
}
t.Run("", func(t *testing.T) {
p := NewPlanner()
ret1 := p.Add(math.Call("add", 1, 2))
ret2 := p.Add(math.Call("add", 3, 4))
ret3 := p.Add(math.Call("add", ret1, ret2))
p.Add(events.Call("logUint", ret3))
receipt := submit(p)
receipt.Expect("LogUint", big.NewInt(10))
})
t.Run("", func(t *testing.T) {
p := NewPlanner()
ret1 := p.Add(sender.Call("sender"))
p.Add(events.Call("logAddress", ret1))
receipt := submit(p)
receipt.Expect("LogAddress", server.Account(0))
})
}