Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic teal #2126

Merged
merged 19 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions cmd/goal/clerk.go
Original file line number Diff line number Diff line change
Expand Up @@ -1080,13 +1080,10 @@ var dryrunCmd = &cobra.Command{
reportErrorf("program size too large: %d > %d", len(txn.Lsig.Logic), params.LogicSigMaxSize)
}
ep := logic.EvalParams{Txn: &txn, Proto: &params, GroupIndex: i, TxnGroup: txgroup}
cost, err := logic.Check(txn.Lsig.Logic, ep)
err := logic.Check(txn.Lsig.Logic, ep)
if err != nil {
reportErrorf("program failed Check: %s", err)
}
if uint64(cost) > params.LogicSigMaxCost {
reportErrorf("program cost too large: %d > %d", cost, params.LogicSigMaxCost)
}
sb := strings.Builder{}
ep = logic.EvalParams{
Txn: &txn,
Expand All @@ -1097,7 +1094,7 @@ var dryrunCmd = &cobra.Command{
}
pass, err := logic.Eval(txn.Lsig.Logic, ep)
// TODO: optionally include `inspect` output here?
fmt.Fprintf(os.Stdout, "tx[%d] cost=%d trace:\n%s\n", i, cost, sb.String())
fmt.Fprintf(os.Stdout, "tx[%d] trace:\n%s\n", i, sb.String())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not printing the cost after eval?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it's hidden inside the evaluation function and not exposed. If we find it important enough, I could rework the interfaces.

if pass {
fmt.Fprintf(os.Stdout, " - pass -\n")
} else {
Expand Down
3 changes: 3 additions & 0 deletions config/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,9 @@ func initConsensusProtocols() {
// Enable transaction Merkle tree.
vFuture.PaysetCommit = PaysetCommitMerkle

// Enable TEAL 4
vFuture.LogicSigVersion = 4

Consensus[protocol.ConsensusFuture] = vFuture
}

Expand Down
5 changes: 3 additions & 2 deletions daemon/algod/api/server/v2/dryrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,9 @@ func init() {

// legder requires proto string and proto params set
var proto config.ConsensusParams
proto.LogicSigVersion = 2
proto.LogicSigMaxCost = 1000
proto.LogicSigVersion = 4
proto.LogicSigMaxCost = 20000
proto.MaxAppProgramCost = 700
proto.MaxAppKeyLen = 64
proto.MaxAppBytesValueLen = 64

Expand Down
2 changes: 2 additions & 0 deletions data/transactions/logic/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
langspec.json
teal.tmLanguage.json
67 changes: 32 additions & 35 deletions data/transactions/logic/assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1033,11 +1033,22 @@ func (ops *OpStream) assemble(fin io.Reader) error {
ops.trace("%d: no fields\n", ops.sourceLine)
continue
}
// we're going to process opcodes, so fix the Version
// we're about to begin processing opcodes, so fix the Version
if ops.Version == assemblerNoVersion {
ops.Version = AssemblerDefaultVersion
}
opstring := fields[0]

if opstring[len(opstring)-1] == ':' {
ops.createLabel(opstring[:len(opstring)-1])
fields = fields[1:]
if len(fields) == 0 {
// There was a label, not need to ops.trace this
continue
}
opstring = fields[0]
}

spec, ok := OpsByName[ops.Version][opstring]
if !ok {
spec, ok = keywords[opstring]
Expand All @@ -1053,10 +1064,6 @@ func (ops *OpStream) assemble(fin io.Reader) error {
ops.trace("\n")
continue
}
if opstring[len(opstring)-1] == ':' {
ops.createLabel(opstring[:len(opstring)-1])
continue
}
// unknown opcode, let's report a good error if version problem
spec, ok = OpsByName[AssemblerMaxVersion][opstring]
if ok {
Expand Down Expand Up @@ -1148,8 +1155,8 @@ func (ops *OpStream) resolveLabels() {
}
// all branch instructions (currently) are opcode byte and 2 offset bytes, and the destination is relative to the next pc as if the branch was a no-op
naturalPc := lr.position + 3
if dest < naturalPc {
ops.errorf("label %v is before reference but only forward jumps are allowed", lr.label)
if ops.Version < backBranchEnabledVersion && dest < naturalPc {
ops.errorf("label %v is a back reference, back jump support was introduced in TEAL v4", lr.label)
tsachiherman marked this conversation as resolved.
Show resolved Hide resolved
continue
}
jump := dest - naturalPc
Expand Down Expand Up @@ -1376,33 +1383,29 @@ func parseIntcblock(program []byte, pc int) (intc []uint64, nextpc int, err erro
return
}

func checkIntConstBlock(cx *evalContext) int {
func checkIntConstBlock(cx *evalContext) error {
pos := cx.pc + 1
numInts, bytesUsed := binary.Uvarint(cx.program[pos:])
if bytesUsed <= 0 {
cx.err = fmt.Errorf("could not decode int const block size at pc=%d", pos)
return 1
return fmt.Errorf("could not decode int const block size at pc=%d", pos)
}
pos += bytesUsed
if numInts > uint64(len(cx.program)) {
cx.err = errTooManyIntc
return 0
return errTooManyIntc
}
//intc = make([]uint64, numInts)
for i := uint64(0); i < numInts; i++ {
if pos >= len(cx.program) {
cx.err = errShortIntcblock
return 0
return errShortIntcblock
}
_, bytesUsed = binary.Uvarint(cx.program[pos:])
if bytesUsed <= 0 {
cx.err = fmt.Errorf("could not decode int const[%d] at pc=%d", i, pos)
return 1
return fmt.Errorf("could not decode int const[%d] at pc=%d", i, pos)
}
pos += bytesUsed
}
cx.nextpc = pos
return 1
return nil
}

var errShortBytecblock = errors.New("bytecblock ran past end of program")
Expand Down Expand Up @@ -1448,44 +1451,38 @@ func parseBytecBlock(program []byte, pc int) (bytec [][]byte, nextpc int, err er
return
}

func checkByteConstBlock(cx *evalContext) int {
func checkByteConstBlock(cx *evalContext) error {
pos := cx.pc + 1
numItems, bytesUsed := binary.Uvarint(cx.program[pos:])
if bytesUsed <= 0 {
cx.err = fmt.Errorf("could not decode []byte const block size at pc=%d", pos)
return 1
return fmt.Errorf("could not decode []byte const block size at pc=%d", pos)
}
pos += bytesUsed
if numItems > uint64(len(cx.program)) {
cx.err = errTooManyItems
return 0
return errTooManyItems
}
//bytec = make([][]byte, numItems)
for i := uint64(0); i < numItems; i++ {
if pos >= len(cx.program) {
cx.err = errShortBytecblock
return 0
return errShortBytecblock
}
itemLen, bytesUsed := binary.Uvarint(cx.program[pos:])
if bytesUsed <= 0 {
cx.err = fmt.Errorf("could not decode []byte const[%d] at pc=%d", i, pos)
return 1
return fmt.Errorf("could not decode []byte const[%d] at pc=%d", i, pos)
}
pos += bytesUsed
if pos >= len(cx.program) {
cx.err = errShortBytecblock
return 0
return errShortBytecblock
}
end := uint64(pos) + itemLen
if end > uint64(len(cx.program)) || end < uint64(pos) {
cx.err = errShortBytecblock
return 0
return errShortBytecblock
}
//bytec[i] = program[pos : pos+int(itemLen)]
pos += int(itemLen)
}
cx.nextpc = pos
return 1
return nil
}

func disIntcblock(dis *disassembleState, spec *OpSpec) (string, error) {
Expand Down Expand Up @@ -1612,9 +1609,9 @@ func disPushInt(dis *disassembleState, spec *OpSpec) (string, error) {
dis.nextpc = pos + bytesUsed
return fmt.Sprintf("%s %d", spec.Name, val), nil
}
func checkPushInt(cx *evalContext) int {
func checkPushInt(cx *evalContext) error {
opPushInt(cx)
return 1
return cx.err
}

func disPushBytes(dis *disassembleState, spec *OpSpec) (string, error) {
Expand All @@ -1632,9 +1629,9 @@ func disPushBytes(dis *disassembleState, spec *OpSpec) (string, error) {
dis.nextpc = int(end)
return fmt.Sprintf("%s 0x%s", spec.Name, hex.EncodeToString(bytes)), nil
}
func checkPushBytes(cx *evalContext) int {
func checkPushBytes(cx *evalContext) error {
opPushBytes(cx)
return 1
return cx.err
Comment on lines -1635 to +1634
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like check* functions needs to be moved to check.go from the assembler

}

// This is also used to disassemble gtxns
Expand Down
106 changes: 65 additions & 41 deletions data/transactions/logic/assembler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,36 @@ pushint 1000
pushbytes "john"
`

// Keep in mind, only use existing int and byte constants, or else use
// push* instead. The idea is to not cause the *cblocks to change.
const v4Nonsense = `
int 1
pushint 2000
int 0
int 2
divw
callsub stuff
b next
stuff:
retsub
next:
int 1
`

var nonsense = map[uint64]string{
1: v1Nonsense,
2: v1Nonsense + v2Nonsense,
3: v1Nonsense + v2Nonsense + v3Nonsense,
4: v1Nonsense + v2Nonsense + v3Nonsense + v4Nonsense,
}

var compiled = map[uint64]string{
1: "012008b7a60cf8acd19181cf959a12f8acd19181cf951af8acd19181cf15f8acd191810f01020026050212340c68656c6c6f20776f726c6421208dae2087fbba51304eb02b91f656948397a7946390e8cb70fc9ea4d95f92251d024242047465737400320032013202320328292929292a0431003101310231043105310731083109310a310b310c310d310e310f3111311231133114311533000033000133000233000433000533000733000833000933000a33000b33000c33000d33000e33000f3300113300123300133300143300152d2e0102222324252104082209240a220b230c240d250e230f23102311231223132314181b1c2b1716154000032903494",
2: "022008b7a60cf8acd19181cf959a12f8acd19181cf951af8acd19181cf15f8acd191810f01020026050212340c68656c6c6f20776f726c6421208dae2087fbba51304eb02b91f656948397a7946390e8cb70fc9ea4d95f92251d024242047465737400320032013202320328292929292a0431003101310231043105310731083109310a310b310c310d310e310f3111311231133114311533000033000133000233000433000533000733000833000933000a33000b33000c33000d33000e33000f3300113300123300133300143300152d2e0102222324252104082209240a220b230c240d250e230f23102311231223132314181b1c2b171615400003290349483403350222231d4a484848482a50512a63222352410003420000432105602105612105270463484821052b62482b642b65484821052b2106662b21056721072b682b692107210570004848210771004848361c0037001a0031183119311b311d311e311f3120210721051e312131223123312431253126312731283129312a312b312c312d312e312f",
3: "032008b7a60cf8acd19181cf959a12f8acd19181cf951af8acd19181cf15f8acd191810f01020026050212340c68656c6c6f20776f726c6421208dae2087fbba51304eb02b91f656948397a7946390e8cb70fc9ea4d95f92251d024242047465737400320032013202320328292929292a0431003101310231043105310731083109310a310b310c310d310e310f3111311231133114311533000033000133000233000433000533000733000833000933000a33000b33000c33000d33000e33000f3300113300123300133300143300152d2e0102222324252104082209240a220b230c240d250e230f23102311231223132314181b1c2b171615400003290349483403350222231d4a484848482a50512a63222352410003420000432105602105612105270463484821052b62482b642b65484821052b2106662b21056721072b682b692107210570004848210771004848361c0037001a0031183119311b311d311e311f3120210721051e312131223123312431253126312731283129312a312b312c312d312e312f4478222105531421055427042106552105082106564c4d4b02210538212106391c0081e80780046a6f686e",
4: "042008b7a60cf8acd19181cf959a12f8acd19181cf951af8acd19181cf15f8acd191810f01020026050212340c68656c6c6f20776f726c6421208dae2087fbba51304eb02b91f656948397a7946390e8cb70fc9ea4d95f92251d024242047465737400320032013202320328292929292a0431003101310231043105310731083109310a310b310c310d310e310f3111311231133114311533000033000133000233000433000533000733000833000933000a33000b33000c33000d33000e33000f3300113300123300133300143300152d2e0102222324252104082209240a220b230c240d250e230f23102311231223132314181b1c2b171615400003290349483403350222231d4a484848482a50512a63222352410003420000432105602105612105270463484821052b62482b642b65484821052b2106662b21056721072b682b692107210570004848210771004848361c0037001a0031183119311b311d311e311f3120210721051e312131223123312431253126312731283129312a312b312c312d312e312f4478222105531421055427042106552105082106564c4d4b02210538212106391c0081e80780046a6f686e210581d00f210721061f880003420001892105",
}

func pseudoOp(opcode string) bool {
// We don't test every combination of
// intcblock,bytecblock,intc*,bytec*,arg* here. Not all of
Expand All @@ -263,44 +293,33 @@ func TestAssemble(t *testing.T) {
// Run test. It should pass.
//
// This doesn't have to be a sensible program to run, it just has to compile.
for _, spec := range OpSpecs {
// Ensure that we have some basic check of all the ops, except
if !strings.Contains(v1Nonsense+v2Nonsense, spec.Name) &&
!pseudoOp(spec.Name) && spec.Version <= 2 {
t.Errorf("v2 nonsense test should contain op %v", spec.Name)
}
}
// First, we test v2, not AssemblerMaxVersion. A higher version is
// allowed to differ (and must, in the first byte).
ops := testProg(t, v1Nonsense+v2Nonsense, 2)
// check that compilation is stable over time and we assemble to the same bytes this month that we did last month.
expectedBytes, _ := hex.DecodeString("022008b7a60cf8acd19181cf959a12f8acd19181cf951af8acd19181cf15f8acd191810f01020026050212340c68656c6c6f20776f726c6421208dae2087fbba51304eb02b91f656948397a7946390e8cb70fc9ea4d95f92251d024242047465737400320032013202320328292929292a0431003101310231043105310731083109310a310b310c310d310e310f3111311231133114311533000033000133000233000433000533000733000833000933000a33000b33000c33000d33000e33000f3300113300123300133300143300152d2e0102222324252104082209240a220b230c240d250e230f23102311231223132314181b1c2b171615400003290349483403350222231d4a484848482a50512a63222352410003420000432105602105612105270463484821052b62482b642b65484821052b2106662b21056721072b682b692107210570004848210771004848361c0037001a0031183119311b311d311e311f3120210721051e312131223123312431253126312731283129312a312b312c312d312e312f")
if bytes.Compare(expectedBytes, ops.Program) != 0 {
// this print is for convenience if the program has been changed. the hex string can be copy pasted back in as a new expected result.
t.Log(hex.EncodeToString(ops.Program))
}
require.Equal(t, expectedBytes, ops.Program)

// We test v3 here, and compare to AssemblerMaxVersion, with
// the intention that the test breaks the next time
// AssemblerMaxVersion is increased. At that point, we would
// add a new test for v4, and leave behind this test for v3.

for _, spec := range OpSpecs {
// Ensure that we have some basic check of all the ops, except
if !strings.Contains(v1Nonsense+v2Nonsense+v3Nonsense, spec.Name) &&
!pseudoOp(spec.Name) && spec.Version <= 3 {
t.Errorf("v3 nonsense test should contain op %v", spec.Name)
}
}
ops = testProg(t, v1Nonsense+v2Nonsense+v3Nonsense, AssemblerMaxVersion)
// check that compilation is stable over time and we assemble to the same bytes this month that we did last month.
expectedBytes, _ = hex.DecodeString("032008b7a60cf8acd19181cf959a12f8acd19181cf951af8acd19181cf15f8acd191810f01020026050212340c68656c6c6f20776f726c6421208dae2087fbba51304eb02b91f656948397a7946390e8cb70fc9ea4d95f92251d024242047465737400320032013202320328292929292a0431003101310231043105310731083109310a310b310c310d310e310f3111311231133114311533000033000133000233000433000533000733000833000933000a33000b33000c33000d33000e33000f3300113300123300133300143300152d2e0102222324252104082209240a220b230c240d250e230f23102311231223132314181b1c2b171615400003290349483403350222231d4a484848482a50512a63222352410003420000432105602105612105270463484821052b62482b642b65484821052b2106662b21056721072b682b692107210570004848210771004848361c0037001a0031183119311b311d311e311f3120210721051e312131223123312431253126312731283129312a312b312c312d312e312f4478222105531421055427042106552105082106564c4d4b02210538212106391c0081e80780046a6f686e")
if bytes.Compare(expectedBytes, ops.Program) != 0 {
// this print is for convenience if the program has been changed. the hex string can be copy pasted back in as a new expected result.
t.Log(hex.EncodeToString(ops.Program))

t.Parallel()
tsachiherman marked this conversation as resolved.
Show resolved Hide resolved
for v := uint64(2); v <= AssemblerMaxVersion; v++ {
t.Run(fmt.Sprintf("v=%d", v), func(t *testing.T) {
for _, spec := range OpSpecs {
// Make sure our nonsense covers the ops
if !strings.Contains(nonsense[v], spec.Name) &&
!pseudoOp(spec.Name) && spec.Version <= v {
t.Errorf("v%d nonsense test should contain op %v", v, spec.Name)
}
}

ops := testProg(t, nonsense[v], v)
// check that compilation is stable over
// time. we must assemble to the same bytes
// this month that we did last month.
expectedBytes, _ := hex.DecodeString(compiled[v])
if bytes.Compare(expectedBytes, ops.Program) != 0 {
// this print is for convenience if
// the program has been changed. the
// hex string can be copy pasted back
// in as a new expected result.
t.Log(hex.EncodeToString(ops.Program))
}
require.Equal(t, expectedBytes, ops.Program)
})
}
require.Equal(t, expectedBytes, ops.Program)
}

func TestAssembleAlias(t *testing.T) {
Expand Down Expand Up @@ -758,9 +777,14 @@ func TestAssembleRejectNegJump(t *testing.T) {
int 1
bnz wat
int 2`
for v := uint64(1); v <= AssemblerMaxVersion; v++ {
for v := uint64(1); v < backBranchEnabledVersion; v++ {
t.Run(fmt.Sprintf("v=%d", v), func(t *testing.T) {
testProg(t, source, v, expect{3, "label wat is before reference but only forward jumps are allowed"})
testProg(t, source, v, expect{3, "label wat is a back reference..."})
})
}
for v := uint64(backBranchEnabledVersion); v <= AssemblerMaxVersion; v++ {
t.Run(fmt.Sprintf("v=%d", v), func(t *testing.T) {
testProg(t, source, v)
})
}
}
Expand Down Expand Up @@ -1628,8 +1652,8 @@ func TestErrShortBytecblock(t *testing.T) {

var cx evalContext
cx.program = ops.Program
checkIntConstBlock(&cx)
require.Equal(t, cx.err, errShortIntcblock)
err = checkIntConstBlock(&cx)
require.Equal(t, err, errShortIntcblock)
}

func TestBranchAssemblyTypeCheck(t *testing.T) {
Expand Down
Loading