Skip to content

Commit a22ef72

Browse files
committed
Get rid of extra requests data copying when calculating requests hash
Change Requests representation to byte array of type + requests data.
1 parent 81c9dda commit a22ef72

File tree

5 files changed

+31
-22
lines changed

5 files changed

+31
-22
lines changed

test/state/requests.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ hash256 calculate_requests_hash(std::span<const Requests> block_requests_list)
2424
// if (requests.data.empty())
2525
// continue;
2626

27-
const bytes requests_bytes = static_cast<uint8_t>(requests.type) + requests.data;
2827
hash256 requests_hash;
2928
crypto::sha256(reinterpret_cast<std::byte*>(requests_hash.bytes),
30-
reinterpret_cast<const std::byte*>(requests_bytes.data()), requests_bytes.size());
29+
reinterpret_cast<const std::byte*>(requests.raw_data.data()), requests.raw_data.size());
3130
requests_hash_list += requests_hash;
3231
}
3332

@@ -78,11 +77,11 @@ Requests collect_deposit_requests(std::span<const TransactionReceipt> receipts)
7877
// Index is padded to word boundary, so takes 32 bytes.
7978
assert(log.data.size() == INDEX_OFFSET + 32);
8079

81-
requests.data.append(&log.data[PUBKEY_OFFSET], PUBKEY_SIZE);
82-
requests.data.append(&log.data[WITHDRAWAL_CREDS_OFFSET], WITHDRAWAL_CREDS_SIZE);
83-
requests.data.append(&log.data[AMOUNT_OFFSET], AMOUNT_SIZE);
84-
requests.data.append(&log.data[SIGNATURE_OFFSET], SIGNATURE_SIZE);
85-
requests.data.append(&log.data[INDEX_OFFSET], INDEX_SIZE);
80+
requests.append({&log.data[PUBKEY_OFFSET], PUBKEY_SIZE});
81+
requests.append({&log.data[WITHDRAWAL_CREDS_OFFSET], WITHDRAWAL_CREDS_SIZE});
82+
requests.append({&log.data[AMOUNT_OFFSET], AMOUNT_SIZE});
83+
requests.append({&log.data[SIGNATURE_OFFSET], SIGNATURE_SIZE});
84+
requests.append({&log.data[INDEX_OFFSET], INDEX_SIZE});
8685
}
8786
}
8887
}

test/state/requests.hpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,24 @@ struct Requests
3434
consolidation = 2,
3535
};
3636

37+
/// Raw encoded data of requests object: first byte is type, the rest is request objects.
38+
evmc::bytes raw_data;
39+
40+
explicit Requests(Type _type, evmc::bytes_view data = {})
41+
{
42+
raw_data.reserve(1 + data.size());
43+
raw_data += static_cast<uint8_t>(_type);
44+
raw_data += data;
45+
}
46+
3747
/// Requests type.
38-
Type type = Type::deposit;
48+
Type type() const noexcept { return static_cast<Type>(raw_data[0]); }
49+
3950
/// Requests data - an opaque byte array, contains zero or more encoded request objects.
40-
evmc::bytes data;
51+
evmc::bytes_view data() const noexcept { return {raw_data.data() + 1, raw_data.size() - 1}; }
4152

42-
explicit Requests(Type _type, evmc::bytes _data = {}) noexcept
43-
: type(_type), data(std::move(_data))
44-
{}
53+
/// Append data to requests object byte array.
54+
void append(bytes_view data) { raw_data.append(data); }
4555
};
4656

4757
/// Calculate commitment value of block requests list

test/state/system_contracts.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ std::pair<StateDiff, std::vector<Requests>> system_call(
9999
Host host{rev, vm, state, block, block_hashes, empty_tx};
100100
const auto res = vm.execute(host, rev, msg, code.data(), code.size());
101101
assert(res.status_code == EVMC_SUCCESS);
102-
requests.emplace_back(request_type, bytes(res.output_data, res.output_size));
102+
requests.emplace_back(request_type, bytes_view{res.output_data, res.output_size});
103103
}
104104

105105
// TODO: Should we return empty diff if no system contracts?

test/t8n/t8n.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ int main(int argc, const char* argv[])
255255
// EIP-7685: General purpose execution layer requests
256256
j_result["requests"] = json::json::array();
257257
for (size_t i = 0; i < requests.size(); ++i)
258-
j_result["requests"][i] = hex0x(requests[i].data);
258+
j_result["requests"][i] = hex0x(requests[i].data());
259259

260260
auto requests_hash = calculate_requests_hash(requests);
261261

test/unittests/state_system_call_test.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ TEST_F(state_system_call, withdrawal)
8888
EXPECT_EQ(storage.at(0x01_bytes32), 0x01_bytes32);
8989

9090
ASSERT_EQ(requests.size(), 2);
91-
EXPECT_EQ(requests[0].type, Requests::Type::withdrawal);
92-
EXPECT_EQ(requests[0].data, bytes(WITHDRAWAL_REQUEST));
93-
EXPECT_EQ(requests[1].type, Requests::Type::consolidation);
94-
EXPECT_TRUE(requests[1].data.empty());
91+
EXPECT_EQ(requests[0].type(), Requests::Type::withdrawal);
92+
EXPECT_EQ(requests[0].data(), bytes(WITHDRAWAL_REQUEST));
93+
EXPECT_EQ(requests[1].type(), Requests::Type::consolidation);
94+
EXPECT_TRUE(requests[1].data().empty());
9595
}
9696

9797
TEST_F(state_system_call, consolidation)
@@ -112,8 +112,8 @@ TEST_F(state_system_call, consolidation)
112112
EXPECT_EQ(storage.at(0x01_bytes32), 0x01_bytes32);
113113

114114
ASSERT_EQ(requests.size(), 2);
115-
EXPECT_EQ(requests[0].type, Requests::Type::withdrawal);
116-
EXPECT_TRUE(requests[0].data.empty());
117-
EXPECT_EQ(requests[1].type, Requests::Type::consolidation);
118-
EXPECT_EQ(requests[1].data, bytes(CONSOLIDATION_REQUEST));
115+
EXPECT_EQ(requests[0].type(), Requests::Type::withdrawal);
116+
EXPECT_TRUE(requests[0].data().empty());
117+
EXPECT_EQ(requests[1].type(), Requests::Type::consolidation);
118+
EXPECT_EQ(requests[1].data(), bytes(CONSOLIDATION_REQUEST));
119119
}

0 commit comments

Comments
 (0)