-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Implement RemoteCadenceArch type for fetching precompiled calls …
…in contract calls
- Loading branch information
Showing
4 changed files
with
203 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package requester | ||
|
||
import ( | ||
"context" | ||
"encoding/hex" | ||
"fmt" | ||
"math/big" | ||
"strings" | ||
|
||
"github.com/onflow/cadence" | ||
errs "github.com/onflow/flow-evm-gateway/models/errors" | ||
evmImpl "github.com/onflow/flow-go/fvm/evm/impl" | ||
evmTypes "github.com/onflow/flow-go/fvm/evm/types" | ||
"github.com/onflow/flow-go/fvm/systemcontracts" | ||
flowGo "github.com/onflow/flow-go/model/flow" | ||
gethCommon "github.com/onflow/go-ethereum/common" | ||
"github.com/onflow/go-ethereum/core/types" | ||
) | ||
|
||
type RemoteCadenceArch struct { | ||
blockHeight uint64 | ||
client *CrossSporkClient | ||
chainID flowGo.ChainID | ||
} | ||
|
||
var _ evmTypes.PrecompiledContract = (*RemoteCadenceArch)(nil) | ||
|
||
func NewRemoteCadenceArch( | ||
blockHeight uint64, | ||
client *CrossSporkClient, | ||
chainID flowGo.ChainID, | ||
) *RemoteCadenceArch { | ||
return &RemoteCadenceArch{ | ||
blockHeight: blockHeight, | ||
client: client, | ||
chainID: chainID, | ||
} | ||
} | ||
|
||
func (rca *RemoteCadenceArch) Address() evmTypes.Address { | ||
return evmTypes.NewAddress( | ||
gethCommon.HexToAddress("0x0000000000000000000000010000000000000001"), | ||
) | ||
} | ||
|
||
func (rca *RemoteCadenceArch) RequiredGas(input []byte) uint64 { | ||
to := gethCommon.HexToAddress("0x0000000000000000000000010000000000000001") | ||
tx := types.NewTx( | ||
&types.LegacyTx{ | ||
Nonce: 0, | ||
To: &to, | ||
Value: big.NewInt(0), | ||
Gas: 55_000, | ||
GasPrice: big.NewInt(0), | ||
Data: input, | ||
}, | ||
) | ||
encodedTx, err := tx.MarshalBinary() | ||
if err != nil { | ||
return 0 | ||
} | ||
hexEncodedTx, err := cadence.NewString(hex.EncodeToString(encodedTx)) | ||
if err != nil { | ||
return 0 | ||
} | ||
|
||
hexEncodedAddress, err := addressToCadenceString(evmTypes.CoinbaseAddress.ToCommon()) | ||
if err != nil { | ||
return 0 | ||
} | ||
|
||
scriptResult, err := rca.client.ExecuteScriptAtBlockHeight( | ||
context.Background(), | ||
rca.blockHeight, | ||
rca.replaceAddresses(dryRunScript), | ||
[]cadence.Value{hexEncodedTx, hexEncodedAddress}, | ||
) | ||
if err != nil { | ||
return 0 | ||
} | ||
|
||
evmResult, err := parseResult(scriptResult) | ||
if err != nil { | ||
return 0 | ||
} | ||
|
||
return evmResult.GasConsumed | ||
} | ||
|
||
func (rca *RemoteCadenceArch) Run(input []byte) ([]byte, error) { | ||
to := gethCommon.HexToAddress("0x0000000000000000000000010000000000000001") | ||
tx := types.NewTx( | ||
&types.LegacyTx{ | ||
Nonce: 0, | ||
To: &to, | ||
Value: big.NewInt(0), | ||
Gas: 55_000, | ||
GasPrice: big.NewInt(0), | ||
Data: input, | ||
}, | ||
) | ||
encodedTx, err := tx.MarshalBinary() | ||
if err != nil { | ||
return nil, err | ||
} | ||
hexEncodedTx, err := cadence.NewString(hex.EncodeToString(encodedTx)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
hexEncodedAddress, err := addressToCadenceString(evmTypes.CoinbaseAddress.ToCommon()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
scriptResult, err := rca.client.ExecuteScriptAtBlockHeight( | ||
context.Background(), | ||
rca.blockHeight, | ||
rca.replaceAddresses(dryRunScript), | ||
[]cadence.Value{hexEncodedTx, hexEncodedAddress}, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
evmResult, err := parseResult(scriptResult) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return evmResult.ReturnedData, nil | ||
} | ||
|
||
func (rca *RemoteCadenceArch) replaceAddresses(script []byte) []byte { | ||
// make the list of all contracts we should replace address for | ||
sc := systemcontracts.SystemContractsForChain(rca.chainID) | ||
contracts := []systemcontracts.SystemContract{sc.EVMContract, sc.FungibleToken, sc.FlowToken} | ||
|
||
s := string(script) | ||
// iterate over all the import name and address pairs and replace them in script | ||
for _, contract := range contracts { | ||
s = strings.ReplaceAll(s, | ||
fmt.Sprintf("import %s", contract.Name), | ||
fmt.Sprintf("import %s from %s", contract.Name, contract.Address.HexWithPrefix()), | ||
) | ||
} | ||
|
||
return []byte(s) | ||
} | ||
|
||
func addressToCadenceString(address gethCommon.Address) (cadence.String, error) { | ||
return cadence.NewString( | ||
strings.TrimPrefix(address.Hex(), "0x"), | ||
) | ||
} | ||
|
||
// parseResult | ||
func parseResult(res cadence.Value) (*evmTypes.ResultSummary, error) { | ||
result, err := evmImpl.ResultSummaryFromEVMResultValue(res) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to decode EVM result of type: %s, with: %w", res.Type().ID(), err) | ||
} | ||
|
||
if result.ErrorCode != 0 { | ||
if result.ErrorCode == evmTypes.ExecutionErrCodeExecutionReverted { | ||
return nil, errs.NewRevertError(result.ReturnedData) | ||
} | ||
return nil, errs.NewFailedTransactionError(result.ErrorMessage) | ||
} | ||
|
||
return result, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters