Skip to content

Commit

Permalink
Merge branch 'master' into yahya-revert-pr4978
Browse files Browse the repository at this point in the history
  • Loading branch information
thep2p authored Jan 19, 2024
2 parents baa1ccb + 2b49cb0 commit 1ad6824
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ func SystemContractChanges(chainID flow.ChainID) []SystemContractChange {
systemContracts.EVM,
evm.ContractCode(
systemContracts.FlowToken.Address,
true,
),
),
}
Expand Down
1 change: 1 addition & 0 deletions engine/execution/computation/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ func DefaultFVMOptions(chainID flow.ChainID, cadenceTracing bool, extensiveTraci
CapabilityControllersEnabled: chainID != flow.Mainnet,
},
)),
fvm.WithEVMEnabled(true),
}

if extensiveTracing {
Expand Down
17 changes: 15 additions & 2 deletions fvm/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,13 @@ type BootstrapParams struct {
minimumStorageReservation cadence.UFix64
storagePerFlow cadence.UFix64
restrictedAccountCreationEnabled cadence.Bool
setupEVMEnabled cadence.Bool

// `setupEVMEnabled` == true && `evmAbiOnly` == true will enable the ABI-only EVM
// `setupEVMEnabled` == true && `evmAbiOnly` == false will enable the full EVM functionality
// `setupEVMEnabled` == false will disable EVM
// This will allow to quickly disable the ABI-only EVM, in case there's a bug or something.
setupEVMEnabled cadence.Bool
evmAbiOnly cadence.Bool

// versionFreezePeriod is the number of blocks in the future where the version
// changes are frozen. The Node version beacon manages the freeze period,
Expand Down Expand Up @@ -219,6 +225,13 @@ func WithSetupEVMEnabled(enabled cadence.Bool) BootstrapProcedureOption {
}
}

func WithEVMABIOnly(evmAbiOnly cadence.Bool) BootstrapProcedureOption {
return func(bp *BootstrapProcedure) *BootstrapProcedure {
bp.evmAbiOnly = evmAbiOnly
return bp
}
}

func WithRestrictedContractDeployment(restricted *bool) BootstrapProcedureOption {
return func(bp *BootstrapProcedure) *BootstrapProcedure {
bp.restrictedContractDeployment = restricted
Expand Down Expand Up @@ -809,7 +822,7 @@ func (b *bootstrapExecutor) setupEVM(serviceAddress, fungibleTokenAddress, flowT
evmAcc := b.createAccount(nil) // account for storage
tx := blueprints.DeployContractTransaction(
serviceAddress,
stdlib.ContractCode(flowTokenAddress),
stdlib.ContractCode(flowTokenAddress, bool(b.evmAbiOnly)),
stdlib.ContractName,
)
// WithEVMEnabled should only be used after we create an account for storage
Expand Down
60 changes: 60 additions & 0 deletions fvm/evm/stdlib/abiOnlyContract.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
access(all)
contract EVM {

/// EVMAddress is an EVM-compatible address
access(all)
struct EVMAddress {

/// Bytes of the address
access(all)
let bytes: [UInt8; 20]

/// Constructs a new EVM address from the given byte representation
init(bytes: [UInt8; 20]) {
self.bytes = bytes
}

}

access(all)
fun encodeABI(_ values: [AnyStruct]): [UInt8] {
return InternalEVM.encodeABI(values)
}

access(all)
fun decodeABI(types: [Type], data: [UInt8]): [AnyStruct] {
return InternalEVM.decodeABI(types: types, data: data)
}

access(all)
fun encodeABIWithSignature(
_ signature: String,
_ values: [AnyStruct]
): [UInt8] {
let methodID = HashAlgorithm.KECCAK_256.hash(
signature.utf8
).slice(from: 0, upTo: 4)
let arguments = InternalEVM.encodeABI(values)

return methodID.concat(arguments)
}

access(all)
fun decodeABIWithSignature(
_ signature: String,
types: [Type],
data: [UInt8]
): [AnyStruct] {
let methodID = HashAlgorithm.KECCAK_256.hash(
signature.utf8
).slice(from: 0, upTo: 4)

for byte in methodID {
if byte != data.removeFirst() {
panic("signature mismatch")
}
}

return InternalEVM.decodeABI(types: types, data: data)
}
}
9 changes: 8 additions & 1 deletion fvm/evm/stdlib/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ import (
//go:embed contract.cdc
var contractCode string

//go:embed abiOnlyContract.cdc
var abiOnlyContractCode string

var flowTokenImportPattern = regexp.MustCompile(`^import "FlowToken"\n`)

func ContractCode(flowTokenAddress flow.Address) []byte {
func ContractCode(flowTokenAddress flow.Address, evmAbiOnly bool) []byte {
if evmAbiOnly {
return []byte(abiOnlyContractCode)
}

return []byte(flowTokenImportPattern.ReplaceAllString(
contractCode,
fmt.Sprintf("import FlowToken from %s", flowTokenAddress.HexWithPrefix()),
Expand Down
Loading

0 comments on commit 1ad6824

Please sign in to comment.