Skip to content

Commit

Permalink
setChannel(): use lastConf
Browse files Browse the repository at this point in the history
  • Loading branch information
nayuta-ueno committed Sep 8, 2019
1 parent 17a975b commit 603218d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
6 changes: 1 addition & 5 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import co.nayuta.lightning.Ptarmigan;
import org.bitcoinj.core.Sha256Hash;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;

public class Main
{
Expand All @@ -28,7 +25,6 @@ public static void main( String[] argv ) {
byte[] dummyBytes = null;
Ptarmigan.ShortChannelParam dummyChan = null;
Ptarmigan.SearchOutPointResult dummySearch = null;
Sha256Hash dummyHash = null;
List<byte[]> listDummy = null;
boolean dummyBool = false;
long dummyLong = 0;
Expand All @@ -38,7 +34,7 @@ public static void main( String[] argv ) {
dummyBytes = ptarm.getGenesisBlockHash();
dummyInt = ptarm.getTxConfirmation(dummyBytes, -1, null, 0);
dummyChan = ptarm.getShortChannelParam(dummyBytes);
dummyHash = ptarm.getTxidFromShortChannelId(0);
dummyBytes = ptarm.getTxidFromShortChannelId(0);
dummySearch = ptarm.searchOutPoint(0, dummyBytes, 0);
listDummy = ptarm.searchVout(0, new ArrayList<byte[]>());
dummyBytes = ptarm.signRawTx(0, dummyBytes);
Expand Down
46 changes: 36 additions & 10 deletions src/co/nayuta/lightning/Ptarmigan.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public class SearchOutPointResult {
//
SearchOutPointResult() {
this.height = 0;
tx = null;
this.tx = null;
}
}
//
Expand Down Expand Up @@ -429,7 +429,11 @@ public byte[] getGenesisBlockHash() {
/** confirmation数取得.
*
* @param txhash target TXID
* @return 取得できない場合0を返す
* @param voutIndex (not -1)funding_tx:index, (-1)not funding_tx
* @param voutWitProg (funding_tx)outpoint witnessProgram
* @param amount (funding_tx)outpoint amount
* @return !0:confirmation, 0:error or fail get confirmation
* @throws PtarmException peer not found count > PEER_FAIL_COUNT_MAX
*/
public int getTxConfirmation(byte[] txhash, int voutIndex, byte[] voutWitProg, long amount) throws PtarmException {
Sha256Hash txHash = Sha256Hash.wrapReversed(txhash);
Expand All @@ -448,6 +452,8 @@ public int getTxConfirmation(byte[] txhash, int voutIndex, byte[] voutWitProg, l
logger.debug("getTxConfirmation: cached conf=" + ch.getConfirmation());
mapChannel.put(Hex.toHexString(ch.peerNodeId()), ch);
return ch.getConfirmation();
} else {
logger.debug("getTxConfirmation(): no short_channel");
}
matchChannel = ch;
break;
Expand All @@ -459,7 +465,17 @@ public int getTxConfirmation(byte[] txhash, int voutIndex, byte[] voutWitProg, l
logger.debug("fail ---> get from block");
return getTxConfirmationFromBlock(matchChannel, txHash, voutIndex, voutWitProg, amount);
}
//

/**
*
* @param channel (not null)target funding_tx, (null)only get confirmation
* @param txHash outpoint:txid
* @param voutIndex (not -1)funding_tx:index, (-1)not funding_tx
* @param voutWitProg: (voutIndex != -1)outpoint:witnessProgram
* @param amount: (voutIndex != -1)outpoint:amount
* @return !0:confirmation, 0:error or fail get confirmation
* @throws PtarmException peer not found count > PEER_FAIL_COUNT_MAX
*/
private int getTxConfirmationFromBlock(PtarmiganChannel channel, Sha256Hash txHash, int voutIndex, byte[] voutWitProg, long amount) throws PtarmException {
try {
logger.debug("getTxConfirmationFromBlock(): txid=" + txHash.toString());
Expand All @@ -478,6 +494,7 @@ private int getTxConfirmationFromBlock(PtarmiganChannel channel, Sha256Hash txHa
while (true) {
Block block = getBlock(blockHash);
if (block == null) {
logger.error("getTxConfirmationFromBlock: fail block");
break;
}
logger.debug("getTxConfirmationFromBlock: blockHash(conf=" + (c + 1) + ")=" + blockHash.toString());
Expand Down Expand Up @@ -566,7 +583,7 @@ public ShortChannelParam getShortChannelParam(byte[] peerId) {
return param;
}
// short_channel_idが指すtxid取得(bitcoinj試作:呼ばれない予定)
public Sha256Hash getTxidFromShortChannelId(long id) throws PtarmException {
public byte[] getTxidFromShortChannelId(long id) throws PtarmException {
logger.debug("getTxidFromShortChannelId(): id=" + id);
ShortChannelParam shortChannelId = new ShortChannelParam(id);
int blks = wak.wallet().getLastBlockSeenHeight() - shortChannelId.height + 1; // 現在のブロックでも1回
Expand All @@ -584,7 +601,7 @@ public Sha256Hash getTxidFromShortChannelId(long id) throws PtarmException {
}
if (block != null) {
logger.debug("getTxidFromShortChannelId(): get");
return block.getTransactions().get(shortChannelId.bIndex).getTxId();
return block.getTransactions().get(shortChannelId.bIndex).getTxId().getReversedBytes();
}
logger.error("getTxidFromShortChannelId(): fail");
return null;
Expand Down Expand Up @@ -934,6 +951,8 @@ public void setChannel(
StoredBlock sb = bs.get(blockHash);
if (sb != null) {
minedHeight = sb.getHeight();
} else {
logger.error("setChannel: fail StoredBlock");
}
} catch (BlockStoreException e) {
logger.error("setChannel 1: " + getStackTrace(e));
Expand Down Expand Up @@ -975,7 +994,13 @@ public void setChannel(
channel.initialize(shortChannelId, fundingOutpoint, (txRaw == null));
channel.setMinedBlockHash(blockHash, minedHeight, -1);
if (minedHeight > 0) {
logger.debug("setChannel: minedConfirm");
channel.setConfirmation(blockHeight - minedHeight + 1);
} else if (lastConfirm > 0) {
logger.debug("setChannel: lastConfirm");
channel.setConfirmation(lastConfirm);
} else {
logger.debug("setChannel: confirm not set");
}
try {
SegwitAddress address = SegwitAddress.fromHash(params, scriptPubKey);
Expand All @@ -984,7 +1009,7 @@ public void setChannel(
logger.error("setChannel 2: " + getStackTrace(e));
}
mapChannel.put(Hex.toHexString(channel.peerNodeId()), channel);
logger.debug("add channel: " + Hex.toHexString(peerId));
logger.debug("setChannel: add channel: " + Hex.toHexString(peerId));

if (!channel.getFundingTxUnspent()) {
checkUnspentFromBlock(channel, fundingOutpoint.getHash(), vIndex);
Expand All @@ -994,6 +1019,7 @@ public void setChannel(
} catch (Exception e) {
logger.error("setChannel: " + getStackTrace(e));
}
logger.debug("setChannel: exit");
}
//
// チャネル情報削除
Expand Down Expand Up @@ -1329,11 +1355,11 @@ private int checkCommitTxids(PtarmiganChannel ch, Sha256Hash txidHash) {
private Block getBlockFromPeer(Sha256Hash blockHash) throws PtarmException {
Block block = null;
Peer peer = getPeer();
if (peer == null) {
logger.error(" getBlockFromPeer() - peer not found");
return null;
}
try {
if (peer == null) {
logger.error(" getBlockFromPeer() - peer not found");
return null;
}
block = peer.getBlock(blockHash).get(TIMEOUT_GET, TimeUnit.MILLISECONDS);
if (block != null) {
logger.debug(" getBlockFromPeer() " + blockHash.toString());
Expand Down

0 comments on commit 603218d

Please sign in to comment.