Client & Blockchain
The final step involved a client
object that handled the actual payment on the blockchain. But what is this mysterious client
? And how do we, as users, interact with our funds on the LazAI network?
This is where the LazAI Client comes in. It is your secure gateway to the LazChain, like a specialized banking app and notary service rolled into one.
The Problem: Talking to a Blockchain is Hard
Blockchains are powerful, but they aren't user-friendly. Interacting with one directly involves managing cryptographic keys, formatting transactions, calculating "gas" fees (the cost of a transaction), and understanding complex smart contract interfaces. A single mistake could lead to lost funds.
Imagine you want to pay for an AI service. You shouldn't need to be a blockchain expert to do it. You need a simple, safe tool that handles all the technical details for you, just like using a credit card online without needing to understand the international banking system.
The Solution: Your Personal Blockchain Assistant
The Client
is our solution. It's a Python class that abstracts away all the low-level blockchain complexity. It provides simple, easy-to-understand methods for all the key actions you might want to perform, such as:
- Checking your account balance.
- Depositing funds to pay for services.
- Registering new compute nodes.
- Signing requests to authorize payment.
It's your secure wallet and remote control for the entire decentralized side of the Alith ecosystem.
How to Use the LazAI Client
Let's see how to use the Client
to manage your account. The first step is to initialize it with your private_key
.
A quick note on private keys: A private key is like the master password to your blockchain account. It should be kept extremely secret. We'll load it from an environment variable, which is a secure way to handle secrets in code.
Output:
Great! We've created our Client
and it's linked to our personal account on the blockchain.
Now, let's perform a simple "read" operation: checking our balance. This just asks the blockchain for information and doesn't cost anything.
Output:
Next, let's perform a "write" operation. We'll deposit some funds into the settlement contract, making them available to pay for AI services. This requires sending a transaction to the blockchain, which the Client
makes incredibly simple.
Output:
Behind the scenes, the Client
created a valid transaction, signed it with your private key, and broadcasted it to the network.
Finally, remember from earlier that paid requests need a signature? The Client
handles that too. The get_request_headers
method creates the special headers needed to prove you approve the payment, without sending a full transaction just yet.
Output:
You would then include these headers when sending a request to a paid AI Service Node.
Under the Hood: How a Transaction is Made
When you call a method like client.deposit(1000)
, how does the Client
turn that into a secure blockchain transaction?
- Prepare the Call: The
Client
identifies which smart contract to talk to (thesettlement_contract
) and which function to call (deposit
). - Build the Transaction: It constructs a transaction object, including the recipient (the contract's address), the value (
1000
), and other details like anonce
(a transaction counter to prevent replays). - Sign: It uses your secret
private_key
to create a unique digital signature for this exact transaction. This proves to the network that you authorized it. - Send: It broadcasts the signed transaction to a blockchain node.
- Confirm: The node validates the signature and, if valid, includes it in a new block, making the state change (your deposit) permanent. The
Client
then waits for this confirmation.
This flow ensures that only you can authorize actions from your account.
Diving into the Code
The power of the Client
comes from two key files: alith/lazai/client.py
and alith/lazai/chain.py
.
The Client
class itself is a high-level interface. Its __init__
method sets up connections to all the important smart contracts on the LazAI network. A contract's address
is its unique location, and its ABI
is like a menu of all its available functions.
This setup means self.settlement_contract
is now a ready-to-use Python object that represents the smart contract on the blockchain.
When you call a method like deposit
, it's a simple wrapper that calls the real workhorse: send_transaction
.
The send_transaction
method (located in the parent ChainManager
class) contains the core logic for signing and sending, as shown in our diagram.
This beautiful abstraction means that adding new blockchain interactions is as simple as creating a new wrapper method in the Client
class. All the difficult and repetitive parts are handled by send_transaction
.