Skip to content

Commit

Permalink
Merge pull request #3039 from onflow/sainati/old-parser
Browse files Browse the repository at this point in the history
Add v0.42 parser package under old_parser
  • Loading branch information
dsainati1 authored Jan 23, 2024
2 parents 5ce1f36 + 110bd77 commit 53bc5bf
Show file tree
Hide file tree
Showing 32 changed files with 31,137 additions and 24 deletions.
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 @@ const (
AccessContract
AccessAccount
AccessAll
AccessPubSettableLegacy // Deprecated
)

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

panic(errors.NewUnreachableError())
Expand All @@ -270,6 +273,8 @@ func (a PrimitiveAccess) Description() string {
return "account"
case AccessContract:
return "contract"
case AccessPubSettableLegacy:
return "legacy public settable"
}

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 @@ const (
DeclarationKindEvent
DeclarationKindField
DeclarationKindInitializer
DeclarationKindDestructorLegacy
DeclarationKindStructureInterface
DeclarationKindResourceInterface
DeclarationKindContractInterface
Expand Down Expand Up @@ -116,6 +117,8 @@ func (k DeclarationKind) Name() string {
return "field"
case DeclarationKindInitializer:
return "initializer"
case DeclarationKindDestructorLegacy:
return "legacy destructor"
case DeclarationKindAttachment:
return "attachment"
case DeclarationKindStructureInterface:
Expand Down Expand Up @@ -173,6 +176,8 @@ func (k DeclarationKind) Keywords() string {
return "event"
case DeclarationKindInitializer:
return "init"
case DeclarationKindDestructorLegacy: // Deprecated
return "destroy"
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

0 comments on commit 53bc5bf

Please sign in to comment.