Skip to content

Commit

Permalink
add parents_hash to BlockMetadata txn
Browse files Browse the repository at this point in the history
1. change type of on-chain-resource BlockMetadataV2
2. handle NewBlockEventV2
3. generate new rpc api
4. update blockmetadata types check
5. upgrade TransactionStorage
  • Loading branch information
simonjiao committed Feb 1, 2024
1 parent 5d8e1a3 commit 61c3d74
Show file tree
Hide file tree
Showing 21 changed files with 370 additions and 132 deletions.
6 changes: 2 additions & 4 deletions chain/open-block/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ pub struct OpenedBlock {
difficulty: U256,
strategy: ConsensusStrategy,
vm_metrics: Option<VMMetrics>,
tips_hash: Option<Vec<HashValue>>,
blue_blocks: Option<Vec<Block>>,
}

Expand Down Expand Up @@ -71,7 +70,7 @@ impl OpenedBlock {
let chain_state =
ChainStateDB::new(storage.into_super_arc(), Some(previous_header.state_root()));
let chain_id = previous_header.chain_id();
let block_meta = BlockMetadata::new(
let block_meta = BlockMetadata::new_with_parents(
previous_block_id,
block_timestamp,
author,
Expand All @@ -80,6 +79,7 @@ impl OpenedBlock {
previous_header.number() + 1,
chain_id,
previous_header.gas_used(),
tips_hash.unwrap_or_default(),
);
let mut opened_block = Self {
previous_block_info: block_info,
Expand All @@ -94,7 +94,6 @@ impl OpenedBlock {
difficulty,
strategy,
vm_metrics,
tips_hash,
blue_blocks,
};
opened_block.initialize()?;
Expand Down Expand Up @@ -299,7 +298,6 @@ impl OpenedBlock {
self.difficulty,
self.strategy,
self.block_meta,
self.tips_hash,
);
Ok(block_template)
}
Expand Down
2 changes: 1 addition & 1 deletion chain/tests/test_block_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn test_chain_filter_events() {
let event_type_tag = TypeTag::Struct(Box::new(StructTag {
address: genesis_address(),
module: Identifier::from_str("Block").unwrap(),
name: Identifier::from_str("NewBlockEvent").unwrap(),
name: Identifier::from_str("NewBlockEventV2").unwrap(),
type_params: vec![],
}));

Expand Down
4 changes: 4 additions & 0 deletions etc/starcoin_types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ BlockMetadata:
- chain_id:
TYPENAME: ChainId
- parent_gas_used: U64
- parents_hash:
OPTION:
SEQ:
TYPENAME: HashValue
ChainId:
STRUCT:
- id: U8
Expand Down
10 changes: 10 additions & 0 deletions rpc/api/generated_rpc_schema/chain.json
Original file line number Diff line number Diff line change
Expand Up @@ -2179,6 +2179,16 @@
"type": "string",
"format": "HashValue"
},
"parents_hash": {
"type": [
"array",
"null"
],
"items": {
"type": "string",
"format": "HashValue"
}
},
"timestamp": {
"type": "string"
},
Expand Down
7 changes: 6 additions & 1 deletion rpc/api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@ pub struct BlockMetadataView {
pub number: StrView<BlockNumber>,
pub chain_id: u8,
pub parent_gas_used: StrView<u64>,
pub parents_hash: Option<Vec<HashValue>>,
}

impl From<BlockMetadata> for BlockMetadataView {
Expand All @@ -681,6 +682,7 @@ impl From<BlockMetadata> for BlockMetadataView {
number,
chain_id,
parent_gas_used,
parents_hash,

Check warning on line 685 in rpc/api/src/types.rs

View check run for this annotation

Codecov / codecov/patch

rpc/api/src/types.rs#L685

Added line #L685 was not covered by tests
) = origin.into_inner();
BlockMetadataView {
parent_hash,
Expand All @@ -691,6 +693,7 @@ impl From<BlockMetadata> for BlockMetadataView {
number: number.into(),
chain_id: chain_id.id(),
parent_gas_used: parent_gas_used.into(),
parents_hash,

Check warning on line 696 in rpc/api/src/types.rs

View check run for this annotation

Codecov / codecov/patch

rpc/api/src/types.rs#L696

Added line #L696 was not covered by tests
}
}
}
Expand All @@ -707,8 +710,9 @@ impl Into<BlockMetadata> for BlockMetadataView {
number,
chain_id,
parent_gas_used,
parents_hash,

Check warning on line 713 in rpc/api/src/types.rs

View check run for this annotation

Codecov / codecov/patch

rpc/api/src/types.rs#L713

Added line #L713 was not covered by tests
} = self;
BlockMetadata::new(
BlockMetadata::new_with_parents(

Check warning on line 715 in rpc/api/src/types.rs

View check run for this annotation

Codecov / codecov/patch

rpc/api/src/types.rs#L715

Added line #L715 was not covered by tests
parent_hash,
timestamp.0,
author,
Expand All @@ -717,6 +721,7 @@ impl Into<BlockMetadata> for BlockMetadataView {
number.0,
genesis_config::ChainId::new(chain_id),
parent_gas_used.0,
parents_hash.unwrap_or_default(),

Check warning on line 724 in rpc/api/src/types.rs

View check run for this annotation

Codecov / codecov/patch

rpc/api/src/types.rs#L724

Added line #L724 was not covered by tests
)
}
}
Expand Down
89 changes: 1 addition & 88 deletions storage/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
use crate::{
define_storage,
storage::{
CodecKVStore, CodecWriteBatch, ColumnFamily, KeyCodec, SchemaStorage, StorageInstance,
ValueCodec,
},
storage::{CodecKVStore, StorageInstance, ValueCodec},
BLOCK_BODY_PREFIX_NAME, BLOCK_HEADER_PREFIX_NAME, BLOCK_HEADER_PREFIX_NAME_V2,
BLOCK_PREFIX_NAME, BLOCK_PREFIX_NAME_V2, BLOCK_TRANSACTIONS_PREFIX_NAME,
BLOCK_TRANSACTION_INFOS_PREFIX_NAME, FAILED_BLOCK_PREFIX_NAME, FAILED_BLOCK_PREFIX_NAME_V2,
Expand Down Expand Up @@ -422,88 +419,4 @@ impl BlockStorage {
self.failed_block_storage
.put_raw(block_id, old_block.encode_value()?)
}

fn upgrade_store<K, V1, V2, T1, T2>(
old_store: T1,
store: T2,
batch_size: usize,
) -> Result<usize>
where
K: KeyCodec + Copy,
V1: ValueCodec + Into<V2>,
V2: ValueCodec,
T1: SchemaStorage + ColumnFamily<Key = K, Value = V1>,
T2: SchemaStorage + ColumnFamily<Key = K, Value = V2>,
{
let mut total_size: usize = 0;
let mut old_iter = old_store.iter()?;
old_iter.seek_to_first();

let mut to_delete = Some(CodecWriteBatch::new());
let mut to_put = Some(CodecWriteBatch::new());
let mut item_count = 0;

for item in old_iter {
let (id, old_block) = item?;
let block: V2 = old_block.into();
to_delete
.as_mut()
.unwrap()
.delete(id)
.expect("should never fail");
to_put
.as_mut()
.unwrap()
.put(id, block)
.expect("should never fail");

item_count += 1;
if item_count == batch_size {
total_size = total_size.saturating_add(item_count);
item_count = 0;
old_store
.write_batch(to_delete.take().unwrap())
.expect("should never fail");
store
.write_batch(to_put.take().unwrap())
.expect("should never fail");

to_delete = Some(CodecWriteBatch::new());
to_put = Some(CodecWriteBatch::new());
}
}
if item_count != 0 {
total_size = total_size.saturating_add(item_count);
old_store
.write_batch(to_delete.take().unwrap())
.expect("should never fail");
store
.write_batch(to_put.take().unwrap())
.expect("should never fail");
}

Ok(total_size)
}

pub fn upgrade_block_header(instance: StorageInstance) -> Result<()> {
const BATCH_SIZE: usize = 1000usize;

let old_header_store = OldBlockHeaderStorage::new(instance.clone());
let header_store = BlockHeaderStorage::new(instance.clone());
let total_size = Self::upgrade_store(old_header_store, header_store, BATCH_SIZE)?;
info!("upgraded {total_size} block headers");

let old_block_store = OldBlockInnerStorage::new(instance.clone());
let block_store = BlockInnerStorage::new(instance.clone());
let total_blocks = Self::upgrade_store(old_block_store, block_store, BATCH_SIZE)?;
info!("upgraded {total_blocks} blocks");

let old_failed_block_store = OldFailedBlockStorage::new(instance.clone());
let failed_block_store = FailedBlockStorage::new(instance);
let total_failed_blocks =
Self::upgrade_store(old_failed_block_store, failed_block_store, BATCH_SIZE)?;
info!("upgraded {total_failed_blocks} failed_blocks");

Ok(())
}
}
2 changes: 2 additions & 0 deletions storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub const STATE_NODE_PREFIX_NAME: ColumnFamilyName = "state_node";
pub const STATE_NODE_PREFIX_NAME_PREV: ColumnFamilyName = "state_node_prev";
pub const CHAIN_INFO_PREFIX_NAME: ColumnFamilyName = "chain_info";
pub const TRANSACTION_PREFIX_NAME: ColumnFamilyName = "transaction";
pub const TRANSACTION_PREFIX_NAME_V2: ColumnFamilyName = "transaction_v2";
pub const TRANSACTION_INFO_PREFIX_NAME: ColumnFamilyName = "transaction_info";
pub const TRANSACTION_INFO_PREFIX_NAME_V2: ColumnFamilyName = "transaction_info_v2";
pub const TRANSACTION_INFO_HASH_PREFIX_NAME: ColumnFamilyName = "transaction_info_hash";
Expand Down Expand Up @@ -168,6 +169,7 @@ static VEC_PREFIX_NAME_V4: Lazy<Vec<ColumnFamilyName>> = Lazy::new(|| {
CONTRACT_EVENT_PREFIX_NAME,
FAILED_BLOCK_PREFIX_NAME,
FAILED_BLOCK_PREFIX_NAME_V2,
TRANSACTION_PREFIX_NAME_V2,
TABLE_INFO_PREFIX_NAME,
]
});
Expand Down
22 changes: 22 additions & 0 deletions storage/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::storage::ValueCodec;
use crate::{define_storage, TRANSACTION_PREFIX_NAME};
use bcs_ext::BCSCodec;
use starcoin_crypto::HashValue;
use starcoin_vm_types::transaction::LegacyTransaction;

define_storage!(
LegacyTransactionStorage,
HashValue,
LegacyTransaction,
TRANSACTION_PREFIX_NAME
);

impl ValueCodec for LegacyTransaction {
fn encode_value(&self) -> anyhow::Result<Vec<u8>> {
self.encode()
}

Check warning on line 17 in storage/src/transaction/legacy.rs

View check run for this annotation

Codecov / codecov/patch

storage/src/transaction/legacy.rs#L15-L17

Added lines #L15 - L17 were not covered by tests

fn decode_value(data: &[u8]) -> anyhow::Result<Self> {
Self::decode(data)
}

Check warning on line 21 in storage/src/transaction/legacy.rs

View check run for this annotation

Codecov / codecov/patch

storage/src/transaction/legacy.rs#L19-L21

Added lines #L19 - L21 were not covered by tests
}
7 changes: 4 additions & 3 deletions storage/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
// SPDX-License-Identifier: Apache-2.0

use crate::storage::{CodecKVStore, CodecWriteBatch, ValueCodec};
use crate::TRANSACTION_PREFIX_NAME;
use crate::{define_storage, TransactionStore};
use crate::{define_storage, TransactionStore, TRANSACTION_PREFIX_NAME_V2};
use anyhow::Result;
use bcs_ext::BCSCodec;
pub use legacy::LegacyTransactionStorage;
use starcoin_crypto::HashValue;
use starcoin_types::transaction::Transaction;

define_storage!(
TransactionStorage,
HashValue,
Transaction,
TRANSACTION_PREFIX_NAME
TRANSACTION_PREFIX_NAME_V2
);

impl ValueCodec for Transaction {
Expand Down Expand Up @@ -46,5 +46,6 @@ impl TransactionStore for TransactionStorage {
}
}

mod legacy;
#[cfg(test)]
mod test;
Loading

0 comments on commit 61c3d74

Please sign in to comment.