forked from ava-labs/subnet-evm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cross_chain_handler.go
81 lines (66 loc) · 2.56 KB
/
cross_chain_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// (c) 2021-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package message
import (
"context"
"encoding/json"
"github.com/MetalBlockchain/metalgo/codec"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/subnet-evm/internal/ethapi"
"github.com/MetalBlockchain/subnet-evm/rpc"
"github.com/ethereum/go-ethereum/log"
)
var _ CrossChainRequestHandler = &crossChainHandler{}
// crossChainHandler implements the CrossChainRequestHandler interface
type crossChainHandler struct {
backend ethapi.Backend
crossChainCodec codec.Manager
}
// NewCrossChainHandler creates and returns a new instance of CrossChainRequestHandler
func NewCrossChainHandler(b ethapi.Backend, codec codec.Manager) CrossChainRequestHandler {
return &crossChainHandler{
backend: b,
crossChainCodec: codec,
}
}
// HandleEthCallRequests returns an encoded EthCallResponse to the given [ethCallRequest]
// This function executes EVM Call against the state associated with [rpc.AcceptedBlockNumber] with the given
// transaction call object [ethCallRequest].
// This function does not return an error as errors are treated as FATAL to the node.
func (c *crossChainHandler) HandleEthCallRequest(ctx context.Context, requestingChainID ids.ID, requestID uint32, ethCallRequest EthCallRequest) ([]byte, error) {
lastAcceptedBlockNumber := rpc.BlockNumber(c.backend.LastAcceptedBlock().NumberU64())
lastAcceptedBlockNumberOrHash := rpc.BlockNumberOrHash{BlockNumber: &lastAcceptedBlockNumber}
transactionArgs := ethapi.TransactionArgs{}
err := json.Unmarshal(ethCallRequest.RequestArgs, &transactionArgs)
if err != nil {
log.Error("error occurred with JSON unmarshalling ethCallRequest.RequestArgs", "err", err)
return nil, nil
}
result, err := ethapi.DoCall(
ctx,
c.backend,
transactionArgs,
lastAcceptedBlockNumberOrHash,
nil,
nil,
c.backend.RPCEVMTimeout(),
c.backend.RPCGasCap())
if err != nil {
log.Error("error occurred with EthCall", "err", err, "transactionArgs", ethCallRequest.RequestArgs, "blockNumberOrHash", lastAcceptedBlockNumberOrHash)
return nil, nil
}
executionResult, err := json.Marshal(&result)
if err != nil {
log.Error("error occurred with JSON marshalling result", "err", err)
return nil, nil
}
response := EthCallResponse{
ExecutionResult: executionResult,
}
responseBytes, err := c.crossChainCodec.Marshal(Version, response)
if err != nil {
log.Error("error occurred with marshalling EthCallResponse", "err", err, "EthCallResponse", response)
return nil, nil
}
return responseBytes, nil
}