Skip to content

Commit

Permalink
Merge pull request #18 from anoma/tiago/rename-tx-notes-map
Browse files Browse the repository at this point in the history
Rename notes map to index
  • Loading branch information
sug0 committed Aug 13, 2024
2 parents bafd149 + 0026f09 commit 17ac659
Show file tree
Hide file tree
Showing 27 changed files with 137 additions and 134 deletions.
2 changes: 1 addition & 1 deletion chain/src/entity/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod chain_state;
pub mod commitment_tree;
pub mod tx_note_map;
pub mod tx_notes_index;
pub mod witness_map;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::BTreeMap;

use orm::notes_map::NotesMapInsertDb;
use orm::notes_index::NotesIndexInsertDb;
use shared::indexed_tx::IndexedTx;

#[derive(Default, Clone, Debug)]
Expand All @@ -20,7 +20,7 @@ impl TxNoteMap {
self.0.is_empty()
}

pub fn into_db(&self) -> Vec<NotesMapInsertDb> {
pub fn into_db(&self) -> Vec<NotesIndexInsertDb> {
self.0
.iter()
.map(
Expand All @@ -31,7 +31,7 @@ impl TxNoteMap {
masp_tx_index,
},
&(is_fee_unshielding, note_pos),
)| NotesMapInsertDb {
)| NotesIndexInsertDb {
is_fee_unshielding,
block_index: block_index.0 as i32,
note_position: note_pos as i32,
Expand Down
8 changes: 4 additions & 4 deletions chain/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::appstate::AppState;
use crate::config::AppConfig;
use crate::entity::chain_state::ChainState;
use crate::entity::commitment_tree::CommitmentTree;
use crate::entity::tx_note_map::TxNoteMap;
use crate::entity::tx_notes_index::TxNoteMap;
use crate::entity::witness_map::WitnessMap;
use crate::services::masp::update_witness_map;
use crate::services::{
Expand Down Expand Up @@ -207,7 +207,7 @@ async fn build_and_commit_masp_data_at_height(
};

let mut shielded_txs = BTreeMap::new();
let mut tx_note_map = TxNoteMap::default();
let mut tx_notes_index = TxNoteMap::default();

tracing::info!(
%block_height,
Expand All @@ -229,7 +229,7 @@ async fn build_and_commit_masp_data_at_height(

update_witness_map(
&commitment_tree,
&mut tx_note_map,
&mut tx_notes_index,
&witness_map,
indexed_tx,
&masp_tx,
Expand All @@ -245,7 +245,7 @@ async fn build_and_commit_masp_data_at_height(
chain_state,
commitment_tree,
witness_map,
tx_note_map,
tx_notes_index,
shielded_txs,
)
.await
Expand Down
12 changes: 6 additions & 6 deletions chain/src/services/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use shared::indexed_tx::IndexedTx;

use crate::entity::chain_state::ChainState;
use crate::entity::commitment_tree::CommitmentTree;
use crate::entity::tx_note_map::TxNoteMap;
use crate::entity::tx_notes_index::TxNoteMap;
use crate::entity::witness_map::WitnessMap;

const MIGRATIONS: EmbeddedMigrations = embed_migrations!("../orm/migrations/");
Expand Down Expand Up @@ -179,7 +179,7 @@ pub async fn commit(
chain_state: ChainState,
commitment_tree: CommitmentTree,
witness_map: WitnessMap,
notes_map: TxNoteMap,
notes_index: TxNoteMap,
shielded_txs: BTreeMap<IndexedTx, Transaction>,
) -> anyhow::Result<()> {
tracing::info!(
Expand Down Expand Up @@ -231,15 +231,15 @@ pub async fn commit(
);
}

if !notes_map.is_empty() {
if !notes_index.is_empty() {
tracing::debug!(
block_height = %chain_state.block_height,
"Pre-committing notes map"
);

let notes_map_db = notes_map.into_db();
diesel::insert_into(schema::notes_map::table)
.values(&notes_map_db)
let notes_index_db = notes_index.into_db();
diesel::insert_into(schema::notes_index::table)
.values(&notes_index_db)
.on_conflict_do_nothing()
.execute(transaction_conn)
.context("Failed to insert notes map into db")?;
Expand Down
6 changes: 3 additions & 3 deletions chain/src/services/masp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ use namada_sdk::masp_primitives::merkle_tree::IncrementalWitness;
use shared::indexed_tx::IndexedTx;

use crate::entity::commitment_tree::CommitmentTree;
use crate::entity::tx_note_map::TxNoteMap;
use crate::entity::tx_notes_index::TxNoteMap;
use crate::entity::witness_map::WitnessMap;

/// Update the merkle tree of witnesses the first time we
/// scan a new MASP transaction.
pub fn update_witness_map(
commitment_tree: &CommitmentTree,
tx_note_map: &mut TxNoteMap,
tx_notes_index: &mut TxNoteMap,
witness_map: &WitnessMap,
indexed_tx: IndexedTx,
shielded: &namada_core::masp_primitives::transaction::Transaction,
) -> anyhow::Result<()> {
let mut note_pos = commitment_tree.size();
tx_note_map
tx_notes_index
.insert(indexed_tx, false /* is_fee_unshielding */, note_pos);

for so in shielded
Expand Down
2 changes: 1 addition & 1 deletion orm/migrations/2024-04-17-131702_notes_map/down.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DROP TABLE notes_map;
DROP TABLE notes_index;
8 changes: 4 additions & 4 deletions orm/migrations/2024-04-17-131702_notes_map/up.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
CREATE TABLE notes_map (
CREATE TABLE notes_index (
note_position INT PRIMARY KEY,
block_index INT NOT NULL,
is_fee_unshielding BOOLEAN NOT NULL,
block_height INT NOT NULL,
masp_tx_index INT NOT NULL
);

CREATE INDEX notes_map_block_height_asc ON notes_map (block_height ASC);
CREATE INDEX notes_map_block_height_desc ON notes_map (block_height DESC);
CREATE INDEX notes_index_block_height_asc ON notes_index (block_height ASC);
CREATE INDEX notes_index_block_height_desc ON notes_index (block_height DESC);

CREATE INDEX notes_map_block_height ON notes_map USING HASH (block_height);
CREATE INDEX notes_index_block_height ON notes_index USING HASH (block_height);
2 changes: 1 addition & 1 deletion orm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod block_index;
pub mod chain_state;
pub mod notes_map;
pub mod notes_index;
pub mod schema;
pub mod tree;
pub mod tx;
Expand Down
10 changes: 5 additions & 5 deletions orm/src/notes_map.rs → orm/src/notes_index.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use diesel::{Insertable, Queryable, Selectable};
use serde::Serialize;

use crate::schema::notes_map;
use crate::schema::notes_index;

#[derive(Serialize, Queryable, Selectable, Clone)]
#[diesel(table_name = notes_map)]
#[diesel(table_name = notes_index)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct NotesMapDb {
pub struct NotesIndexDb {
pub block_index: i32,
pub is_fee_unshielding: bool,
pub note_position: i32,
Expand All @@ -15,9 +15,9 @@ pub struct NotesMapDb {
}

#[derive(Serialize, Insertable, Clone)]
#[diesel(table_name = notes_map)]
#[diesel(table_name = notes_index)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct NotesMapInsertDb {
pub struct NotesIndexInsertDb {
pub block_index: i32,
pub is_fee_unshielding: bool,
pub note_position: i32,
Expand Down
4 changes: 2 additions & 2 deletions orm/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ diesel::table! {
}

diesel::table! {
notes_map (note_position) {
notes_index (note_position) {
note_position -> Int4,
block_index -> Int4,
is_fee_unshielding -> Bool,
Expand Down Expand Up @@ -56,7 +56,7 @@ diesel::allow_tables_to_appear_in_same_query!(
block_index,
chain_state,
commitment_tree,
notes_map,
notes_index,
tx,
witness,
);
8 changes: 4 additions & 4 deletions swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/LatestHeightResponse'
/notes-map:
/notes-index:
get:
parameters:
- in: query
Expand All @@ -58,7 +58,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/NotesMapResponse'
$ref: '#/components/schemas/NotesIndexResponse'
/witness-map:
get:
parameters:
Expand Down Expand Up @@ -139,10 +139,10 @@ components:
type: integer
minimum: 0
description: The block height of the commitment tree.
NotesMapResponse:
NotesIndexResponse:
type: object
properties:
notes_map:
notes_index:
type: array
items:
type: object
Expand Down
5 changes: 4 additions & 1 deletion webserver/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ impl ApplicationServer {
"/witness-map",
get(handler::witness_map::get_witness_map),
)
.route("/notes-map", get(handler::notes_map::get_notes_map))
.route(
"/notes-index",
get(handler::notes_index::get_notes_index),
)
.route("/tx", get(handler::tx::get_tx))
.route("/height", get(handler::namada_state::get_latest_height))
.route(
Expand Down
2 changes: 1 addition & 1 deletion webserver/src/dto/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod notes_map;
pub mod notes_index;
pub mod tree;
pub mod txs;
pub mod witness;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
use validator::Validate;

#[derive(Clone, Serialize, Deserialize, Validate)]
pub struct NotesMapQueryParams {
pub struct NotesIndexQueryParams {
#[validate(range(min = 1))]
pub height: u64,
}
2 changes: 1 addition & 1 deletion webserver/src/error/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod api;
pub mod namada_state;
pub mod notes_map;
pub mod notes_index;
pub mod tree;
pub mod tx;
pub mod witness_map;
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ use thiserror::Error;
use crate::response::api::ApiErrorResponse;

#[derive(Error, Debug)]
pub enum NotesMapError {
#[error("NotesMap not found")]
pub enum NotesIndexError {
#[error("NotesIndex not found")]
NotFound,
#[error("Database error: {0}")]
Database(String),
}

impl IntoResponse for NotesMapError {
impl IntoResponse for NotesIndexError {
fn into_response(self) -> Response {
let status_code = match self {
NotesMapError::NotFound => StatusCode::NOT_FOUND,
NotesMapError::Database(_) => StatusCode::INTERNAL_SERVER_ERROR,
NotesIndexError::NotFound => StatusCode::NOT_FOUND,
NotesIndexError::Database(_) => StatusCode::INTERNAL_SERVER_ERROR,
};

ApiErrorResponse::send(status_code.as_u16(), Some(self.to_string()))
Expand Down
2 changes: 1 addition & 1 deletion webserver/src/handler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod namada_state;
pub mod notes_map;
pub mod notes_index;
pub mod tree;
pub mod tx;
pub mod witness_map;
29 changes: 29 additions & 0 deletions webserver/src/handler/notes_index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use axum::extract::{Query, State};
use axum::Json;
use axum_macros::debug_handler;
use axum_trace_id::TraceId;
use shared::error::InspectWrap;

use crate::dto::notes_index::NotesIndexQueryParams;
use crate::error::notes_index::NotesIndexError;
use crate::response::notes_index::NotesIndexResponse;
use crate::state::common::CommonState;

#[debug_handler]
pub async fn get_notes_index(
_trace_id: TraceId<String>,
State(state): State<CommonState>,
Query(query_params): Query<NotesIndexQueryParams>,
) -> Result<Json<NotesIndexResponse>, NotesIndexError> {
let from_block_height = query_params.height;

let notes_index = state
.notes_index_service
.get_notes_index(from_block_height)
.await
.inspect_wrap("get_notes_index", |err| {
NotesIndexError::Database(err.to_string())
})?;

Ok(Json(NotesIndexResponse::new(notes_index)))
}
29 changes: 0 additions & 29 deletions webserver/src/handler/notes_map.rs

This file was deleted.

2 changes: 1 addition & 1 deletion webserver/src/repository/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod namada_state;
pub mod notes_map;
pub mod notes_index;
pub mod tree;
pub mod tx;
pub mod witness_map;
Loading

0 comments on commit 17ac659

Please sign in to comment.