wallet_config.rs
Overview
The wallet_config.rs file provides utilities for constructing internal messages related to wallet operations within the Block Keeper system, specifically focusing on the creation of "slash" messages. These messages are used to trigger slashing actions on wallets, typically for penalizing misbehavior or enforcing rules in a blockchain environment. The file leverages types and abstractions from the tvm_block and tvm_types crates to build and serialize messages that can be sent internally within the system.
Function Details
create_wallet_slash_message
pub fn create_wallet_slash_message(data: &BlockKeeperSlashData) -> anyhow::Result<Message>
Purpose:
Constructs an internalMessagethat commands a wallet to perform a slashing operation based on provided slashing data.Parameters:
data: &BlockKeeperSlashData
A reference to a struct containing all necessary slashing information:addr: The wallet address to be slashed.slash_type: Numeric type of slash applied.bls_pubkey: BLS public key associated with the wallet (used for identification and verification).
Returns:
Result<Message, anyhow::Error>
On success, returns a fully constructed internalMessageready to be sent. On failure, returns an error detailing the cause.
Usage Example:
let slash_data = BlockKeeperSlashData { addr: some_address, slash_type: 1, bls_pubkey: some_bls_key, }; let slash_message = create_wallet_slash_message(&slash_data)?; // slash_message can now be sent or processed furtherImplementation Details:
Message Parameter Construction:
Formats JSON parameters embedding the slash type and the hexadecimal-encoded BLS public key.Message Body Encoding:
Callstvm_abi::encode_function_callwith:ABI definition (
BLOCK_KEEPER_WALLET_ABI),Function name
"slash",The formatted parameters,
Encoding flags for internal message formatting.
Address Handling:
Both source and destination addresses are set to the wallet address (addr) cloned from the input data.Internal Message Header:
AnInternalMessageHeaderis created with the source and destination addresses wrapped asMsgAddressIntwith a standard address format. The attached currency is set to 0.1 grams (100,000,000 nanograms).Message Body Serialization:
The encoded function call is serialized into aSliceDatastructure for inclusion in the message body.Message Construction:
Combines the header and body into aMessageusingMessage::with_int_header_and_body.
Error Handling:
Each step that can fail (address creation, ABI encoding, serialization) wraps errors inanyhow::Errorwith descriptive messages, enabling easier debugging.
Interactions with Other Modules
Utilizes types from the
tvm_blockcrate (InternalMessageHeader,CurrencyCollection,Grams,Message,MsgAddressInt) to build and structure messages.Employs
tvm_types::SliceDatafor binary data serialization.References ABI definitions and slash data structures from the
block_keeper_systemcrate, specifically:BLOCK_KEEPER_WALLET_ABI— ABI interface used for encoding the slash function call.BlockKeeperSlashData— input data struct encapsulating slash parameters.
This function serves as a bridge between high-level slash data and the low-level internal message format required by the Block Keeper wallet system to initiate slashing procedures.
Diagram: Function Workflow
flowchart TD
A[Input: BlockKeeperSlashData] --> B[Format Parameters JSON]
B --> C[Encode Function Call with BLOCK_KEEPER_WALLET_ABI]
C --> D[Create Source & Destination MsgAddressInt]
D --> E[Create InternalMessageHeader with Addresses & Currency]
E --> F[Serialize Encoded Body into SliceData]
F --> G[Construct Message with Header and Body]
G --> H[Return Message or Error]
This flowchart illustrates the step-by-step transformation of slash data into a fully-fledged internal message suitable for dispatch within the wallet subsystem.