Skip to content

Commit ab9ad13

Browse files
authored
updates for dynamicOpcode (#215)
1 parent a9ab0dd commit ab9ad13

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

logic/logic.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ func ReadProgram(program []byte, args [][]byte) (ints []uint64, byteArrays [][]b
7070
for _, arg := range args {
7171
length += len(arg)
7272
}
73+
7374
if length > types.LogicSigMaxSize {
7475
err = fmt.Errorf("program too long")
7576
return
@@ -129,8 +130,9 @@ func ReadProgram(program []byte, args [][]byte) (ints []uint64, byteArrays [][]b
129130
pc = pc + size
130131
}
131132

132-
if cost > types.LogicSigMaxCost {
133-
err = fmt.Errorf("program too costly to run")
133+
// costs calculated dynamically starting in v4
134+
if version < 4 && cost > types.LogicSigMaxCost {
135+
err = fmt.Errorf("program too costly for Teal version < 4. consider using v4.")
134136
}
135137

136138
return

logic/logic_test.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,22 @@ func TestCheckProgram(t *testing.T) {
5050
err = CheckProgram(program, args)
5151
require.NoError(t, err)
5252

53-
// check 800x keccak256 fail
53+
// check 800x keccak256 fail for v3 and below
54+
versions := []byte{0x1, 0x2, 0x3}
5455
program = append(program, []byte(strings.Repeat("\x02", 800))...) // append 800x keccak256
55-
err = CheckProgram(program, args)
56-
require.EqualError(t, err, "program too costly to run")
56+
for _, v := range versions {
57+
program[0] = v
58+
err = CheckProgram(program, args)
59+
require.EqualError(t, err, "program too costly for Teal version < 4. consider using v4.")
60+
}
61+
62+
// check 800x keccak256 ok for v4 and above
63+
versions = []byte{0x4}
64+
for _, v := range versions {
65+
program[0] = v
66+
err = CheckProgram(program, args)
67+
require.NoError(t, err)
68+
}
5769
}
5870

5971
func TestCheckProgramV2(t *testing.T) {
@@ -137,4 +149,14 @@ func TestCheckProgramV4(t *testing.T) {
137149
program = []byte{0x04, 0x26, 0x03, 0x01, 0x11, 0x01, 0x10, 0x01, 0x01, 0x28, 0x29, 0xad, 0x2a, 0x12} // byte 0x11; byte 0x10; b^; byte 0x01; ==
138150
err = CheckProgram(program, args)
139151
require.NoError(t, err)
152+
153+
// callsub, retsub.
154+
program = []byte{0x04, 0x20, 0x02, 0x01, 0x02, 0x22, 0x88, 0x00, 0x03, 0x23, 0x12, 0x43, 0x49, 0x08, 0x89} // int 1; callsub double; int 2; ==; return; double: dup; +; retsub;
155+
err = CheckProgram(program, args)
156+
require.NoError(t, err)
157+
158+
// loop
159+
program = []byte{0x04, 0x20, 0x04, 0x01, 0x02, 0x0a, 0x10, 0x22, 0x23, 0x0b, 0x49, 0x24, 0x0c, 0x40, 0xff, 0xf8, 0x25, 0x12} // int 1; loop: int 2; *; dup; int 10; <; bnz loop; int 16; ==
160+
err = CheckProgram(program, args)
161+
require.NoError(t, err)
140162
}

0 commit comments

Comments
 (0)