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

Update to Cadence v1.0.0-M9 #5498

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
321 changes: 177 additions & 144 deletions fvm/evm/stdlib/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,73 +112,85 @@ func (e abiDecodingError) Error() string {

func reportABIEncodingComputation(
inter *interpreter.Interpreter,
locationRange interpreter.LocationRange,
values *interpreter.ArrayValue,
evmAddressTypeID common.TypeID,
reportComputation func(intensity uint),
) {
values.Iterate(inter, func(element interpreter.Value) (resume bool) {
switch value := element.(type) {
case *interpreter.StringValue:
// Dynamic variables, such as strings, are encoded
// in 2+ chunks of 32 bytes. The first chunk contains
// the index where information for the string begin,
// the second chunk contains the number of bytes the
// string occupies, and the third chunk contains the
// value of the string itself.
computation := uint(2 * abiEncodingByteSize)
stringLength := len(value.Str)
chunks := math.Ceil(float64(stringLength) / float64(abiEncodingByteSize))
computation += uint(chunks * abiEncodingByteSize)
reportComputation(computation)

case interpreter.BoolValue,
interpreter.UInt8Value,
interpreter.UInt16Value,
interpreter.UInt32Value,
interpreter.UInt64Value,
interpreter.UInt128Value,
interpreter.UInt256Value,
interpreter.Int8Value,
interpreter.Int16Value,
interpreter.Int32Value,
interpreter.Int64Value,
interpreter.Int128Value,
interpreter.Int256Value:

// Numeric and bool variables are also static variables
// with a fixed size of 32 bytes.
reportComputation(abiEncodingByteSize)

case *interpreter.CompositeValue:
if value.TypeID() == evmAddressTypeID {
// EVM addresses are static variables with a fixed
// size of 32 bytes.
values.Iterate(
inter,
func(element interpreter.Value) (resume bool) {
switch value := element.(type) {
case *interpreter.StringValue:
// Dynamic variables, such as strings, are encoded
// in 2+ chunks of 32 bytes. The first chunk contains
// the index where information for the string begin,
// the second chunk contains the number of bytes the
// string occupies, and the third chunk contains the
// value of the string itself.
computation := uint(2 * abiEncodingByteSize)
stringLength := len(value.Str)
chunks := math.Ceil(float64(stringLength) / float64(abiEncodingByteSize))
computation += uint(chunks * abiEncodingByteSize)
reportComputation(computation)

case interpreter.BoolValue,
interpreter.UInt8Value,
interpreter.UInt16Value,
interpreter.UInt32Value,
interpreter.UInt64Value,
interpreter.UInt128Value,
interpreter.UInt256Value,
interpreter.Int8Value,
interpreter.Int16Value,
interpreter.Int32Value,
interpreter.Int64Value,
interpreter.Int128Value,
interpreter.Int256Value:

// Numeric and bool variables are also static variables
// with a fixed size of 32 bytes.
reportComputation(abiEncodingByteSize)
} else {

case *interpreter.CompositeValue:
if value.TypeID() == evmAddressTypeID {
// EVM addresses are static variables with a fixed
// size of 32 bytes.
reportComputation(abiEncodingByteSize)
} else {
panic(abiEncodingError{
Type: value.StaticType(inter),
})
}
case *interpreter.ArrayValue:
// Dynamic variables, such as arrays & slices, are encoded
// in 2+ chunks of 32 bytes. The first chunk contains
// the index where information for the array begin,
// the second chunk contains the number of bytes the
// array occupies, and the third chunk contains the
// values of the array itself.
computation := uint(2 * abiEncodingByteSize)
reportComputation(computation)
reportABIEncodingComputation(
inter,
locationRange,
value,
evmAddressTypeID,
reportComputation,
)

default:
panic(abiEncodingError{
Type: value.StaticType(inter),
Type: element.StaticType(inter),
})
}
case *interpreter.ArrayValue:
// Dynamic variables, such as arrays & slices, are encoded
// in 2+ chunks of 32 bytes. The first chunk contains
// the index where information for the array begin,
// the second chunk contains the number of bytes the
// array occupies, and the third chunk contains the
// values of the array itself.
computation := uint(2 * abiEncodingByteSize)
reportComputation(computation)
reportABIEncodingComputation(inter, value, evmAddressTypeID, reportComputation)

default:
panic(abiEncodingError{
Type: element.StaticType(inter),
})
}

// continue iteration
return true
})
// continue iteration
return true
},
false,
locationRange,
)
}

// EVM.encodeABI
Expand Down Expand Up @@ -221,6 +233,7 @@ func newInternalEVMTypeEncodeABIFunction(

reportABIEncodingComputation(
inter,
locationRange,
valuesArray,
evmAddressTypeID,
func(intensity uint) {
Expand All @@ -233,24 +246,29 @@ func newInternalEVMTypeEncodeABIFunction(
values := make([]any, 0, size)
arguments := make(gethABI.Arguments, 0, size)

valuesArray.Iterate(inter, func(element interpreter.Value) (resume bool) {
value, ty, err := encodeABI(
inter,
locationRange,
element,
element.StaticType(inter),
evmAddressTypeID,
)
if err != nil {
panic(err)
}

values = append(values, value)
arguments = append(arguments, gethABI.Argument{Type: ty})

// continue iteration
return true
})
valuesArray.Iterate(
inter,
func(element interpreter.Value) (resume bool) {
value, ty, err := encodeABI(
inter,
locationRange,
element,
element.StaticType(inter),
evmAddressTypeID,
)
if err != nil {
panic(err)
}

values = append(values, value)
arguments = append(arguments, gethABI.Argument{Type: ty})

// continue iteration
return true
},
false,
locationRange,
)

encodedValues, err := arguments.Pack(values...)
if err != nil {
Expand Down Expand Up @@ -542,26 +560,31 @@ func encodeABI(
}

var index int
value.Iterate(inter, func(element interpreter.Value) (resume bool) {
value.Iterate(
inter,
func(element interpreter.Value) (resume bool) {

arrayElement, _, err := encodeABI(
inter,
locationRange,
element,
element.StaticType(inter),
evmAddressTypeID,
)
if err != nil {
panic(err)
}
arrayElement, _, err := encodeABI(
inter,
locationRange,
element,
element.StaticType(inter),
evmAddressTypeID,
)
if err != nil {
panic(err)
}

result.Index(index).Set(reflect.ValueOf(arrayElement))
result.Index(index).Set(reflect.ValueOf(arrayElement))

index++
index++

// continue iteration
return true
})
// continue iteration
return true
},
false,
locationRange,
)

return result.Interface(), arrayGethABIType, nil
}
Expand Down Expand Up @@ -631,31 +654,36 @@ func newInternalEVMTypeDecodeABIFunction(
}

var arguments gethABI.Arguments
typesArray.Iterate(inter, func(element interpreter.Value) (resume bool) {
typeValue, ok := element.(interpreter.TypeValue)
if !ok {
panic(errors.NewUnreachableError())
}

staticType := typeValue.Type

gethABITy, ok := gethABIType(staticType, evmAddressTypeID)
if !ok {
panic(abiDecodingError{
Type: staticType,
})
}

arguments = append(
arguments,
gethABI.Argument{
Type: gethABITy,
},
)

// continue iteration
return true
})
typesArray.Iterate(
inter,
func(element interpreter.Value) (resume bool) {
typeValue, ok := element.(interpreter.TypeValue)
if !ok {
panic(errors.NewUnreachableError())
}

staticType := typeValue.Type

gethABITy, ok := gethABIType(staticType, evmAddressTypeID)
if !ok {
panic(abiDecodingError{
Type: staticType,
})
}

arguments = append(
arguments,
gethABI.Argument{
Type: gethABITy,
},
)

// continue iteration
return true
},
false,
locationRange,
)

decodedValues, err := arguments.Unpack(data)
if err != nil {
Expand All @@ -665,33 +693,38 @@ func newInternalEVMTypeDecodeABIFunction(
var index int
values := make([]interpreter.Value, 0, len(decodedValues))

typesArray.Iterate(inter, func(element interpreter.Value) (resume bool) {
typeValue, ok := element.(interpreter.TypeValue)
if !ok {
panic(errors.NewUnreachableError())
}

staticType := typeValue.Type

value, err := decodeABI(
inter,
locationRange,
decodedValues[index],
staticType,
location,
evmAddressTypeID,
)
if err != nil {
panic(err)
}

index++

values = append(values, value)

// continue iteration
return true
})
typesArray.Iterate(
inter,
func(element interpreter.Value) (resume bool) {
typeValue, ok := element.(interpreter.TypeValue)
if !ok {
panic(errors.NewUnreachableError())
}

staticType := typeValue.Type

value, err := decodeABI(
inter,
locationRange,
decodedValues[index],
staticType,
location,
evmAddressTypeID,
)
if err != nil {
panic(err)
}

index++

values = append(values, value)

// continue iteration
return true
},
false,
locationRange,
)

arrayType := interpreter.NewVariableSizedStaticType(
inter,
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ require (
github.com/multiformats/go-multiaddr-dns v0.3.1
github.com/multiformats/go-multihash v0.2.3
github.com/onflow/atree v0.6.1-0.20230711151834-86040b30171f
github.com/onflow/cadence v1.0.0-M8
github.com/onflow/cadence v1.0.0-M9
github.com/onflow/crypto v0.25.0
github.com/onflow/flow v0.3.4
github.com/onflow/flow-core-contracts/lib/go/contracts v0.15.2-0.20240227190927-0e6ce7e3222b
github.com/onflow/flow-core-contracts/lib/go/templates v0.15.2-0.20240227190927-0e6ce7e3222b
github.com/onflow/flow-go-sdk v1.0.0-M7
github.com/onflow/flow-go-sdk v1.0.0-M8
github.com/onflow/flow/protobuf/go/flow v0.3.7
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58
github.com/pierrec/lz4 v2.6.1+incompatible
Expand Down
Loading
Loading