Skip to content

Commit

Permalink
avm-abi: Update functions migrated to avm-abi library (#4979)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlgoStephenAkiki authored Jan 19, 2023
1 parent 9efd9f3 commit a9fe2c7
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 377 deletions.
4 changes: 2 additions & 2 deletions cmd/catchpointdump/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ import (

"github.com/spf13/cobra"

"github.com/algorand/avm-abi/apps"
cmdutil "github.com/algorand/go-algorand/cmd/util"
"github.com/algorand/go-algorand/config"
"github.com/algorand/go-algorand/data/basics"
"github.com/algorand/go-algorand/data/bookkeeping"
"github.com/algorand/go-algorand/data/transactions/logic"
"github.com/algorand/go-algorand/ledger"
"github.com/algorand/go-algorand/ledger/ledgercore"
"github.com/algorand/go-algorand/ledger/store"
Expand Down Expand Up @@ -436,7 +436,7 @@ func printAccountsDatabase(databaseName string, stagingTables bool, fileHeader l

func printKeyValue(writer *bufio.Writer, key, value []byte) {
var pretty string
ai, rest, err := logic.SplitBoxKey(string(key))
ai, rest, err := apps.SplitBoxKey(string(key))
if err == nil {
pretty = fmt.Sprintf("box(%d, %s)", ai, base64.StdEncoding.EncodeToString([]byte(rest)))
} else {
Expand Down
23 changes: 12 additions & 11 deletions cmd/goal/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/spf13/cobra"

"github.com/algorand/avm-abi/abi"
"github.com/algorand/avm-abi/apps"
"github.com/algorand/go-algorand/crypto"
apiclient "github.com/algorand/go-algorand/daemon/algod/api/client"
"github.com/algorand/go-algorand/data/basics"
Expand Down Expand Up @@ -198,25 +199,25 @@ func panicIfErr(err error) {
}
}

func newAppCallBytes(arg string) logic.AppCallBytes {
appBytes, err := logic.NewAppCallBytes(arg)
func newAppCallBytes(arg string) apps.AppCallBytes {
appBytes, err := apps.NewAppCallBytes(arg)
if err != nil {
reportErrorf(err.Error())
}
return appBytes
}

type appCallInputs struct {
Accounts []string `codec:"accounts"`
ForeignApps []uint64 `codec:"foreignapps"`
ForeignAssets []uint64 `codec:"foreignassets"`
Boxes []boxRef `codec:"boxes"`
Args []logic.AppCallBytes `codec:"args"`
Accounts []string `codec:"accounts"`
ForeignApps []uint64 `codec:"foreignapps"`
ForeignAssets []uint64 `codec:"foreignassets"`
Boxes []boxRef `codec:"boxes"`
Args []apps.AppCallBytes `codec:"args"`
}

type boxRef struct {
appID uint64 `codec:"app"`
name logic.AppCallBytes `codec:"name"`
appID uint64 `codec:"app"`
name apps.AppCallBytes `codec:"name"`
}

// newBoxRef parses a command-line box ref, which is an optional appId, a comma,
Expand Down Expand Up @@ -335,7 +336,7 @@ func processAppInputFile() (args [][]byte, accounts []string, foreignApps []uint
return parseAppInputs(inputs)
}

func getAppInputs() (args [][]byte, accounts []string, apps []uint64, assets []uint64, boxes []transactions.BoxRef) {
func getAppInputs() (args [][]byte, accounts []string, _ []uint64, assets []uint64, boxes []transactions.BoxRef) {
if appInputFilename != "" {
if appArgs != nil || appStrAccounts != nil || foreignApps != nil || foreignAssets != nil {
reportErrorf("Cannot specify both command-line arguments/resources and JSON input filename")
Expand All @@ -348,7 +349,7 @@ func getAppInputs() (args [][]byte, accounts []string, apps []uint64, assets []u
// on it. appArgs became `StringArrayVar` in order to support abi arguments
// which contain commas.

var encodedArgs []logic.AppCallBytes
var encodedArgs []apps.AppCallBytes
for _, arg := range appArgs {
if len(arg) > 0 {
encodedArgs = append(encodedArgs, newAppCallBytes(arg))
Expand Down
3 changes: 2 additions & 1 deletion cmd/goal/interact.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

"github.com/spf13/cobra"

"github.com/algorand/avm-abi/apps"
"github.com/algorand/go-algorand/crypto"
apiclient "github.com/algorand/go-algorand/daemon/algod/api/client"
"github.com/algorand/go-algorand/data/basics"
Expand Down Expand Up @@ -513,7 +514,7 @@ var appExecuteCmd = &cobra.Command{

var inputs appCallInputs
for _, arg := range proc.Args {
var callArg logic.AppCallBytes
var callArg apps.AppCallBytes
callArg.Encoding = arg.Kind

if !procFlags.Changed(arg.Name) && arg.Default != "" {
Expand Down
7 changes: 4 additions & 3 deletions daemon/algod/api/server/v2/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

"github.com/labstack/echo/v4"

"github.com/algorand/avm-abi/apps"
"github.com/algorand/go-codec/codec"

"github.com/algorand/go-algorand/agreement"
Expand Down Expand Up @@ -1382,7 +1383,7 @@ func (v2 *Handlers) GetApplicationBoxes(ctx echo.Context, applicationID uint64,
appIdx := basics.AppIndex(applicationID)
ledger := v2.Node.LedgerForAPI()
lastRound := ledger.Latest()
keyPrefix := logic.MakeBoxKey(appIdx, "")
keyPrefix := apps.MakeBoxKey(uint64(appIdx), "")

requestedMax, algodMax := nilToZero(params.Max), v2.Node.Config().MaxAPIBoxPerApplication
max := applicationBoxesMaxKeys(requestedMax, algodMax)
Expand Down Expand Up @@ -1428,7 +1429,7 @@ func (v2 *Handlers) GetApplicationBoxByName(ctx echo.Context, applicationID uint
lastRound := ledger.Latest()

encodedBoxName := params.Name
boxNameBytes, err := logic.NewAppCallBytes(encodedBoxName)
boxNameBytes, err := apps.NewAppCallBytes(encodedBoxName)
if err != nil {
return badRequest(ctx, err, err.Error(), v2.Log)
}
Expand All @@ -1437,7 +1438,7 @@ func (v2 *Handlers) GetApplicationBoxByName(ctx echo.Context, applicationID uint
return badRequest(ctx, err, err.Error(), v2.Log)
}

value, err := ledger.LookupKv(lastRound, logic.MakeBoxKey(appIdx, string(boxName)))
value, err := ledger.LookupKv(lastRound, apps.MakeBoxKey(uint64(appIdx), string(boxName)))
if err != nil {
return internalError(ctx, err, errFailedLookingUpLedger, v2.Log)
}
Expand Down
34 changes: 0 additions & 34 deletions data/transactions/logic/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package logic

import (
"encoding/binary"
"fmt"

"github.com/algorand/go-algorand/data/basics"
Expand Down Expand Up @@ -283,36 +282,3 @@ func opBoxPut(cx *EvalContext) error {
appAddr := cx.getApplicationAddress(cx.appID)
return cx.Ledger.NewBox(cx.appID, name, value, appAddr)
}

const boxPrefix = "bx:"
const boxPrefixLength = len(boxPrefix)
const boxNameIndex = boxPrefixLength + 8 // len("bx:") + 8 (appIdx, big-endian)

// MakeBoxKey creates the key that a box named `name` under app `appIdx` should use.
func MakeBoxKey(appIdx basics.AppIndex, name string) string {
/* This format is chosen so that a simple indexing scheme on the key would
allow for quick lookups of all the boxes of a certain app, or even all
the boxes of a certain app with a certain prefix.
The "bx:" prefix is so that the kvstore might be usable for things
besides boxes.
*/
key := make([]byte, boxNameIndex+len(name))
copy(key, boxPrefix)
binary.BigEndian.PutUint64(key[boxPrefixLength:], uint64(appIdx))
copy(key[boxNameIndex:], name)
return string(key)
}

// SplitBoxKey extracts an appid and box name from a string that was created by MakeBoxKey()
func SplitBoxKey(key string) (basics.AppIndex, string, error) {
if len(key) < boxNameIndex {
return 0, "", fmt.Errorf("SplitBoxKey() cannot extract AppIndex as key (%s) too short (length=%d)", key, len(key))
}
if key[:boxPrefixLength] != boxPrefix {
return 0, "", fmt.Errorf("SplitBoxKey() illegal app box prefix in key (%s). Expected prefix '%s'", key, boxPrefix)
}
keyBytes := []byte(key)
app := basics.AppIndex(binary.BigEndian.Uint64(keyBytes[boxPrefixLength:boxNameIndex]))
return app, key[boxNameIndex:], nil
}
43 changes: 0 additions & 43 deletions data/transactions/logic/box_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/algorand/go-algorand/data/txntest"
"github.com/algorand/go-algorand/protocol"
"github.com/algorand/go-algorand/test/partitiontest"
"github.com/stretchr/testify/require"
)

func TestBoxNewDel(t *testing.T) {
Expand Down Expand Up @@ -568,45 +567,3 @@ func TestBoxTotals(t *testing.T) {
logic.TestApp(t, `int 888; app_params_get AppAddress; assert;
acct_params_get AcctTotalBoxBytes; pop; int 35; ==`, ep)
}

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

type testCase struct {
description string
name string
app basics.AppIndex
key string
err string
}

pp := func(tc testCase) string {
return fmt.Sprintf("<<<%s>>> (name, app) = (%#v, %d) --should--> key = %#v (err = [%s])", tc.description, tc.name, tc.app, tc.key, tc.err)
}

var testCases = []testCase{
// COPACETIC:
{"zero appid", "stranger", 0, "bx:\x00\x00\x00\x00\x00\x00\x00\x00stranger", ""},
{"typical", "348-8uj", 131231, "bx:\x00\x00\x00\x00\x00\x02\x00\x9f348-8uj", ""},
{"empty box name", "", 42, "bx:\x00\x00\x00\x00\x00\x00\x00*", ""},
{"random byteslice", "{\xbb\x04\a\xd1\xe2\xc6I\x81{", 13475904583033571713, "bx:\xbb\x04\a\xd1\xe2\xc6I\x81{\xbb\x04\a\xd1\xe2\xc6I\x81{", ""},

// ERRORS:
{"too short", "", 0, "stranger", "SplitBoxKey() cannot extract AppIndex as key (stranger) too short (length=8)"},
{"wrong prefix", "", 0, "strangersINTHEdark", "SplitBoxKey() illegal app box prefix in key (strangersINTHEdark). Expected prefix 'bx:'"},
}

for _, tc := range testCases {
app, name, err := logic.SplitBoxKey(tc.key)

if tc.err == "" {
key := logic.MakeBoxKey(tc.app, tc.name)
require.Equal(t, tc.app, app, pp(tc))
require.Equal(t, tc.name, name, pp(tc))
require.Equal(t, tc.key, key, pp(tc))
} else {
require.EqualError(t, err, tc.err, pp(tc))
}
}
}
105 changes: 0 additions & 105 deletions data/transactions/logic/parsing.go

This file was deleted.

Loading

0 comments on commit a9fe2c7

Please sign in to comment.