Skip to content

Commit

Permalink
feat: dynamic flyway for epoch store
Browse files Browse the repository at this point in the history
  • Loading branch information
Sotatek-QuanLeA committed May 29, 2024
1 parent 1d08342 commit efb67f2
Show file tree
Hide file tree
Showing 24 changed files with 324 additions and 285 deletions.
1 change: 1 addition & 0 deletions application/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies {
implementation(libs.yaci.store.governance.starter)
implementation(libs.yaci.store.starter.assets)
implementation(libs.yaci.store.starter.block)
implementation(libs.yaci.store.starter.epoch)
implementation(libs.yaci.store.starter.metadata)

implementation(libs.cardano.client.lib)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public Flyway flywayConfig() throws SQLException {
locations.add("classpath:db/migration/ledgersync/blocks");
}

if (!storeProperties.getEpoch().isEnabled()) {
locations.add("classpath:db/migration/ledgersync/epoch");
}

Flyway flyway = Flyway.configure()
.dataSource(dataSource)
.sqlMigrationPrefix(flywayProperties.getSqlMigrationPrefix())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class StoreProperties {
private Assets assets = new Assets();
private Metadata metadata = new Metadata();
private Blocks blocks = new Blocks();
private Epoch epoch = new Epoch();

@Getter
@Setter
Expand All @@ -30,5 +31,11 @@ public static final class Metadata {
private boolean enabled = true;
}

@Getter
@Setter
public static final class Epoch {
private boolean enabled = true;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.Set;

@Repository
public interface CostModelRepository extends JpaRepository<CostModel, Long> {
public interface CostModelRepositoryLS extends JpaRepository<CostModel, Long> {

@Query("SELECT MAX(c.id) FROM CostModel c")
Optional<Long> findCostModeMaxId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.Collection;
import java.util.Optional;

public interface EpochParamRepository extends JpaRepository<EpochParam, Long> {
public interface EpochParamRepositoryLS extends JpaRepository<EpochParam, Long> {

@Query(value = "SELECT ep from EpochParam ep"
+ " WHERE ep.epochNo = (SELECT MAX(e.epochNo) FROM EpochParam e)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.cardanofoundation.ledgersync.common.common.cost.mdl.PlutusV2Keys;
import org.cardanofoundation.ledgersync.common.util.JsonUtil;
import org.cardanofoundation.ledgersync.consumercommon.entity.CostModel;
import org.cardanofoundation.ledgersync.repository.CostModelRepository;
import org.cardanofoundation.ledgersync.repository.CostModelRepositoryLS;
import org.cardanofoundation.ledgersync.service.CostModelService;
import org.cardanofoundation.ledgersync.service.impl.plutus.PlutusKey;
import org.springframework.stereotype.Service;
Expand All @@ -36,7 +36,7 @@
@FieldDefaults(level = AccessLevel.PRIVATE)
public class CostModelServiceImpl implements CostModelService {

final CostModelRepository costModelRepository;
final CostModelRepositoryLS costModelRepositoryLS;

@Override
public void handleCostModel(AggregatedTx tx) {
Expand Down Expand Up @@ -83,16 +83,16 @@ public void handleCostModel(AggregatedTx tx) {
, (past, future) -> future));

if (!ObjectUtils.isEmpty(costModels)) {
costModelRepository.existHash(
costModelRepositoryLS.existHash(
costModels.keySet())
.forEach(costModels::remove);
costModelRepository.saveAll(costModels.values());
costModelRepositoryLS.saveAll(costModels.values());
}
}

@Override
public CostModel findCostModelByHash(String hash) {
var costModelOptional = costModelRepository.findByHash(hash);
var costModelOptional = costModelRepositoryLS.findByHash(hash);
return costModelOptional.orElse(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public class EpochParamServiceImpl implements EpochParamService {

final BlockRepositoryLS blockRepositoryLS;
final ParamProposalRepository paramProposalRepository;
final EpochParamRepository epochParamRepository;
final EpochParamRepositoryLS epochParamRepository;
final EpochRepository epochRepository;
final CostModelRepository costModelRepository;
final CostModelRepositoryLS costModelRepository;
// final CostModelService costModelService;
final GenesisDataService genesisDataService;
final EpochParamMapper epochParamMapper;
Expand All @@ -44,8 +44,8 @@ public class EpochParamServiceImpl implements EpochParamService {
EpochParam defConwayEpochParam;

public EpochParamServiceImpl(BlockRepositoryLS blockRepositoryLS, ParamProposalRepository paramProposalRepository,
EpochParamRepository epochParamRepository, EpochRepository epochRepository,
CostModelRepository costModelRepository,
EpochParamRepositoryLS epochParamRepository, EpochRepository epochRepository,
CostModelRepositoryLS costModelRepository,
@Lazy GenesisDataService genesisDataService,
EpochParamMapper epochParamMapper, ObjectMapper objectMapper) {
this.blockRepositoryLS = blockRepositoryLS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ private void handleTxContents(Collection<AggregatedTx> successTxs,
}

//param proposal
paramProposalService.handleParamProposals(successTxs, txMap);
if (!storeProperties.getEpoch().isEnabled()) {
paramProposalService.handleParamProposals(successTxs, txMap);
}

// reference inputs
referenceInputService.handleReferenceInputs(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.extern.slf4j.Slf4j;
import org.cardanofoundation.ledgersync.aggregate.AggregatedBlock;
import org.cardanofoundation.ledgersync.aggregate.AggregatedSlotLeader;
import org.cardanofoundation.ledgersync.configuration.StoreProperties;
import org.cardanofoundation.ledgersync.consumercommon.entity.Block;
import org.cardanofoundation.ledgersync.consumercommon.entity.SlotLeader;
import org.cardanofoundation.ledgersync.repository.BlockRepositoryLS;
Expand Down Expand Up @@ -35,6 +36,8 @@ public class BlockSyncServiceImpl implements BlockSyncService {
MetricCollectorService metricCollectorService;
AggregatedDataCachingService aggregatedDataCachingService;

StoreProperties storeProperties;

@Override
@Transactional
@Timed(value = "consumer.block.processing.timer", description = "Time spent syncing blocks")
Expand Down Expand Up @@ -74,7 +77,9 @@ public void handleBlockSync() {
epochService.handleEpoch(allAggregatedBlocks);

// Handle epoch param
epochParamService.handleEpochParams();
if (!storeProperties.getEpoch().isEnabled()) {
epochParamService.handleEpochParams();
}

// Cache latest txs
aggregatedDataCachingService.saveLatestTxs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class RollbackServiceImpl implements RollbackService {
FailedTxOutRepository failedTxOutRepository;
MaTxMintRepository maTxMintRepository;
MultiAssetTxOutRepository multiAssetTxOutRepository;
EpochParamRepository epochParamRepository;
EpochParamRepositoryLS epochParamRepositoryLS;
ParamProposalRepository paramProposalRepository;
PoolMetadataRefRepository poolMetadataRefRepository;
PoolOwnerRepository poolOwnerRepository;
Expand Down Expand Up @@ -134,7 +134,7 @@ private void removeAllRollbackBlockData(List<Block> rollbackBlocks) {
failedTxOutRepository.deleteAllByTxIn(txsForRollback);
maTxMintRepository.deleteAllByTxIn(txsForRollback);
multiAssetTxOutRepository.deleteAllByTxOutTxIn(txsForRollback);
epochParamRepository.deleteAllByBlockIn(rollbackBlocks);
epochParamRepositoryLS.deleteAllByBlockIn(rollbackBlocks);
paramProposalRepository.deleteAllByRegisteredTxIn(txsForRollback);
poolMetadataRefRepository.deleteAllByRegisteredTxIn(txsForRollback);
poolOwnerRepository.deleteAllByPoolUpdateRegisteredTxIn(txsForRollback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.cardanofoundation.ledgersync.aggregate.*;
import org.cardanofoundation.ledgersync.common.common.Era;
import org.cardanofoundation.ledgersync.common.util.HexUtil;
import org.cardanofoundation.ledgersync.configuration.StoreProperties;
import org.cardanofoundation.ledgersync.consumercommon.entity.*;
import org.cardanofoundation.ledgersync.consumercommon.enumeration.TokenType;
import org.cardanofoundation.ledgersync.converter.AvvmAddressConverter;
Expand Down Expand Up @@ -153,6 +154,7 @@ public class GenesisDataServiceImpl implements GenesisDataService {
final CostModelService costModelService;
final EpochParamService epochParamService;
final GenesisFetching genesisFetching;
final StoreProperties storeProperties;

@PostConstruct
void init(){
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@ CREATE TABLE IF NOT EXISTS ada_pots
);


--
-- Name: cost_model; Type: TABLE;
--

CREATE TABLE IF NOT EXISTS cost_model
(
id bigint NOT NULL,
costs text NOT NULL,
hash character varying(64) NOT NULL
);

--
-- Name: datum; Type: TABLE;
Expand Down Expand Up @@ -82,68 +72,6 @@ CREATE TABLE IF NOT EXISTS epoch
);


--
-- Name: epoch_param; Type: TABLE;
--

CREATE TABLE IF NOT EXISTS epoch_param
(
id bigint NOT NULL,
coins_per_utxo_size numeric(20, 0),
collateral_percent integer,
decentralisation double precision NOT NULL,
epoch_no integer NOT NULL,
extra_entropy character varying(64),
influence double precision NOT NULL,
key_deposit numeric(20, 0) NOT NULL,
max_bh_size integer NOT NULL,
max_block_ex_mem numeric(20, 0),
max_block_ex_steps numeric(20, 0),
max_block_size integer NOT NULL,
max_collateral_inputs integer,
max_epoch integer NOT NULL,
max_tx_ex_mem numeric(20, 0),
max_tx_ex_steps numeric(20, 0),
max_tx_size integer NOT NULL,
max_val_size numeric(20, 0),
min_fee_a integer NOT NULL,
min_fee_b integer NOT NULL,
min_pool_cost numeric(20, 0) NOT NULL,
min_utxo_value numeric(20, 0),
monetary_expand_rate double precision NOT NULL,
nonce character varying(64),
optimal_pool_count integer NOT NULL,
pool_deposit numeric(20, 0) NOT NULL,
price_mem double precision,
price_step double precision,
protocol_major integer NOT NULL,
protocol_minor integer NOT NULL,
treasury_growth_rate double precision NOT NULL,
block_id bigint NOT NULL,
cost_model_id bigint,
pvt_motion_no_confidence double precision,
pvt_commit_normal double precision,
pvt_committee_no_confidence double precision,
pvt_hard_fork_initiation double precision,
pvt_p_p_security_group double precision,
pvt_p_p_technical_group double precision,
pvt_p_p_gov_group double precision,
pvt_treasury_withdrawal double precision,
dvt_motion_no_confidence double precision,
dvt_commitee_normal double precision,
dvt_committee_no_confidence double precision,
dvt_update_to_constitution double precision,
dvt_hard_fork_initiation double precision,
dvt_p_p_network_group double precision,
dvt_p_p_economic_group double precision,
committee_min_size numeric(20, 0),
committee_max_term_length numeric(20, 0),
gov_action_lifetime numeric(20, 0),
gov_action_deposit numeric(20, 0),
drep_deposit numeric(20, 0),
drep_activity numeric(20, 0),
min_fee_ref_script_cost_per_byte numeric(20, 0)
);

--
-- Name: epoch_stake; Type: TABLE;
Expand Down Expand Up @@ -230,69 +158,6 @@ CREATE TABLE IF NOT EXISTS meta



--
-- Name: param_proposal; Type: TABLE;
--

CREATE TABLE IF NOT EXISTS param_proposal
(
id bigint NOT NULL,
coins_per_utxo_size numeric(19, 2),
collateral_percent integer,
decentralisation double precision,
entropy character varying(64),
epoch_no integer NOT NULL,
influence double precision,
key character varying(56) NOT NULL,
key_deposit numeric(19, 2),
max_bh_size numeric(20, 0),
max_block_ex_mem numeric(20, 0),
max_block_ex_steps numeric(20, 0),
max_block_size numeric(20, 0),
max_collateral_inputs integer,
max_epoch numeric(20, 0),
max_tx_ex_mem numeric(20, 0),
max_tx_ex_steps numeric(20, 0),
max_tx_size numeric(20, 0),
max_val_size numeric(20, 0),
min_fee_a numeric(20, 0),
min_fee_b numeric(20, 0),
min_pool_cost numeric(20, 0),
min_utxo_value numeric(20, 0),
monetary_expand_rate double precision,
optimal_pool_count numeric(20, 0),
pool_deposit numeric(20, 0),
price_mem double precision,
price_step double precision,
protocol_major integer,
protocol_minor integer,
treasury_growth_rate double precision,
cost_model_id bigint,
registered_tx_id bigint NOT NULL,
pvt_motion_no_confidence double precision,
pvt_commit_normal double precision,
pvt_committee_no_confidence double precision,
pvt_hard_fork_initiation double precision,
pvt_p_p_security_group double precision,
pvt_p_p_technical_group double precision,
pvt_p_p_gov_group double precision,
pvt_treasury_withdrawal double precision,
dvt_motion_no_confidence double precision,
dvt_commitee_normal double precision,
dvt_committee_no_confidence double precision,
dvt_update_to_constitution double precision,
dvt_hard_fork_initiation double precision,
dvt_p_p_network_group double precision,
dvt_p_p_economic_group double precision,
committee_min_size numeric(20, 0),
committee_max_term_length numeric(20, 0),
gov_action_lifetime numeric(20, 0),
gov_action_deposit numeric(20, 0),
drep_deposit numeric(20, 0),
drep_activity numeric(20, 0),
min_fee_ref_script_cost_per_byte numeric(20, 0)
);

--
-- Name: pool_hash; Type: TABLE;
--
Expand Down
Loading

0 comments on commit efb67f2

Please sign in to comment.