Skip to content

Commit

Permalink
[block] use address for block id instead of a move struct
Browse files Browse the repository at this point in the history
A block id as move struct is not rendered as raw bytes but a json object. This commit changes it back to address.
  • Loading branch information
zekun000 committed Aug 19, 2022
1 parent 7fc9759 commit f3aa425
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
"data": {
"type": "0x1::state_storage::StateStorageUsage",
"data": {
"bytes": "279972",
"bytes": "279220",
"epoch": "1",
"items": "98"
"items": "97"
}
},
"type": "write_resource"
Expand Down Expand Up @@ -82,11 +82,8 @@
"data": {
"epoch": "0",
"failed_proposer_indices": [],
"hash": "",
"height": "1",
"id": {
"high": "44867518937664683910420312596687572060",
"low": "2791876110695916617903004309989527630"
},
"previous_block_votes_bitvec": "0x00",
"proposer": "0x66b22b7465af92a9dc8d07d2878716d9dffbbb280830a7d8cf80334b5da812ef",
"round": "1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,8 @@
"data": {
"epoch": "0",
"failed_proposer_indices": [],
"hash": "",
"height": "13",
"id": {
"high": "173413796498303312109083670418497741528",
"low": "171822840942694342301788627058978757260"
},
"previous_block_votes_bitvec": "0x00",
"proposer": "0x66b22b7465af92a9dc8d07d2878716d9dffbbb280830a7d8cf80334b5da812ef",
"round": "1",
Expand Down Expand Up @@ -336,11 +333,8 @@
"data": {
"epoch": "0",
"failed_proposer_indices": [],
"hash": "",
"height": "14",
"id": {
"high": "165041164866981096228134403602969134918",
"low": "306311126780348191810628321309952818078"
},
"previous_block_votes_bitvec": "0x00",
"proposer": "0x66b22b7465af92a9dc8d07d2878716d9dffbbb280830a7d8cf80334b5da812ef",
"round": "1",
Expand Down Expand Up @@ -590,11 +584,8 @@
"data": {
"epoch": "0",
"failed_proposer_indices": [],
"hash": "",
"height": "15",
"id": {
"high": "318823719137598282177681024651100300043",
"low": "247831944748971205718998755914685013053"
},
"previous_block_votes_bitvec": "0x00",
"proposer": "0x66b22b7465af92a9dc8d07d2878716d9dffbbb280830a7d8cf80334b5da812ef",
"round": "1",
Expand Down Expand Up @@ -844,11 +835,8 @@
"data": {
"epoch": "0",
"failed_proposer_indices": [],
"hash": "",
"height": "16",
"id": {
"high": "305795692603525369058075758017459311369",
"low": "307484336998466477300468312440493966685"
},
"previous_block_votes_bitvec": "0x00",
"proposer": "0x66b22b7465af92a9dc8d07d2878716d9dffbbb280830a7d8cf80334b5da812ef",
"round": "1",
Expand Down Expand Up @@ -1098,11 +1086,8 @@
"data": {
"epoch": "0",
"failed_proposer_indices": [],
"hash": "",
"height": "17",
"id": {
"high": "13516278485545741463755032675832669560",
"low": "83744187991019746630766558975023501522"
},
"previous_block_votes_bitvec": "0x00",
"proposer": "0x66b22b7465af92a9dc8d07d2878716d9dffbbb280830a7d8cf80334b5da812ef",
"round": "1",
Expand Down Expand Up @@ -1352,11 +1337,8 @@
"data": {
"epoch": "0",
"failed_proposer_indices": [],
"hash": "",
"height": "18",
"id": {
"high": "238922821468272615626599870290521523420",
"low": "292020867058513978735577988047270237864"
},
"previous_block_votes_bitvec": "0x00",
"proposer": "0x66b22b7465af92a9dc8d07d2878716d9dffbbb280830a7d8cf80334b5da812ef",
"round": "1",
Expand Down Expand Up @@ -1606,11 +1588,8 @@
"data": {
"epoch": "0",
"failed_proposer_indices": [],
"hash": "",
"height": "19",
"id": {
"high": "211777759457659092342153853296919848423",
"low": "933246800001400738309287850642844692"
},
"previous_block_votes_bitvec": "0x00",
"proposer": "0x66b22b7465af92a9dc8d07d2878716d9dffbbb280830a7d8cf80334b5da812ef",
"round": "1",
Expand Down Expand Up @@ -1860,11 +1839,8 @@
"data": {
"epoch": "0",
"failed_proposer_indices": [],
"hash": "",
"height": "20",
"id": {
"high": "252540448581468420831212755017538344047",
"low": "260894870013625805388635433013070122742"
},
"previous_block_votes_bitvec": "0x00",
"proposer": "0x66b22b7465af92a9dc8d07d2878716d9dffbbb280830a7d8cf80334b5da812ef",
"round": "1",
Expand Down
12 changes: 5 additions & 7 deletions aptos-move/framework/aptos-framework/sources/block.move
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module aptos_framework::block {
use std::vector;
use std::option;
use aptos_std::event::{Self, EventHandle};
use aptos_std::crypto_hash::{Self, HashValue};

use aptos_framework::timestamp;
use aptos_framework::system_addresses;
Expand All @@ -26,7 +25,7 @@ module aptos_framework::block {

/// Should be in-sync with NewBlockEvent rust struct in new_block.rs
struct NewBlockEvent has drop, store {
id: HashValue,
hash: address,
epoch: u64,
round: u64,
height: u64,
Expand Down Expand Up @@ -81,7 +80,7 @@ module aptos_framework::block {
/// The runtime always runs this before executing the transactions in a block.
fun block_prologue(
vm: signer,
id: vector<u128>,
hash: address,
epoch: u64,
round: u64,
proposer: address,
Expand All @@ -107,10 +106,9 @@ module aptos_framework::block {

let block_metadata_ref = borrow_global_mut<BlockResource>(@aptos_framework);
block_metadata_ref.height = event::counter(&block_metadata_ref.new_block_events);
let id = crypto_hash::new_hash_value(*vector::borrow(&id, 0), *vector::borrow(&id, 1));

let new_block_event = NewBlockEvent {
id,
hash,
epoch,
round,
height: block_metadata_ref.height,
Expand Down Expand Up @@ -150,12 +148,12 @@ module aptos_framework::block {
/// reconfiguration event.
fun emit_genesis_block_event(vm: signer) acquires BlockResource {
let block_metadata_ref = borrow_global_mut<BlockResource>(@aptos_framework);
let genesis_id = crypto_hash::new_hash_value(0 ,0);
let genesis_id = @0x0;
emit_new_block_event(
&vm,
&mut block_metadata_ref.new_block_events,
NewBlockEvent {
id: genesis_id,
hash: genesis_id,
epoch: 0,
round: 0,
height: 0,
Expand Down

This file was deleted.

6 changes: 3 additions & 3 deletions consensus/src/liveness/leader_reputation_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::liveness::{
proposer_election::{choose_index, ProposerElection},
};
use aptos_bitvec::BitVec;
use aptos_crypto::{bls12381, HashValue};
use aptos_crypto::bls12381;
use aptos_infallible::Mutex;
use aptos_keygen::KeyGen;
use aptos_types::{
Expand Down Expand Up @@ -77,7 +77,7 @@ impl TestBlockBuilder {
) -> NewBlockEvent {
self.round += 1 + failed_proposers.len() as u64;
NewBlockEvent::new(
HashValue::random(),
AccountAddress::random(),
self.epoch,
self.round,
self.round,
Expand Down Expand Up @@ -468,7 +468,7 @@ impl MockDbReader {
*idx,
TypeTag::Struct(NewBlockEvent::struct_tag()),
bcs::to_bytes(&NewBlockEvent::new(
HashValue::random(),
AccountAddress::random(),
epoch,
round,
round,
Expand Down
14 changes: 3 additions & 11 deletions crates/aptos-rest-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,16 +419,9 @@ impl Client {
start: Option<u64>,
limit: Option<u16>,
) -> Result<Response<Vec<VersionedNewBlockEvent>>> {
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct BlockId {
#[serde(deserialize_with = "deserialize_from_string")]
low: u128,
#[serde(deserialize_with = "deserialize_from_string")]
high: u128,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct NewBlockEventResponse {
id: BlockId,
hash: String,
#[serde(deserialize_with = "deserialize_from_string")]
epoch: u64,
#[serde(deserialize_with = "deserialize_from_string")]
Expand Down Expand Up @@ -462,11 +455,10 @@ impl Client {
serde_json::from_value::<NewBlockEventResponse>(event.data)
.map_err(|e| anyhow!(e))
.and_then(|e| {
let mut raw_bytes = e.id.low.to_le_bytes().to_vec();
raw_bytes.append(&mut e.id.high.to_le_bytes().to_vec());
Ok(VersionedNewBlockEvent {
event: NewBlockEvent::new(
HashValue::from_slice(raw_bytes)?,
AccountAddress::from_hex_literal(&e.hash)
.map_err(|e| anyhow!(e))?,
e.epoch,
e.round,
e.height,
Expand Down
4 changes: 2 additions & 2 deletions storage/aptosdb/src/event_store/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ fn test_index_get_impl(event_batches: Vec<Vec<ContractEvent>>) {

prop_compose! {
fn arb_new_block_events()(
id in any::<HashValue>(),
hash in any::<AccountAddress>(),
address in any::<AccountAddress>(),
mut version in 1..10000u64,
mut timestamp in 0..1000000u64, // initial timestamp
Expand All @@ -275,7 +275,7 @@ prop_compose! {
version += v;
timestamp += t;
let new_block_event = NewBlockEvent::new(
id,
hash,
0, // epoch
seq, // round
seq, // height
Expand Down
7 changes: 3 additions & 4 deletions types/src/account_config/events/new_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::{
event::{EventHandle, EventKey},
};
use anyhow::Result;
use aptos_crypto::HashValue;
use move_deps::move_core_types::{
ident_str,
identifier::IdentStr,
Expand All @@ -20,7 +19,7 @@ use serde::{Deserialize, Serialize};
/// Should be kept in-sync with NewBlockEvent move struct in block.move.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct NewBlockEvent {
id: HashValue,
hash: AccountAddress,
epoch: u64,
round: u64,
height: u64,
Expand Down Expand Up @@ -67,7 +66,7 @@ impl NewBlockEvent {
}

pub fn new(
id: HashValue,
hash: AccountAddress,
epoch: u64,
round: u64,
height: u64,
Expand All @@ -77,7 +76,7 @@ impl NewBlockEvent {
timestamp: u64,
) -> Self {
Self {
id,
hash,
epoch,
round,
height,
Expand Down
6 changes: 1 addition & 5 deletions types/src/block_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use aptos_crypto::HashValue;
use move_deps::move_core_types::{account_address::AccountAddress, value::MoveValue};
use serde::{Deserialize, Serialize};
use std::convert::TryInto;

/// Struct that will be persisted on chain to store the information of the current block.
///
Expand Down Expand Up @@ -55,12 +54,9 @@ impl BlockMetadata {
}

pub fn get_prologue_move_args(self, signer: AccountAddress) -> Vec<MoveValue> {
let bytes = self.id.to_vec();
let low = u128::from_le_bytes(bytes[0..16].try_into().unwrap());
let high = u128::from_le_bytes(bytes[16..32].try_into().unwrap());
vec![
MoveValue::Signer(signer),
MoveValue::Vector(vec![MoveValue::U128(low), MoveValue::U128(high)]),
MoveValue::Address(AccountAddress::from_bytes(&self.id.to_vec()).unwrap()),
MoveValue::U64(self.epoch),
MoveValue::U64(self.round),
MoveValue::Address(self.proposer),
Expand Down

0 comments on commit f3aa425

Please sign in to comment.