|
| 1 | +package evm |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math/big" |
| 6 | + |
| 7 | + "github.com/ethereum/go-ethereum/accounts/abi" |
| 8 | + "github.com/oasisprotocol/oasis-core/go/common/cbor" |
| 9 | + |
| 10 | + "github.com/oasisprotocol/nexus/analyzer/util/addresses" |
| 11 | + "github.com/oasisprotocol/nexus/api/v1/types" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + // 0x0100000000000000000000000000000000000103 |
| 16 | + subcallPrecompile = addresses.FromBech32("oasis1qzr543mmela3xwflqtz3e0l8jzp8tupf3v59r6qn") |
| 17 | + |
| 18 | + typeUint256, _ = abi.NewType("uint256", "", nil) |
| 19 | + typeBytes, _ = abi.NewType("bytes", "", nil) |
| 20 | +) |
| 21 | + |
| 22 | +// IsSubcallPrecompile checks if the address is the subcall precompile address. |
| 23 | +func IsSubcallPrecompile(address types.Address) bool { |
| 24 | + return address == subcallPrecompile |
| 25 | +} |
| 26 | + |
| 27 | +// EVMMaybeUnmarshalPrecompileResult tries to unmarshal a precompile result. |
| 28 | +func EVMMaybeUnmarshalPrecompileResult(result []byte) (uint32, string, error) { |
| 29 | + // Try parsing the output: |
| 30 | + // https://github.com/oasisprotocol/oasis-sdk/blob/deca9369166757b3075dc4414d7450340fdaa779/runtime-sdk/modules/evm/src/precompile/subcall.rs#L97-L106 |
| 31 | + var output []byte |
| 32 | + if err := cbor.Unmarshal(result, &output); err != nil { |
| 33 | + return 0, "", fmt.Errorf("unmarshal precompile result: %w", err) |
| 34 | + } |
| 35 | + |
| 36 | + args := abi.Arguments{ |
| 37 | + {Type: typeUint256, Name: "status_code"}, |
| 38 | + {Type: typeBytes, Name: "module"}, |
| 39 | + } |
| 40 | + out, err := args.Unpack(output) |
| 41 | + if err != nil { |
| 42 | + return 0, "", fmt.Errorf("unpack precompile result: %w", err) |
| 43 | + } |
| 44 | + if len(out) != 2 { |
| 45 | + return 0, "", fmt.Errorf("expected 2 arguments, got %d", len(out)) |
| 46 | + } |
| 47 | + |
| 48 | + statusCode := out[0].(*big.Int).Uint64() |
| 49 | + if statusCode == 0 { |
| 50 | + return 0, "", nil |
| 51 | + } |
| 52 | + |
| 53 | + return uint32(statusCode), string(out[1].([]byte)), nil |
| 54 | +} |
0 commit comments