diff --git a/src/Main.java b/src/Main.java index 28f2655..59dea7b 100644 --- a/src/Main.java +++ b/src/Main.java @@ -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 { @@ -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 listDummy = null; boolean dummyBool = false; long dummyLong = 0; @@ -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()); dummyBytes = ptarm.signRawTx(0, dummyBytes); diff --git a/src/co/nayuta/lightning/Ptarmigan.java b/src/co/nayuta/lightning/Ptarmigan.java index 4a14908..98cead7 100644 --- a/src/co/nayuta/lightning/Ptarmigan.java +++ b/src/co/nayuta/lightning/Ptarmigan.java @@ -119,7 +119,7 @@ public class SearchOutPointResult { // SearchOutPointResult() { this.height = 0; - tx = null; + this.tx = null; } } // @@ -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); @@ -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; @@ -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()); @@ -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()); @@ -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回 @@ -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; @@ -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)); @@ -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); @@ -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); @@ -994,6 +1019,7 @@ public void setChannel( } catch (Exception e) { logger.error("setChannel: " + getStackTrace(e)); } + logger.debug("setChannel: exit"); } // // チャネル情報削除 @@ -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());