Files
SHiNE-server/SHiNE-server/shine-server-db/src/main/java/shine/db/dao/CanonicalChainResolver.java
T
AidarKC 67010a57f2 Улучшить Arweave-синхронизацию и ротацию ключей
Улучшено скачивание блоков из Arweave и обработка канонической цепочки.

Ротация ключей переведена на новую схему с пересборкой публичных каналов; личные сообщения пока не меняются.
2026-10-06 16:39:39 +03:00

37 lines
2.8 KiB
Java

package shine.db.dao;
import shine.db.DbController;
import shine.db.entities.BlockEntry;
import java.sql.*;
/** Resolves a logical block number to the physical fork that owns it in a selected canonical history. */
public final class CanonicalChainResolver {
private static volatile CanonicalChainResolver instance;
private final DbController db=DbController.getInstance();
private final BlocksDAO blocks=BlocksDAO.getInstance();
private final BlockchainForkSegmentsDAO segments=BlockchainForkSegmentsDAO.getInstance();
private CanonicalChainResolver(){}
public static CanonicalChainResolver getInstance(){if(instance==null)synchronized(CanonicalChainResolver.class){if(instance==null)instance=new CanonicalChainResolver();}return instance;}
public BlockEntry getBlock(String blockchainName,int blockNumber)throws SQLException{try(Connection c=db.getConnection()){return getBlock(c,blockchainName,blockNumber);}}
public BlockEntry getBlock(Connection c,String blockchainName,int blockNumber)throws SQLException{
ChainRef r=ref(blockchainName); int physical=resolveFork(c,r.login,r.fork,blockNumber); return blocks.getByNumber(c,r.login+"-"+String.format("%03d",physical),blockNumber);
}
public byte[] getHash(String blockchainName,int blockNumber)throws SQLException{BlockEntry b=getBlock(blockchainName,blockNumber);return b==null?null:b.getBlockHash();}
public byte[] getHash(Connection c,String blockchainName,int blockNumber)throws SQLException{BlockEntry b=getBlock(c,blockchainName,blockNumber);return b==null?null:b.getBlockHash();}
public int resolveFork(Connection c,String login,int activeFork,int blockNumber)throws SQLException{
if(blockNumber<0)throw new IllegalArgumentException("blockNumber < 0"); int f=activeFork; int guard=0;
while(true){ if(++guard>1000)throw new SQLException("fork ancestry cycle for "+login); BlockchainForkSegmentsDAO.Segment s=segments.get(c,login,f);
if(s==null){ // legacy self-contained fork
return f;
}
if(s.parentForkNumber()==null || blockNumber>=s.firstPhysicalBlockNumber()) return f;
f=s.parentForkNumber();
}
}
public BlockEntry getLastLineBlock(Connection c,String blockchainName,int msgType,int lineCode,int beforeBlockExclusive)throws SQLException{
ChainRef r=ref(blockchainName); for(int n=beforeBlockExclusive-1;n>=0;n--){ BlockEntry b=getBlock(c,blockchainName,n); if(b!=null && b.getMsgType()==msgType && b.getLineCode()!=null && b.getLineCode()==lineCode)return b; } return null;
}
private record ChainRef(String login,int fork){}
private static ChainRef ref(String n){int d=n==null?-1:n.lastIndexOf('-');if(d<=0||n.length()-d-1!=3)throw new IllegalArgumentException("Bad blockchainName: "+n);return new ChainRef(n.substring(0,d),Integer.parseInt(n.substring(d+1)));}
}