import { VoteStaple, Vote, VoteBlockHashMap } from '../vote'; import { Block, BlockHash } from '../block'; import type { FeeAmountAndToken, VoteBlockHash, VoteQuote } from '../vote'; import type { AccountKeyAlgorithm, GenericAccount, IdentifierAddress, NetworkAddress, StorageAddress, TokenAddress } from '../account'; import Account from '../account'; import type Node from '../node'; import type { BloomFilter } from '../utils/bloom'; import type { ComputedEffectOfBlocks } from './effects'; import type { ACLRow, GetAllBalancesResponse, LedgerStatistics, CertificateWithIntermediates, AccountInfoForType, ACLPrincipalType } from './types'; import LedgerRequestCache from './cache'; import type { Logger } from '../log'; import type { CertificateHash } from '../utils/certificate'; import { StatsPending } from '../stats'; import { BufferStorage } from '../utils/buffer'; /** * Kind of ledger */ export declare enum LedgerKind { REPRESENTATIVE = 0, ACCOUNT = 1 } /** * Ledger configuration */ export interface LedgerConfig { initialTrustedAccount: Account; network: bigint; subnet?: bigint; /** * Kind of ledger */ kind: LedgerKind; /** * Private key for the ledger if it is acting as a representative */ privateKey?: Account; /** * Provided function to compute fees for a given set of Blocks and computed effects */ computeFeeFromBlocks: (ledger: Ledger, blocks: Block[], effects: ComputedEffectOfBlocks) => FeeAmountAndToken | FeeAmountAndToken[] | null; /** * Storage mechanism */ storageDriver: LedgerStorageAPI; /** * Options to pass to the storage driver */ storageOptions?: any; /** * Is this ledger in read-only mode ? * * bootstrap-only: Bootstrapping can still occur * read-only: No bootstrapping, no voting * read-write: Normal mode (read-write enabled) * no-voting: Normal mode (read-write enabled), but no voting */ ledgerWriteMode?: 'bootstrap-only' | 'read-only' | 'read-write' | 'no-voting'; /** * Timeout for storage operations */ transactionRetries?: ({ timeout?: number; maxRetries?: never; } | { timeout?: never; maxRetries?: number; }); /** * Logging method */ log?: Logger; /** * Operation specific parameters */ operations?: { setRep?: (account: GenericAccount, rep: Account | null) => Promise; enableTokenAdminModifyBalance?: boolean; }; } /** * Which ledger to store this data in */ export type LedgerStorage = 'main' | 'side'; /** * Which ledger(s) to pull the data from */ export type LedgerSelector = LedgerStorage | 'both'; /** * A set of votes and a pointer to the next set/page */ export type PaginatedVotes = { votes: Vote[]; nextKey?: string; }; /** * Options for "getVotesAfter" */ export type GetVotesAfterOptions = { /** * Limit the page size when requesting votes */ maxVotesPerPage?: number; /** * Bloom filter to apply to VoteStaples */ bloomFilter?: BloomFilter; /** * Timeout for fetching votes */ timeout?: number; }; /** * Filters for listing ACLs by entity */ export interface ListACLsByEntityFilters { /** * If provided, only return ACL rows of this type */ principalType?: ACLPrincipalType; } type IdempotentKeyString = string & { readonly __idempotentKey: never; }; /** * Idempotent Key */ export declare class IdempotentKey extends BufferStorage { readonly account: GenericAccount | undefined; readonly userIdempotent: Buffer | undefined; static readonly isInstance: (obj: any, strict?: boolean) => obj is IdempotentKey; static readonly Set: import("../utils/helper").InstanceSetConstructor; static fromAccountAndIdempotent(account: GenericAccount, idempotent: string | Buffer): IdempotentKey; constructor(idempotentKey: ConstructorParameters[0], account?: GenericAccount, idempotent?: Buffer); toJSON(): IdempotentKeyString; toString(): IdempotentKeyString; } /** * Each transaction can contain the node object making the request to access things like timing information */ export interface LedgerStorageTransactionBaseOptions { node?: Node; moment?: Date; identifier: string; readOnly?: boolean; } export declare class LedgerStorageTransactionBase implements LedgerStorageTransactionBaseOptions { node?: Node; moment: Date; identifier: string; readOnly: boolean; statsPending: StatsPending; constructor(options: LedgerStorageTransactionBaseOptions); } /** * Each Ledger Storage backend must implement this interface */ export interface LedgerStorageAPI { /** * Initialization */ init: (config: LedgerConfig, ledger: Ledger) => void; /** * Optional destructor */ destroy?: () => Promise; /** * Begin a transaction */ beginTransaction: (transactionBase: LedgerStorageTransactionBaseOptions) => Promise; /** * Commit an active transaction */ commitTransaction: (transaction: any) => Promise; /** * Abort an active transaction */ abortTransaction: (transaction: any) => Promise; /** * Evaluate error and return expected error (eg. KeetaNetLedgerError) * @param error */ evaluateError: (error: any) => Promise; /** * Get the amount of delegated weight for an account, * if "rep" is not supplied get the total delegated weight, if "rep" is of type Account.Set, return the sum of the weights for the provided account(s) */ delegatedWeight: (transaction: any, rep?: Account | InstanceType) => Promise; /** * Get the balance of an account or token */ getBalance: (transaction: any, account: GenericAccount, token: TokenAddress) => Promise; /** * Get all balances on a user account for a token */ getAllBalances: (transaction: any, account: GenericAccount) => Promise; /** * Get account information (name, description) * If token account, return supply * If non user account, returns default permissions */ getAccountInfo: (transaction: any, account: Account | string) => Promise>; /** * List all owners of an account */ listOwners: (transaction: any, identifier: IdentifierAddress, target?: GenericAccount) => Promise; /** * List permissions principal has on all provided entity's */ listACLsByPrincipal: (transaction: any, principal: ACLRow['principal'], entityList?: GenericAccount[]) => Promise; /** * List permissions any principal has on provided entity */ listACLsByEntity: (transaction: any, entity: GenericAccount, options?: ListACLsByEntityFilters) => Promise; /** * Adjust the ledger by performing a set of changes based on some blocks and votes */ adjust: (transaction: any, input: VoteStaple, changes: ComputedEffectOfBlocks) => Promise; /** * Add a transitional vote (to the side-ledger) */ addPendingVote: (transaction: any, blocksAndVote: VoteStaple) => Promise; /** * Request a block */ getBlock: (transaction: any, block: BlockHash, from: LedgerSelector) => Promise; /** * Get the block height from a given block hash */ getBlockHeight: (transaction: any, blockHash: BlockHash, account: GenericAccount) => Promise; /** * Get multiple block heights for a given set of block hashes and chains. */ getBlockHeights: (transaction: any, toFetch: { blockHash: BlockHash; account: GenericAccount; }[]) => Promise<{ [blockHash: string]: bigint | null; }>; /** * Get multiple block hashes and heights for a given set of chains and optional block hashes. * If no block hash is provided it returns the head block of the chain */ getAccountsBlockHeightInfo: (transaction: any, toFetch: { account: GenericAccount; blockHash?: BlockHash; }[]) => Promise<{ [account: string]: { blockHash: BlockHash; height: bigint | null; } | null; }>; /** * Get the votes for a given block */ getVotes: (transaction: any, block: BlockHash, from: LedgerSelector) => Promise; /** * Get a vote based on the previous hash of a block */ getVotesFromPrevious: (transaction: any, block: BlockHash, from: LedgerSelector) => Promise; /** * Get multiple votes based on the successors of provided block hashes (both sides) */ getVotesFromMultiplePrevious: (transaction: any, prevBlocks: BlockHash[], from: LedgerSelector, issuer?: Account) => Promise<{ [hash: string]: Vote[] | null; }>; /** * Get a vote staple from a Vote Block Hash, which uniquely identifies a set of blocks voted on together */ getVoteStaples: (transaction: any, voteBlockHashes: VoteBlockHash[], from: LedgerSelector) => Promise>; /** * Get the history for a specific account or all accounts if account is null */ getHistory: (transaction: any, account: GenericAccount | null, start: VoteBlockHash | null, limit?: number) => Promise; /** * Get multiple head blocks at the same time for a set of accounts */ getHeadBlocks: (transaction: any, accounts: GenericAccount[], from: LedgerSelector) => Promise<{ [account: string]: Block | null; }>; /** * Get multiple head block hashes at the same time for a set of accounts */ getHeadBlockHashes: (transaction: any, accounts: InstanceType) => Promise<{ [account: string]: BlockHash | null; }>; /** * Get the HEAD block for an account (implemented by LedgerStorageBase using getHeadBlocks()) */ getHeadBlock: (transaction: any, account: GenericAccount, from: LedgerSelector) => Promise; /** * Get a block based on the previous hash of a block */ getBlockFromPrevious: (transaction: any, block: BlockHash, from: LedgerSelector) => Promise; /** * Get the Account Representative */ getAccountRep: (transaction: any, account: GenericAccount | string) => Promise; /** * Get Votes after a specific moment */ getVotesAfter: (transaction: any, moment: Date, startKey?: string, options?: GetVotesAfterOptions) => Promise; /** * Get Vote Staples from a set of block hashes (optional) */ getVoteStaplesFromBlockHash?: (transaction: any, blocks: BlockHash[], from: LedgerSelector) => Promise; /** * Get the cache for a transaction (optional) */ cache?: (transaction: any) => LedgerRequestCache | undefined; /** * Perform Garbage Collection */ gc: (transaction: any, timeLimitMS?: number) => Promise; /** * Get the next serial number for a representative */ getNextSerialNumber: (transaction?: any) => Promise; /** * Get the X.509 Certificates associated with an account */ getAccountCertificates: (transaction: any, account: GenericAccount) => Promise; /** * Get the X.509 Certificate associated with an account using the certificate hash */ getAccountCertificateByHash: (transaction: any, account: GenericAccount, hash: CertificateHash) => Promise; /** * Get block hash from idempotent key */ getIdempotentBlockHash: (transaction: any, idempotent: IdempotentKey, from: LedgerSelector, excludeBlockHash?: BlockHash) => Promise; /** * Get ledger statistics */ stats: () => Promise; } /** * Atomic transactional interface to a storage backend */ declare class LedgerAtomicInterface { #private; constructor(transaction: LedgerStorageTransactionBase, storage: LedgerStorageAPI, config: LedgerConfig, ledger: Ledger); commit(): Promise; abort(): Promise; vote(blocks: Block[], otherVotes?: Vote[], quote?: VoteQuote): Promise; quote(blocks: Block[]): Promise; add(votesAndBlocks: VoteStaple, from?: 'bootstrap' | string): Promise; getBalance(account: GenericAccount, token: TokenAddress): Promise; getAllBalances(account: GenericAccount): Promise; getAccountCertificates(account: GenericAccount): Promise; getAccountCertificateByHash(account: GenericAccount, hash: CertificateHash): Promise; listACLsByPrincipal(principal: ACLRow['principal'], entityList?: GenericAccount[]): Promise; listACLsByEntity(entity: GenericAccount, options?: ListACLsByEntityFilters): Promise; votingPower(rep?: Account): Promise; getVotes(block: BlockHash, from?: LedgerStorage): Promise; getVotesFromMultiplePrevious(prevBlocks: BlockHash[], from?: LedgerSelector, issuer?: Account): Promise<{ [hash: string]: Vote[] | null; }>; getBlockFromPrevious(block: BlockHash, from: LedgerSelector): Promise; getHeadBlocks(accounts: GenericAccount[], from: LedgerSelector): Promise<{ [account: string]: Block | null; }>; getHeadBlock(account: GenericAccount, from: LedgerSelector): Promise; getAccountRep(account: GenericAccount | string): Promise; getAccountInfo(account: Account | string): Promise>; getBlock(blockhash: BlockHash, from?: LedgerSelector): Promise; getAccountsBlockHeightInfo(toFetch: { account: GenericAccount; blockHash?: BlockHash; }[]): Promise<{ [account: string]: { blockHash: BlockHash; height: bigint | null; } | null; }>; getVoteStaple(stapleBlockHash: VoteBlockHash, from?: LedgerSelector): Promise; getVoteStaples(stapleBlockHashes: VoteBlockHash[], from?: LedgerSelector): Promise>; getHistory(account: GenericAccount | null, start: VoteBlockHash | null, limit?: number): Promise; getStaplesFromBlockHashes(hashes: BlockHash[]): Promise; getVoteStaplesAfter(moment: Date, limit?: number, options?: GetVotesAfterOptions): Promise; gc(timeLimitMS?: number): Promise; getFee(blocks: Block[], effectsInput?: ComputedEffectOfBlocks): Promise; getIdempotentBlockHash(account: GenericAccount, idempotent: string | Buffer, from?: LedgerSelector, excludeBlockHash?: BlockHash): Promise; getBlockFromIdempotent(account: GenericAccount, idempotent: string | Buffer, from?: LedgerSelector, excludeBlockHash?: BlockHash): Promise; _testingRunStorageFunction(code: (storage: LedgerStorageAPI, transaction: LedgerStorageTransactionBase) => Promise): Promise; } /** * The core Ledger components */ export declare class Ledger implements Omit { #private; static Kind: typeof LedgerKind; readonly baseToken: TokenAddress; readonly networkAddress: NetworkAddress; readonly initialTrustedAccount: Account; readonly node?: Node; static isInstance: (obj: any, strict?: boolean) => obj is Ledger; constructor(config: LedgerConfig, node?: Node, existingStorage?: LedgerStorageAPI); get ledgerWriteMode(): NonNullable; copy(newNode: Node): Ledger; getFeePayToAndToken(accounts?: (Account | StorageAddress)[], token?: TokenAddress): Partial; /** * Execute some code with a transaction held, if the code fails the * transaction is aborted, otherwise it is committed * * @param code - Code to run * @returns The return value from "code" */ run(identifier: string, code: (transaction: LedgerAtomicInterface) => Promise, readOnly?: boolean): Promise; runReadOnly(identifier: string, code: (transaction: LedgerAtomicInterface) => Promise): ReturnType; beginTransaction(identifier: string, readOnly?: boolean): Promise; vote(...args: Parameters): ReturnType; quote(...args: Parameters): ReturnType; add(...args: Parameters): ReturnType; listACLsByPrincipal(...args: Parameters): ReturnType; listACLsByEntity(...args: Parameters): ReturnType; getBalance(...args: Parameters): ReturnType; getAllBalances(...args: Parameters): ReturnType; getAccountCertificates(...args: Parameters): ReturnType; getAccountCertificateByHash(...args: Parameters): ReturnType; votingPower(...args: Parameters): ReturnType; getVotes(...args: Parameters): ReturnType; getVotesFromMultiplePrevious(...args: Parameters): ReturnType; getBlockFromPrevious(...args: Parameters): ReturnType; getHeadBlocks(...args: Parameters): ReturnType; getHeadBlock(...args: Parameters): ReturnType; getAccountRep(...args: Parameters): ReturnType; getAccountInfo(account: Account | string): Promise>; getBlock(...args: Parameters): ReturnType; getAccountsBlockHeightInfo(...args: Parameters): ReturnType; getVoteStaple(...args: Parameters): ReturnType; getVoteStaples(...args: Parameters): ReturnType; getHistory(...args: Parameters): ReturnType; getStaplesFromBlockHashes(...args: Parameters): ReturnType; getVoteStaplesAfter(...args: Parameters): ReturnType; gc(...args: Parameters): ReturnType; getFee(...args: Parameters): ReturnType; getIdempotentBlockHash(...args: Parameters): ReturnType; getBlockFromIdempotent(...args: Parameters): ReturnType; stats(): Promise; _testingRunStorageFunction(code: (storage: LedgerStorageAPI, transaction: LedgerStorageTransactionBase) => Promise): Promise; } export default Ledger;