prover.rs
1 // Copyright (c) 2025 ADnet Contributors 2 // This file is part of the AlphaOS library. 3 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at: 7 8 // http://www.apache.org/licenses/LICENSE-2.0 9 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 use crate::LedgerService; 17 use alphavm::{ 18 ledger::{ 19 Block, 20 PendingBlock, 21 Transaction, 22 committee::Committee, 23 narwhal::{BatchCertificate, Data, Subdag, Transmission, TransmissionID}, 24 puzzle::{Solution, SolutionID}, 25 }, 26 prelude::{Address, ConsensusVersion, Field, Network, Result, Zero, bail}, 27 }; 28 29 use indexmap::IndexMap; 30 use std::ops::Range; 31 32 /// A ledger service for a prover. 33 #[derive(Clone, Debug, Default)] 34 pub struct ProverLedgerService<N: Network> { 35 _network: std::marker::PhantomData<N>, 36 } 37 38 impl<N: Network> ProverLedgerService<N> { 39 /// Initializes a new prover ledger service. 40 pub fn new() -> Self { 41 Self { _network: Default::default() } 42 } 43 } 44 45 #[async_trait] 46 impl<N: Network> LedgerService<N> for ProverLedgerService<N> { 47 /// Returns the latest round in the ledger. 48 fn latest_round(&self) -> u64 { 49 0u64 50 } 51 52 /// Returns the latest block height in the ledger. 53 fn latest_block_height(&self) -> u32 { 54 0u32 55 } 56 57 /// Returns the latest block in the ledger. 58 fn latest_block(&self) -> Block<N> { 59 unreachable!("Latest block does not exist in prover") 60 } 61 62 /// Returns the latest restrictions ID in the ledger. 63 fn latest_restrictions_id(&self) -> Field<N> { 64 Field::zero() 65 } 66 67 /// Returns the latest cached leader and its associated round. 68 fn latest_leader(&self) -> Option<(u64, Address<N>)> { 69 unreachable!("Latest leader does not exist in prover"); 70 } 71 72 /// Updates the latest cached leader and its associated round. 73 fn update_latest_leader(&self, _round: u64, _leader: Address<N>) { 74 unreachable!("Latest leader does not exist in prover"); 75 } 76 77 /// Returns `true` if the given block height exists in the ledger. 78 fn contains_block_height(&self, _height: u32) -> bool { 79 false 80 } 81 82 /// Returns the block height for the given block hash, if it exists. 83 fn get_block_height(&self, hash: &N::BlockHash) -> Result<u32> { 84 bail!("Block hash '{hash}' does not exist in prover") 85 } 86 87 /// Returns the block hash for the given block height, if it exists. 88 fn get_block_hash(&self, height: u32) -> Result<N::BlockHash> { 89 bail!("Block {height} does not exist in prover") 90 } 91 92 /// Returns the block round for the given block height, if it exists. 93 fn get_block_round(&self, height: u32) -> Result<u64> { 94 bail!("Block {height} does not exist in prover") 95 } 96 97 /// Returns the block for the given block height. 98 fn get_block(&self, height: u32) -> Result<Block<N>> { 99 bail!("Block {height} does not exist in prover") 100 } 101 102 /// Returns the blocks in the given block range. 103 /// The range is inclusive of the start and exclusive of the end. 104 fn get_blocks(&self, heights: Range<u32>) -> Result<Vec<Block<N>>> { 105 bail!("Blocks {heights:?} do not exist in prover") 106 } 107 108 /// Returns the solution for the given solution ID. 109 fn get_solution(&self, solution_id: &SolutionID<N>) -> Result<Solution<N>> { 110 bail!("Solution '{solution_id}' does not exist in prover") 111 } 112 113 /// Returns the unconfirmed transaction for the given transaction ID. 114 fn get_unconfirmed_transaction(&self, transaction_id: N::TransactionID) -> Result<Transaction<N>> { 115 bail!("Transaction '{transaction_id}' does not exist in prover") 116 } 117 118 /// Returns the batch certificate for the given batch certificate ID. 119 fn get_batch_certificate(&self, certificate_id: &Field<N>) -> Result<BatchCertificate<N>> { 120 bail!("Batch certificate '{certificate_id}' does not exist in prover") 121 } 122 123 /// Returns the current committee. 124 fn current_committee(&self) -> Result<Committee<N>> { 125 bail!("Committee does not exist in prover") 126 } 127 128 /// Returns the committee for the given round. 129 fn get_committee_for_round(&self, round: u64) -> Result<Committee<N>> { 130 bail!("Committee for round {round} does not exist in prover") 131 } 132 133 /// Returns the committee lookback for the given round. 134 fn get_committee_lookback_for_round(&self, round: u64) -> Result<Committee<N>> { 135 bail!("Previous committee for round {round} does not exist in prover") 136 } 137 138 /// Returns `true` if the ledger contains the given certificate ID in block history. 139 fn contains_certificate(&self, certificate_id: &Field<N>) -> Result<bool> { 140 bail!("Certificate '{certificate_id}' does not exist in prover") 141 } 142 143 /// Returns `true` if the transmission exists in the ledger. 144 fn contains_transmission(&self, transmission_id: &TransmissionID<N>) -> Result<bool> { 145 bail!("Transmission '{transmission_id}' does not exist in prover") 146 } 147 148 /// Ensures that the given transmission is not a fee and matches the given transmission ID. 149 fn ensure_transmission_is_well_formed( 150 &self, 151 _transmission_id: TransmissionID<N>, 152 _transmission: &mut Transmission<N>, 153 ) -> Result<()> { 154 Ok(()) 155 } 156 157 /// Checks the given solution is well-formed. 158 async fn check_solution_basic(&self, _solution_id: SolutionID<N>, _solution: Data<Solution<N>>) -> Result<()> { 159 Ok(()) 160 } 161 162 /// Checks the given transaction is well-formed and unique. 163 async fn check_transaction_basic( 164 &self, 165 _transaction_id: N::TransactionID, 166 _transaction: Transaction<N>, 167 ) -> Result<()> { 168 Ok(()) 169 } 170 171 fn check_block_subdag(&self, _block: Block<N>, _prefix: &[PendingBlock<N>]) -> Result<PendingBlock<N>> { 172 bail!("Cannot check block subDAG in prover") 173 } 174 175 fn check_block_content(&self, _pending_block: PendingBlock<N>) -> Result<Block<N>> { 176 bail!("Cannot check block content in prover") 177 } 178 179 /// Checks the given block is valid next block. 180 fn check_next_block(&self, _block: &Block<N>) -> Result<()> { 181 Ok(()) 182 } 183 184 /// Returns a candidate for the next block in the ledger, using a committed subdag and its transmissions. 185 #[cfg(feature = "ledger-write")] 186 fn prepare_advance_to_next_quorum_block( 187 &self, 188 _subdag: Subdag<N>, 189 _transmissions: IndexMap<TransmissionID<N>, Transmission<N>>, 190 ) -> Result<Block<N>> { 191 bail!("Cannot prepare advance to next quorum block in prover") 192 } 193 194 /// Adds the given block as the next block in the ledger. 195 #[cfg(feature = "ledger-write")] 196 fn advance_to_next_block(&self, block: &Block<N>) -> Result<()> { 197 bail!("Cannot advance to next block in prover - {block}") 198 } 199 200 /// Returns the spent cost for a transaction in microcredits. 201 fn transaction_spend_in_microcredits( 202 &self, 203 _transaction: &Transaction<N>, 204 _consensus_version: ConsensusVersion, 205 ) -> Result<u64> { 206 bail!("Cannot compute spent_cost in prover") 207 } 208 }