Skip to content

Commit 99fd9b8

Browse files
Move to the use of DataBlobHash. (#4367)
## Motivation The `BaseRuntime` and `ContractRuntime` are a little bit disorganised with their input and output: * `read_data_blob` / `assert_data_blob_exists` are taking a `CryptoHash` as input. * `create_data_blob` is returning a `BlobId`. ## Proposal The natural type is the `DataBlobHash` that is the natural type for this. Previous to this PR, it is in the `linera-sdk`. We are forced to move it to `linera-base`. ## Test Plan The CI. ## Release Plan - Nothing to do / These changes follow the usual release cycle. ## Links None.
1 parent cfd1ea9 commit 99fd9b8

File tree

24 files changed

+208
-133
lines changed

24 files changed

+208
-133
lines changed

examples/non-fungible/src/contract.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use std::collections::BTreeSet;
99

1010
use fungible::Account;
1111
use linera_sdk::{
12-
linera_base_types::{AccountOwner, WithContractAbi},
12+
linera_base_types::{AccountOwner, DataBlobHash, WithContractAbi},
1313
views::{RootView, View},
14-
Contract, ContractRuntime, DataBlobHash,
14+
Contract, ContractRuntime,
1515
};
1616
use non_fungible::{Message, Nft, NonFungibleTokenAbi, Operation, TokenId};
1717

examples/non-fungible/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ use async_graphql::{InputObject, Request, Response, SimpleObject};
99
use fungible::Account;
1010
use linera_sdk::{
1111
graphql::GraphQLMutationRoot,
12-
linera_base_types::{AccountOwner, ApplicationId, ChainId, ContractAbi, ServiceAbi},
13-
DataBlobHash, ToBcsBytes,
12+
linera_base_types::{
13+
AccountOwner, ApplicationId, ChainId, ContractAbi, DataBlobHash, ServiceAbi,
14+
},
15+
ToBcsBytes,
1416
};
1517
use serde::{Deserialize, Serialize};
1618

examples/non-fungible/src/service.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use async_graphql::{EmptySubscription, Object, Request, Response, Schema};
1414
use base64::engine::{general_purpose::STANDARD_NO_PAD, Engine as _};
1515
use fungible::Account;
1616
use linera_sdk::{
17-
linera_base_types::{AccountOwner, WithServiceAbi},
17+
linera_base_types::{AccountOwner, DataBlobHash, WithServiceAbi},
1818
views::View,
19-
DataBlobHash, Service, ServiceRuntime,
19+
Service, ServiceRuntime,
2020
};
2121
use non_fungible::{NftOutput, Operation, TokenId};
2222

linera-base/src/identifiers.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,18 @@ impl<'a> Deserialize<'a> for BlobId {
308308
}
309309
}
310310

311+
/// Hash of a data blob.
312+
#[derive(
313+
Eq, Hash, PartialEq, Debug, Serialize, Deserialize, Clone, Copy, WitType, WitLoad, WitStore,
314+
)]
315+
pub struct DataBlobHash(pub CryptoHash);
316+
317+
impl From<DataBlobHash> for BlobId {
318+
fn from(hash: DataBlobHash) -> BlobId {
319+
BlobId::new(hash.0, BlobType::Data)
320+
}
321+
}
322+
311323
/// A unique identifier for a user application from a blob.
312324
#[derive(Debug, WitLoad, WitStore, WitType)]
313325
#[cfg_attr(with_testing, derive(Default, test_strategy::Arbitrary))]
@@ -1079,6 +1091,7 @@ impl From<ChainDescription> for ChainId {
10791091
}
10801092

10811093
bcs_scalar!(ApplicationId, "A unique identifier for a user application");
1094+
doc_scalar!(DataBlobHash, "Hash of a Data Blob");
10821095
doc_scalar!(
10831096
GenericApplicationId,
10841097
"A unique identifier for a user application or for the system application"

linera-execution/solidity/Linera.sol

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,8 @@ library Linera {
383383
function read_data_blob(bytes32 hash) internal returns (bytes memory) {
384384
address precompile = address(0x0b);
385385
LineraTypes.CryptoHash memory hash2 = LineraTypes.CryptoHash(hash);
386-
LineraTypes.BaseRuntimePrecompile memory base = LineraTypes.BaseRuntimePrecompile_case_read_data_blob(hash2);
386+
LineraTypes.DataBlobHash memory hash3 = LineraTypes.DataBlobHash(hash2);
387+
LineraTypes.BaseRuntimePrecompile memory base = LineraTypes.BaseRuntimePrecompile_case_read_data_blob(hash3);
387388
LineraTypes.RuntimePrecompile memory input1 = LineraTypes.RuntimePrecompile_case_base(base);
388389
bytes memory input2 = LineraTypes.bcs_serialize_RuntimePrecompile(input1);
389390
(bool success, bytes memory output) = precompile.call(input2);
@@ -394,7 +395,8 @@ library Linera {
394395
function assert_data_blob_exists(bytes32 hash) internal {
395396
address precompile = address(0x0b);
396397
LineraTypes.CryptoHash memory hash2 = LineraTypes.CryptoHash(hash);
397-
LineraTypes.BaseRuntimePrecompile memory base = LineraTypes.BaseRuntimePrecompile_case_assert_data_blob_exists(hash2);
398+
LineraTypes.DataBlobHash memory hash3 = LineraTypes.DataBlobHash(hash2);
399+
LineraTypes.BaseRuntimePrecompile memory base = LineraTypes.BaseRuntimePrecompile_case_assert_data_blob_exists(hash3);
398400
LineraTypes.RuntimePrecompile memory input1 = LineraTypes.RuntimePrecompile_case_base(base);
399401
bytes memory input2 = LineraTypes.bcs_serialize_RuntimePrecompile(input1);
400402
(bool success, bytes memory output) = precompile.call(input2);

linera-execution/solidity/LineraTypes.sol

Lines changed: 65 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,9 @@ library LineraTypes {
266266
// choice=7 corresponds to ReadBalanceOwners
267267
// choice=8 corresponds to ChainOwnership
268268
// choice=9 corresponds to ReadDataBlob
269-
CryptoHash read_data_blob;
269+
DataBlobHash read_data_blob;
270270
// choice=10 corresponds to AssertDataBlobExists
271-
CryptoHash assert_data_blob_exists;
271+
DataBlobHash assert_data_blob_exists;
272272
}
273273

274274
function BaseRuntimePrecompile_case_chain_id()
@@ -277,8 +277,8 @@ library LineraTypes {
277277
returns (BaseRuntimePrecompile memory)
278278
{
279279
AccountOwner memory read_owner_balance;
280-
CryptoHash memory read_data_blob;
281-
CryptoHash memory assert_data_blob_exists;
280+
DataBlobHash memory read_data_blob;
281+
DataBlobHash memory assert_data_blob_exists;
282282
return BaseRuntimePrecompile(uint8(0), read_owner_balance, read_data_blob, assert_data_blob_exists);
283283
}
284284

@@ -288,8 +288,8 @@ library LineraTypes {
288288
returns (BaseRuntimePrecompile memory)
289289
{
290290
AccountOwner memory read_owner_balance;
291-
CryptoHash memory read_data_blob;
292-
CryptoHash memory assert_data_blob_exists;
291+
DataBlobHash memory read_data_blob;
292+
DataBlobHash memory assert_data_blob_exists;
293293
return BaseRuntimePrecompile(uint8(1), read_owner_balance, read_data_blob, assert_data_blob_exists);
294294
}
295295

@@ -299,8 +299,8 @@ library LineraTypes {
299299
returns (BaseRuntimePrecompile memory)
300300
{
301301
AccountOwner memory read_owner_balance;
302-
CryptoHash memory read_data_blob;
303-
CryptoHash memory assert_data_blob_exists;
302+
DataBlobHash memory read_data_blob;
303+
DataBlobHash memory assert_data_blob_exists;
304304
return BaseRuntimePrecompile(uint8(2), read_owner_balance, read_data_blob, assert_data_blob_exists);
305305
}
306306

@@ -310,8 +310,8 @@ library LineraTypes {
310310
returns (BaseRuntimePrecompile memory)
311311
{
312312
AccountOwner memory read_owner_balance;
313-
CryptoHash memory read_data_blob;
314-
CryptoHash memory assert_data_blob_exists;
313+
DataBlobHash memory read_data_blob;
314+
DataBlobHash memory assert_data_blob_exists;
315315
return BaseRuntimePrecompile(uint8(3), read_owner_balance, read_data_blob, assert_data_blob_exists);
316316
}
317317

@@ -321,8 +321,8 @@ library LineraTypes {
321321
returns (BaseRuntimePrecompile memory)
322322
{
323323
AccountOwner memory read_owner_balance;
324-
CryptoHash memory read_data_blob;
325-
CryptoHash memory assert_data_blob_exists;
324+
DataBlobHash memory read_data_blob;
325+
DataBlobHash memory assert_data_blob_exists;
326326
return BaseRuntimePrecompile(uint8(4), read_owner_balance, read_data_blob, assert_data_blob_exists);
327327
}
328328

@@ -331,8 +331,8 @@ library LineraTypes {
331331
pure
332332
returns (BaseRuntimePrecompile memory)
333333
{
334-
CryptoHash memory read_data_blob;
335-
CryptoHash memory assert_data_blob_exists;
334+
DataBlobHash memory read_data_blob;
335+
DataBlobHash memory assert_data_blob_exists;
336336
return BaseRuntimePrecompile(uint8(5), read_owner_balance, read_data_blob, assert_data_blob_exists);
337337
}
338338

@@ -342,8 +342,8 @@ library LineraTypes {
342342
returns (BaseRuntimePrecompile memory)
343343
{
344344
AccountOwner memory read_owner_balance;
345-
CryptoHash memory read_data_blob;
346-
CryptoHash memory assert_data_blob_exists;
345+
DataBlobHash memory read_data_blob;
346+
DataBlobHash memory assert_data_blob_exists;
347347
return BaseRuntimePrecompile(uint8(6), read_owner_balance, read_data_blob, assert_data_blob_exists);
348348
}
349349

@@ -353,8 +353,8 @@ library LineraTypes {
353353
returns (BaseRuntimePrecompile memory)
354354
{
355355
AccountOwner memory read_owner_balance;
356-
CryptoHash memory read_data_blob;
357-
CryptoHash memory assert_data_blob_exists;
356+
DataBlobHash memory read_data_blob;
357+
DataBlobHash memory assert_data_blob_exists;
358358
return BaseRuntimePrecompile(uint8(7), read_owner_balance, read_data_blob, assert_data_blob_exists);
359359
}
360360

@@ -364,28 +364,28 @@ library LineraTypes {
364364
returns (BaseRuntimePrecompile memory)
365365
{
366366
AccountOwner memory read_owner_balance;
367-
CryptoHash memory read_data_blob;
368-
CryptoHash memory assert_data_blob_exists;
367+
DataBlobHash memory read_data_blob;
368+
DataBlobHash memory assert_data_blob_exists;
369369
return BaseRuntimePrecompile(uint8(8), read_owner_balance, read_data_blob, assert_data_blob_exists);
370370
}
371371

372-
function BaseRuntimePrecompile_case_read_data_blob(CryptoHash memory read_data_blob)
372+
function BaseRuntimePrecompile_case_read_data_blob(DataBlobHash memory read_data_blob)
373373
internal
374374
pure
375375
returns (BaseRuntimePrecompile memory)
376376
{
377377
AccountOwner memory read_owner_balance;
378-
CryptoHash memory assert_data_blob_exists;
378+
DataBlobHash memory assert_data_blob_exists;
379379
return BaseRuntimePrecompile(uint8(9), read_owner_balance, read_data_blob, assert_data_blob_exists);
380380
}
381381

382-
function BaseRuntimePrecompile_case_assert_data_blob_exists(CryptoHash memory assert_data_blob_exists)
382+
function BaseRuntimePrecompile_case_assert_data_blob_exists(DataBlobHash memory assert_data_blob_exists)
383383
internal
384384
pure
385385
returns (BaseRuntimePrecompile memory)
386386
{
387387
AccountOwner memory read_owner_balance;
388-
CryptoHash memory read_data_blob;
388+
DataBlobHash memory read_data_blob;
389389
return BaseRuntimePrecompile(uint8(10), read_owner_balance, read_data_blob, assert_data_blob_exists);
390390
}
391391

@@ -398,10 +398,10 @@ library LineraTypes {
398398
return abi.encodePacked(input.choice, bcs_serialize_AccountOwner(input.read_owner_balance));
399399
}
400400
if (input.choice == 9) {
401-
return abi.encodePacked(input.choice, bcs_serialize_CryptoHash(input.read_data_blob));
401+
return abi.encodePacked(input.choice, bcs_serialize_DataBlobHash(input.read_data_blob));
402402
}
403403
if (input.choice == 10) {
404-
return abi.encodePacked(input.choice, bcs_serialize_CryptoHash(input.assert_data_blob_exists));
404+
return abi.encodePacked(input.choice, bcs_serialize_DataBlobHash(input.assert_data_blob_exists));
405405
}
406406
return abi.encodePacked(input.choice);
407407
}
@@ -418,13 +418,13 @@ library LineraTypes {
418418
if (choice == 5) {
419419
(new_pos, read_owner_balance) = bcs_deserialize_offset_AccountOwner(new_pos, input);
420420
}
421-
CryptoHash memory read_data_blob;
421+
DataBlobHash memory read_data_blob;
422422
if (choice == 9) {
423-
(new_pos, read_data_blob) = bcs_deserialize_offset_CryptoHash(new_pos, input);
423+
(new_pos, read_data_blob) = bcs_deserialize_offset_DataBlobHash(new_pos, input);
424424
}
425-
CryptoHash memory assert_data_blob_exists;
425+
DataBlobHash memory assert_data_blob_exists;
426426
if (choice == 10) {
427-
(new_pos, assert_data_blob_exists) = bcs_deserialize_offset_CryptoHash(new_pos, input);
427+
(new_pos, assert_data_blob_exists) = bcs_deserialize_offset_DataBlobHash(new_pos, input);
428428
}
429429
require(choice < 11);
430430
return (new_pos, BaseRuntimePrecompile(choice, read_owner_balance, read_data_blob, assert_data_blob_exists));
@@ -1160,6 +1160,41 @@ library LineraTypes {
11601160
return value;
11611161
}
11621162

1163+
struct DataBlobHash {
1164+
CryptoHash value;
1165+
}
1166+
1167+
function bcs_serialize_DataBlobHash(DataBlobHash memory input)
1168+
internal
1169+
pure
1170+
returns (bytes memory)
1171+
{
1172+
return bcs_serialize_CryptoHash(input.value);
1173+
}
1174+
1175+
function bcs_deserialize_offset_DataBlobHash(uint256 pos, bytes memory input)
1176+
internal
1177+
pure
1178+
returns (uint256, DataBlobHash memory)
1179+
{
1180+
uint256 new_pos;
1181+
CryptoHash memory value;
1182+
(new_pos, value) = bcs_deserialize_offset_CryptoHash(pos, input);
1183+
return (new_pos, DataBlobHash(value));
1184+
}
1185+
1186+
function bcs_deserialize_DataBlobHash(bytes memory input)
1187+
internal
1188+
pure
1189+
returns (DataBlobHash memory)
1190+
{
1191+
uint256 new_pos;
1192+
DataBlobHash memory value;
1193+
(new_pos, value) = bcs_deserialize_offset_DataBlobHash(0, input);
1194+
require(new_pos == input.length, "incomplete deserialization");
1195+
return value;
1196+
}
1197+
11631198
struct GenericApplicationId {
11641199
uint8 choice;
11651200
// choice=0 corresponds to System

linera-execution/solidity/LineraTypes.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ CryptoHash:
7575
TUPLEARRAY:
7676
CONTENT: U8
7777
SIZE: 32
78+
DataBlobHash:
79+
NEWTYPESTRUCT:
80+
TYPENAME: CryptoHash
7881
BlockHeight:
7982
NEWTYPESTRUCT: U64
8083
ChainId:
@@ -126,11 +129,11 @@ BaseRuntimePrecompile:
126129
9:
127130
ReadDataBlob:
128131
NEWTYPE:
129-
TYPENAME: CryptoHash
132+
TYPENAME: DataBlobHash
130133
10:
131134
AssertDataBlobExists:
132135
NEWTYPE:
133-
TYPENAME: CryptoHash
136+
TYPENAME: DataBlobHash
134137
ContractRuntimePrecompile:
135138
ENUM:
136139
0:

linera-execution/src/evm/revm.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ use crate::{
3737
data_types::AmountU256,
3838
database::{DatabaseRuntime, StorageStats, EVM_SERVICE_GAS_LIMIT},
3939
},
40-
BaseRuntime, ContractRuntime, ContractSyncRuntimeHandle, EvmExecutionError, EvmRuntime,
41-
ExecutionError, ServiceRuntime, ServiceSyncRuntimeHandle, UserContract, UserContractInstance,
42-
UserContractModule, UserService, UserServiceInstance, UserServiceModule,
40+
BaseRuntime, ContractRuntime, ContractSyncRuntimeHandle, DataBlobHash, EvmExecutionError,
41+
EvmRuntime, ExecutionError, ServiceRuntime, ServiceSyncRuntimeHandle, UserContract,
42+
UserContractInstance, UserContractModule, UserService, UserServiceInstance, UserServiceModule,
4343
};
4444

4545
/// This is the selector of the `execute_message` that should be called
@@ -495,9 +495,9 @@ enum BaseRuntimePrecompile {
495495
/// Calling `chain_ownership` of `BaseRuntime`
496496
ChainOwnership,
497497
/// Calling `read_data_blob` of `BaseRuntime`
498-
ReadDataBlob(CryptoHash),
498+
ReadDataBlob(DataBlobHash),
499499
/// Calling `assert_data_blob_exists` of `BaseRuntime`
500-
AssertDataBlobExists(CryptoHash),
500+
AssertDataBlobExists(DataBlobHash),
501501
}
502502

503503
/// Some functionalities from the ContractRuntime not in BaseRuntime
@@ -648,9 +648,9 @@ fn base_runtime_call<Runtime: BaseRuntime>(
648648
let chain_ownership = runtime.chain_ownership()?;
649649
Ok(bcs::to_bytes(&chain_ownership)?)
650650
}
651-
BaseRuntimePrecompile::ReadDataBlob(hash) => runtime.read_data_blob(&hash),
651+
BaseRuntimePrecompile::ReadDataBlob(hash) => runtime.read_data_blob(hash),
652652
BaseRuntimePrecompile::AssertDataBlobExists(hash) => {
653-
runtime.assert_data_blob_exists(&hash)?;
653+
runtime.assert_data_blob_exists(hash)?;
654654
Ok(Vec::new())
655655
}
656656
}

linera-execution/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use linera_base::{
4141
},
4242
doc_scalar, hex_debug, http,
4343
identifiers::{
44-
Account, AccountOwner, ApplicationId, BlobId, BlobType, ChainId, EventId,
44+
Account, AccountOwner, ApplicationId, BlobId, BlobType, ChainId, DataBlobHash, EventId,
4545
GenericApplicationId, ModuleId, StreamName,
4646
},
4747
ownership::ChainOwnership,
@@ -668,10 +668,10 @@ pub trait BaseRuntime {
668668
fn assert_before(&mut self, timestamp: Timestamp) -> Result<(), ExecutionError>;
669669

670670
/// Reads a data blob specified by a given hash.
671-
fn read_data_blob(&mut self, hash: &CryptoHash) -> Result<Vec<u8>, ExecutionError>;
671+
fn read_data_blob(&mut self, hash: DataBlobHash) -> Result<Vec<u8>, ExecutionError>;
672672

673673
/// Asserts the existence of a data blob with the given hash.
674-
fn assert_data_blob_exists(&mut self, hash: &CryptoHash) -> Result<(), ExecutionError>;
674+
fn assert_data_blob_exists(&mut self, hash: DataBlobHash) -> Result<(), ExecutionError>;
675675
}
676676

677677
pub trait ServiceRuntime: BaseRuntime {
@@ -803,8 +803,8 @@ pub trait ContractRuntime: BaseRuntime {
803803
required_application_ids: Vec<ApplicationId>,
804804
) -> Result<ApplicationId, ExecutionError>;
805805

806-
/// Creates a new data blob and returns its ID.
807-
fn create_data_blob(&mut self, bytes: Vec<u8>) -> Result<BlobId, ExecutionError>;
806+
/// Creates a new data blob and returns its hash.
807+
fn create_data_blob(&mut self, bytes: Vec<u8>) -> Result<DataBlobHash, ExecutionError>;
808808

809809
/// Publishes a module with contract and service bytecode and returns the module ID.
810810
fn publish_module(

0 commit comments

Comments
 (0)