Skip to content

Commit a3e1bb5

Browse files
committed
chain, graph, runtime: Don't clone API version needlessly
1 parent e83ef25 commit a3e1bb5

File tree

10 files changed

+35
-28
lines changed

10 files changed

+35
-28
lines changed

chain/ethereum/src/runtime/runtime_adapter.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use graph::blockchain::ChainIdentifier;
1111
use graph::components::subgraph::HostMetrics;
1212
use graph::data::store::ethereum::call;
1313
use graph::data::store::scalar::BigInt;
14-
use graph::data::subgraph::API_VERSION_0_0_9;
14+
use graph::data::subgraph::{API_VERSION_0_0_4, API_VERSION_0_0_9};
1515
use graph::data_source;
1616
use graph::data_source::common::{ContractCall, MappingABI};
1717
use graph::prelude::web3::types::H160;
@@ -26,7 +26,6 @@ use graph::{
2626
EthereumCallCache,
2727
},
2828
runtime::{asc_get, asc_new, AscPtr, HostExportError},
29-
semver::Version,
3029
slog::Logger,
3130
};
3231
use graph_runtime_wasm::asc_abi::class::{AscBigInt, AscEnumArray, AscWrapped, EthereumValueKind};
@@ -185,7 +184,7 @@ fn ethereum_call(
185184
// For apiVersion >= 0.0.4 the call passed from the mapping includes the
186185
// function signature; subgraphs using an apiVersion < 0.0.4 don't pass
187186
// the signature along with the call.
188-
let call: UnresolvedContractCall = if ctx.heap.api_version() >= Version::new(0, 0, 4) {
187+
let call: UnresolvedContractCall = if ctx.heap.api_version() >= &API_VERSION_0_0_4 {
189188
asc_get::<_, AscUnresolvedContractCall_0_0_4, _>(ctx.heap, wasm_ptr.into(), &ctx.gas, 0)?
190189
} else {
191190
asc_get::<_, AscUnresolvedContractCall, _>(ctx.heap, wasm_ptr.into(), &ctx.gas, 0)?
@@ -215,7 +214,7 @@ fn eth_get_balance(
215214
ctx.gas
216215
.consume_host_fn_with_metrics(ETH_GET_BALANCE, "eth_get_balance")?;
217216

218-
if ctx.heap.api_version() < API_VERSION_0_0_9 {
217+
if ctx.heap.api_version() < &API_VERSION_0_0_9 {
219218
return Err(HostExportError::Deterministic(anyhow!(
220219
"ethereum.getBalance call is not supported before API version 0.0.9"
221220
)));
@@ -249,7 +248,7 @@ fn eth_has_code(
249248
ctx.gas
250249
.consume_host_fn_with_metrics(ETH_HAS_CODE, "eth_has_code")?;
251250

252-
if ctx.heap.api_version() < API_VERSION_0_0_9 {
251+
if ctx.heap.api_version() < &API_VERSION_0_0_9 {
253252
return Err(HostExportError::Deterministic(anyhow!(
254253
"ethereum.hasCode call is not supported before API version 0.0.9"
255254
)));

chain/ethereum/src/trigger.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl ToAscPtr for MappingTrigger {
151151
log.as_ref(),
152152
&params,
153153
);
154-
if api_version >= API_VERSION_0_0_7 {
154+
if api_version >= &API_VERSION_0_0_7 {
155155
asc_new::<
156156
AscEthereumEvent_0_0_7<
157157
AscEthereumTransaction_0_0_6,
@@ -161,14 +161,14 @@ impl ToAscPtr for MappingTrigger {
161161
_,
162162
>(heap, &(ethereum_event_data, receipt.as_deref()), gas)?
163163
.erase()
164-
} else if api_version >= API_VERSION_0_0_6 {
164+
} else if api_version >= &API_VERSION_0_0_6 {
165165
asc_new::<
166166
AscEthereumEvent<AscEthereumTransaction_0_0_6, AscEthereumBlock_0_0_6>,
167167
_,
168168
_,
169169
>(heap, &ethereum_event_data, gas)?
170170
.erase()
171-
} else if api_version >= API_VERSION_0_0_2 {
171+
} else if api_version >= &API_VERSION_0_0_2 {
172172
asc_new::<
173173
AscEthereumEvent<AscEthereumTransaction_0_0_2, AscEthereumBlock>,
174174
_,
@@ -192,14 +192,14 @@ impl ToAscPtr for MappingTrigger {
192192
outputs,
193193
} => {
194194
let call = EthereumCallData::new(&block, &transaction, &call, &inputs, &outputs);
195-
if heap.api_version() >= Version::new(0, 0, 6) {
195+
if heap.api_version() >= &Version::new(0, 0, 6) {
196196
asc_new::<
197197
AscEthereumCall_0_0_3<AscEthereumTransaction_0_0_6, AscEthereumBlock_0_0_6>,
198198
_,
199199
_,
200200
>(heap, &call, gas)?
201201
.erase()
202-
} else if heap.api_version() >= Version::new(0, 0, 3) {
202+
} else if heap.api_version() >= &Version::new(0, 0, 3) {
203203
asc_new::<
204204
AscEthereumCall_0_0_3<AscEthereumTransaction_0_0_2, AscEthereumBlock>,
205205
_,
@@ -212,7 +212,7 @@ impl ToAscPtr for MappingTrigger {
212212
}
213213
MappingTrigger::Block { block } => {
214214
let block = EthereumBlockData::from(block.as_ref());
215-
if heap.api_version() >= Version::new(0, 0, 6) {
215+
if heap.api_version() >= &Version::new(0, 0, 6) {
216216
asc_new::<AscEthereumBlock_0_0_6, _, _>(heap, &block, gas)?.erase()
217217
} else {
218218
asc_new::<AscEthereumBlock, _, _>(heap, &block, gas)?.erase()

chain/near/src/trigger.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,8 @@ mod tests {
497497
Ok(init_slice(src, buffer))
498498
}
499499

500-
fn api_version(&self) -> graph::semver::Version {
501-
self.api_version.clone()
500+
fn api_version(&self) -> &graph::semver::Version {
501+
&self.api_version
502502
}
503503

504504
fn asc_type_id(

graph/src/data/subgraph/api_version.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ use thiserror::Error;
55

66
pub const API_VERSION_0_0_2: Version = Version::new(0, 0, 2);
77

8+
/// Changed calling convention for `ethereum.call`
9+
pub const API_VERSION_0_0_4: Version = Version::new(0, 0, 4);
10+
811
/// This version adds a new subgraph validation step that rejects manifests whose mappings have
912
/// different API versions if at least one of them is equal to or higher than `0.0.5`.
1013
pub const API_VERSION_0_0_5: Version = Version::new(0, 0, 5);

graph/src/runtime/asc_heap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub trait AscHeap {
2828

2929
fn read_u32(&self, offset: u32, gas: &GasCounter) -> Result<u32, DeterministicHostError>;
3030

31-
fn api_version(&self) -> Version;
31+
fn api_version(&self) -> &Version;
3232

3333
fn asc_type_id(&mut self, type_id_index: IndexForAscTypeId) -> Result<u32, HostExportError>;
3434
}

graph/src/runtime/asc_ptr.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::data::subgraph::API_VERSION_0_0_4;
2+
13
use super::gas::GasCounter;
24
use super::{padding_to_16, DeterministicHostError, HostExportError};
35

@@ -61,7 +63,7 @@ impl<C: AscType> AscPtr<C> {
6163
let len = match heap.api_version() {
6264
// TODO: The version check here conflicts with the comment on C::asc_size,
6365
// which states "Only used for version <= 0.0.3."
64-
version if version <= Version::new(0, 0, 4) => C::asc_size(self, heap, gas),
66+
version if version <= &API_VERSION_0_0_4 => C::asc_size(self, heap, gas),
6567
_ => self.read_len(heap, gas),
6668
}?;
6769

@@ -91,7 +93,7 @@ impl<C: AscType> AscPtr<C> {
9193
C: AscIndexId,
9294
{
9395
match heap.api_version() {
94-
version if version <= Version::new(0, 0, 4) => {
96+
version if version <= &API_VERSION_0_0_4 => {
9597
let heap_ptr = heap.raw_new(&asc_obj.to_asc_bytes()?, gas)?;
9698
Ok(AscPtr::new(heap_ptr))
9799
}

runtime/wasm/src/asc_abi/class.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use ethabi;
22

33
use graph::{
4-
data::store::{self, scalar::Timestamp},
4+
data::{
5+
store::{self, scalar::Timestamp},
6+
subgraph::API_VERSION_0_0_4,
7+
},
58
runtime::{
69
gas::GasCounter, AscHeap, AscIndexId, AscType, AscValue, HostExportError,
710
IndexForAscTypeId, ToAscObj,
@@ -27,10 +30,10 @@ pub enum ArrayBuffer {
2730
impl ArrayBuffer {
2831
pub(crate) fn new<T: AscType>(
2932
values: &[T],
30-
api_version: Version,
33+
api_version: &Version,
3134
) -> Result<Self, DeterministicHostError> {
3235
match api_version {
33-
version if version <= Version::new(0, 0, 4) => {
36+
version if version <= &API_VERSION_0_0_4 => {
3437
Ok(Self::ApiVersion0_0_4(v0_0_4::ArrayBuffer::new(values)?))
3538
}
3639
_ => Ok(Self::ApiVersion0_0_5(v0_0_5::ArrayBuffer::new(values)?)),
@@ -95,7 +98,7 @@ impl<T: AscValue> TypedArray<T> {
9598
gas: &GasCounter,
9699
) -> Result<Self, HostExportError> {
97100
match heap.api_version() {
98-
version if version <= Version::new(0, 0, 4) => Ok(Self::ApiVersion0_0_4(
101+
version if version <= &API_VERSION_0_0_4 => Ok(Self::ApiVersion0_0_4(
99102
v0_0_4::TypedArray::new(content, heap, gas)?,
100103
)),
101104
_ => Ok(Self::ApiVersion0_0_5(v0_0_5::TypedArray::new(
@@ -201,9 +204,9 @@ pub enum AscString {
201204
}
202205

203206
impl AscString {
204-
pub fn new(content: &[u16], api_version: Version) -> Result<Self, DeterministicHostError> {
207+
pub fn new(content: &[u16], api_version: &Version) -> Result<Self, DeterministicHostError> {
205208
match api_version {
206-
version if version <= Version::new(0, 0, 4) => {
209+
version if version <= &API_VERSION_0_0_4 => {
207210
Ok(Self::ApiVersion0_0_4(v0_0_4::AscString::new(content)?))
208211
}
209212
_ => Ok(Self::ApiVersion0_0_5(v0_0_5::AscString::new(content)?)),
@@ -275,7 +278,7 @@ impl<T: AscValue> Array<T> {
275278
gas: &GasCounter,
276279
) -> Result<Self, HostExportError> {
277280
match heap.api_version() {
278-
version if version <= Version::new(0, 0, 4) => Ok(Self::ApiVersion0_0_4(
281+
version if version <= &API_VERSION_0_0_4 => Ok(Self::ApiVersion0_0_4(
279282
v0_0_4::Array::new(content, heap, gas)?,
280283
)),
281284
_ => Ok(Self::ApiVersion0_0_5(v0_0_5::Array::new(

runtime/wasm/src/asc_abi/v0_0_4.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl ArrayBuffer {
5454
&self,
5555
byte_offset: u32,
5656
length: u32,
57-
api_version: Version,
57+
api_version: &Version,
5858
) -> Result<Vec<T>, DeterministicHostError> {
5959
let length = length as usize;
6060
let byte_offset = byte_offset as usize;

runtime/wasm/src/asc_abi/v0_0_5.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ impl ArrayBuffer {
5252
&self,
5353
byte_offset: u32,
5454
length: u32,
55-
api_version: Version,
55+
api_version: &Version,
5656
) -> Result<Vec<T>, DeterministicHostError> {
5757
let length = length as usize;
5858
let byte_offset = byte_offset as usize;
5959

6060
self.content[byte_offset..]
6161
.chunks(size_of::<T>())
6262
.take(length)
63-
.map(|asc_obj| T::from_asc_bytes(asc_obj, &api_version))
63+
.map(|asc_obj| T::from_asc_bytes(asc_obj, api_version))
6464
.collect()
6565
}
6666
}

runtime/wasm/src/module/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ impl AscHeap for WasmInstanceContext<'_> {
344344
Ok(init_slice(src, buffer))
345345
}
346346

347-
fn api_version(&self) -> Version {
348-
self.asc_heap().api_version.clone()
347+
fn api_version(&self) -> &Version {
348+
&self.asc_heap().api_version
349349
}
350350

351351
fn asc_type_id(&mut self, type_id_index: IndexForAscTypeId) -> Result<u32, HostExportError> {

0 commit comments

Comments
 (0)