transaction Class
namespace keychain
→ class transaction
Overview
The transaction
class represents consensus-based ledger transactions with cryptographic proofs, enabling distributed agreement and immutable record-keeping across network participants.
Class Hierarchy
namespace keychain {
class transaction : public verifiable {
// Consensus-based ledger transaction
};
}
Constructor
transaction();
transaction(const std::string& transaction_id);
transaction(const transaction& other);
Properties
Transaction Identity
std::string get_transaction_id() const;
void set_transaction_id(const std::string& id);
std::string get_hash() const;
Unique identifier and cryptographic hash of the transaction.
Consensus Configuration
consensus_algorithm get_consensus_algorithm() const;
void set_consensus_algorithm(consensus_algorithm algorithm);
consensus_stage get_consensus_stage() const;
Consensus mechanism and current processing stage.
Transaction Data
serialized_data get_payload() const;
void set_payload(const serialized_data& data);
std::vector<std::string> get_participants() const;
void add_participant(const std::string& participant_did);
Transaction payload and participating entities.
Methods
Consensus Operations
void submit_to_consensus();
bool wait_for_confirmation(std::chrono::milliseconds timeout = std::chrono::milliseconds(30000));
bool is_confirmed() const;
bool is_finalized() const;
Submit transaction to consensus and monitor progress.
Usage Examples
Creating and Submitting Transaction
#include <keychain/keychain.h>
// Create transaction
keychain::transaction tx;
tx.set_transaction_id("tx_" + std::to_string(std::time(nullptr)));
tx.set_consensus_algorithm(keychain::consensus_algorithm::PROOF_OF_STAKE);
tx.set_ledger_id("keychain_network");
// Set transaction payload
keychain::serialized_data payload;
payload.set_value("Transfer 100 tokens from Alice to Bob");
tx.set_payload(payload);
// Add participants
tx.add_participant("did:keychain:alice.personal");
tx.add_participant("did:keychain:bob.work");
// Sign transaction
keychain::persona alice = gateway.get_persona("alice", "personal");
auto signed_tx = gateway.sign_transaction(alice, tx);
// Submit to consensus
signed_tx.submit_to_consensus();
// Monitor progress
std::cout << "Transaction submitted: " << signed_tx.get_transaction_id() << std::endl;
Monitoring Transaction Status
void monitor_transaction_progress(keychain::transaction& tx) {
std::cout << "Monitoring transaction: " << tx.get_transaction_id() << std::endl;
while (true) {
auto stage = tx.get_consensus_stage();
switch (stage) {
case keychain::consensus_stage::PENDING:
std::cout << "Status: Pending in mempool" << std::endl;
break;
case keychain::consensus_stage::VALIDATING:
std::cout << "Status: Being validated by consensus" << std::endl;
break;
case keychain::consensus_stage::CONFIRMED:
std::cout << "Status: Confirmed in block "
<< tx.get_block_number() << std::endl;
break;
case keychain::consensus_stage::FINALIZED:
std::cout << "Status: Finalized - transaction complete!" << std::endl;
return;
case keychain::consensus_stage::REJECTED:
std::cout << "Status: Rejected" << std::endl;
auto errors = tx.get_error_details();
for (const auto& error : errors) {
std::cout << " Error: " << error << std::endl;
}
return;
}
std::this_thread::sleep_for(std::chrono::seconds(2));
}
}
Transaction Verification
bool verify_transaction_integrity(const keychain::transaction& tx) {
// Verify all signatures
auto signature_results = tx.verify_signatures();
bool signatures_valid = std::all_of(signature_results.begin(), signature_results.end(),
[](const keychain::verification_result& result) {
return result.is_verified();
});
if (!signatures_valid) {
std::cout << "Transaction signature verification failed" << std::endl;
return false;
}
// Verify consensus proof if available
if (tx.is_confirmed() && !tx.verify_consensus_proof()) {
std::cout << "Consensus proof verification failed" << std::endl;
return false;
}
// Check transaction result
auto result = tx.get_result();
if (result != keychain::ledger_result::SUCCESS) {
std::cout << "Transaction failed with result: " << static_cast<int>(result) << std::endl;
return false;
}
std::cout << "Transaction verification successful" << std::endl;
return true;
}
Multi-Party Transaction
// Create multi-party transaction requiring multiple signatures
keychain::transaction multi_party_tx;
multi_party_tx.set_transaction_id("multisig_" + generate_uuid());
multi_party_tx.set_consensus_algorithm(keychain::consensus_algorithm::PRACTICAL_BYZANTINE_FAULT_TOLERANCE);
// Set complex payload
keychain::serialized_data contract_data;
contract_data.set_value(load_contract_terms("partnership_agreement.json"));
multi_party_tx.set_payload(contract_data);
// Add all required participants
std::vector<std::string> participants = {
"did:keychain:alice.legal",
"did:keychain:bob.finance",
"did:keychain:carol.operations"
};
for (const auto& participant : participants) {
multi_party_tx.add_participant(participant);
}
// Each participant signs the transaction
keychain::transaction signed_by_alice = gateway.sign_transaction(alice, multi_party_tx);
keychain::transaction signed_by_bob = gateway.sign_transaction(bob, signed_by_alice);
keychain::transaction final_tx = gateway.sign_transaction(carol, signed_by_bob);
// Submit when all signatures collected
if (final_tx.verify_signatures().size() == participants.size()) {
final_tx.submit_to_consensus();
if (final_tx.wait_for_confirmation()) {
std::cout << "Multi-party transaction confirmed!" << std::endl;
}
}
Transaction Lifecycle
stateDiagram-v2
[*] --> Created : new transaction()
Created --> Signed : add signatures
Signed --> Submitted : submit_to_consensus()
Submitted --> Pending : enters mempool
Pending --> Validating : picked by consensus
Validating --> Confirmed : consensus reached
Validating --> Rejected : validation failed
Confirmed --> Finalized : finality achieved
Rejected --> [*]
Finalized --> [*]
Consensus Algorithm Comparison
Algorithm | Confirmation Time | Finality | Energy Use | Throughput |
---|---|---|---|---|
Proof of Work |
~10 minutes |
~1 hour |
Very High |
7 TPS |
Proof of Stake |
~12 seconds |
~6 minutes |
Low |
15 TPS |
Delegated PoS |
~3 seconds |
~3 seconds |
Very Low |
3000 TPS |
Proof of Authority |
~5 seconds |
Immediate |
Minimal |
1000 TPS |
pBFT |
~1 second |
Immediate |
Low |
1000 TPS |
Error Handling
// Handle transaction errors
void handle_transaction_result(const keychain::transaction& tx) {
auto result = tx.get_result();
switch (result) {
case keychain::ledger_result::SUCCESS:
std::cout << "Transaction successful" << std::endl;
break;
case keychain::ledger_result::INSUFFICIENT_FUNDS:
std::cout << "Transaction failed: Insufficient funds" << std::endl;
break;
case keychain::ledger_result::INVALID_SIGNATURE:
std::cout << "Transaction failed: Invalid signature" << std::endl;
break;
case keychain::ledger_result::CONSENSUS_TIMEOUT:
std::cout << "Transaction failed: Consensus timeout" << std::endl;
break;
default:
auto errors = tx.get_error_details();
std::cout << "Transaction failed with errors:" << std::endl;
for (const auto& error : errors) {
std::cout << " - " << error << std::endl;
}
break;
}
}
Related Classes
-
consensus_algorithm - Consensus mechanisms
-
consensus_stage - Transaction stages
-
ledger_result - Transaction results
-
attestation - Transaction signatures
See Also
-
gateway - Transaction operations
-
verifiable_data - Verifiable data containers
-
{blockchain-fundamentals}[Blockchain Fundamentals]
-
{consensus-algorithms}[Consensus Algorithm Survey]