Skip to content

Commit

Permalink
Re onflow#467: Remove contract hex encoding in templates package
Browse files Browse the repository at this point in the history
  • Loading branch information
nozim committed Sep 25, 2023
1 parent 32654fd commit 7b84bf2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 20 deletions.
17 changes: 3 additions & 14 deletions templates/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package templates

import (
"encoding/hex"
"fmt"
"strings"

Expand All @@ -44,16 +43,6 @@ type Contract struct {
Source string
}

// SourceBytes returns the UTF-8 encoded source code (Source) of the contract.
func (c Contract) SourceBytes() []byte {
return []byte(c.Source)
}

// SourceHex returns the UTF-8 encoded source code (Source) of the contract as a hex string.
func (c Contract) SourceHex() string {
return hex.EncodeToString(c.SourceBytes())
}

func exportType(t sema.Type) cadence.Type {
return runtime.ExportType(t, map[sema.TypeID]cadence.Type{})
}
Expand Down Expand Up @@ -206,7 +195,7 @@ func CreateAccountAndFund(
for i, contract := range contracts {
contractKeyPairs[i] = cadence.KeyValuePair{
Key: cadence.String(contract.Name),
Value: cadence.String(contract.SourceHex()),
Value: cadence.String(contract.Source),
}
}

Expand Down Expand Up @@ -263,7 +252,7 @@ func CreateAccountAndFund(
// UpdateAccountContract generates a transaction that updates a contract deployed at an account.
func UpdateAccountContract(address flow.Address, contract Contract) *flow.Transaction {
cadenceName := cadence.String(contract.Name)
cadenceCode := cadence.String(contract.SourceHex())
cadenceCode := cadence.String(contract.Source)

return flow.NewTransaction().
SetScript([]byte(templates.UpdateContract)).
Expand All @@ -275,7 +264,7 @@ func UpdateAccountContract(address flow.Address, contract Contract) *flow.Transa
// AddAccountContract generates a transaction that deploys a contract to an account.
func AddAccountContract(address flow.Address, contract Contract) *flow.Transaction {
cadenceName := cadence.String(contract.Name)
cadenceCode := cadence.String(contract.SourceHex())
cadenceCode := cadence.String(contract.Source)

return flow.NewTransaction().
SetScript([]byte(templates.AddContract)).
Expand Down
11 changes: 5 additions & 6 deletions templates/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,22 @@ package templates_test
import (
"testing"

"github.com/stretchr/testify/require"

"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/templates"
"github.com/stretchr/testify/require"
)

func TestCreateAccount(t *testing.T) {
// Converting transaction arguments to Cadence values can increase their size.
// If this is not taken into account the transaction can quickly grow over the maximum transaction size limit.
t.Run("Transaction should not grow uncontrollably in size", func(t *testing.T) {
contractLen := 1000
contractCode := make([]byte, contractLen)

testContractBody := "test contract"
tx, err := templates.CreateAccount(
[]*flow.AccountKey{},
[]templates.Contract{{
Name: "contract",
Source: string(contractCode),
Source: testContractBody,
}},
flow.HexToAddress("01"))

Expand All @@ -49,7 +48,7 @@ func TestCreateAccount(t *testing.T) {
argumentsSize += len(argument)
}
require.Less(t, txSize, 1000, "The create account script should not grow over 1kB.")
require.Less(t, argumentsSize, contractLen*2+500,
require.Less(t, argumentsSize, len(testContractBody)*2+500,
"The create account argument size should not grow over "+
"2 times the contract code (converted to hex) + 500 bytes of extra data.")
})
Expand Down

0 comments on commit 7b84bf2

Please sign in to comment.