Post
Topic
Board Development & Technical Discussion
Merits 4 from 2 users
Re: How to initially sync Bitcoin as fast as possible
by
lcharles123
on 02/01/2024, 01:46:15 UTC
⭐ Merited by ABCbits (2) ,satscraper (2)
Answering the OP I just found a way to sync as fast as possible using RAMdisk, it will work even if you has a slow HDD or NAS. RAMdisk is faster than SSD but requires some complexity, like linking files

I observed which files are written frequently on sync, and I saw that are only the blk and rev dat ones on blocks folder.
bitcoind has the -blocksdir option, to specify the blocks folder, you just need to point it to a folder on RAMdisk.
I write the following script that check for completed blk and rev files on ramdisk/blocks folder and move them to persistent storage, and create a link to them, because these .dat files are used to build chainstate after completing initial sync (or successfully shutting down before it).

With this strategy the initial sync will have only network speed as bottleneck. At least for me, I have at most 10 Mb/s.

I am using a single core notebook from 2011 with debian 11, 16GB of RAM and two 500GB HDDs merged using mergerfs.
mergerfs was always using CPU before, and with this strategy, it use 0 almost all time, only when a dat file are completed.
I will post the results here after finishing the initial sync

Code:
#!/bin/bash


# first of all create and mount a ramdisk, i.e.
# sudo mkdir -p /mount/point/folder -m 755 alice
# sudo mount -o size=12G -t tmpfs tmpdir /mount/point/folder
# ajust the variables below

# point only blocks folder to ramdisk, i.e. -blocksdir=/mount/point/folder
# use it on first run
# run this script BEFORE starting bitcoind
# otherwise move some last blk and rev files to ramdisk
# symlink all blk and rev files from ramfs -> hdd/ssd
# make sure your pc doesnt run out of power
# I have a nobreak for that
# with this setup, the sync will be limited by others factors than media speed
# shutdown bitcoind with ctrl + c, and move all non link elements to hdd/ssd

# get the blocks starting from BNBER
# this will let at least 7 blk and 7 rev files on ramdisk
BNBER=7
ACTUALDIR=/home/alice/bcdata/BTC/blocks
RAMDISK=/home/alice/temp/BTC/blocks
set -e

while true
do
    # iterate only over regular files excluding links and the BNBER most recent ones
    for blkfile in $(find $RAMDISK/blk*.dat  -type f -printf "%f\n" | sort -r | tail -n +$BNBER)
    do
        mv $RAMDISK/$blkfile $ACTUALDIR/$blkfile
        ln -s $ACTUALDIR/$blkfile $RAMDISK/$blkfile
    done
    for blkfile in $(find $RAMDISK/rev*.dat  -type f -printf "%f\n" | sort -r | tail -n +$BNBER)
    do
        mv $RAMDISK/$blkfile $ACTUALDIR/$blkfile
        ln -s $ACTUALDIR/$blkfile $RAMDISK/$blkfile
    done

    sleep 10m
done