Skip to content
Draft
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
11 changes: 11 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"time"

"github.com/onflow/cadence"
"github.com/onflow/cadence/activations"
"github.com/onflow/cadence/ast"
"github.com/onflow/cadence/common"
Expand Down Expand Up @@ -435,3 +436,13 @@ func (h *StandardLibraryHandler) IsContractBeingAdded(common.AddressLocation) bo
// NO-OP
return false
}

func (h *StandardLibraryHandler) ExportValue(
_ interpreter.Value,
_ interpreter.ValueExportContext,
) (
cadence.Value,
error,
) {
return nil, goerrors.New("exporting values is not available in this environment")
}
2 changes: 2 additions & 0 deletions common/computationkind.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,6 @@ const (
// RLP
ComputationKindSTDLIBRLPDecodeString
ComputationKindSTDLIBRLPDecodeList
// CCF
ComputationKindSTDLIBCCFEncode
)
7 changes: 4 additions & 3 deletions common/computationkind_string.go

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

2 changes: 1 addition & 1 deletion encoding/ccf/decode_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (d *Decoder) decodeSimpleTypeID() (cadence.Type, error) {
return nil, err
}

ty := typeBySimpleTypeID(SimpleType(simpleTypeID))
ty := TypeBySimpleTypeID(SimpleType(simpleTypeID))
if ty == nil {
return nil, fmt.Errorf("unsupported encoded simple type ID %d", simpleTypeID)
}
Expand Down
2 changes: 1 addition & 1 deletion encoding/ccf/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -1429,7 +1429,7 @@ func (e *Encoder) encodeTypeValue(typ cadence.Type, visited ccfTypeIDByCadenceTy
visited[cadenceTypeID] = ccfTypeID(len(visited))
}

simpleTypeID, ok := simpleTypeIDByType(typ)
simpleTypeID, ok := SimpleTypeIDByType(typ)
if ok {
return e.encodeSimpleTypeValue(simpleTypeID)
}
Expand Down
2 changes: 1 addition & 1 deletion encoding/ccf/encode_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type encodeTypeFn func(typ cadence.Type, tids ccfTypeIDByCadenceType) error
// All exported Cadence types need to be supported by this function,
// including abstract and interface types.
func (e *Encoder) encodeInlineType(typ cadence.Type, tids ccfTypeIDByCadenceType) error {
simpleTypeID, ok := simpleTypeIDByType(typ)
simpleTypeID, ok := SimpleTypeIDByType(typ)
if ok {
return e.encodeSimpleType(simpleTypeID)
}
Expand Down
8 changes: 4 additions & 4 deletions encoding/ccf/simpletype.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
// When new simple cadence.Type is added,
// - ADD new ID to the end of existing IDs,
// - ADD new simple cadence.Type and its ID
// to simpleTypeIDByType *and* typeBySimpleTypeID
// to SimpleTypeIDByType *and* TypeBySimpleTypeID

type SimpleType uint64

Expand Down Expand Up @@ -156,7 +156,7 @@ const ( // Cadence simple type IDs
SimpleType_Count
)

// NOTE: cadence.FunctionType isn't included in simpleTypeIDByType
// NOTE: cadence.FunctionType isn't included in SimpleTypeIDByType
// because this function is used by both inline-type and type-value.
// cadence.FunctionType needs to be handled differently when this
// function is used by inline-type and type-value.
Expand Down Expand Up @@ -278,7 +278,7 @@ func initSimpleTypeIDBiMap() (m *bimap.BiMap[cadence.PrimitiveType, SimpleType])

var simpleTypeIDBiMap *bimap.BiMap[cadence.PrimitiveType, SimpleType] = initSimpleTypeIDBiMap()

func simpleTypeIDByType(typ cadence.Type) (SimpleType, bool) {
func SimpleTypeIDByType(typ cadence.Type) (SimpleType, bool) {
switch typ := typ.(type) {
case cadence.BytesType:
return SimpleTypeBytes, true
Expand All @@ -289,7 +289,7 @@ func simpleTypeIDByType(typ cadence.Type) (SimpleType, bool) {
return 0, false
}

func typeBySimpleTypeID(simpleTypeID SimpleType) cadence.Type {
func TypeBySimpleTypeID(simpleTypeID SimpleType) cadence.Type {
if simpleTypeID == SimpleTypeBytes {
return cadence.TheBytesType
}
Expand Down
7 changes: 4 additions & 3 deletions encoding/ccf/simpletype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
* limitations under the License.
*/

package ccf
package ccf_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/onflow/cadence"
"github.com/onflow/cadence/encoding/ccf"
"github.com/onflow/cadence/interpreter"
"github.com/onflow/cadence/runtime"
"github.com/onflow/cadence/sema"
Expand All @@ -43,10 +44,10 @@ func TestTypeConversion(t *testing.T) {

cadenceType := runtime.ExportType(semaType, map[sema.TypeID]cadence.Type{})

simpleTypeID, ok := simpleTypeIDByType(cadenceType)
simpleTypeID, ok := ccf.SimpleTypeIDByType(cadenceType)
require.True(t, ok)

ty2 := typeBySimpleTypeID(simpleTypeID)
ty2 := ccf.TypeBySimpleTypeID(simpleTypeID)
require.Equal(t, cadence.PrimitiveType(ty), ty2)
})
}
Expand Down
107 changes: 107 additions & 0 deletions runtime/ccf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright Flow Foundation
*
* 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 runtime_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

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

func TestRuntimeCCFEncodeStruct(t *testing.T) {

t.Parallel()

type testCase struct {
name string
value string
output []byte
}

tests := []testCase{
{
name: "String",
value: `"test"`,
output: []byte{0xd8, 0x82, 0x82, 0xd8, 0x89, 0x1, 0x64, 0x74, 0x65, 0x73, 0x74},
},
{
name: "Bool",
value: `true`,
output: []byte{0xd8, 0x82, 0x82, 0xd8, 0x89, 0x0, 0xf5},
},
{
name: "function",
value: `fun (): Int { return 1 }`,
output: []byte{0xd8, 0x82, 0x82, 0xd8, 0x89, 0x18, 0x33, 0x84, 0x80, 0x80, 0xd8, 0xb9, 0x4, 0x0},
},
}

test := func(test testCase) {
t.Run(test.name, func(t *testing.T) {

t.Parallel()

runtime := NewTestRuntime()

runtimeInterface := &TestRuntimeInterface{
Storage: NewTestLedger(nil, nil),
}

script := []byte(fmt.Sprintf(
`
access(all) fun main(): [UInt8]? {
let value = %s
return CCF.encode(&value as &AnyStruct)
}
`,
test.value,
))

result, err := runtime.ExecuteScript(
Script{
Source: script,
},
Context{
Interface: runtimeInterface,
Location: common.ScriptLocation{},
UseVM: *compile,
},
)
require.NoError(t, err)

assert.Equal(t,
cadence.NewOptional(
cadence.ByteSliceToByteArray(test.output),
),
result,
)
})
}

for _, testCase := range tests {
test(testCase)
}
}
12 changes: 12 additions & 0 deletions runtime/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package runtime
import (
"time"

"github.com/onflow/cadence"
"github.com/onflow/cadence/activations"

"github.com/onflow/cadence/common"
Expand Down Expand Up @@ -109,6 +110,7 @@ var _ stdlib.BLSPoPVerifier = &InterpreterEnvironment{}
var _ stdlib.BLSPublicKeyAggregator = &InterpreterEnvironment{}
var _ stdlib.BLSSignatureAggregator = &InterpreterEnvironment{}
var _ stdlib.Hasher = &InterpreterEnvironment{}
var _ stdlib.Exporter = &InterpreterEnvironment{}
var _ ArgumentDecoder = &InterpreterEnvironment{}

func NewInterpreterEnvironment(config Config) *InterpreterEnvironment {
Expand Down Expand Up @@ -575,3 +577,13 @@ func (e *InterpreterEnvironment) getBaseActivation(
func (e *InterpreterEnvironment) ProgramLog(message string) error {
return e.Interface.ProgramLog(message)
}

func (*InterpreterEnvironment) ExportValue(
value interpreter.Value,
context interpreter.ValueExportContext,
) (
cadence.Value,
error,
) {
return ExportValue(value, context)
}
9 changes: 9 additions & 0 deletions runtime/vm_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package runtime
import (
"fmt"

"github.com/onflow/cadence"
"github.com/onflow/cadence/activations"
"github.com/onflow/cadence/bbq"
"github.com/onflow/cadence/bbq/commons"
Expand Down Expand Up @@ -81,6 +82,7 @@ var _ stdlib.BLSPoPVerifier = &vmEnvironment{}
var _ stdlib.BLSPublicKeyAggregator = &vmEnvironment{}
var _ stdlib.BLSSignatureAggregator = &vmEnvironment{}
var _ stdlib.Hasher = &vmEnvironment{}
var _ stdlib.Exporter = &vmEnvironment{}
var _ ArgumentDecoder = &vmEnvironment{}

func newVMEnvironment(config Config) *vmEnvironment {
Expand Down Expand Up @@ -639,3 +641,10 @@ func (e *vmEnvironment) vmBuiltinGlobals(
}
return
}

func (*vmEnvironment) ExportValue(
value interpreter.Value,
context interpreter.ValueExportContext,
) (cadence.Value, error) {
return ExportValue(value, context)
}
10 changes: 10 additions & 0 deletions stdlib/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type StandardLibraryHandler interface {
BLSPublicKeyAggregator
BLSSignatureAggregator
Hasher
Exporter
}

var DefaultStandardLibraryTypes = []StandardLibraryType{
Expand All @@ -50,6 +51,11 @@ var DefaultStandardLibraryTypes = []StandardLibraryType{
Name: RLPTypeName,
Kind: common.DeclarationKindContract,
},
{
Type: CCFType,
Name: CCFTypeName,
Kind: common.DeclarationKindContract,
},
}

func InterpreterDefaultStandardLibraryValues(handler StandardLibraryHandler) []StandardLibraryValue {
Expand All @@ -68,6 +74,7 @@ func InterpreterDefaultStandardLibraryValues(handler StandardLibraryHandler) []S
NewInterpreterHashAlgorithmConstructor(handler),
RLPContract,
NewBLSContract(nil, handler),
NewCCFContract(nil, handler),
}
}

Expand All @@ -87,6 +94,7 @@ func VMDefaultStandardLibraryValues(handler StandardLibraryHandler) []StandardLi
NewVMHashAlgorithmConstructor(handler),
RLPContract,
NewBLSContract(nil, handler),
NewCCFContract(nil, handler),
}
}

Expand Down Expand Up @@ -138,6 +146,8 @@ func VMFunctions(handler StandardLibraryHandler) []VMFunction {
NewVMBLSAggregatePublicKeysFunction(handler),
NewVMBLSAggregateSignaturesFunction(handler),

NewVMCCFEncodeFunction(handler),

NewVMHashAlgorithmHashFunction(handler),
NewVMHashAlgorithmHashWithTagFunction(handler),

Expand Down
7 changes: 7 additions & 0 deletions stdlib/ccf.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
access(all)
contract CCF {
/// Encodes an encodable value to CCF.
/// Returns nil if the value cannot be encoded.
access(all)
view fun encode(_ input: &Any): [UInt8]?
}
Loading
Loading