// Copyright 2019-2022 ChainSafe Systems // SPDX-License-Identifier: Apache-2.0, MIT use fil_actor_market::balance_table::BALANCE_TABLE_BITWIDTH; use fil_actor_market::policy::detail::DEAL_MAX_LABEL_SIZE; use fil_actor_market::{ deal_id_key, ext, ActivateDealsParams, Actor as MarketActor, ClientDealProposal, DealArray, DealMetaArray, Label, Method, PublishStorageDealsParams, PublishStorageDealsReturn, State, WithdrawBalanceParams, NO_ALLOCATION_ID, PROPOSALS_AMT_BITWIDTH, STATES_AMT_BITWIDTH, }; use fil_actors_runtime::cbor::{deserialize, serialize}; use fil_actors_runtime::network::EPOCHS_IN_DAY; use fil_actors_runtime::runtime::{builtins::Type, Policy, Runtime}; use fil_actors_runtime::test_utils::*; use fil_actors_runtime::{ make_empty_map, make_map_with_root_and_bitwidth, ActorError, BatchReturn, Map, SetMultimap, BURNT_FUNDS_ACTOR_ADDR, CALLER_TYPES_SIGNABLE, DATACAP_TOKEN_ACTOR_ADDR, SYSTEM_ACTOR_ADDR, VERIFIED_REGISTRY_ACTOR_ADDR, }; use frc46_token::token::types::{TransferFromParams, TransferFromReturn}; use fvm_ipld_amt::Amt; use fvm_ipld_encoding::{to_vec, RawBytes}; use fvm_shared::address::Address; use fvm_shared::clock::{ChainEpoch, EPOCH_UNDEFINED}; use fvm_shared::crypto::signature::Signature; use fvm_shared::deal::DealID; use fvm_shared::econ::TokenAmount; use fvm_shared::error::ExitCode; use fvm_shared::piece::PaddedPieceSize; use fvm_shared::sector::StoragePower; use fvm_shared::{HAMT_BIT_WIDTH, METHOD_CONSTRUCTOR, METHOD_SEND}; use regex::Regex; use std::ops::Add; use fil_actor_market::ext::account::{AuthenticateMessageParams, AUTHENTICATE_MESSAGE_METHOD}; use fil_actor_market::ext::verifreg::{AllocationID, AllocationRequest, AllocationsResponse}; use num_traits::{FromPrimitive, Zero}; mod harness; use harness::*; #[test] fn test_remove_all_error() { let market_actor = Address::new_id(100); let rt = MockRuntime { receiver: market_actor, ..Default::default() }; SetMultimap::new(&rt.store()).remove_all(42).expect("expected no error"); } // TODO add array stuff #[test] fn simple_construction() { let mut rt = MockRuntime { receiver: Address::new_id(100), caller: SYSTEM_ACTOR_ADDR, caller_type: *INIT_ACTOR_CODE_ID, ..Default::default() }; rt.expect_validate_caller_addr(vec![SYSTEM_ACTOR_ADDR]); assert_eq!( RawBytes::default(), rt.call::(METHOD_CONSTRUCTOR, &RawBytes::default()).unwrap() ); rt.verify(); let store = &rt.store; let empty_balance_table = make_empty_map::<_, TokenAmount>(store, BALANCE_TABLE_BITWIDTH).flush().unwrap(); let empty_map = make_empty_map::<_, ()>(store, HAMT_BIT_WIDTH).flush().unwrap(); let empty_proposals_array = Amt::<(), _>::new_with_bit_width(store, PROPOSALS_AMT_BITWIDTH).flush().unwrap(); let empty_states_array = Amt::<(), _>::new_with_bit_width(store, STATES_AMT_BITWIDTH).flush().unwrap(); let empty_multimap = SetMultimap::new(store).root().unwrap(); let state_data: State = rt.get_state(); assert_eq!(empty_proposals_array, state_data.proposals); assert_eq!(empty_states_array, state_data.states); assert_eq!(empty_map, state_data.pending_proposals); assert_eq!(empty_balance_table, state_data.escrow_table); assert_eq!(empty_balance_table, state_data.locked_table); assert_eq!(0, state_data.next_id); assert_eq!(empty_multimap, state_data.deal_ops_by_epoch); assert_eq!(state_data.last_cron, EPOCH_UNDEFINED); } #[test] fn label_cbor() { let label = Label::String("i_am_random_string____i_am_random_string____".parse().unwrap()); let _ = to_vec(&label) .map_err(|e| ActorError::from(e).wrap("failed to serialize DealProposal")) .unwrap(); let label2 = Label::Bytes(b"i_am_random_____i_am_random_____".to_vec()); let _ = to_vec(&label2) .map_err(|e| ActorError::from(e).wrap("failed to serialize DealProposal")) .unwrap(); let empty_string_label = Label::String("".parse().unwrap()); let sv_bz = to_vec(&empty_string_label).unwrap(); assert_eq!(vec![0x60], sv_bz); let empty_bytes_label = Label::Bytes(b"".to_vec()); let sv_bz = to_vec(&empty_bytes_label).unwrap(); assert_eq!(vec![0x40], sv_bz); } #[test] fn label_from_cbor() { // empty string, b001_00000 let empty_cbor_text = vec![0x60]; let label1: Label = deserialize(&RawBytes::from(empty_cbor_text), "empty cbor string").unwrap(); if let Label::String(s) = label1 { assert_eq!("", s) } else { panic!("expected string label not bytes") } // valid utf8 string b011_01000 "deadbeef" let end_valid_cbor_text = b"deadbeef".to_vec(); let mut valid_cbor_text = vec![0x68]; for i in end_valid_cbor_text { valid_cbor_text.push(i); } let label2: Label = deserialize(&RawBytes::from(valid_cbor_text), "valid cbor string").unwrap(); if let Label::String(s) = label2 { assert_eq!("deadbeef", s) } else { panic!("expected string label not bytes") } // invalid utf8 string 0b011_00100 0xde 0xad 0xbe 0xeef let invalid_cbor_text = vec![0x64, 0xde, 0xad, 0xbe, 0xef]; let out = deserialize::