Skip to content

Commit

Permalink
[compiler-v2] fixed unittest of starcoin-stdlib, starcoin-token, star…
Browse files Browse the repository at this point in the history
…coin-token-object (#4312)

* [compiler-v2 framework] Solved the problem that the session extension list was empty in the unit test scenario

* [compiler-v2 framework] fixed some unittest

* [compiler-v2 framework] fixed test_chain_id

* [compiler-v2 framework] block some error for test

* [compiler-v2 framework] fixed create object error

* [compiler-v2 framework] remove log

* [compiler-v2 framework] move truncate_16 from `from_bcs` into `bcs_util.move`

* [compiler-v2 framework] In order to pass the test, the Contract Event v2 part was transplanted

* [compiler-v2 framework] In order to pass the test, the Contract Event v2 part was transplanted

* [compiler-v2 framework] fixed object create address error

* [compiler-v2 framework] fixed object create address error

* [compiler-v2 framework] remove unused codes

* [compiler-v2 framework] Fixed cargo fmt
  • Loading branch information
welbon authored Dec 1, 2024
1 parent 54860e9 commit 35d5716
Show file tree
Hide file tree
Showing 48 changed files with 1,824 additions and 13,075 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -429,15 +429,11 @@ move-prover-test-utils = { git = "https://github.com/starcoinorg/move", rev = "a
move-resource-viewer = { git = "https://github.com/starcoinorg/move", rev = "a14d2393e91b88465f1b3b351b20d34f0373e01f" }
move-stdlib = { git = "https://github.com/starcoinorg/move", rev = "a14d2393e91b88465f1b3b351b20d34f0373e01f" }
move-transactional-test-runner = { git = "https://github.com/starcoinorg/move", rev = "a14d2393e91b88465f1b3b351b20d34f0373e01f" }
move-unit-test = { git = "https://github.com/starcoinorg/move", rev = "a14d2393e91b88465f1b3b351b20d34f0373e01f", features = [
"table-extension",
] }
move-unit-test = { git = "https://github.com/starcoinorg/move", rev = "a14d2393e91b88465f1b3b351b20d34f0373e01f" }
move-vm-runtime = { git = "https://github.com/starcoinorg/move", rev = "a14d2393e91b88465f1b3b351b20d34f0373e01f" }
move-vm-types = { git = "https://github.com/starcoinorg/move", rev = "a14d2393e91b88465f1b3b351b20d34f0373e01f" }
move-table-extension = { git = "https://github.com/starcoinorg/move", rev = "a14d2393e91b88465f1b3b351b20d34f0373e01f" }
move-vm-test-utils = { git = "https://github.com/starcoinorg/move", rev = "a14d2393e91b88465f1b3b351b20d34f0373e01f", features = [
"table-extension",
] }
move-vm-test-utils = { git = "https://github.com/starcoinorg/move", rev = "a14d2393e91b88465f1b3b351b20d34f0373e01f" }

names = { version = "0.14.0", default-features = false }
network-api = { path = "network/api", package = "network-api" }
Expand Down
6 changes: 4 additions & 2 deletions account/service/src/account_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ impl EventHandler<Self, ContractEventNotification> for AccountEventService {
}

for i in item.0 .1.as_ref() {
if watched_keys.contains(i.contract_event.key()) {
let event = i.contract_event.v1().expect("contract event not v1");
if watched_keys.contains(event.key()) {
if let Err(e) = self.handle_contract_event(&i.contract_event) {
error!(
"fail to save accept token event: {:?}, err: {}",
Expand All @@ -66,7 +67,8 @@ impl EventHandler<Self, ContractEventNotification> for AccountEventService {
}

impl AccountEventService {
fn handle_contract_event(&self, event: &ContractEvent) -> Result<(), Error> {
fn handle_contract_event(&self, contract_event: &ContractEvent) -> Result<(), Error> {
let event = contract_event.v1()?;
if event.is::<AcceptTokenEvent>() {
let evt = event.decode_event::<AcceptTokenEvent>()?;
let addr = event.key().get_creator_address();
Expand Down
3 changes: 2 additions & 1 deletion cmd/starcoin/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ impl From<TransactionEventView> for EventView {

impl From<ContractEvent> for EventView {
/// Tries to convert the provided byte array into Event Key.
fn from(event: ContractEvent) -> Self {
fn from(contract_event: ContractEvent) -> Self {
let event = contract_event.v1().expect("not v1");
let event_data = EventDataView::new(event.type_tag(), event.event_data());
Self {
key: *event.key(),
Expand Down
4 changes: 2 additions & 2 deletions dataformat-generator/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use starcoin_types::account_config::{
VoteChangedEvent, WithdrawEvent,
};
use starcoin_types::block_metadata::BlockMetadata;
use starcoin_types::contract_event::{ContractEvent, ContractEventV0};
use starcoin_types::contract_event::{ContractEvent, ContractEventV1};
use starcoin_types::event::EventKey;
use starcoin_types::language_storage::TypeTag;
use starcoin_types::sign_message::{SignedMessage, SigningMessage};
Expand Down Expand Up @@ -74,7 +74,7 @@ fn generate() -> Result<(), Error> {
tracer.trace_type::<BlockMetadata>(&samples)?;

tracer.trace_value(&mut samples, &EventKey::new(0, AccountAddress::random()))?;
tracer.trace_type::<ContractEventV0>(&samples)?;
tracer.trace_type::<ContractEventV1>(&samples)?;
tracer.trace_type::<ContractEvent>(&samples)?;
tracer.trace_type::<StateKey>(&samples)?;
tracer.trace_type::<PersistedStateValueMetadata>(&samples)?;
Expand Down
15 changes: 12 additions & 3 deletions etc/starcoin_types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,26 @@ ChainId:
ContractEvent:
ENUM:
0:
V0:
V1:
NEWTYPE:
TYPENAME: ContractEventV1
1:
V2:
NEWTYPE:
TYPENAME: ContractEventV0
ContractEventV0:
TYPENAME: ContractEventV2
ContractEventV1:
STRUCT:
- key:
TYPENAME: EventKey
- sequence_number: U64
- type_tag:
TYPENAME: TypeTag
- event_data: BYTES
ContractEventV2:
STRUCT:
- type_tag:
TYPENAME: TypeTag
- event_data: BYTES
DataPath:
ENUM:
0:
Expand Down
21 changes: 14 additions & 7 deletions rpc/api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use std::convert::{TryFrom, TryInto};
use std::str::FromStr;

pub type ByteCode = Vec<u8>;

mod node_api_types;
pub mod pubsub;

Expand Down Expand Up @@ -1119,23 +1120,25 @@ pub struct TransactionEventView {

impl From<ContractEventInfo> for TransactionEventView {
fn from(info: ContractEventInfo) -> Self {
let event = info.event.v1().expect("not v1");
Self {
block_hash: Some(info.block_hash),
block_number: Some(info.block_number.into()),
transaction_hash: Some(info.transaction_hash),
transaction_index: Some(info.transaction_index),
transaction_global_index: Some(info.transaction_global_index.into()),
data: StrView(info.event.event_data().to_vec()),
type_tag: info.event.type_tag().clone().into(),
data: StrView(event.event_data().to_vec()),
type_tag: event.type_tag().clone().into(),
event_index: Some(info.event_index),
event_key: *info.event.key(),
event_seq_number: info.event.sequence_number().into(),
event_key: *event.key(),
event_seq_number: event.sequence_number().into(),
}
}
}

impl From<ContractEvent> for TransactionEventView {
fn from(event: ContractEvent) -> Self {
fn from(contract_event: ContractEvent) -> Self {
let event = contract_event.v1().expect("not v1");
Self {
block_hash: None,
block_number: None,
Expand All @@ -1161,6 +1164,7 @@ impl TransactionEventView {
event_index: Option<u32>,
contract_event: &ContractEvent,
) -> Self {
let event = contract_event.v1().expect("not v1");
Self {
block_hash,
block_number: block_number.map(Into::into),
Expand All @@ -1170,8 +1174,8 @@ impl TransactionEventView {
data: StrView(contract_event.event_data().to_vec()),
type_tag: contract_event.type_tag().clone().into(),
event_index,
event_key: *contract_event.key(),
event_seq_number: contract_event.sequence_number().into(),
event_key: *event.key(),
event_seq_number: event.sequence_number().into(),
}
}
}
Expand Down Expand Up @@ -1239,6 +1243,7 @@ impl From<TransactionOutput> for TransactionOutputView {
}
}
}

impl From<(AccessPath, WriteOp)> for TransactionOutputAction {
fn from((access_path, op): (AccessPath, WriteOp)) -> Self {
let (action, value) = match op {
Expand All @@ -1260,6 +1265,7 @@ impl From<(AccessPath, WriteOp)> for TransactionOutputAction {
}
}
}

#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
pub struct TransactionOutputAction {
pub access_path: AccessPath,
Expand Down Expand Up @@ -1842,6 +1848,7 @@ pub struct ConnectLocal;
impl ServiceRequest for ConnectLocal {
type Response = RpcChannel;
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct AccumulatorInfoView {
/// Accumulator root hash
Expand Down
9 changes: 6 additions & 3 deletions test-helper/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub fn execute_and_apply(chain_state: &ChainStateDB, txn: Transaction) -> Transa

output
}

pub fn current_block_number<S: StateView>(state_view: &S) -> u64 {
let mut ret = execute_readonly_function(
state_view,
Expand Down Expand Up @@ -173,13 +174,15 @@ pub fn account_execute(
) -> Result<TransactionOutput> {
user_execute(*account.address(), account.private_key(), state, payload)
}

pub fn account_execute_should_success(
account: &Account,
state: &ChainStateDB,
payload: TransactionPayload,
) -> Result<TransactionOutput> {
user_execute_should_success(*account.address(), account.private_key(), state, payload)
}

pub fn account_execute_with_output(
account: &Account,
state: &ChainStateDB,
Expand Down Expand Up @@ -296,7 +299,7 @@ pub fn expect_event<Event: MoveResource>(output: &TransactionOutput) -> Contract
output
.events()
.iter()
.filter(|event| event.is::<Event>())
.filter(|event| event.v1().expect("not v1").is::<Event>())
.last()
.cloned()
.unwrap_or_else(|| panic!("Expect event: {}", Event::struct_tag()))
Expand All @@ -306,9 +309,9 @@ pub fn expect_decode_event<Event: MoveResource>(output: &TransactionOutput) -> E
output
.events()
.iter()
.filter(|event| event.is::<Event>())
.filter(|event| event.v1().expect("not v1").is::<Event>())
.last()
.cloned()
.and_then(|event| event.decode_event::<Event>().ok())
.and_then(|event| event.v1().expect("not v1").decode_event::<Event>().ok())
.unwrap_or_else(|| panic!("Expect event: {}", Event::struct_tag()))
}
7 changes: 5 additions & 2 deletions types/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,13 @@ impl Default for Filter {

impl Filter {
pub fn matching(&self, block_number: BlockNumber, e: &ContractEvent) -> bool {
let event_key = e.v1().unwrap().key();
if self.from_block <= block_number
&& block_number <= self.to_block
&& (self.event_keys.is_empty() || self.event_keys.contains(e.key()))
&& (self.addrs.is_empty() || self.addrs.contains(&e.key().get_creator_address()))
&& (self.event_keys.is_empty()
|| self.event_keys.contains(event_key)
&& (self.addrs.is_empty()
|| self.addrs.contains(&event_key.get_creator_address())))
{
if self.type_tags.is_empty() {
return true;
Expand Down
Loading

0 comments on commit 35d5716

Please sign in to comment.