Skip to content

Commit

Permalink
AVM: Simplify conversion and fix a spurious complaint from static ana…
Browse files Browse the repository at this point in the history
…lysis (#5421)
  • Loading branch information
jannotti authored May 26, 2023
1 parent e7e76fc commit a10efe5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 37 deletions.
27 changes: 9 additions & 18 deletions data/transactions/logic/debugger.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func makeDebugState(cx *EvalContext) *DebugState {
if err != nil {
sv = stackValue{Bytes: []byte(err.Error())}
}
globals[fs.field] = stackValueToTealValue(&sv)
globals[fs.field] = sv.toEncodedTealValue()
}
ds.Globals = globals

Expand Down Expand Up @@ -244,22 +244,13 @@ func (d *DebugState) PCToLine(pc int) int {
return len(strings.Split(d.Disassembly[:offset], "\n")) - one
}

func stackValueToTealValue(sv *stackValue) basics.TealValue {
tv := sv.toTealValue()
return basics.TealValue{
Type: tv.Type,
Bytes: base64.StdEncoding.EncodeToString([]byte(tv.Bytes)),
Uint: tv.Uint,
}
}

// valueDeltaToValueDelta converts delta's bytes to base64 in a new struct
func valueDeltaToValueDelta(vd *basics.ValueDelta) basics.ValueDelta {
return basics.ValueDelta{
Action: vd.Action,
Bytes: base64.StdEncoding.EncodeToString([]byte(vd.Bytes)),
Uint: vd.Uint,
// toEncodedTealValue converts stackValue to basics.TealValue, with the Bytes
// field b64 encoded, so it is suitable for conversion to JSON.
func (sv stackValue) toEncodedTealValue() basics.TealValue {
if sv.avmType() == avmBytes {
return basics.TealValue{Type: basics.TealBytesType, Bytes: base64.StdEncoding.EncodeToString(sv.Bytes)}
}
return basics.TealValue{Type: basics.TealUintType, Uint: sv.Uint}
}

// parseCallStack initializes an array of CallFrame objects from the raw
Expand Down Expand Up @@ -296,12 +287,12 @@ func (a *debuggerEvalTracerAdaptor) refreshDebugState(cx *EvalContext, evalError

stack := make([]basics.TealValue, len(cx.stack))
for i, sv := range cx.stack {
stack[i] = stackValueToTealValue(&sv)
stack[i] = sv.toEncodedTealValue()
}

scratch := make([]basics.TealValue, len(cx.scratch))
for i, sv := range cx.scratch {
scratch[i] = stackValueToTealValue(&sv)
scratch[i] = sv.toEncodedTealValue()
}

ds.Stack = stack
Expand Down
18 changes: 0 additions & 18 deletions data/transactions/logic/debugger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
package logic

import (
"encoding/base64"
"testing"

"github.com/algorand/go-algorand/data/basics"
"github.com/algorand/go-algorand/test/partitiontest"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -63,22 +61,6 @@ func TestLineToPC(t *testing.T) {
require.Equal(t, 0, pc)
}

func TestValueDeltaToValueDelta(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

vDelta := basics.ValueDelta{
Action: basics.SetUintAction,
Bytes: "some string",
Uint: uint64(0xffffffff),
}
ans := valueDeltaToValueDelta(&vDelta)
require.Equal(t, vDelta.Action, ans.Action)
require.NotEqual(t, vDelta.Bytes, ans.Bytes)
require.Equal(t, base64.StdEncoding.EncodeToString([]byte(vDelta.Bytes)), ans.Bytes)
require.Equal(t, vDelta.Uint, ans.Uint)
}

const testCallStackProgram string = `intcblock 1
callsub label1
intc_0
Expand Down
2 changes: 1 addition & 1 deletion data/transactions/logic/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (sv stackValue) string(limit int) (string, error) {
return string(sv.Bytes), nil
}

func (sv stackValue) toTealValue() (tv basics.TealValue) {
func (sv stackValue) toTealValue() basics.TealValue {
if sv.avmType() == avmBytes {
return basics.TealValue{Type: basics.TealBytesType, Bytes: string(sv.Bytes)}
}
Expand Down

0 comments on commit a10efe5

Please sign in to comment.