- Save active and paused torrents to torrents.edn on add, remove, pause, resume, and metadata fetch, then restore them on router startup. - Default path is $MOON_I2P_DATA_DIR, then $XDG_DATA_HOME, then ~/.local/share/moon-i2p-browser. Add --torrent-state <file> to override, or --no-torrent-state to disable. - Write the state file atomically (temp file, fsync, rename). A corrupt or unsupported file logs a warning and does not block startup. - Verify restored pieces incrementally in the background for large torrents; these show as state "checking" in /api/torrents. - Expose state_file in /api/status and the startup banner. - Add standard RFC 4648 Base64 encode/decode helpers, used for the info-b64 field. - Add the moonedn dependency and pass --torrent-state from the Android service.
153 lines
4.7 KiB
MoonBit
153 lines
4.7 KiB
MoonBit
///|
|
|
test "bt_step_verify_pieces verifies multi-MiB torrent incrementally across ticks" {
|
|
let rand_hex = @encoding.hex_encode(@ntcp2_net.random_bytes(4))
|
|
let base_dir = "/tmp/moon_i2p_incr_verify_test_\{rand_hex}"
|
|
@ntcp2_net.mkdir_p(base_dir)
|
|
|
|
let piece_len = 1048576
|
|
let num_pieces = 5
|
|
let total_len = piece_len * num_pieces
|
|
let piece0 = Bytes::make(piece_len, b'\x11')
|
|
let piece1 = Bytes::make(piece_len, b'\x22')
|
|
let piece2 = Bytes::make(piece_len, b'\x33')
|
|
let piece3 = Bytes::make(piece_len, b'\x44')
|
|
let piece4 = Bytes::make(piece_len, b'\x55')
|
|
let pieces_data = [piece0, piece1, piece2, piece3, piece4]
|
|
|
|
let hashes = @encoding.Writer::new()
|
|
for i = 0; i < num_pieces; i = i + 1 {
|
|
@ntcp2_net.write_file_at(
|
|
base_dir + "/incr.bin",
|
|
(i * piece_len).to_int64(),
|
|
pieces_data[i],
|
|
)
|
|
hashes.write_bytes(@crypto.sha1(pieces_data[i]))
|
|
}
|
|
let tbytes = @bittorrent.bencode_encode(
|
|
BDict([
|
|
(
|
|
b"info",
|
|
BDict([
|
|
(b"length", BInt(total_len.to_int64())),
|
|
(b"name", BBytes(b"incr.bin")),
|
|
(b"piece length", BInt(piece_len.to_int64())),
|
|
(b"pieces", BBytes(hashes.to_bytes())),
|
|
]),
|
|
),
|
|
]),
|
|
)
|
|
let meta = @bittorrent.parse_torrent(tbytes)
|
|
let r = Router::start({
|
|
sam_host: "127.0.0.1",
|
|
sam_port: 0,
|
|
ntcp2_host: "127.0.0.1",
|
|
ntcp2_port: 0,
|
|
})
|
|
defer r.close()
|
|
r.enable_torrent_engine(download_dir=base_dir)
|
|
let eng = match r.torrent_engine {
|
|
Some(e) => e
|
|
None => fail("missing torrent engine")
|
|
}
|
|
let hex = @encoding.hex_encode(meta.info_hash)
|
|
let t : ActiveTorrent = {
|
|
info_hash: meta.info_hash,
|
|
info_hash_hex: hex,
|
|
name: meta.name,
|
|
download_dir: base_dir,
|
|
metainfo: Some(meta),
|
|
raw_info: meta.raw_info,
|
|
paused: false,
|
|
completed: false,
|
|
error: "",
|
|
last_tracker_status: "",
|
|
downloaded_bytes: 0UL,
|
|
uploaded_bytes: 0UL,
|
|
have_pieces: Array::make(num_pieces, false),
|
|
inflight_pieces: [],
|
|
verify_cursor: 0,
|
|
meta_size: meta.raw_info.length(),
|
|
meta_pieces: [],
|
|
meta_req_ms: 0UL,
|
|
known_peers: [],
|
|
peer_backoff: [],
|
|
peers: [],
|
|
trackers: [],
|
|
last_dht_lookup_ms: 0UL,
|
|
last_pex_ms: 0UL,
|
|
}
|
|
eng.torrents.push(t)
|
|
|
|
assert_true(r.build_api_torrents_json().contains("\"state\":\"checking\""))
|
|
// Tick 1: 4 MiB budget verifies pieces 0..3 and leaves piece 4 pending
|
|
assert_true(r.bt_step_verify_pieces(eng))
|
|
assert_eq(t.verify_cursor, 4)
|
|
assert_eq(t.downloaded_bytes, (4 * piece_len).to_uint64())
|
|
assert_true(!t.completed)
|
|
assert_true(r.build_api_torrents_json().contains("\"state\":\"checking\""))
|
|
|
|
// Tick 2: finishes piece 4 and clears checking state
|
|
assert_true(r.bt_step_verify_pieces(eng))
|
|
assert_eq(t.verify_cursor, -1)
|
|
assert_eq(t.downloaded_bytes, total_len.to_uint64())
|
|
assert_true(t.completed)
|
|
assert_eq(t.have_pieces, [true, true, true, true, true])
|
|
assert_true(r.build_api_torrents_json().contains("\"state\":\"seeding\""))
|
|
assert_true(!r.bt_step_verify_pieces(eng))
|
|
}
|
|
|
|
///|
|
|
test "EDN persistence round-trips large >64 KiB info-b64 payloads" {
|
|
let rand_hex = @encoding.hex_encode(@ntcp2_net.random_bytes(4))
|
|
let base_dir = "/tmp/moon_i2p_large_edn_test_\{rand_hex}"
|
|
let dl_dir = "\{base_dir}/dl"
|
|
let state_file = "\{base_dir}/torrents.edn"
|
|
@ntcp2_net.mkdir_p(dl_dir)
|
|
|
|
// 3,000 pieces * 20 bytes = 60,000-byte raw pieces => >60 KiB raw_info => >80 KiB base64 token
|
|
let raw_pieces = Bytes::make(60000, b'\xAB')
|
|
let tbytes = @bittorrent.bencode_encode(
|
|
BDict([
|
|
(b"announce", BBytes(b"http://tracker.insulaocculta.i2p/a")),
|
|
(
|
|
b"info",
|
|
BDict([
|
|
(b"length", BInt(3000L * 262144L)),
|
|
(b"name", BBytes(b"large-catalog.bin")),
|
|
(b"piece length", BInt(262144L)),
|
|
(b"pieces", BBytes(raw_pieces)),
|
|
]),
|
|
),
|
|
]),
|
|
)
|
|
let meta = @bittorrent.parse_torrent(tbytes)
|
|
assert_true(@encoding.base64_std_encode(meta.raw_info).length() > 70000)
|
|
|
|
let r1 = Router::start({
|
|
sam_host: "127.0.0.1",
|
|
sam_port: 0,
|
|
ntcp2_host: "127.0.0.1",
|
|
ntcp2_port: 0,
|
|
})
|
|
r1.enable_torrent_engine(download_dir=dl_dir, state_file~)
|
|
let ih = r1.add_torrent_bytes(tbytes, download_dir=dl_dir)
|
|
r1.close()
|
|
|
|
let r2 = Router::start({
|
|
sam_host: "127.0.0.1",
|
|
sam_port: 0,
|
|
ntcp2_host: "127.0.0.1",
|
|
ntcp2_port: 0,
|
|
})
|
|
defer r2.close()
|
|
r2.enable_torrent_engine(download_dir=dl_dir, state_file~)
|
|
let eng2 = match r2.torrent_engine {
|
|
Some(e) => e
|
|
None => fail("missing eng2")
|
|
}
|
|
assert_eq(eng2.torrents.length(), 1)
|
|
assert_eq(eng2.torrents[0].info_hash_hex, ih)
|
|
assert_eq(eng2.torrents[0].raw_info, meta.raw_info)
|
|
assert_eq(eng2.torrents[0].verify_cursor, 0)
|
|
}
|