-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevm_request_processor.cpp
More file actions
78 lines (63 loc) · 2.41 KB
/
evm_request_processor.cpp
File metadata and controls
78 lines (63 loc) · 2.41 KB
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
//
// Created by sky on 2023/5/31.
//
#include "evm_request_processor.h"
#include "state/rlp.hpp"
#include <set>
#include <iostream>
using std::cout;
using std::endl;
using namespace evmone;
const std::set<std::string> uris_supported = {URI_CONTRACT_CREATE, URI_CONTRACT_CALL};
std::variant<json, std::error_code> evm_request_processor::do_request(const std::string &uri, const char *req) {
cout << "uri:[" << uri << "], request_json body:[" << req << "]" << endl;
if(!uris_supported.contains(uri)) {
return make_error_code(UNSUPPORTED_URI);
}
nlohmann::json request_json;
nlohmann::json ret;
try {
request_json = nlohmann::json::parse(req);
} catch (json::parse_error& e) {
cout << "parse request_json body failed, err:" << e.what() << endl;
return make_error_code(INVALID_JSON);
}
// TODO check request_json
evmone::state::Transaction tx {
.kind = DEFAULT_TX_KIND,
.gas_limit = DEFAULT_TX_GAS_LIMIT,
.max_gas_price = DEFAULT_MAX_GAS_PRICE,
.max_priority_gas_price = DEFAULT_MAX_PRIORITY_GAS_PRICE,
.value = request_json["value"],
.chain_id = DEFAULT_CHAIN_ID,
};
auto from_addr = operator""_address(request_json["from"].get<std::string>().c_str());
auto from_acc = executor.create_account(from_addr);
tx.sender = from_addr;
tx.nonce = from_acc.nonce;
if(uri == URI_CONTRACT_CREATE) {
tx.data = evmc::from_hex(request_json["code"].get<std::string_view>()).value();
} else if(uri == URI_CONTRACT_CALL) {
tx.to = operator""_address(request_json["to"].get<std::string>().c_str());
tx.data = evmc::from_hex(request_json["input"].get<std::string_view>()).value();
executor.execute_tx(tx);
}
auto tx_hash = evmc::hex(keccak256(rlp::encode(tx)));
auto tx_ret = executor.execute_tx(tx);
if (holds_alternative<tx_result>(tx_ret)) {
auto result = std::get<tx_result>(tx_ret);
ret["status"] = "success";
ret["tx_hash"] = tx_hash;
ret["evm_status"] = result.receipt.status;
ret["gas_used"] = result.receipt.gas_used;
if(!tx.to) {
ret["contract_address"] = evmc::hex(result.addr_created);
}
} else {
auto err = std::get<std::error_code>(tx_ret);
ret["status"] = "fail";
ret["error"] = err.message();
}
cout << "tx executed:" << ret.dump() << endl;
return ret;
}