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

Remove contract hex encoding #480

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
8 changes: 5 additions & 3 deletions templates/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ type Contract struct {
Source string
}

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

// Deprecated
// 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())
Expand Down Expand Up @@ -207,7 +209,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 @@ -264,7 +266,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 @@ -276,7 +278,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
8 changes: 3 additions & 5 deletions templates/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@ 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 @@ -50,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.")
Copy link
Member

Choose a reason for hiding this comment

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

This message needs updating as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated :)

})
Copy link
Contributor

Choose a reason for hiding this comment

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

I think here should be fixed for non-hex case.

Copy link
Contributor Author

@nozim nozim Sep 26, 2023

Choose a reason for hiding this comment

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

it already checks utf8 contract length to be below the limit.

Expand Down