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

Add v0.42 parser package under old_parser #3039

Merged
merged 5 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions runtime/ast/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@
AccessContract
AccessAccount
AccessAll
AccessPubSettableLegacy // Deprecated
)

func PrimitiveAccessCount() int {
Expand Down Expand Up @@ -253,6 +254,8 @@
return "access(account)"
case AccessContract:
return "access(contract)"
case AccessPubSettableLegacy:
return "pub(set)"

Check warning on line 258 in runtime/ast/access.go

View check run for this annotation

Codecov / codecov/patch

runtime/ast/access.go#L257-L258

Added lines #L257 - L258 were not covered by tests
}

panic(errors.NewUnreachableError())
Expand All @@ -270,6 +273,8 @@
return "account"
case AccessContract:
return "contract"
case AccessPubSettableLegacy:
return "legacy public settable"

Check warning on line 277 in runtime/ast/access.go

View check run for this annotation

Codecov / codecov/patch

runtime/ast/access.go#L276-L277

Added lines #L276 - L277 were not covered by tests
}

panic(errors.NewUnreachableError())
Expand Down
5 changes: 3 additions & 2 deletions runtime/ast/primitiveaccess_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions runtime/ast/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,9 +537,10 @@ func (t *FunctionType) CheckEqual(other Type, checker TypeEqualityChecker) error

// ReferenceType
type ReferenceType struct {
Type Type `json:"ReferencedType"`
StartPos Position `json:"-"`
Authorization Authorization `json:"Authorization"`
Type Type `json:"ReferencedType"`
StartPos Position `json:"-"`
LegacyAuthorized bool
Authorization Authorization `json:"Authorization"`
}

var _ Type = &ReferenceType{}
Expand Down Expand Up @@ -634,7 +635,8 @@ func (t *ReferenceType) CheckEqual(other Type, checker TypeEqualityChecker) erro
// IntersectionType

type IntersectionType struct {
Types []*NominalType
LegacyRestrictedType Type // Deprecated
Types []*NominalType
Range
}

Expand Down
2 changes: 2 additions & 0 deletions runtime/ast/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,7 @@ func TestReferenceType_MarshalJSON(t *testing.T) {
`
{
"Type": "ReferenceType",
"LegacyAuthorized": false,
"Authorization": {
"ConjunctiveElements": [
{
Expand Down Expand Up @@ -1347,6 +1348,7 @@ func TestIntersectionType_MarshalJSON(t *testing.T) {
`
{
"Type": "IntersectionType",
"LegacyRestrictedType": null,
"Types": [
{
"Type": "NominalType",
Expand Down
5 changes: 5 additions & 0 deletions runtime/common/declarationkind.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
DeclarationKindEvent
DeclarationKindField
DeclarationKindInitializer
DeclarationKindDestructorLegacy
dsainati1 marked this conversation as resolved.
Show resolved Hide resolved
DeclarationKindStructureInterface
DeclarationKindResourceInterface
DeclarationKindContractInterface
Expand Down Expand Up @@ -116,6 +117,8 @@
return "field"
case DeclarationKindInitializer:
return "initializer"
case DeclarationKindDestructorLegacy:
return "legacy destructor"

Check warning on line 121 in runtime/common/declarationkind.go

View check run for this annotation

Codecov / codecov/patch

runtime/common/declarationkind.go#L120-L121

Added lines #L120 - L121 were not covered by tests
case DeclarationKindAttachment:
return "attachment"
case DeclarationKindStructureInterface:
Expand Down Expand Up @@ -173,6 +176,8 @@
return "event"
case DeclarationKindInitializer:
return "init"
case DeclarationKindDestructorLegacy: // Deprecated
return "destroy"

Check warning on line 180 in runtime/common/declarationkind.go

View check run for this annotation

Codecov / codecov/patch

runtime/common/declarationkind.go#L179-L180

Added lines #L179 - L180 were not covered by tests
case DeclarationKindAttachment:
return "attachment"
case DeclarationKindStructureInterface:
Expand Down
37 changes: 19 additions & 18 deletions runtime/common/declarationkind_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

230 changes: 230 additions & 0 deletions runtime/old_parser/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright Dapper Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package old_parser

import (
"fmt"
"math"
"math/rand"
"strconv"
"strings"
"testing"

"github.com/onflow/cadence/runtime/common"
)

func BenchmarkParseDeploy(b *testing.B) {

b.Run("byte array", func(b *testing.B) {

var builder strings.Builder
for i := 0; i < 15000; i++ {
if i > 0 {
builder.WriteString(", ")
}
builder.WriteString(strconv.Itoa(rand.Intn(math.MaxUint8)))
}

transaction := []byte(fmt.Sprintf(`
transaction {
execute {
AuthAccount(publicKeys: [], code: [%s])
}
}
`,
builder.String(),
))

b.ResetTimer()

for i := 0; i < b.N; i++ {
_, err := ParseProgram(nil, transaction, Config{})
if err != nil {
b.FailNow()
}
}
})

b.Run("decode hex", func(b *testing.B) {

var builder strings.Builder
for i := 0; i < 15000; i++ {
builder.WriteString(fmt.Sprintf("%02x", i))
}

transaction := []byte(fmt.Sprintf(`
transaction {
execute {
AuthAccount(publicKeys: [], code: "%s".decodeHex())
}
}
`,
builder.String(),
))

b.ResetTimer()

for i := 0; i < b.N; i++ {
_, err := ParseProgram(nil, transaction, Config{})
if err != nil {
b.FailNow()
}
}
})
}

const fungibleTokenContract = `
pub contract FungibleToken {

pub resource interface Provider {
pub fun withdraw(amount: Int): @Vault {
pre {
amount > 0:
"Withdrawal amount must be positive"
}
post {
result.balance == amount:
"Incorrect amount returned"
}
}
}

pub resource interface Receiver {
pub balance: Int

init(balance: Int) {
pre {
balance >= 0:
"Initial balance must be non-negative"
}
post {
self.balance == balance:
"Balance must be initialized to the initial balance"
}
}

pub fun deposit(from: @Receiver) {
pre {
from.balance > 0:
"Deposit balance needs to be positive!"
}
post {
self.balance == before(self.balance) + before(from.balance):
"Incorrect amount removed"
}
}
}

pub resource Vault: Provider, Receiver {

pub var balance: Int

init(balance: Int) {
self.balance = balance
}

pub fun withdraw(amount: Int): @Vault {
self.balance = self.balance - amount
return <-create Vault(balance: amount)
}

// transfer combines withdraw and deposit into one function call
pub fun transfer(to: &Receiver, amount: Int) {
pre {
amount <= self.balance:
"Insufficient funds"
}
post {
self.balance == before(self.balance) - amount:
"Incorrect amount removed"
}
to.deposit(from: <-self.withdraw(amount: amount))
}

pub fun deposit(from: @Receiver) {
self.balance = self.balance + from.balance
destroy from
}

pub fun createEmptyVault(): @Vault {
return <-create Vault(balance: 0)
}
}

pub fun createEmptyVault(): @Vault {
return <-create Vault(balance: 0)
}

pub resource VaultMinter {
pub fun mintTokens(amount: Int, recipient: &Receiver) {
recipient.deposit(from: <-create Vault(balance: amount))
}
}

init() {
let oldVault <- self.account.storage[Vault] <- create Vault(balance: 30)
destroy oldVault

let oldMinter <- self.account.storage[VaultMinter] <- create VaultMinter()
destroy oldMinter
}
}
`

type testMemoryGauge struct {
meter map[common.MemoryKind]uint64
}

func (g *testMemoryGauge) MeterMemory(usage common.MemoryUsage) error {
g.meter[usage.Kind] += usage.Amount
return nil
}

func BenchmarkParseFungibleToken(b *testing.B) {

code := []byte(fungibleTokenContract)

b.Run("Without memory metering", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
_, err := ParseProgram(nil, code, Config{})
if err != nil {
b.Fatal(err)
}
}
})

b.Run("With memory metering", func(b *testing.B) {
meter := &testMemoryGauge{
meter: make(map[common.MemoryKind]uint64),
}

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
_, err := ParseProgram(meter, code, Config{})
if err != nil {
b.Fatal(err)
}
}
})
}
Loading
Loading