Skip to content

Commit

Permalink
Add migration
Browse files Browse the repository at this point in the history
  • Loading branch information
contrun committed Jan 21, 2025
1 parent 102b66b commit 8f14f6b
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 0 deletions.
55 changes: 55 additions & 0 deletions migrate/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions migrate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ serde_json = "1.0.135"
fiber_v020 = { package = "fnn", git = "https://github.com/nervosnetwork/fiber.git", tag = "v0.2.0" }
fiber_v021 = { package = "fnn", git = "https://github.com/nervosnetwork/fiber.git", tag = "v0.2.1" }
fiber_v030 = { package = "fnn", git = "https://github.com/nervosnetwork/fiber.git", tag = "v0.3.0-rc1" }
fiber_v031 = { package = "fnn", git = "https://github.com/contrun/fiber.git", rev = "102b66b86b11bf961a87ec4a5419e186457f2e48" }

[features]
default = []
Expand Down
96 changes: 96 additions & 0 deletions migrate/src/migrations/mig_20250120.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use fiber::{store::migration::Migration, Error};
use indicatif::ProgressBar;
use rocksdb::ops::Iterate;
use rocksdb::ops::Put;
use rocksdb::DB;
use std::sync::Arc;
use tracing::info;

use crate::util::convert;

const MIGRATION_DB_VERSION: &str = "20250120090023";

pub use fiber_v030::fiber::graph::PaymentSession as OldPaymentSession;
pub use fiber_v030::fiber::network::SendPaymentData as OldSendPaymentData;
pub use fiber_v031::fiber::graph::PaymentSession as NewPaymentSession;
pub use fiber_v031::fiber::network::SendPaymentData as NewSendPaymentData;

pub struct MigrationObj {
version: String,
}

impl MigrationObj {
pub fn new() -> Self {
Self {
version: MIGRATION_DB_VERSION.to_string(),
}
}
}

impl Migration for MigrationObj {
fn migrate(
&self,
db: Arc<DB>,
_pb: Arc<dyn Fn(u64) -> ProgressBar + Send + Sync>,
) -> Result<Arc<DB>, Error> {
info!(
"MigrationObj::migrate to {} ...........",
MIGRATION_DB_VERSION
);

const PAYMENT_SESSION_PREFIX: u8 = 192;
let prefix = vec![PAYMENT_SESSION_PREFIX];

for (k, v) in db
.prefix_iterator(prefix.as_slice())
.take_while(move |(col_key, _)| col_key.starts_with(prefix.as_slice()))
{
let old_payment_session: OldPaymentSession =
bincode::deserialize(&v).expect("deserialize to old channel state");

let old_request = old_payment_session.request.clone();

let request = NewSendPaymentData {
target_pubkey: convert(old_request.target_pubkey),
amount: old_request.amount,
payment_hash: convert(old_request.payment_hash),
invoice: old_request.invoice,
final_tlc_expiry_delta: old_request.final_tlc_expiry_delta,
tlc_expiry_limit: old_request.tlc_expiry_limit,
timeout: old_request.timeout,
max_fee_amount: old_request.max_fee_amount,
max_parts: old_request.max_parts,
keysend: old_request.keysend,
udt_type_script: old_request.udt_type_script,
preimage: convert(old_request.preimage),
allow_self_payment: old_request.allow_self_payment,
dry_run: old_request.dry_run,
// The meaning of hop_hints changed, we are dropping previous hop hints.
hop_hints: vec![],
};

let new_payment_session = NewPaymentSession {
request: request,
retried_times: old_payment_session.retried_times,
last_error: old_payment_session.last_error,
try_limit: old_payment_session.try_limit,
status: convert(old_payment_session.status),
created_at: old_payment_session.created_at,
last_updated_at: old_payment_session.last_updated_at,
route: convert(old_payment_session.route),
session_key: old_payment_session.session_key,
};

let new_payment_session_bytes =
bincode::serialize(&new_payment_session).expect("serialize to new channel state");

db.put(k, new_payment_session_bytes)
.expect("save new channel state");
}
Ok(db)
}

fn version(&self) -> &str {
&self.version
}
}
1 change: 1 addition & 0 deletions migrate/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

pub mod mig_20250114;
pub mod mig_20250115;
pub mod mig_20250120;

0 comments on commit 8f14f6b

Please sign in to comment.