Bitcoinj
使用 bitcoinj 獲取餘額/下載區塊鏈
我正在玩 bitoinj,但我似乎連最基本的功能都無法執行。
我想做的事:我有一個錢包和一個地址。我想下載區塊鏈並從中獲取錢包餘額。
我所做的是遵循程式碼(在 scala 中,但它或多或少與 Java 相同),錢包位於
address.walet
,我希望將 blockain 保存在saved.blockchain
:val params = NetworkParameters.prodNet val walletFile = new java.io.File("address.wallet"); val wallet = Wallet.loadFromFile(walletFile) val key = wallet.keychain.get(0) val address:String = key.toAddress(params).toString println("ADDRESS IS : "+address) val blockStore = new BoundedOverheadBlockStore(params, new java.io.File("saved.blockchain")) val chain = new BlockChain(params, wallet, blockStore) val peerGroup = new PeerGroup(params, chain) peerGroup.setUserAgent("BitToy", "0.1") peerGroup.addPeerDiscovery(new DnsDiscovery(params)) peerGroup.addWallet(wallet) println("START DOWNLOADING BLOCKCHAIN") peerGroup.start() peerGroup.downloadBlockChain() peerGroup.stop() println("DONE; BALANCE IS :") println(wallet.getBalance)
我錢包裡的地址確實有餘額。
現在,當我第一次執行它時,區塊鏈(或者更確切地說,標頭)被下載,我看到了正確的餘額。但是,當我第二次執行它時,我只看到:
ADDRESS IS : [correct address here] Aug 19, 2012 8:06:00 AM com.google.bitcoin.store.BoundedOverheadBlockStore load INFO: Reading block store from saved.blockchain Aug 19, 2012 8:06:00 AM com.google.bitcoin.store.BoundedOverheadBlockStore load INFO: Read chain head from disk: 00000000000001b10c347b5ec05f9a1910d541905c5cd8827784a5c3e0695c95 Aug 19, 2012 8:06:00 AM com.google.bitcoin.core.BlockChain <init> INFO: chain head is at height 194575: v1 block: previous block: 00000000000005ac50dc06021362cd4c284fafd4a1bdb7bad2849c978a875bb1 merkle root: 158aedd0bc3386c7686f85ab933fd9e3ecb38e348b121ed0e61e2e776eda04a1 time: [1345356028] Sun Aug 19 08:00:28 CEST 2012 difficulty target (nBits): 436709470 nonce: 4238997601 START DOWNLOADING BLOCKCHAIN Aug 19, 2012 8:06:31 AM com.google.bitcoin.discovery.DnsDiscovery getPeers INFO: DNS lookup for dnsseed.bitcoin.dashjr.org failed. Aug 19, 2012 8:06:36 AM com.google.bitcoin.core.TCPNetworkConnection$NetworkHandler channelConnected INFO: Announcing ourselves as: /BitCoinJ:0.6-SNAPSHOT/BitMessage:0.1/ Aug 19, 2012 8:06:41 AM com.google.bitcoin.core.Peer$PeerHandler exceptionCaught INFO: com.google.bitcoin.core.Peer$PeerHandler@193f6e2 - No route to host Aug 19, 2012 8:06:46 AM com.google.bitcoin.core.Peer$PeerHandler exceptionCaught INFO: com.google.bitcoin.core.Peer$PeerHandler@61736e - No route to host Aug 19, 2012 8:06:52 AM com.google.bitcoin.core.TCPNetworkConnection$NetworkHandler channelConnected INFO: Announcing ourselves as: /BitCoinJ:0.6-SNAPSHOT/BitMessage:0.1/ Aug 19, 2012 8:06:52 AM com.google.bitcoin.core.PeerGroup handleNewPeer INFO: Peer(seed.bitcoin.sipa.be/50.130.4.89:8333): New peer Aug 19, 2012 8:06:52 AM com.google.bitcoin.core.PeerGroup handleNewPeer INFO: starting block chain download Aug 19, 2012 8:06:52 AM com.google.bitcoin.core.PeerGroup setDownloadPeer INFO: Setting download peer: Peer(seed.bitcoin.sipa.be/50.130.4.89:8333) Downloading block chain of size 0. Done downloading block chain Aug 19, 2012 8:06:52 AM com.google.bitcoin.core.Peer blockChainDownload INFO: Peer(seed.bitcoin.sipa.be/50.130.4.89:8333): blockChainDownload(0000000000000000000000000000000000000000000000000000000000000000) current head = 00000000000001b10c347b5ec05f9a1910d541905c5cd8827784a5c3e0695c95 DONE; BALANCE IS : 0 Aug 19, 2012 8:06:52 AM com.google.bitcoin.core.PeerGroup$PeerGroupThread run INFO: shutdown start
餘額為零。如果我刪除保存的區塊鏈並讓它重新下載,它會再次工作,但只有一次。
我究竟做錯了什麼?
好的,現在我明白了。
我不能。
Bitcoinj 僅保存標頭。而且 - 我不知道 - 你不能只用標題來平衡。
有關更多資訊,您可以在此處查看標題中的確切描述<https://en.bitcoin.it/wiki/Block_hashing_algorithm>
但是,我能做的就是事後立即保存錢包。我會做的。
以前的“答案”/大聲思考:
我在原始碼中四處尋找。
似乎重要的部分是何時
sendTransactionsToWallet
被呼叫BlockChain.java
- 但它只在期間被呼叫connectBlock
,沒有別的。
connectBlock
僅在 期間呼叫add
。唯一的add
ed 塊是來自網路的新塊,而不是來自 BlockStore 的塊。但我會多看看程式碼。
我不確定,如果我重寫
BlockChain.java
from 的建構子會發生什麼可怕的事情:/** * Constructs a BlockChain connected to the given list of wallets and a store. */ public BlockChain(NetworkParameters params, List<Wallet> wallets, BlockStore blockStore) throws BlockStoreException { this.blockStore = blockStore; chainHead = blockStore.getChainHead(); log.info("chain head is at height {}:\n{}", chainHead.getHeight(), chainHead.getHeader()); this.params = params; this.wallets = new ArrayList<Wallet>(wallets); }
to(從我的腦海中寫作,getter/setter 可能不正確,等等)
/** * Constructs a BlockChain connected to the given list of wallets and a store. */ public BlockChain(NetworkParameters params, List<Wallet> wallets, BlockStore blockStore) throws BlockStoreException { this.blockStore = blockStore; chainHead = blockStore.getChainHead(); log.info("chain head is at height {}:\n{}", chainHead.getHeight(), chainHead.getHeader()); this.params = params; this.wallets = new ArrayList<Wallet>(wallets); StoredBlock sBlock = blockStore.getChainHead(); Sha256Hash prevHash = sBlock.header.prevBlockHash(); while (prevHash!=Sha256Hash.ZERO_HASH) { for (Wallet wallet:wallets){ sendTransactionsToWallet(sBlock, NewBlockType.SIDE_CHAIN, wallet, sBlock.transactions); } sBlock = blockStore.get(hash); prevHash = sBlock.header.prevBlockHash(); } }
但同樣,我不想重新發明輪子或任何東西。