├── README.md ├── part1-Basic-Prototype ├── BLC │ ├── Block.go │ ├── Blockchain.go │ └── utils.go └── main.go ├── part10-boltdb-create-db └── main.go ├── part11-boltdb-createBucket └── main.go ├── part12-boltdb-update-view └── main.go ├── part13-boltdb-update-view └── main.go ├── part14-block-boltdb ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── ProofOfWork.go │ └── utils.go └── main.go ├── part15-persistence-creategenesisblock ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── ProofOfWork.go │ └── utils.go └── main.go ├── part16-persistence-addblock ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── ProofOfWork.go │ └── utils.go └── main.go ├── part17-persistence-Iterator ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── ProofOfWork.go │ └── utils.go └── main.go ├── part18-persistence-Iterator-time-format ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── ProofOfWork.go │ └── utils.go └── main.go ├── part19-persistence-Iterator ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── ProofOfWork.go │ └── utils.go └── main.go ├── part2-Basic-Prototype ├── BLC │ ├── Block.go │ ├── Blockchain.go │ └── utils.go └── main.go ├── part20-persistence-Iterator-seperate ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── BlockchainIterator.go │ ├── ProofOfWork.go │ └── utils.go └── main.go ├── part21-cli-flag └── main.go ├── part22-cli-osArgs └── main.go ├── part23-cli └── main.go ├── part24-persistence-cli ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── BlockchainIterator.go │ ├── CLI.go │ ├── ProofOfWork.go │ └── utils.go └── main.go ├── part25-persistence-cli-createblockchain ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── BlockchainIterator.go │ ├── CLI.go │ ├── ProofOfWork.go │ └── utils.go └── main.go ├── part26-persistence-cli-modify-creategenesismethod ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── BlockchainIterator.go │ ├── CLI.go │ ├── ProofOfWork.go │ └── utils.go └── main.go ├── part27-persistence-cli-blockchain-object ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── BlockchainIterator.go │ ├── CLI.go │ ├── ProofOfWork.go │ └── utils.go └── main.go ├── part28-transaction ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── BlockchainIterator.go │ ├── CLI.go │ ├── ProofOfWork.go │ ├── Transaction.go │ └── utils.go └── main.go ├── part29-transaction-UTXO ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── BlockchainIterator.go │ ├── CLI.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ └── utils.go └── main.go ├── part3-Basic-Prototype ├── BLC │ ├── Block.go │ ├── Blockchain.go │ └── utils.go └── main.go ├── part30-transaction-UTXO-send ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── BlockchainIterator.go │ ├── CLI.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ └── utils.go └── main.go ├── part31-transaction-UTXO-JSON-TO-Array ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── BlockchainIterator.go │ ├── CLI.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ └── utils.go └── main.go ├── part32-transaction-UTXO-Send-Method ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── BlockchainIterator.go │ ├── CLI.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ └── utils.go └── main.go ├── part33-transaction-UTXO-NewBlock ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── BlockchainIterator.go │ ├── CLI.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ └── utils.go └── main.go ├── part34-transaction-new-transaction-block2 ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── BlockchainIterator.go │ ├── CLI.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ └── utils.go └── main.go ├── part35-transaction-new-transaction-block3 ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── BlockchainIterator.go │ ├── CLI.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ └── utils.go └── main.go ├── part36-transaction-new-transaction ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── BlockchainIterator.go │ ├── CLI.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ └── utils.go └── main.go ├── part37-transaction-new-transaction-UTXO ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── BlockchainIterator.go │ ├── CLI.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ └── utils.go └── main.go ├── part38-transaction-new-transaction-UTXO ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── BlockchainIterator.go │ ├── CLI.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ └── utils.go └── main.go ├── part38-transaction-new-transaction-balance ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── BlockchainIterator.go │ ├── CLI.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ └── utils.go └── main.go ├── part39-transaction-new-transaction-send ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── BlockchainIterator.go │ ├── CLI.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── UTXO.go │ └── utils.go └── main.go ├── part4-Basic-Prototype ├── BLC │ ├── Block.go │ ├── Blockchain.go │ └── utils.go └── main.go ├── part40-transaction-new-transaction-cli ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_createblockchain.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_UTXO.go │ └── Utils.go └── main.go ├── part41-transaction-new-transaction-single-transaction ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_createblockchain.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_UTXO.go │ └── Utils.go └── main.go ├── part42-transaction-new-transaction-multi-transaction ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_createblockchain.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_UTXO.go │ └── Utils.go └── main.go ├── part43-sha256 └── main.go ├── part44-ripemd160 └── main.go ├── part45-base58 ├── BLC │ ├── base58.go │ └── utils.go └── main.go ├── part46-base64 └── main.go ├── part47-base64 └── main.go ├── part48-base58 ├── BLC │ ├── base58.go │ └── utils.go └── main.go ├── part49-base58-test ├── BLC │ ├── base58.go │ └── utils.go └── main.go ├── part5-proof-of-work ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── ProofOfWork.go │ └── utils.go └── main.go ├── part50-wallet ├── BLC │ ├── Base58.go │ ├── Utils.go │ └── Wallet.go └── main.go ├── part51-wallet-address ├── BLC │ ├── Base58.go │ ├── Utils.go │ └── Wallet.go └── main.go ├── part52-wallet-address ├── BLC │ ├── Base58.go │ ├── Utils.go │ └── Wallet.go └── main.go ├── part53-wallets ├── BLC │ ├── Base58.go │ ├── Utils.go │ ├── Wallet.go │ └── Wallets.go └── main.go ├── part54-wallets ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_UTXO.go │ ├── Utils.go │ ├── Wallet.go │ └── Wallets.go └── main.go ├── part55-wallets-getaddresslists ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_UTXO.go │ ├── Utils.go │ ├── Wallet.go │ └── Wallets.go └── main.go ├── part56-wallets-addresslists ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_UTXO.go │ ├── Utils.go │ ├── Wallet.go │ └── Wallets.go └── main.go ├── part57-wallets-input-output-update ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_UTXO.go │ ├── Utils.go │ ├── Wallet.go │ └── Wallets.go └── main.go ├── part58-wallets-Signature ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_UTXO.go │ ├── Utils.go │ ├── Wallet.go │ └── Wallets.go └── main.go ├── part59-wallets-Signature-Verify ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_UTXO.go │ ├── Utils.go │ ├── Wallet.go │ └── Wallets.go └── main.go ├── part6-proof-of-work ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── ProofOfWork.go │ └── utils.go └── main.go ├── part60-Transaction-Reward ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_UTXO.go │ ├── Utils.go │ ├── Wallet.go │ └── Wallets.go └── main.go ├── part61-UTXOSet ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Wallet.go │ └── Wallets.go └── main.go ├── part62-UTXOSet ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Wallet.go │ └── Wallets.go └── main.go ├── part63-UTXOSet ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── CLI_testmethod.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Wallet.go │ └── Wallets.go └── main.go ├── part64-UTXOSet ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── CLI_testmethod.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Wallet.go │ └── Wallets.go └── main.go ├── part65-UTXOSet-GetBalance ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── CLI_testmethod.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Wallet.go │ └── Wallets.go └── main.go ├── part66-UTXOSet-send ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── CLI_testmethod.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Wallet.go │ └── Wallets.go └── main.go ├── part67-UTXOSet-Update ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── CLI_testmethod.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Wallet.go │ └── Wallets.go └── main.go ├── part68-Merkle-tree ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── CLI_testmethod.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Wallet.go │ ├── Wallets.go │ └── merkle_tree.go └── main.go ├── part69-Merkle-tree ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_UTXO_Reset.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Wallet.go │ ├── Wallets.go │ └── merkle_tree.go └── main.go ├── part7-proof-of-work ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── ProofOfWork.go │ └── utils.go └── main.go ├── part70-Merkle-NODE_ID ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_UTXO_Reset.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Wallet.go │ ├── Wallets.go │ └── merkle_tree.go └── main.go ├── part71-Net-NODE_ID_CONFIG ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_UTXO_Reset.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Wallet.go │ ├── Wallets.go │ └── merkle_tree.go └── main.go ├── part72-Net-mine_cli ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_UTXO_Reset.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Wallet.go │ ├── Wallets.go │ └── merkle_tree.go └── main.go ├── part73-net-tcp ├── client ├── client.go ├── server └── server.go ├── part74-Net-TCP ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_UTXO_Reset.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── CLI_startnode.go │ ├── ProofOfWork.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Wallet.go │ ├── Wallets.go │ └── merkle_tree.go └── main.go ├── part75-Net-Server ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_UTXO_Reset.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── CLI_startnode.go │ ├── Constant.go │ ├── ProofOfWork.go │ ├── Server.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Wallet.go │ ├── Wallets.go │ └── merkle_tree.go └── main.go ├── part76-Net-Version ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_UTXO_Reset.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── CLI_startnode.go │ ├── Constant.go │ ├── ProofOfWork.go │ ├── Server.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Version.go │ ├── Wallet.go │ ├── Wallets.go │ └── merkle_tree.go └── main.go ├── part77-Net-GetBestHeight ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_UTXO_Reset.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── CLI_startnode.go │ ├── Constant.go │ ├── ProofOfWork.go │ ├── Server.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Version.go │ ├── Wallet.go │ ├── Wallets.go │ └── merkle_tree.go └── main.go ├── part78-Net-Conn ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_UTXO_Reset.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── CLI_startnode.go │ ├── Constant.go │ ├── ProofOfWork.go │ ├── Server.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Version.go │ ├── Wallet.go │ ├── Wallets.go │ └── merkle_tree.go └── main.go ├── part79-Net-HandleVersion ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_UTXO_Reset.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── CLI_startnode.go │ ├── Constant.go │ ├── ProofOfWork.go │ ├── Server.go │ ├── Server_handle.go │ ├── Server_send.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Version.go │ ├── Wallet.go │ ├── Wallets.go │ └── merkle_tree.go └── main.go ├── part8-proof-of-work ├── BLC │ ├── Block.go │ ├── Blockchain.go │ ├── ProofOfWork.go │ └── utils.go └── main.go ├── part80-Net-sendGetBlocks ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_UTXO_Reset.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── CLI_startnode.go │ ├── Constant.go │ ├── ProofOfWork.go │ ├── Server.go │ ├── Server_GetData.go │ ├── Server_Inv.go │ ├── Server_Version.go │ ├── Server_getblocks.go │ ├── Server_handle.go │ ├── Server_send.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Wallet.go │ ├── Wallets.go │ └── merkle_tree.go └── main.go ├── part81-Net-sendGetBlocks ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_UTXO_Reset.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── CLI_startnode.go │ ├── Constant.go │ ├── ProofOfWork.go │ ├── Server.go │ ├── Server_GetData.go │ ├── Server_Inv.go │ ├── Server_Version.go │ ├── Server_block.go │ ├── Server_getblocks.go │ ├── Server_handle.go │ ├── Server_send.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Wallet.go │ ├── Wallets.go │ └── merkle_tree.go └── main.go ├── part82-Net-blockhash_handle ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_UTXO_Reset.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── CLI_startnode.go │ ├── Constant.go │ ├── ProofOfWork.go │ ├── Server.go │ ├── Server_GetData.go │ ├── Server_Inv.go │ ├── Server_Version.go │ ├── Server_block.go │ ├── Server_getblocks.go │ ├── Server_handle.go │ ├── Server_send.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Wallet.go │ ├── Wallets.go │ └── merkle_tree.go └── main.go ├── part83-Net-handleblock ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_UTXO_Reset.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── CLI_startnode.go │ ├── Constant.go │ ├── ProofOfWork.go │ ├── Server.go │ ├── Server_GetData.go │ ├── Server_Inv.go │ ├── Server_Version.go │ ├── Server_block.go │ ├── Server_getblocks.go │ ├── Server_handle.go │ ├── Server_send.go │ ├── Server_var.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Wallet.go │ ├── Wallets.go │ └── merkle_tree.go └── main.go ├── part84-Net-validation ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_UTXO_Reset.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── CLI_startnode.go │ ├── Constant.go │ ├── ProofOfWork.go │ ├── Server.go │ ├── Server_GetData.go │ ├── Server_Inv.go │ ├── Server_Version.go │ ├── Server_block.go │ ├── Server_getblocks.go │ ├── Server_handle.go │ ├── Server_send.go │ ├── Server_var.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Wallet.go │ ├── Wallets.go │ └── merkle_tree.go └── main.go ├── part85-Net-validation-fix-bug ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_UTXO_Reset.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── CLI_startnode.go │ ├── Constant.go │ ├── ProofOfWork.go │ ├── Server.go │ ├── Server_GetData.go │ ├── Server_Inv.go │ ├── Server_Version.go │ ├── Server_block.go │ ├── Server_getblocks.go │ ├── Server_handle.go │ ├── Server_send.go │ ├── Server_var.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Wallet.go │ ├── Wallets.go │ └── merkle_tree.go └── main.go ├── part86-Net-send-logcal ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_UTXO_Reset.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── CLI_startnode.go │ ├── Constant.go │ ├── ProofOfWork.go │ ├── Server.go │ ├── Server_GetData.go │ ├── Server_Inv.go │ ├── Server_Tx.go │ ├── Server_Version.go │ ├── Server_block.go │ ├── Server_getblocks.go │ ├── Server_handle.go │ ├── Server_send.go │ ├── Server_var.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Wallet.go │ ├── Wallets.go │ └── merkle_tree.go └── main.go ├── part87-Net-send-logcal ├── BLC │ ├── Base58.go │ ├── Block.go │ ├── Blockchain.go │ ├── Blockchain_Iterator.go │ ├── CLI.go │ ├── CLI_UTXO_Reset.go │ ├── CLI_createWallet.go │ ├── CLI_createblockchain.go │ ├── CLI_getaddresslists.go │ ├── CLI_getbalance.go │ ├── CLI_printchain.go │ ├── CLI_send.go │ ├── CLI_startnode.go │ ├── Constant.go │ ├── ProofOfWork.go │ ├── Server.go │ ├── Server_GetData.go │ ├── Server_Inv.go │ ├── Server_Tx.go │ ├── Server_Version.go │ ├── Server_block.go │ ├── Server_getblocks.go │ ├── Server_handle.go │ ├── Server_send.go │ ├── Server_var.go │ ├── Transaction.go │ ├── Transaction_TXInput.go │ ├── Transaction_TXOutput.go │ ├── Transaction_TXOutputs.go │ ├── Transaction_UTXO.go │ ├── UTXO_Set.go │ ├── Utils.go │ ├── Wallet.go │ ├── Wallets.go │ └── merkle_tree.go └── main.go └── part9-Serialize-DeserializeBlock ├── BLC ├── Block.go ├── Blockchain.go ├── ProofOfWork.go └── utils.go └── main.go /part1-Basic-Prototype/BLC/Blockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | -------------------------------------------------------------------------------- /part1-Basic-Prototype/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "log" 7 | ) 8 | 9 | // 将int64转换为字节数组 10 | func IntToHex(num int64) []byte { 11 | buff := new(bytes.Buffer) 12 | err := binary.Write(buff, binary.BigEndian, num) 13 | if err != nil { 14 | log.Panic(err) 15 | } 16 | 17 | return buff.Bytes() 18 | } 19 | -------------------------------------------------------------------------------- /part1-Basic-Prototype/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "kongyixueyuan.com/publicChain/part1-Basic-Prototype/BLC" 5 | "fmt" 6 | ) 7 | 8 | func main() { 9 | 10 | block := BLC.NewBlock("Genenis Block",1,[]byte{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}) 11 | 12 | fmt.Println(block) 13 | 14 | } 15 | -------------------------------------------------------------------------------- /part10-boltdb-create-db/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/boltdb/bolt" 5 | "log" 6 | ) 7 | 8 | func main() { 9 | // Open the my.db data file in your current directory. 10 | // It will be created if it doesn't exist. 11 | db, err := bolt.Open("my.db", 0600, nil) 12 | if err != nil { 13 | log.Fatal(err) 14 | } 15 | defer db.Close() 16 | } -------------------------------------------------------------------------------- /part14-block-boltdb/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "log" 7 | ) 8 | 9 | // 将int64转换为字节数组 10 | func IntToHex(num int64) []byte { 11 | buff := new(bytes.Buffer) 12 | err := binary.Write(buff, binary.BigEndian, num) 13 | if err != nil { 14 | log.Panic(err) 15 | } 16 | 17 | return buff.Bytes() 18 | } 19 | -------------------------------------------------------------------------------- /part15-persistence-creategenesisblock/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "log" 7 | ) 8 | 9 | // 将int64转换为字节数组 10 | func IntToHex(num int64) []byte { 11 | buff := new(bytes.Buffer) 12 | err := binary.Write(buff, binary.BigEndian, num) 13 | if err != nil { 14 | log.Panic(err) 15 | } 16 | 17 | return buff.Bytes() 18 | } 19 | -------------------------------------------------------------------------------- /part16-persistence-addblock/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "log" 7 | ) 8 | 9 | // 将int64转换为字节数组 10 | func IntToHex(num int64) []byte { 11 | buff := new(bytes.Buffer) 12 | err := binary.Write(buff, binary.BigEndian, num) 13 | if err != nil { 14 | log.Panic(err) 15 | } 16 | 17 | return buff.Bytes() 18 | } 19 | -------------------------------------------------------------------------------- /part17-persistence-Iterator/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "log" 7 | ) 8 | 9 | // 将int64转换为字节数组 10 | func IntToHex(num int64) []byte { 11 | buff := new(bytes.Buffer) 12 | err := binary.Write(buff, binary.BigEndian, num) 13 | if err != nil { 14 | log.Panic(err) 15 | } 16 | 17 | return buff.Bytes() 18 | } 19 | -------------------------------------------------------------------------------- /part18-persistence-Iterator-time-format/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "log" 7 | ) 8 | 9 | // 将int64转换为字节数组 10 | func IntToHex(num int64) []byte { 11 | buff := new(bytes.Buffer) 12 | err := binary.Write(buff, binary.BigEndian, num) 13 | if err != nil { 14 | log.Panic(err) 15 | } 16 | 17 | return buff.Bytes() 18 | } 19 | -------------------------------------------------------------------------------- /part19-persistence-Iterator/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "log" 7 | ) 8 | 9 | // 将int64转换为字节数组 10 | func IntToHex(num int64) []byte { 11 | buff := new(bytes.Buffer) 12 | err := binary.Write(buff, binary.BigEndian, num) 13 | if err != nil { 14 | log.Panic(err) 15 | } 16 | 17 | return buff.Bytes() 18 | } 19 | -------------------------------------------------------------------------------- /part2-Basic-Prototype/BLC/Blockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | -------------------------------------------------------------------------------- /part2-Basic-Prototype/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "log" 7 | ) 8 | 9 | // 将int64转换为字节数组 10 | func IntToHex(num int64) []byte { 11 | buff := new(bytes.Buffer) 12 | err := binary.Write(buff, binary.BigEndian, num) 13 | if err != nil { 14 | log.Panic(err) 15 | } 16 | 17 | return buff.Bytes() 18 | } 19 | -------------------------------------------------------------------------------- /part2-Basic-Prototype/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "kongyixueyuan.com/publicChain/part2-Basic-Prototype/BLC" 5 | "fmt" 6 | ) 7 | 8 | func main() { 9 | 10 | genesisBlock := BLC.CreateGenesisBlock("Genesis Block.....") 11 | 12 | fmt.Println(genesisBlock) 13 | 14 | } 15 | -------------------------------------------------------------------------------- /part20-persistence-Iterator-seperate/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "log" 7 | ) 8 | 9 | // 将int64转换为字节数组 10 | func IntToHex(num int64) []byte { 11 | buff := new(bytes.Buffer) 12 | err := binary.Write(buff, binary.BigEndian, num) 13 | if err != nil { 14 | log.Panic(err) 15 | } 16 | 17 | return buff.Bytes() 18 | } 19 | -------------------------------------------------------------------------------- /part22-cli-osArgs/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | "fmt" 6 | ) 7 | 8 | func main() { 9 | 10 | args := os.Args; 11 | 12 | fmt.Printf("%v\n",args) 13 | fmt.Printf("%v\n",args[1]) 14 | 15 | } 16 | 17 | 18 | //go build -o bc main.go 19 | 20 | //bc 21 | // ./bc addBlock -data "liyuechun.org" 22 | 23 | 24 | // ./bc printchain 25 | // 即将输出所有block 26 | -------------------------------------------------------------------------------- /part24-persistence-cli/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "log" 7 | ) 8 | 9 | // 将int64转换为字节数组 10 | func IntToHex(num int64) []byte { 11 | buff := new(bytes.Buffer) 12 | err := binary.Write(buff, binary.BigEndian, num) 13 | if err != nil { 14 | log.Panic(err) 15 | } 16 | 17 | return buff.Bytes() 18 | } 19 | -------------------------------------------------------------------------------- /part24-persistence-cli/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "kongyixueyuan.com/publicChain/part24-persistence-cli/BLC" 4 | 5 | func main() { 6 | 7 | blockchain := BLC.CreateBlockchainWithGenesisBlock() 8 | 9 | cli := BLC.CLI{blockchain} 10 | 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part25-persistence-cli-createblockchain/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "log" 7 | ) 8 | 9 | // 将int64转换为字节数组 10 | func IntToHex(num int64) []byte { 11 | buff := new(bytes.Buffer) 12 | err := binary.Write(buff, binary.BigEndian, num) 13 | if err != nil { 14 | log.Panic(err) 15 | } 16 | 17 | return buff.Bytes() 18 | } 19 | -------------------------------------------------------------------------------- /part25-persistence-cli-createblockchain/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "kongyixueyuan.com/publicChain/part25-persistence-cli-createblockchain/BLC" 4 | 5 | func main() { 6 | 7 | cli := BLC.CLI{} 8 | 9 | cli.Run() 10 | } -------------------------------------------------------------------------------- /part26-persistence-cli-modify-creategenesismethod/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "log" 7 | ) 8 | 9 | // 将int64转换为字节数组 10 | func IntToHex(num int64) []byte { 11 | buff := new(bytes.Buffer) 12 | err := binary.Write(buff, binary.BigEndian, num) 13 | if err != nil { 14 | log.Panic(err) 15 | } 16 | 17 | return buff.Bytes() 18 | } 19 | -------------------------------------------------------------------------------- /part26-persistence-cli-modify-creategenesismethod/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "kongyixueyuan.com/publicChain/part26-persistence-cli-modify-creategenesismethod/BLC" 4 | 5 | func main() { 6 | 7 | cli := BLC.CLI{} 8 | 9 | cli.Run() 10 | } -------------------------------------------------------------------------------- /part27-persistence-cli-blockchain-object/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "log" 7 | ) 8 | 9 | // 将int64转换为字节数组 10 | func IntToHex(num int64) []byte { 11 | buff := new(bytes.Buffer) 12 | err := binary.Write(buff, binary.BigEndian, num) 13 | if err != nil { 14 | log.Panic(err) 15 | } 16 | 17 | return buff.Bytes() 18 | } 19 | -------------------------------------------------------------------------------- /part27-persistence-cli-blockchain-object/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "kongyixueyuan.com/publicChain/part27-persistence-cli-blockchain-object/BLC" 5 | ) 6 | 7 | func main() { 8 | 9 | cli := BLC.CLI{} 10 | cli.Run() 11 | } -------------------------------------------------------------------------------- /part28-transaction/BLC/Transaction.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // UTXO 5 | type Transaction struct { 6 | 7 | //1. 交易hash 8 | 9 | TxHash []byte 10 | 11 | //2. 输入 12 | 13 | //3. 输出 14 | } 15 | 16 | 17 | -------------------------------------------------------------------------------- /part28-transaction/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "log" 7 | ) 8 | 9 | // 将int64转换为字节数组 10 | func IntToHex(num int64) []byte { 11 | buff := new(bytes.Buffer) 12 | err := binary.Write(buff, binary.BigEndian, num) 13 | if err != nil { 14 | log.Panic(err) 15 | } 16 | 17 | return buff.Bytes() 18 | } 19 | -------------------------------------------------------------------------------- /part28-transaction/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "kongyixueyuan.com/publicChain/part28-transaction/BLC" 5 | ) 6 | 7 | func main() { 8 | 9 | cli := BLC.CLI{} 10 | cli.Run() 11 | } -------------------------------------------------------------------------------- /part29-transaction-UTXO/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type TXInput struct { 4 | // 1. 交易的Hash 5 | TxHash []byte 6 | // 2. 存储TXOutput在Vout里面的索引 7 | Vout int 8 | // 3. 用户名 9 | ScriptSig string // 花费的是谁的钱 10 | } 11 | -------------------------------------------------------------------------------- /part29-transaction-UTXO/BLC/Transaction_TXOutput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | 5 | //TXOutput{100,"zhangbozhi"} 6 | //TXOutput{30,"xietingfeng"} 7 | //TXOutput{40,"zhangbozhi"} 8 | 9 | 10 | type TXOutput struct { 11 | Value int64 12 | ScriptPubKey string //用户名 13 | } 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /part29-transaction-UTXO/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "log" 7 | ) 8 | 9 | // 将int64转换为字节数组 10 | func IntToHex(num int64) []byte { 11 | buff := new(bytes.Buffer) 12 | err := binary.Write(buff, binary.BigEndian, num) 13 | if err != nil { 14 | log.Panic(err) 15 | } 16 | 17 | return buff.Bytes() 18 | } 19 | -------------------------------------------------------------------------------- /part29-transaction-UTXO/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "kongyixueyuan.com/publicChain/part29-transaction-UTXO/BLC" 5 | ) 6 | 7 | func main() { 8 | 9 | cli := BLC.CLI{} 10 | cli.Run() 11 | } -------------------------------------------------------------------------------- /part3-Basic-Prototype/BLC/Blockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type Blockchain struct { 4 | Blocks []*Block // 存储有序的区块 5 | } 6 | 7 | 8 | 9 | 10 | 11 | //1. 创建带有创世区块的区块链 12 | func CreateBlockchainWithGenesisBlock() *Blockchain { 13 | // 创建创世区块 14 | genesisBlock := CreateGenesisBlock("Genesis Data.......") 15 | // 返回区块链对象 16 | return &Blockchain{[]*Block{genesisBlock}} 17 | } -------------------------------------------------------------------------------- /part3-Basic-Prototype/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "log" 7 | ) 8 | 9 | // 将int64转换为字节数组 10 | func IntToHex(num int64) []byte { 11 | buff := new(bytes.Buffer) 12 | err := binary.Write(buff, binary.BigEndian, num) 13 | if err != nil { 14 | log.Panic(err) 15 | } 16 | 17 | return buff.Bytes() 18 | } 19 | -------------------------------------------------------------------------------- /part3-Basic-Prototype/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "kongyixueyuan.com/publicChain/part3-Basic-Prototype/BLC" 5 | "fmt" 6 | ) 7 | 8 | func main() { 9 | 10 | genesisBlockchain := BLC.CreateBlockchainWithGenesisBlock() 11 | 12 | fmt.Println(genesisBlockchain) 13 | 14 | fmt.Println(genesisBlockchain.Blocks) 15 | fmt.Println(genesisBlockchain.Blocks[0]) 16 | 17 | } 18 | -------------------------------------------------------------------------------- /part30-transaction-UTXO-send/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type TXInput struct { 4 | // 1. 交易的Hash 5 | TxHash []byte 6 | // 2. 存储TXOutput在Vout里面的索引 7 | Vout int 8 | // 3. 用户名 9 | ScriptSig string 10 | } 11 | -------------------------------------------------------------------------------- /part30-transaction-UTXO-send/BLC/Transaction_TXOutput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | 5 | //TXOutput{100,"zhangbozhi"} 6 | //TXOutput{30,"xietingfeng"} 7 | //TXOutput{40,"zhangbozhi"} 8 | 9 | 10 | type TXOutput struct { 11 | Value int64 12 | ScriptPubKey string //用户名 13 | } 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /part30-transaction-UTXO-send/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "log" 7 | ) 8 | 9 | // 将int64转换为字节数组 10 | func IntToHex(num int64) []byte { 11 | buff := new(bytes.Buffer) 12 | err := binary.Write(buff, binary.BigEndian, num) 13 | if err != nil { 14 | log.Panic(err) 15 | } 16 | 17 | return buff.Bytes() 18 | } 19 | -------------------------------------------------------------------------------- /part30-transaction-UTXO-send/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "kongyixueyuan.com/publicChain/part30-transaction-UTXO-send/BLC" 5 | ) 6 | 7 | func main() { 8 | 9 | cli := BLC.CLI{} 10 | cli.Run() 11 | } -------------------------------------------------------------------------------- /part31-transaction-UTXO-JSON-TO-Array/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type TXInput struct { 4 | // 1. 交易的Hash 5 | TxHash []byte 6 | // 2. 存储TXOutput在Vout里面的索引 7 | Vout int 8 | // 3. 用户名 9 | ScriptSig string 10 | } 11 | -------------------------------------------------------------------------------- /part31-transaction-UTXO-JSON-TO-Array/BLC/Transaction_TXOutput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | 5 | //TXOutput{100,"zhangbozhi"} 6 | //TXOutput{30,"xietingfeng"} 7 | //TXOutput{40,"zhangbozhi"} 8 | 9 | 10 | type TXOutput struct { 11 | Value int64 12 | ScriptPubKey string //用户名 13 | } 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /part31-transaction-UTXO-JSON-TO-Array/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "kongyixueyuan.com/publicChain/part31-transaction-UTXO-JSON-TO-Array/BLC" 5 | ) 6 | 7 | func main() { 8 | 9 | cli := BLC.CLI{} 10 | cli.Run() 11 | } -------------------------------------------------------------------------------- /part32-transaction-UTXO-Send-Method/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type TXInput struct { 4 | // 1. 交易的Hash 5 | TxHash []byte 6 | // 2. 存储TXOutput在Vout里面的索引 7 | Vout int 8 | // 3. 用户名 9 | ScriptSig string 10 | } 11 | -------------------------------------------------------------------------------- /part32-transaction-UTXO-Send-Method/BLC/Transaction_TXOutput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | 5 | //TXOutput{100,"zhangbozhi"} 6 | //TXOutput{30,"xietingfeng"} 7 | //TXOutput{40,"zhangbozhi"} 8 | 9 | 10 | type TXOutput struct { 11 | Value int64 12 | ScriptPubKey string //用户名 13 | } 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /part32-transaction-UTXO-Send-Method/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "kongyixueyuan.com/publicChain/part32-transaction-UTXO-Send-Method/BLC" 5 | ) 6 | 7 | func main() { 8 | 9 | cli := BLC.CLI{} 10 | cli.Run() 11 | } -------------------------------------------------------------------------------- /part33-transaction-UTXO-NewBlock/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type TXInput struct { 4 | // 1. 交易的Hash 5 | TxHash []byte 6 | // 2. 存储TXOutput在Vout里面的索引 7 | Vout int 8 | // 3. 用户名 9 | ScriptSig string 10 | } 11 | -------------------------------------------------------------------------------- /part33-transaction-UTXO-NewBlock/BLC/Transaction_TXOutput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | 5 | //TXOutput{100,"zhangbozhi"} 6 | //TXOutput{30,"xietingfeng"} 7 | //TXOutput{40,"zhangbozhi"} 8 | 9 | 10 | type TXOutput struct { 11 | Value int64 12 | ScriptPubKey string //用户名 13 | } 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /part33-transaction-UTXO-NewBlock/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "kongyixueyuan.com/publicChain/part33-transaction-UTXO-NewBlock/BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part34-transaction-new-transaction-block2/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type TXInput struct { 4 | // 1. 交易的Hash 5 | TxHash []byte 6 | // 2. 存储TXOutput在Vout里面的索引 7 | Vout int 8 | // 3. 用户名 9 | ScriptSig string 10 | } 11 | -------------------------------------------------------------------------------- /part34-transaction-new-transaction-block2/BLC/Transaction_TXOutput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | 5 | //TXOutput{100,"zhangbozhi"} 6 | //TXOutput{30,"xietingfeng"} 7 | //TXOutput{40,"zhangbozhi"} 8 | 9 | 10 | type TXOutput struct { 11 | Value int64 12 | ScriptPubKey string //用户名 13 | } 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /part34-transaction-new-transaction-block2/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "kongyixueyuan.com/publicChain/part34-transaction-new-transaction-block2/BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part35-transaction-new-transaction-block3/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type TXInput struct { 4 | // 1. 交易的Hash 5 | TxHash []byte 6 | // 2. 存储TXOutput在Vout里面的索引 7 | Vout int 8 | // 3. 用户名 9 | ScriptSig string 10 | } 11 | -------------------------------------------------------------------------------- /part35-transaction-new-transaction-block3/BLC/Transaction_TXOutput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | 5 | //TXOutput{100,"zhangbozhi"} 6 | //TXOutput{30,"xietingfeng"} 7 | //TXOutput{40,"zhangbozhi"} 8 | 9 | 10 | type TXOutput struct { 11 | Value int64 12 | ScriptPubKey string //用户名 13 | } 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /part35-transaction-new-transaction-block3/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "kongyixueyuan.com/publicChain/part35-transaction-new-transaction-block3/BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part36-transaction-new-transaction/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type TXInput struct { 4 | // 1. 交易的Hash 5 | TxHash []byte 6 | // 2. 存储TXOutput在Vout里面的索引 7 | Vout int 8 | // 3. 用户名 9 | ScriptSig string 10 | } 11 | -------------------------------------------------------------------------------- /part36-transaction-new-transaction/BLC/Transaction_TXOutput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | 5 | //TXOutput{100,"zhangbozhi"} 6 | //TXOutput{30,"xietingfeng"} 7 | //TXOutput{40,"zhangbozhi"} 8 | 9 | 10 | type TXOutput struct { 11 | Value int64 12 | ScriptPubKey string //用户名 13 | } 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /part36-transaction-new-transaction/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "kongyixueyuan.com/publicChain/part36-transaction-new-transaction/BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part37-transaction-new-transaction-UTXO/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type TXInput struct { 4 | // 1. 交易的Hash 5 | TxHash []byte 6 | // 2. 存储TXOutput在Vout里面的索引 7 | Vout int 8 | // 3. 用户名 9 | ScriptSig string 10 | } 11 | 12 | 13 | 14 | // 判断当前的消费是谁的钱 15 | func (txInput *TXInput) UnLockWithAddress(address string) bool { 16 | 17 | return txInput.ScriptSig == address 18 | } -------------------------------------------------------------------------------- /part37-transaction-new-transaction-UTXO/BLC/Transaction_TXOutput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | 5 | //TXOutput{100,"zhangbozhi"} 6 | //TXOutput{30,"xietingfeng"} 7 | //TXOutput{40,"zhangbozhi"} 8 | 9 | 10 | type TXOutput struct { 11 | Value int64 12 | ScriptPubKey string //用户名 13 | } 14 | 15 | // 解锁 16 | func (txOutput *TXOutput) UnLockScriptPubKeyWithAddress(address string) bool { 17 | 18 | return txOutput.ScriptPubKey == address 19 | } 20 | 21 | 22 | -------------------------------------------------------------------------------- /part37-transaction-new-transaction-UTXO/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "kongyixueyuan.com/publicChain/part37-transaction-new-transaction-UTXO/BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part38-transaction-new-transaction-UTXO/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type TXInput struct { 4 | // 1. 交易的Hash 5 | TxHash []byte 6 | // 2. 存储TXOutput在Vout里面的索引 7 | Vout int 8 | // 3. 用户名 9 | ScriptSig string 10 | } 11 | 12 | 13 | 14 | // 判断当前的消费是谁的钱 15 | func (txInput *TXInput) UnLockWithAddress(address string) bool { 16 | 17 | return txInput.ScriptSig == address 18 | } -------------------------------------------------------------------------------- /part38-transaction-new-transaction-UTXO/BLC/Transaction_TXOutput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | 5 | //TXOutput{100,"zhangbozhi"} 6 | //TXOutput{30,"xietingfeng"} 7 | //TXOutput{40,"zhangbozhi"} 8 | 9 | 10 | type TXOutput struct { 11 | Value int64 12 | ScriptPubKey string //用户名 13 | } 14 | 15 | // 解锁 16 | func (txOutput *TXOutput) UnLockScriptPubKeyWithAddress(address string) bool { 17 | 18 | return txOutput.ScriptPubKey == address 19 | } 20 | 21 | 22 | -------------------------------------------------------------------------------- /part38-transaction-new-transaction-UTXO/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "kongyixueyuan.com/publicChain/part37-transaction-new-transaction-UTXO/BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part38-transaction-new-transaction-balance/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type TXInput struct { 4 | // 1. 交易的Hash 5 | TxHash []byte 6 | // 2. 存储TXOutput在Vout里面的索引 7 | Vout int 8 | // 3. 用户名 9 | ScriptSig string 10 | } 11 | 12 | 13 | 14 | // 判断当前的消费是谁的钱 15 | func (txInput *TXInput) UnLockWithAddress(address string) bool { 16 | 17 | return txInput.ScriptSig == address 18 | } -------------------------------------------------------------------------------- /part38-transaction-new-transaction-balance/BLC/Transaction_TXOutput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | 5 | //TXOutput{100,"zhangbozhi"} 6 | //TXOutput{30,"xietingfeng"} 7 | //TXOutput{40,"zhangbozhi"} 8 | 9 | 10 | type TXOutput struct { 11 | Value int64 12 | ScriptPubKey string //用户名 13 | } 14 | 15 | // 解锁 16 | func (txOutput *TXOutput) UnLockScriptPubKeyWithAddress(address string) bool { 17 | 18 | return txOutput.ScriptPubKey == address 19 | } 20 | 21 | 22 | -------------------------------------------------------------------------------- /part38-transaction-new-transaction-balance/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "kongyixueyuan.com/publicChain/part38-transaction-new-transaction-balance/BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part39-transaction-new-transaction-send/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type TXInput struct { 4 | // 1. 交易的Hash 5 | TxHash []byte 6 | // 2. 存储TXOutput在Vout里面的索引 7 | Vout int 8 | // 3. 用户名 9 | ScriptSig string 10 | } 11 | 12 | 13 | 14 | // 判断当前的消费是谁的钱 15 | func (txInput *TXInput) UnLockWithAddress(address string) bool { 16 | 17 | return txInput.ScriptSig == address 18 | } -------------------------------------------------------------------------------- /part39-transaction-new-transaction-send/BLC/Transaction_TXOutput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | 5 | //TXOutput{100,"zhangbozhi"} 6 | //TXOutput{30,"xietingfeng"} 7 | //TXOutput{40,"zhangbozhi"} 8 | 9 | 10 | type TXOutput struct { 11 | Value int64 12 | ScriptPubKey string //用户名 13 | } 14 | 15 | // 解锁 16 | func (txOutput *TXOutput) UnLockScriptPubKeyWithAddress(address string) bool { 17 | 18 | return txOutput.ScriptPubKey == address 19 | } 20 | 21 | 22 | -------------------------------------------------------------------------------- /part39-transaction-new-transaction-send/BLC/UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | -------------------------------------------------------------------------------- /part39-transaction-new-transaction-send/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "kongyixueyuan.com/publicChain/part39-transaction-new-transaction-send/BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part4-Basic-Prototype/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "log" 7 | ) 8 | 9 | // 将int64转换为字节数组 10 | func IntToHex(num int64) []byte { 11 | buff := new(bytes.Buffer) 12 | err := binary.Write(buff, binary.BigEndian, num) 13 | if err != nil { 14 | log.Panic(err) 15 | } 16 | 17 | return buff.Bytes() 18 | } 19 | -------------------------------------------------------------------------------- /part40-transaction-new-transaction-cli/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address) 8 | defer blockchain.DB.Close() 9 | } -------------------------------------------------------------------------------- /part40-transaction-new-transaction-cli/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | blockchain := BlockchainObject() 11 | defer blockchain.DB.Close() 12 | 13 | amount := blockchain.GetBalance(address) 14 | 15 | fmt.Printf("%s一共有%d个Token\n",address,amount) 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /part40-transaction-new-transaction-cli/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) printchain() { 9 | 10 | if DBExists() == false { 11 | fmt.Println("数据不存在.......") 12 | os.Exit(1) 13 | } 14 | 15 | blockchain := BlockchainObject() 16 | 17 | defer blockchain.DB.Close() 18 | 19 | blockchain.Printchain() 20 | 21 | } -------------------------------------------------------------------------------- /part40-transaction-new-transaction-cli/BLC/CLI_send.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // 转账 9 | func (cli *CLI) send(from []string,to []string,amount []string) { 10 | 11 | 12 | if DBExists() == false { 13 | fmt.Println("数据不存在.......") 14 | os.Exit(1) 15 | } 16 | 17 | blockchain := BlockchainObject() 18 | defer blockchain.DB.Close() 19 | 20 | blockchain.MineNewBlock(from,to,amount) 21 | 22 | } -------------------------------------------------------------------------------- /part40-transaction-new-transaction-cli/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type TXInput struct { 4 | // 1. 交易的Hash 5 | TxHash []byte 6 | // 2. 存储TXOutput在Vout里面的索引 7 | Vout int 8 | // 3. 用户名 9 | ScriptSig string 10 | } 11 | 12 | 13 | 14 | // 判断当前的消费是谁的钱 15 | func (txInput *TXInput) UnLockWithAddress(address string) bool { 16 | 17 | return txInput.ScriptSig == address 18 | } -------------------------------------------------------------------------------- /part40-transaction-new-transaction-cli/BLC/Transaction_TXOutput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | 5 | //TXOutput{100,"zhangbozhi"} 6 | //TXOutput{30,"xietingfeng"} 7 | //TXOutput{40,"zhangbozhi"} 8 | 9 | 10 | type TXOutput struct { 11 | Value int64 12 | ScriptPubKey string //用户名 13 | } 14 | 15 | // 解锁 16 | func (txOutput *TXOutput) UnLockScriptPubKeyWithAddress(address string) bool { 17 | 18 | return txOutput.ScriptPubKey == address 19 | } 20 | 21 | 22 | -------------------------------------------------------------------------------- /part40-transaction-new-transaction-cli/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | -------------------------------------------------------------------------------- /part40-transaction-new-transaction-cli/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "kongyixueyuan.com/publicChain/part40-transaction-new-transaction-cli/BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part41-transaction-new-transaction-single-transaction/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address) 8 | defer blockchain.DB.Close() 9 | } -------------------------------------------------------------------------------- /part41-transaction-new-transaction-single-transaction/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | blockchain := BlockchainObject() 11 | defer blockchain.DB.Close() 12 | 13 | amount := blockchain.GetBalance(address) 14 | 15 | fmt.Printf("%s一共有%d个Token\n",address,amount) 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /part41-transaction-new-transaction-single-transaction/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) printchain() { 9 | 10 | if DBExists() == false { 11 | fmt.Println("数据不存在.......") 12 | os.Exit(1) 13 | } 14 | 15 | blockchain := BlockchainObject() 16 | 17 | defer blockchain.DB.Close() 18 | 19 | blockchain.Printchain() 20 | 21 | } -------------------------------------------------------------------------------- /part41-transaction-new-transaction-single-transaction/BLC/CLI_send.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // 转账 9 | func (cli *CLI) send(from []string,to []string,amount []string) { 10 | 11 | 12 | if DBExists() == false { 13 | fmt.Println("数据不存在.......") 14 | os.Exit(1) 15 | } 16 | 17 | blockchain := BlockchainObject() 18 | defer blockchain.DB.Close() 19 | 20 | blockchain.MineNewBlock(from,to,amount) 21 | 22 | } -------------------------------------------------------------------------------- /part41-transaction-new-transaction-single-transaction/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type TXInput struct { 4 | // 1. 交易的Hash 5 | TxHash []byte 6 | // 2. 存储TXOutput在Vout里面的索引 7 | Vout int 8 | // 3. 用户名 9 | ScriptSig string 10 | } 11 | 12 | 13 | 14 | // 判断当前的消费是谁的钱 15 | func (txInput *TXInput) UnLockWithAddress(address string) bool { 16 | 17 | return txInput.ScriptSig == address 18 | } -------------------------------------------------------------------------------- /part41-transaction-new-transaction-single-transaction/BLC/Transaction_TXOutput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | 5 | //TXOutput{100,"zhangbozhi"} 6 | //TXOutput{30,"xietingfeng"} 7 | //TXOutput{40,"zhangbozhi"} 8 | 9 | 10 | type TXOutput struct { 11 | Value int64 12 | ScriptPubKey string //用户名 13 | } 14 | 15 | // 解锁 16 | func (txOutput *TXOutput) UnLockScriptPubKeyWithAddress(address string) bool { 17 | 18 | return txOutput.ScriptPubKey == address 19 | } 20 | 21 | 22 | -------------------------------------------------------------------------------- /part41-transaction-new-transaction-single-transaction/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part41-transaction-new-transaction-single-transaction/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "kongyixueyuan.com/publicChain/part41-transaction-new-transaction-single-transaction/BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part42-transaction-new-transaction-multi-transaction/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address) 8 | defer blockchain.DB.Close() 9 | } -------------------------------------------------------------------------------- /part42-transaction-new-transaction-multi-transaction/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | blockchain := BlockchainObject() 11 | defer blockchain.DB.Close() 12 | 13 | amount := blockchain.GetBalance(address) 14 | 15 | fmt.Printf("%s一共有%d个Token\n",address,amount) 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /part42-transaction-new-transaction-multi-transaction/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) printchain() { 9 | 10 | if DBExists() == false { 11 | fmt.Println("数据不存在.......") 12 | os.Exit(1) 13 | } 14 | 15 | blockchain := BlockchainObject() 16 | 17 | defer blockchain.DB.Close() 18 | 19 | blockchain.Printchain() 20 | 21 | } -------------------------------------------------------------------------------- /part42-transaction-new-transaction-multi-transaction/BLC/CLI_send.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // 转账 9 | func (cli *CLI) send(from []string,to []string,amount []string) { 10 | 11 | 12 | if DBExists() == false { 13 | fmt.Println("数据不存在.......") 14 | os.Exit(1) 15 | } 16 | 17 | blockchain := BlockchainObject() 18 | defer blockchain.DB.Close() 19 | 20 | blockchain.MineNewBlock(from,to,amount) 21 | 22 | } -------------------------------------------------------------------------------- /part42-transaction-new-transaction-multi-transaction/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type TXInput struct { 4 | // 1. 交易的Hash 5 | TxHash []byte 6 | // 2. 存储TXOutput在Vout里面的索引 7 | Vout int 8 | // 3. 用户名 9 | ScriptSig string 10 | } 11 | 12 | 13 | 14 | // 判断当前的消费是谁的钱 15 | func (txInput *TXInput) UnLockWithAddress(address string) bool { 16 | 17 | return txInput.ScriptSig == address 18 | } -------------------------------------------------------------------------------- /part42-transaction-new-transaction-multi-transaction/BLC/Transaction_TXOutput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | 5 | //TXOutput{100,"zhangbozhi"} 6 | //TXOutput{30,"xietingfeng"} 7 | //TXOutput{40,"zhangbozhi"} 8 | 9 | 10 | type TXOutput struct { 11 | Value int64 12 | ScriptPubKey string //用户名 13 | } 14 | 15 | // 解锁 16 | func (txOutput *TXOutput) UnLockScriptPubKeyWithAddress(address string) bool { 17 | 18 | return txOutput.ScriptPubKey == address 19 | } 20 | 21 | 22 | -------------------------------------------------------------------------------- /part42-transaction-new-transaction-multi-transaction/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part42-transaction-new-transaction-multi-transaction/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "kongyixueyuan.com/publicChain/part42-transaction-new-transaction-multi-transaction/BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part43-sha256/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "crypto/sha256" 5 | "fmt" 6 | ) 7 | 8 | func main() { 9 | 10 | // 256 11 | //7b28134d636423d716aaa45227099629783ae6d6012b331049466658b88bf3b5 12 | // 0111 1011 13 | hasher := sha256.New() 14 | 15 | hasher.Write([]byte("http://liyuechun.org")) 16 | 17 | bytes := hasher.Sum(nil) 18 | 19 | fmt.Printf("%x\n",bytes) 20 | //7b28134d636423d716aaa45227099629783ae6d6012b331049466658b88bf3b5 21 | 22 | } 23 | -------------------------------------------------------------------------------- /part44-ripemd160/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "golang.org/x/crypto/ripemd160" 5 | "fmt" 6 | ) 7 | 8 | func main() { 9 | 10 | //sha256 256bit 11 | //ripemd160 160bit 12 | 13 | 14 | // 160 15 | // b66140b4bfd22da44399f352b07182864098123f 16 | // bit 160 20个字节 17 | hasher := ripemd160.New() 18 | 19 | hasher.Write([]byte("http://liyuechun.org")) 20 | 21 | bytes := hasher.Sum(nil) 22 | 23 | fmt.Printf("%x\n",bytes) 24 | 25 | } 26 | -------------------------------------------------------------------------------- /part45-base58/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | // 字节数组反转 4 | func ReverseBytes(data []byte) { 5 | for i, j := 0, len(data)-1; i < j; i, j = i+1, j-1 { 6 | data[i], data[j] = data[j], data[i] 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /part45-base58/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "kongyixueyuan.com/publicChain/part45-base58/BLC" 5 | "fmt" 6 | ) 7 | 8 | func main() { 9 | 10 | bytes := []byte("http://liyuechun.org") 11 | 12 | bytes58 := BLC.Base58Encode(bytes) 13 | 14 | fmt.Printf("%x\n",bytes58) 15 | 16 | 17 | fmt.Printf("%s\n",bytes58) 18 | //12TQQJ2URa4LWVGUKRWw8MJvahxzJ 19 | 20 | bytesStr := BLC.Base58Decode(bytes58) 21 | 22 | fmt.Printf("%s\n",bytesStr[1:]) 23 | 24 | 25 | } 26 | -------------------------------------------------------------------------------- /part46-base64/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/base64" 5 | "fmt" 6 | ) 7 | 8 | func main() { 9 | 10 | msg := "Hello, 世界" 11 | encoded := base64.StdEncoding.EncodeToString([]byte(msg)) 12 | 13 | fmt.Println(encoded) //SGVsbG8sIOS4lueVjA== 14 | 15 | //SGVsbG8sIOS4lueVjA== 16 | decoded, err := base64.StdEncoding.DecodeString(encoded) 17 | if err != nil { 18 | fmt.Println("decode error:", err) 19 | return 20 | } 21 | fmt.Println(string(decoded)) 22 | 23 | 24 | 25 | 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /part48-base58/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | // 字节数组反转 4 | func ReverseBytes(data []byte) { 5 | for i, j := 0, len(data)-1; i < j; i, j = i+1, j-1 { 6 | data[i], data[j] = data[j], data[i] 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /part49-base58-test/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | // 字节数组反转 4 | func ReverseBytes(data []byte) { 5 | for i, j := 0, len(data)-1; i < j; i, j = i+1, j-1 { 6 | data[i], data[j] = data[j], data[i] 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /part5-proof-of-work/BLC/ProofOfWork.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type ProofOfWork struct { 4 | Block *Block // 5 | } 6 | 7 | func (proofOfWork *ProofOfWork) Run() ([]byte,int64) { 8 | 9 | 10 | return nil,0 11 | } 12 | 13 | 14 | // 创建新的工作量证明对象 15 | func NewProofOfWork(block *Block) *ProofOfWork { 16 | 17 | return &ProofOfWork{block} 18 | } 19 | 20 | 21 | -------------------------------------------------------------------------------- /part5-proof-of-work/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "log" 7 | ) 8 | 9 | // 将int64转换为字节数组 10 | func IntToHex(num int64) []byte { 11 | buff := new(bytes.Buffer) 12 | err := binary.Write(buff, binary.BigEndian, num) 13 | if err != nil { 14 | log.Panic(err) 15 | } 16 | 17 | return buff.Bytes() 18 | } 19 | -------------------------------------------------------------------------------- /part50-wallet/BLC/Utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | // 字节数组反转 4 | func ReverseBytes(data []byte) { 5 | for i, j := 0, len(data)-1; i < j; i, j = i+1, j-1 { 6 | data[i], data[j] = data[j], data[i] 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /part50-wallet/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "kongyixueyuan.com/publicChain/part50-wallet/BLC" 5 | ) 6 | 7 | func main() { 8 | 9 | BLC.NewWallet() 10 | 11 | } -------------------------------------------------------------------------------- /part51-wallet-address/BLC/Utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | // 字节数组反转 4 | func ReverseBytes(data []byte) { 5 | for i, j := 0, len(data)-1; i < j; i, j = i+1, j-1 { 6 | data[i], data[j] = data[j], data[i] 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /part51-wallet-address/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "kongyixueyuan.com/publicChain/part51-wallet-address/BLC" 5 | "fmt" 6 | ) 7 | 8 | func main() { 9 | 10 | wallet := BLC.NewWallet() 11 | 12 | address := wallet.GetAddress() 13 | 14 | fmt.Printf("address:%s\n",address) 15 | // 1EESFLjfwLqoSq4LJA8eymoPi5pc5Sq6VC 16 | // 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa 17 | 18 | 19 | } -------------------------------------------------------------------------------- /part52-wallet-address/BLC/Utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | // 字节数组反转 4 | func ReverseBytes(data []byte) { 5 | for i, j := 0, len(data)-1; i < j; i, j = i+1, j-1 { 6 | data[i], data[j] = data[j], data[i] 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /part52-wallet-address/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "kongyixueyuan.com/publicChain/part52-wallet-address/BLC" 5 | "fmt" 6 | ) 7 | 8 | func main() { 9 | 10 | wallet := BLC.NewWallet() 11 | 12 | address := wallet.GetAddress() 13 | 14 | fmt.Printf("address:%s\n",address) 15 | 16 | // 1Gh5pL2uFsS8AFuSkd6za521FzsWHJcXDb 17 | // 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa 18 | 19 | isValid := BLC.IsValidForAdress(address) 20 | 21 | fmt.Printf("%s 这个地址为 %v\n",address,isValid) 22 | 23 | } -------------------------------------------------------------------------------- /part53-wallets/BLC/Utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | // 字节数组反转 4 | func ReverseBytes(data []byte) { 5 | for i, j := 0, len(data)-1; i < j; i, j = i+1, j-1 { 6 | data[i], data[j] = data[j], data[i] 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /part53-wallets/BLC/Wallets.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | type Wallets struct { 6 | Wallets map[string]*Wallet 7 | } 8 | 9 | 10 | 11 | // 创建钱包集合 12 | func NewWallets() *Wallets { 13 | 14 | wallets := &Wallets{} 15 | wallets.Wallets = make(map[string]*Wallet) 16 | return wallets 17 | } 18 | 19 | // 创建一个新钱包 20 | func (w *Wallets) CreateNewWallet() { 21 | 22 | wallet := NewWallet() 23 | fmt.Printf("Address:%s\n",wallet.GetAddress()) 24 | w.Wallets[string(wallet.GetAddress())] = wallet 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /part53-wallets/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "kongyixueyuan.com/publicChain/part53-wallets/BLC" 5 | "fmt" 6 | ) 7 | 8 | func main() { 9 | 10 | wallets := BLC.NewWallets() 11 | 12 | fmt.Println(wallets.Wallets) 13 | 14 | wallets.CreateNewWallet() 15 | wallets.CreateNewWallet() 16 | 17 | fmt.Println(wallets.Wallets) 18 | 19 | } -------------------------------------------------------------------------------- /part54-wallets/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet() { 6 | 7 | wallets := NewWallets() 8 | 9 | wallets.CreateNewWallet() 10 | 11 | fmt.Println(wallets.Wallets) 12 | } 13 | -------------------------------------------------------------------------------- /part54-wallets/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address) 8 | defer blockchain.DB.Close() 9 | } -------------------------------------------------------------------------------- /part54-wallets/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | blockchain := BlockchainObject() 11 | defer blockchain.DB.Close() 12 | 13 | amount := blockchain.GetBalance(address) 14 | 15 | fmt.Printf("%s一共有%d个Token\n",address,amount) 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /part54-wallets/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) printchain() { 9 | 10 | if DBExists() == false { 11 | fmt.Println("数据不存在.......") 12 | os.Exit(1) 13 | } 14 | 15 | blockchain := BlockchainObject() 16 | 17 | defer blockchain.DB.Close() 18 | 19 | blockchain.Printchain() 20 | 21 | } -------------------------------------------------------------------------------- /part54-wallets/BLC/CLI_send.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // 转账 9 | func (cli *CLI) send(from []string,to []string,amount []string) { 10 | 11 | 12 | if DBExists() == false { 13 | fmt.Println("数据不存在.......") 14 | os.Exit(1) 15 | } 16 | 17 | blockchain := BlockchainObject() 18 | defer blockchain.DB.Close() 19 | 20 | blockchain.MineNewBlock(from,to,amount) 21 | 22 | } -------------------------------------------------------------------------------- /part54-wallets/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type TXInput struct { 4 | // 1. 交易的Hash 5 | TxHash []byte 6 | // 2. 存储TXOutput在Vout里面的索引 7 | Vout int 8 | // 3. 用户名 9 | ScriptSig string 10 | } 11 | 12 | 13 | 14 | // 判断当前的消费是谁的钱 15 | func (txInput *TXInput) UnLockWithAddress(address string) bool { 16 | 17 | return txInput.ScriptSig == address 18 | } -------------------------------------------------------------------------------- /part54-wallets/BLC/Transaction_TXOutput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | 5 | //TXOutput{100,"zhangbozhi"} 6 | //TXOutput{30,"xietingfeng"} 7 | //TXOutput{40,"zhangbozhi"} 8 | 9 | 10 | type TXOutput struct { 11 | Value int64 12 | ScriptPubKey string //用户名 13 | } 14 | 15 | // 解锁 16 | func (txOutput *TXOutput) UnLockScriptPubKeyWithAddress(address string) bool { 17 | 18 | return txOutput.ScriptPubKey == address 19 | } 20 | 21 | 22 | -------------------------------------------------------------------------------- /part54-wallets/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part54-wallets/BLC/Wallets.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | type Wallets struct { 6 | Wallets map[string]*Wallet 7 | } 8 | 9 | 10 | 11 | // 创建钱包集合 12 | func NewWallets() *Wallets { 13 | 14 | wallets := &Wallets{} 15 | wallets.Wallets = make(map[string]*Wallet) 16 | return wallets 17 | } 18 | 19 | // 创建一个新钱包 20 | func (w *Wallets) CreateNewWallet() { 21 | 22 | wallet := NewWallet() 23 | fmt.Printf("Address:%s\n",wallet.GetAddress()) 24 | w.Wallets[string(wallet.GetAddress())] = wallet 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /part54-wallets/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "kongyixueyuan.com/publicChain/part54-wallets/BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part55-wallets-getaddresslists/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet() { 6 | 7 | wallets,_ := NewWallets() 8 | 9 | wallets.CreateNewWallet() 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part55-wallets-getaddresslists/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address) 8 | defer blockchain.DB.Close() 9 | } -------------------------------------------------------------------------------- /part55-wallets-getaddresslists/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists() { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | } -------------------------------------------------------------------------------- /part55-wallets-getaddresslists/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | blockchain := BlockchainObject() 11 | defer blockchain.DB.Close() 12 | 13 | amount := blockchain.GetBalance(address) 14 | 15 | fmt.Printf("%s一共有%d个Token\n",address,amount) 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /part55-wallets-getaddresslists/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) printchain() { 9 | 10 | if DBExists() == false { 11 | fmt.Println("数据不存在.......") 12 | os.Exit(1) 13 | } 14 | 15 | blockchain := BlockchainObject() 16 | 17 | defer blockchain.DB.Close() 18 | 19 | blockchain.Printchain() 20 | 21 | } -------------------------------------------------------------------------------- /part55-wallets-getaddresslists/BLC/CLI_send.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // 转账 9 | func (cli *CLI) send(from []string,to []string,amount []string) { 10 | 11 | 12 | if DBExists() == false { 13 | fmt.Println("数据不存在.......") 14 | os.Exit(1) 15 | } 16 | 17 | blockchain := BlockchainObject() 18 | defer blockchain.DB.Close() 19 | 20 | blockchain.MineNewBlock(from,to,amount) 21 | 22 | } -------------------------------------------------------------------------------- /part55-wallets-getaddresslists/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type TXInput struct { 4 | // 1. 交易的Hash 5 | TxHash []byte 6 | // 2. 存储TXOutput在Vout里面的索引 7 | Vout int 8 | // 3. 用户名 9 | ScriptSig string 10 | 11 | } 12 | 13 | 14 | 15 | // 判断当前的消费是谁的钱 16 | func (txInput *TXInput) UnLockWithAddress(address string) bool { 17 | 18 | return txInput.ScriptSig == address 19 | } -------------------------------------------------------------------------------- /part55-wallets-getaddresslists/BLC/Transaction_TXOutput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | 5 | //TXOutput{100,"zhangbozhi"} 6 | //TXOutput{30,"xietingfeng"} 7 | //TXOutput{40,"zhangbozhi"} 8 | 9 | 10 | type TXOutput struct { 11 | Value int64 12 | ScriptPubKey string //用户名 13 | } 14 | 15 | // 解锁 16 | func (txOutput *TXOutput) UnLockScriptPubKeyWithAddress(address string) bool { 17 | 18 | return txOutput.ScriptPubKey == address 19 | } 20 | 21 | 22 | -------------------------------------------------------------------------------- /part55-wallets-getaddresslists/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part55-wallets-getaddresslists/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "kongyixueyuan.com/publicChain/part55-wallets-getaddresslists/BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part56-wallets-addresslists/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet() { 6 | 7 | wallets,_ := NewWallets() 8 | 9 | wallets.CreateNewWallet() 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part56-wallets-addresslists/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address) 8 | defer blockchain.DB.Close() 9 | } -------------------------------------------------------------------------------- /part56-wallets-addresslists/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists() { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets() 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part56-wallets-addresslists/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | blockchain := BlockchainObject() 11 | defer blockchain.DB.Close() 12 | 13 | amount := blockchain.GetBalance(address) 14 | 15 | fmt.Printf("%s一共有%d个Token\n",address,amount) 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /part56-wallets-addresslists/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) printchain() { 9 | 10 | if DBExists() == false { 11 | fmt.Println("数据不存在.......") 12 | os.Exit(1) 13 | } 14 | 15 | blockchain := BlockchainObject() 16 | 17 | defer blockchain.DB.Close() 18 | 19 | blockchain.Printchain() 20 | 21 | } -------------------------------------------------------------------------------- /part56-wallets-addresslists/BLC/CLI_send.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // 转账 9 | func (cli *CLI) send(from []string,to []string,amount []string) { 10 | 11 | 12 | if DBExists() == false { 13 | fmt.Println("数据不存在.......") 14 | os.Exit(1) 15 | } 16 | 17 | blockchain := BlockchainObject() 18 | defer blockchain.DB.Close() 19 | 20 | blockchain.MineNewBlock(from,to,amount) 21 | 22 | } -------------------------------------------------------------------------------- /part56-wallets-addresslists/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type TXInput struct { 4 | // 1. 交易的Hash 5 | TxHash []byte 6 | // 2. 存储TXOutput在Vout里面的索引 7 | Vout int 8 | // 3. 用户名 9 | ScriptSig string 10 | 11 | } 12 | 13 | 14 | 15 | // 判断当前的消费是谁的钱 16 | func (txInput *TXInput) UnLockWithAddress(address string) bool { 17 | 18 | return txInput.ScriptSig == address 19 | } -------------------------------------------------------------------------------- /part56-wallets-addresslists/BLC/Transaction_TXOutput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | 5 | //TXOutput{100,"zhangbozhi"} 6 | //TXOutput{30,"xietingfeng"} 7 | //TXOutput{40,"zhangbozhi"} 8 | 9 | 10 | type TXOutput struct { 11 | Value int64 12 | ScriptPubKey string //用户名 13 | } 14 | 15 | // 解锁 16 | func (txOutput *TXOutput) UnLockScriptPubKeyWithAddress(address string) bool { 17 | 18 | return txOutput.ScriptPubKey == address 19 | } 20 | 21 | 22 | -------------------------------------------------------------------------------- /part56-wallets-addresslists/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part56-wallets-addresslists/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "kongyixueyuan.com/publicChain/part56-wallets-addresslists/BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part57-wallets-input-output-update/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet() { 6 | 7 | wallets,_ := NewWallets() 8 | 9 | wallets.CreateNewWallet() 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part57-wallets-input-output-update/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address) 8 | defer blockchain.DB.Close() 9 | } -------------------------------------------------------------------------------- /part57-wallets-input-output-update/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists() { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets() 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part57-wallets-input-output-update/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | blockchain := BlockchainObject() 11 | defer blockchain.DB.Close() 12 | 13 | amount := blockchain.GetBalance(address) 14 | 15 | fmt.Printf("%s一共有%d个Token\n",address,amount) 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /part57-wallets-input-output-update/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) printchain() { 9 | 10 | if DBExists() == false { 11 | fmt.Println("数据不存在.......") 12 | os.Exit(1) 13 | } 14 | 15 | blockchain := BlockchainObject() 16 | 17 | defer blockchain.DB.Close() 18 | 19 | blockchain.Printchain() 20 | 21 | } -------------------------------------------------------------------------------- /part57-wallets-input-output-update/BLC/CLI_send.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // 转账 9 | func (cli *CLI) send(from []string,to []string,amount []string) { 10 | 11 | 12 | if DBExists() == false { 13 | fmt.Println("数据不存在.......") 14 | os.Exit(1) 15 | } 16 | 17 | blockchain := BlockchainObject() 18 | defer blockchain.DB.Close() 19 | 20 | blockchain.MineNewBlock(from,to,amount) 21 | 22 | } -------------------------------------------------------------------------------- /part57-wallets-input-output-update/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part57-wallets-input-output-update/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "kongyixueyuan.com/publicChain/part57-wallets-input-output-update/BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part58-wallets-Signature/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet() { 6 | 7 | wallets,_ := NewWallets() 8 | 9 | wallets.CreateNewWallet() 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part58-wallets-Signature/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address) 8 | defer blockchain.DB.Close() 9 | } -------------------------------------------------------------------------------- /part58-wallets-Signature/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists() { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets() 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part58-wallets-Signature/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | blockchain := BlockchainObject() 11 | defer blockchain.DB.Close() 12 | 13 | amount := blockchain.GetBalance(address) 14 | 15 | fmt.Printf("%s一共有%d个Token\n",address,amount) 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /part58-wallets-Signature/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) printchain() { 9 | 10 | if DBExists() == false { 11 | fmt.Println("数据不存在.......") 12 | os.Exit(1) 13 | } 14 | 15 | blockchain := BlockchainObject() 16 | 17 | defer blockchain.DB.Close() 18 | 19 | blockchain.Printchain() 20 | 21 | } -------------------------------------------------------------------------------- /part58-wallets-Signature/BLC/CLI_send.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // 转账 9 | func (cli *CLI) send(from []string,to []string,amount []string) { 10 | 11 | 12 | if DBExists() == false { 13 | fmt.Println("数据不存在.......") 14 | os.Exit(1) 15 | } 16 | 17 | blockchain := BlockchainObject() 18 | defer blockchain.DB.Close() 19 | 20 | blockchain.MineNewBlock(from,to,amount) 21 | 22 | } -------------------------------------------------------------------------------- /part58-wallets-Signature/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "bytes" 4 | 5 | type TXInput struct { 6 | // 1. 交易的Hash 7 | TxHash []byte 8 | // 2. 存储TXOutput在Vout里面的索引 9 | Vout int 10 | 11 | Signature []byte // 数字签名 12 | 13 | PublicKey []byte // 公钥,钱包里面 14 | } 15 | 16 | 17 | 18 | // 判断当前的消费是谁的钱 19 | func (txInput *TXInput) UnLockRipemd160Hash(ripemd160Hash []byte) bool { 20 | 21 | publicKey := Ripemd160Hash(txInput.PublicKey) 22 | 23 | return bytes.Compare(publicKey,ripemd160Hash) == 0 24 | } -------------------------------------------------------------------------------- /part58-wallets-Signature/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part58-wallets-Signature/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "kongyixueyuan.com/publicChain/part59-wallets-Signature-Verify/BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part59-wallets-Signature-Verify/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet() { 6 | 7 | wallets,_ := NewWallets() 8 | 9 | wallets.CreateNewWallet() 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part59-wallets-Signature-Verify/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address) 8 | defer blockchain.DB.Close() 9 | } -------------------------------------------------------------------------------- /part59-wallets-Signature-Verify/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists() { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets() 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part59-wallets-Signature-Verify/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | blockchain := BlockchainObject() 11 | defer blockchain.DB.Close() 12 | 13 | amount := blockchain.GetBalance(address) 14 | 15 | fmt.Printf("%s一共有%d个Token\n",address,amount) 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /part59-wallets-Signature-Verify/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) printchain() { 9 | 10 | if DBExists() == false { 11 | fmt.Println("数据不存在.......") 12 | os.Exit(1) 13 | } 14 | 15 | blockchain := BlockchainObject() 16 | 17 | defer blockchain.DB.Close() 18 | 19 | blockchain.Printchain() 20 | 21 | } -------------------------------------------------------------------------------- /part59-wallets-Signature-Verify/BLC/CLI_send.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // 转账 9 | func (cli *CLI) send(from []string,to []string,amount []string) { 10 | 11 | 12 | if DBExists() == false { 13 | fmt.Println("数据不存在.......") 14 | os.Exit(1) 15 | } 16 | 17 | blockchain := BlockchainObject() 18 | defer blockchain.DB.Close() 19 | 20 | blockchain.MineNewBlock(from,to,amount) 21 | 22 | } -------------------------------------------------------------------------------- /part59-wallets-Signature-Verify/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part59-wallets-Signature-Verify/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "kongyixueyuan.com/publicChain/part59-wallets-Signature-Verify/BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part6-proof-of-work/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "log" 7 | ) 8 | 9 | // 将int64转换为字节数组 10 | func IntToHex(num int64) []byte { 11 | buff := new(bytes.Buffer) 12 | err := binary.Write(buff, binary.BigEndian, num) 13 | if err != nil { 14 | log.Panic(err) 15 | } 16 | 17 | return buff.Bytes() 18 | } 19 | -------------------------------------------------------------------------------- /part60-Transaction-Reward/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet() { 6 | 7 | wallets,_ := NewWallets() 8 | 9 | wallets.CreateNewWallet() 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part60-Transaction-Reward/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address) 8 | defer blockchain.DB.Close() 9 | } -------------------------------------------------------------------------------- /part60-Transaction-Reward/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists() { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets() 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part60-Transaction-Reward/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | blockchain := BlockchainObject() 11 | defer blockchain.DB.Close() 12 | 13 | amount := blockchain.GetBalance(address) 14 | 15 | fmt.Printf("%s一共有%d个Token\n",address,amount) 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /part60-Transaction-Reward/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) printchain() { 9 | 10 | if DBExists() == false { 11 | fmt.Println("数据不存在.......") 12 | os.Exit(1) 13 | } 14 | 15 | blockchain := BlockchainObject() 16 | 17 | defer blockchain.DB.Close() 18 | 19 | blockchain.Printchain() 20 | 21 | } -------------------------------------------------------------------------------- /part60-Transaction-Reward/BLC/CLI_send.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // 转账 9 | func (cli *CLI) send(from []string,to []string,amount []string) { 10 | 11 | 12 | if DBExists() == false { 13 | fmt.Println("数据不存在.......") 14 | os.Exit(1) 15 | } 16 | 17 | blockchain := BlockchainObject() 18 | defer blockchain.DB.Close() 19 | 20 | blockchain.MineNewBlock(from,to,amount) 21 | 22 | } -------------------------------------------------------------------------------- /part60-Transaction-Reward/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "bytes" 4 | 5 | type TXInput struct { 6 | // 1. 交易的Hash 7 | TxHash []byte 8 | // 2. 存储TXOutput在Vout里面的索引 9 | Vout int 10 | 11 | Signature []byte // 数字签名 12 | 13 | PublicKey []byte // 公钥,钱包里面 14 | } 15 | 16 | 17 | 18 | // 判断当前的消费是谁的钱 19 | func (txInput *TXInput) UnLockRipemd160Hash(ripemd160Hash []byte) bool { 20 | 21 | publicKey := Ripemd160Hash(txInput.PublicKey) 22 | 23 | return bytes.Compare(publicKey,ripemd160Hash) == 0 24 | } -------------------------------------------------------------------------------- /part60-Transaction-Reward/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part60-Transaction-Reward/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "kongyixueyuan.com/publicChain/part60-Transaction-Reward/BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part61-UTXOSet/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet() { 6 | 7 | wallets,_ := NewWallets() 8 | 9 | wallets.CreateNewWallet() 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part61-UTXOSet/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address) 8 | defer blockchain.DB.Close() 9 | } -------------------------------------------------------------------------------- /part61-UTXOSet/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists() { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets() 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part61-UTXOSet/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | blockchain := BlockchainObject() 11 | defer blockchain.DB.Close() 12 | 13 | amount := blockchain.GetBalance(address) 14 | 15 | fmt.Printf("%s一共有%d个Token\n",address,amount) 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /part61-UTXOSet/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) printchain() { 9 | 10 | if DBExists() == false { 11 | fmt.Println("数据不存在.......") 12 | os.Exit(1) 13 | } 14 | 15 | blockchain := BlockchainObject() 16 | 17 | defer blockchain.DB.Close() 18 | 19 | blockchain.Printchain() 20 | 21 | } -------------------------------------------------------------------------------- /part61-UTXOSet/BLC/CLI_send.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // 转账 9 | func (cli *CLI) send(from []string,to []string,amount []string) { 10 | 11 | 12 | if DBExists() == false { 13 | fmt.Println("数据不存在.......") 14 | os.Exit(1) 15 | } 16 | 17 | blockchain := BlockchainObject() 18 | defer blockchain.DB.Close() 19 | 20 | blockchain.MineNewBlock(from,to,amount) 21 | 22 | } -------------------------------------------------------------------------------- /part61-UTXOSet/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "bytes" 4 | 5 | type TXInput struct { 6 | // 1. 交易的Hash 7 | TxHash []byte 8 | // 2. 存储TXOutput在Vout里面的索引 9 | Vout int 10 | 11 | Signature []byte // 数字签名 12 | 13 | PublicKey []byte // 公钥,钱包里面 14 | } 15 | 16 | 17 | 18 | // 判断当前的消费是谁的钱 19 | func (txInput *TXInput) UnLockRipemd160Hash(ripemd160Hash []byte) bool { 20 | 21 | publicKey := Ripemd160Hash(txInput.PublicKey) 22 | 23 | return bytes.Compare(publicKey,ripemd160Hash) == 0 24 | } -------------------------------------------------------------------------------- /part61-UTXOSet/BLC/Transaction_TXOutputs.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type TXOutputs struct { 4 | TxOutputs []*TXOutput 5 | } 6 | -------------------------------------------------------------------------------- /part61-UTXOSet/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part61-UTXOSet/BLC/UTXO_Set.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | 5 | 6 | // 1.有一个方法,功能: 7 | 8 | // 遍历整个数据库,读取所有的未花费的UTXO,然后将所有的UTXO存储到数据库 9 | // reset 10 | // 去遍历数据库时, 11 | // []*TXOutputs 12 | 13 | //[string]*TXOutputs 14 | // 15 | //txHash,TXOutputs := range txOutputsMap { 16 | // 17 | // 18 | // 19 | // 20 | //} 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /part61-UTXOSet/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "kongyixueyuan.com/publicChain/part61-UTXOSet/BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part62-UTXOSet/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet() { 6 | 7 | wallets,_ := NewWallets() 8 | 9 | wallets.CreateNewWallet() 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part62-UTXOSet/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address) 8 | defer blockchain.DB.Close() 9 | } -------------------------------------------------------------------------------- /part62-UTXOSet/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists() { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets() 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part62-UTXOSet/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | blockchain := BlockchainObject() 11 | defer blockchain.DB.Close() 12 | 13 | amount := blockchain.GetBalance(address) 14 | 15 | fmt.Printf("%s一共有%d个Token\n",address,amount) 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /part62-UTXOSet/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) printchain() { 9 | 10 | if DBExists() == false { 11 | fmt.Println("数据不存在.......") 12 | os.Exit(1) 13 | } 14 | 15 | blockchain := BlockchainObject() 16 | 17 | defer blockchain.DB.Close() 18 | 19 | blockchain.Printchain() 20 | 21 | } -------------------------------------------------------------------------------- /part62-UTXOSet/BLC/CLI_send.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // 转账 9 | func (cli *CLI) send(from []string,to []string,amount []string) { 10 | 11 | 12 | if DBExists() == false { 13 | fmt.Println("数据不存在.......") 14 | os.Exit(1) 15 | } 16 | 17 | blockchain := BlockchainObject() 18 | defer blockchain.DB.Close() 19 | 20 | blockchain.MineNewBlock(from,to,amount) 21 | 22 | } -------------------------------------------------------------------------------- /part62-UTXOSet/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "bytes" 4 | 5 | type TXInput struct { 6 | // 1. 交易的Hash 7 | TxHash []byte 8 | // 2. 存储TXOutput在Vout里面的索引 9 | Vout int 10 | 11 | Signature []byte // 数字签名 12 | 13 | PublicKey []byte // 公钥,钱包里面 14 | } 15 | 16 | 17 | 18 | // 判断当前的消费是谁的钱 19 | func (txInput *TXInput) UnLockRipemd160Hash(ripemd160Hash []byte) bool { 20 | 21 | publicKey := Ripemd160Hash(txInput.PublicKey) 22 | 23 | return bytes.Compare(publicKey,ripemd160Hash) == 0 24 | } -------------------------------------------------------------------------------- /part62-UTXOSet/BLC/Transaction_TXOutputs.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type TXOutputs struct { 4 | TxOutputs []*TXOutput 5 | } 6 | -------------------------------------------------------------------------------- /part62-UTXOSet/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part62-UTXOSet/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "kongyixueyuan.com/publicChain/part62-UTXOSet/BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part63-UTXOSet/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet() { 6 | 7 | wallets,_ := NewWallets() 8 | 9 | wallets.CreateNewWallet() 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part63-UTXOSet/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address) 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | } -------------------------------------------------------------------------------- /part63-UTXOSet/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists() { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets() 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part63-UTXOSet/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | blockchain := BlockchainObject() 11 | defer blockchain.DB.Close() 12 | 13 | amount := blockchain.GetBalance(address) 14 | 15 | fmt.Printf("%s一共有%d个Token\n",address,amount) 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /part63-UTXOSet/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) printchain() { 9 | 10 | if DBExists() == false { 11 | fmt.Println("数据不存在.......") 12 | os.Exit(1) 13 | } 14 | 15 | blockchain := BlockchainObject() 16 | 17 | defer blockchain.DB.Close() 18 | 19 | blockchain.Printchain() 20 | 21 | } -------------------------------------------------------------------------------- /part63-UTXOSet/BLC/CLI_send.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // 转账 9 | func (cli *CLI) send(from []string,to []string,amount []string) { 10 | 11 | 12 | if DBExists() == false { 13 | fmt.Println("数据不存在.......") 14 | os.Exit(1) 15 | } 16 | 17 | blockchain := BlockchainObject() 18 | defer blockchain.DB.Close() 19 | 20 | blockchain.MineNewBlock(from,to,amount) 21 | 22 | } -------------------------------------------------------------------------------- /part63-UTXOSet/BLC/CLI_testmethod.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) TestMethod() { 6 | 7 | 8 | blockchain := BlockchainObject() 9 | 10 | defer blockchain.DB.Close() 11 | 12 | utxoMap := blockchain.FindUTXOMap() 13 | 14 | fmt.Println(utxoMap) 15 | 16 | 17 | } 18 | -------------------------------------------------------------------------------- /part63-UTXOSet/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "bytes" 4 | 5 | type TXInput struct { 6 | // 1. 交易的Hash 7 | TxHash []byte 8 | // 2. 存储TXOutput在Vout里面的索引 9 | Vout int 10 | 11 | Signature []byte // 数字签名 12 | 13 | PublicKey []byte // 公钥,钱包里面 14 | } 15 | 16 | 17 | 18 | // 判断当前的消费是谁的钱 19 | func (txInput *TXInput) UnLockRipemd160Hash(ripemd160Hash []byte) bool { 20 | 21 | publicKey := Ripemd160Hash(txInput.PublicKey) 22 | 23 | return bytes.Compare(publicKey,ripemd160Hash) == 0 24 | } -------------------------------------------------------------------------------- /part63-UTXOSet/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part63-UTXOSet/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "kongyixueyuan.com/publicChain/part63-UTXOSet/BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part64-UTXOSet/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet() { 6 | 7 | wallets,_ := NewWallets() 8 | 9 | wallets.CreateNewWallet() 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part64-UTXOSet/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address) 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | } -------------------------------------------------------------------------------- /part64-UTXOSet/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists() { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets() 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part64-UTXOSet/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | blockchain := BlockchainObject() 11 | defer blockchain.DB.Close() 12 | 13 | utxoSet := &UTXOSet{blockchain} 14 | 15 | amount := utxoSet.GetBalance(address) 16 | 17 | //amount := blockchain.GetBalance(address) 18 | 19 | fmt.Printf("%s一共有%d个Token\n",address,amount) 20 | 21 | 22 | 23 | } 24 | -------------------------------------------------------------------------------- /part64-UTXOSet/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) printchain() { 9 | 10 | if DBExists() == false { 11 | fmt.Println("数据不存在.......") 12 | os.Exit(1) 13 | } 14 | 15 | blockchain := BlockchainObject() 16 | 17 | defer blockchain.DB.Close() 18 | 19 | blockchain.Printchain() 20 | 21 | } -------------------------------------------------------------------------------- /part64-UTXOSet/BLC/CLI_send.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // 转账 9 | func (cli *CLI) send(from []string,to []string,amount []string) { 10 | 11 | 12 | if DBExists() == false { 13 | fmt.Println("数据不存在.......") 14 | os.Exit(1) 15 | } 16 | 17 | blockchain := BlockchainObject() 18 | defer blockchain.DB.Close() 19 | 20 | blockchain.MineNewBlock(from,to,amount) 21 | 22 | } -------------------------------------------------------------------------------- /part64-UTXOSet/BLC/CLI_testmethod.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | //"fmt" 5 | //"encoding/hex" 6 | ) 7 | 8 | func (cli *CLI) TestMethod() { 9 | 10 | 11 | blockchain := BlockchainObject() 12 | 13 | defer blockchain.DB.Close() 14 | 15 | utxo_set := &UTXOSet{blockchain} 16 | utxo_set.ResetUTXOSet() 17 | 18 | } 19 | -------------------------------------------------------------------------------- /part64-UTXOSet/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "bytes" 4 | 5 | type TXInput struct { 6 | // 1. 交易的Hash 7 | TxHash []byte 8 | // 2. 存储TXOutput在Vout里面的索引 9 | Vout int 10 | 11 | Signature []byte // 数字签名 12 | 13 | PublicKey []byte // 公钥,钱包里面 14 | } 15 | 16 | 17 | 18 | // 判断当前的消费是谁的钱 19 | func (txInput *TXInput) UnLockRipemd160Hash(ripemd160Hash []byte) bool { 20 | 21 | publicKey := Ripemd160Hash(txInput.PublicKey) 22 | 23 | return bytes.Compare(publicKey,ripemd160Hash) == 0 24 | } -------------------------------------------------------------------------------- /part64-UTXOSet/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part64-UTXOSet/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "./BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part65-UTXOSet-GetBalance/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet() { 6 | 7 | wallets,_ := NewWallets() 8 | 9 | wallets.CreateNewWallet() 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part65-UTXOSet-GetBalance/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address) 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | } -------------------------------------------------------------------------------- /part65-UTXOSet-GetBalance/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists() { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets() 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part65-UTXOSet-GetBalance/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | blockchain := BlockchainObject() 11 | defer blockchain.DB.Close() 12 | 13 | utxoSet := &UTXOSet{blockchain} 14 | 15 | amount := utxoSet.GetBalance(address) 16 | 17 | fmt.Printf("%s一共有%d个Token\n",address,amount) 18 | 19 | } 20 | -------------------------------------------------------------------------------- /part65-UTXOSet-GetBalance/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) printchain() { 9 | 10 | if DBExists() == false { 11 | fmt.Println("数据不存在.......") 12 | os.Exit(1) 13 | } 14 | 15 | blockchain := BlockchainObject() 16 | 17 | defer blockchain.DB.Close() 18 | 19 | blockchain.Printchain() 20 | 21 | } -------------------------------------------------------------------------------- /part65-UTXOSet-GetBalance/BLC/CLI_send.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // 转账 9 | func (cli *CLI) send(from []string,to []string,amount []string) { 10 | 11 | 12 | if DBExists() == false { 13 | fmt.Println("数据不存在.......") 14 | os.Exit(1) 15 | } 16 | 17 | blockchain := BlockchainObject() 18 | defer blockchain.DB.Close() 19 | 20 | blockchain.MineNewBlock(from,to,amount) 21 | 22 | } -------------------------------------------------------------------------------- /part65-UTXOSet-GetBalance/BLC/CLI_testmethod.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) TestMethod() { 6 | 7 | 8 | fmt.Println("TestMethod") 9 | 10 | blockchain := BlockchainObject() 11 | 12 | defer blockchain.DB.Close() 13 | 14 | utxoSet := &UTXOSet{blockchain} 15 | 16 | utxoSet.ResetUTXOSet() 17 | 18 | //fmt.Println(blockchain.FindUTXOMap()) 19 | } 20 | -------------------------------------------------------------------------------- /part65-UTXOSet-GetBalance/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "bytes" 4 | 5 | type TXInput struct { 6 | // 1. 交易的Hash 7 | TxHash []byte 8 | // 2. 存储TXOutput在Vout里面的索引 9 | Vout int 10 | 11 | Signature []byte // 数字签名 12 | 13 | PublicKey []byte // 公钥,钱包里面 14 | } 15 | 16 | 17 | 18 | // 判断当前的消费是谁的钱 19 | func (txInput *TXInput) UnLockRipemd160Hash(ripemd160Hash []byte) bool { 20 | 21 | publicKey := Ripemd160Hash(txInput.PublicKey) 22 | 23 | return bytes.Compare(publicKey,ripemd160Hash) == 0 24 | } -------------------------------------------------------------------------------- /part65-UTXOSet-GetBalance/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part65-UTXOSet-GetBalance/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "kongyixueyuan.com/publicChain/part65-UTXOSet-GetBalance/BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part66-UTXOSet-send/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet() { 6 | 7 | wallets,_ := NewWallets() 8 | 9 | wallets.CreateNewWallet() 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part66-UTXOSet-send/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address) 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | } 14 | 15 | //blocks 16 | //utxoTable -------------------------------------------------------------------------------- /part66-UTXOSet-send/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists() { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets() 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part66-UTXOSet-send/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | blockchain := BlockchainObject() 11 | defer blockchain.DB.Close() 12 | 13 | utxoSet := &UTXOSet{blockchain} 14 | 15 | amount := utxoSet.GetBalance(address) 16 | 17 | fmt.Printf("%s一共有%d个Token\n",address,amount) 18 | 19 | } 20 | -------------------------------------------------------------------------------- /part66-UTXOSet-send/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) printchain() { 9 | 10 | if DBExists() == false { 11 | fmt.Println("数据不存在.......") 12 | os.Exit(1) 13 | } 14 | 15 | blockchain := BlockchainObject() 16 | 17 | defer blockchain.DB.Close() 18 | 19 | blockchain.Printchain() 20 | 21 | } -------------------------------------------------------------------------------- /part66-UTXOSet-send/BLC/CLI_send.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // 转账 9 | func (cli *CLI) send(from []string,to []string,amount []string) { 10 | 11 | 12 | if DBExists() == false { 13 | fmt.Println("数据不存在.......") 14 | os.Exit(1) 15 | } 16 | 17 | blockchain := BlockchainObject() 18 | defer blockchain.DB.Close() 19 | 20 | blockchain.MineNewBlock(from,to,amount) 21 | 22 | } -------------------------------------------------------------------------------- /part66-UTXOSet-send/BLC/CLI_testmethod.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) TestMethod() { 6 | 7 | 8 | fmt.Println("TestMethod") 9 | 10 | blockchain := BlockchainObject() 11 | 12 | defer blockchain.DB.Close() 13 | 14 | utxoSet := &UTXOSet{blockchain} 15 | 16 | utxoSet.ResetUTXOSet() 17 | 18 | //fmt.Println(blockchain.FindUTXOMap()) 19 | } 20 | -------------------------------------------------------------------------------- /part66-UTXOSet-send/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "bytes" 4 | 5 | type TXInput struct { 6 | // 1. 交易的Hash 7 | TxHash []byte 8 | // 2. 存储TXOutput在Vout里面的索引 9 | Vout int 10 | 11 | Signature []byte // 数字签名 12 | 13 | PublicKey []byte // 公钥,钱包里面 14 | } 15 | 16 | 17 | 18 | // 判断当前的消费是谁的钱 19 | func (txInput *TXInput) UnLockRipemd160Hash(ripemd160Hash []byte) bool { 20 | 21 | publicKey := Ripemd160Hash(txInput.PublicKey) 22 | 23 | return bytes.Compare(publicKey,ripemd160Hash) == 0 24 | } -------------------------------------------------------------------------------- /part66-UTXOSet-send/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part66-UTXOSet-send/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "kongyixueyuan.com/publicChain/part66-UTXOSet-send/BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part67-UTXOSet-Update/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet() { 6 | 7 | wallets,_ := NewWallets() 8 | 9 | wallets.CreateNewWallet() 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part67-UTXOSet-Update/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address) 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | } 14 | 15 | //blocks 16 | //utxoTable -------------------------------------------------------------------------------- /part67-UTXOSet-Update/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists() { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets() 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part67-UTXOSet-Update/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | blockchain := BlockchainObject() 11 | defer blockchain.DB.Close() 12 | 13 | utxoSet := &UTXOSet{blockchain} 14 | 15 | amount := utxoSet.GetBalance(address) 16 | 17 | fmt.Printf("%s一共有%d个Token\n",address,amount) 18 | 19 | } 20 | -------------------------------------------------------------------------------- /part67-UTXOSet-Update/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) printchain() { 9 | 10 | if DBExists() == false { 11 | fmt.Println("数据不存在.......") 12 | os.Exit(1) 13 | } 14 | 15 | blockchain := BlockchainObject() 16 | 17 | defer blockchain.DB.Close() 18 | 19 | blockchain.Printchain() 20 | 21 | } -------------------------------------------------------------------------------- /part67-UTXOSet-Update/BLC/CLI_send.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // 转账 9 | func (cli *CLI) send(from []string,to []string,amount []string) { 10 | 11 | 12 | if DBExists() == false { 13 | fmt.Println("数据不存在.......") 14 | os.Exit(1) 15 | } 16 | 17 | blockchain := BlockchainObject() 18 | defer blockchain.DB.Close() 19 | 20 | blockchain.MineNewBlock(from,to,amount) 21 | 22 | utxoSet := &UTXOSet{blockchain} 23 | 24 | //转账成功以后,需要更新一下 25 | utxoSet.Update() 26 | 27 | } 28 | 29 | -------------------------------------------------------------------------------- /part67-UTXOSet-Update/BLC/CLI_testmethod.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) TestMethod() { 6 | 7 | 8 | fmt.Println("TestMethod") 9 | 10 | blockchain := BlockchainObject() 11 | 12 | defer blockchain.DB.Close() 13 | 14 | utxoSet := &UTXOSet{blockchain} 15 | 16 | utxoSet.ResetUTXOSet() 17 | 18 | //fmt.Println(blockchain.FindUTXOMap()) 19 | } 20 | -------------------------------------------------------------------------------- /part67-UTXOSet-Update/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "bytes" 4 | 5 | type TXInput struct { 6 | // 1. 交易的Hash 7 | TxHash []byte 8 | // 2. 存储TXOutput在Vout里面的索引 9 | Vout int 10 | 11 | Signature []byte // 数字签名 12 | 13 | PublicKey []byte // 公钥,钱包里面 14 | } 15 | 16 | 17 | 18 | // 判断当前的消费是谁的钱 19 | func (txInput *TXInput) UnLockRipemd160Hash(ripemd160Hash []byte) bool { 20 | 21 | publicKey := Ripemd160Hash(txInput.PublicKey) 22 | 23 | return bytes.Compare(publicKey,ripemd160Hash) == 0 24 | } -------------------------------------------------------------------------------- /part67-UTXOSet-Update/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part67-UTXOSet-Update/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "kongyixueyuan.com/publicChain/part67-UTXOSet-Update/BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part68-Merkle-tree/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet() { 6 | 7 | wallets,_ := NewWallets() 8 | 9 | wallets.CreateNewWallet() 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part68-Merkle-tree/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address) 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | } 14 | 15 | //blocks 16 | //utxoTable -------------------------------------------------------------------------------- /part68-Merkle-tree/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists() { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets() 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part68-Merkle-tree/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | blockchain := BlockchainObject() 11 | defer blockchain.DB.Close() 12 | 13 | utxoSet := &UTXOSet{blockchain} 14 | 15 | amount := utxoSet.GetBalance(address) 16 | 17 | fmt.Printf("%s一共有%d个Token\n",address,amount) 18 | 19 | } 20 | -------------------------------------------------------------------------------- /part68-Merkle-tree/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) printchain() { 9 | 10 | if DBExists() == false { 11 | fmt.Println("数据不存在.......") 12 | os.Exit(1) 13 | } 14 | 15 | blockchain := BlockchainObject() 16 | 17 | defer blockchain.DB.Close() 18 | 19 | blockchain.Printchain() 20 | 21 | } -------------------------------------------------------------------------------- /part68-Merkle-tree/BLC/CLI_send.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // 转账 9 | func (cli *CLI) send(from []string,to []string,amount []string) { 10 | 11 | 12 | if DBExists() == false { 13 | fmt.Println("数据不存在.......") 14 | os.Exit(1) 15 | } 16 | 17 | blockchain := BlockchainObject() 18 | defer blockchain.DB.Close() 19 | 20 | blockchain.MineNewBlock(from,to,amount) 21 | 22 | utxoSet := &UTXOSet{blockchain} 23 | 24 | //转账成功以后,需要更新一下 25 | utxoSet.Update() 26 | 27 | } 28 | 29 | -------------------------------------------------------------------------------- /part68-Merkle-tree/BLC/CLI_testmethod.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) TestMethod() { 6 | 7 | 8 | fmt.Println("TestMethod") 9 | 10 | blockchain := BlockchainObject() 11 | 12 | defer blockchain.DB.Close() 13 | 14 | utxoSet := &UTXOSet{blockchain} 15 | 16 | utxoSet.ResetUTXOSet() 17 | 18 | //fmt.Println(blockchain.FindUTXOMap()) 19 | } 20 | -------------------------------------------------------------------------------- /part68-Merkle-tree/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "bytes" 4 | 5 | type TXInput struct { 6 | // 1. 交易的Hash 7 | TxHash []byte 8 | // 2. 存储TXOutput在Vout里面的索引 9 | Vout int 10 | 11 | Signature []byte // 数字签名 12 | 13 | PublicKey []byte // 公钥,钱包里面 14 | } 15 | 16 | 17 | 18 | // 判断当前的消费是谁的钱 19 | func (txInput *TXInput) UnLockRipemd160Hash(ripemd160Hash []byte) bool { 20 | 21 | publicKey := Ripemd160Hash(txInput.PublicKey) 22 | 23 | return bytes.Compare(publicKey,ripemd160Hash) == 0 24 | } -------------------------------------------------------------------------------- /part68-Merkle-tree/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part68-Merkle-tree/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "./BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part69-Merkle-tree/BLC/CLI_UTXO_Reset.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) resetUTXOSet() { 5 | 6 | blockchain := BlockchainObject() 7 | 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | 14 | } 15 | -------------------------------------------------------------------------------- /part69-Merkle-tree/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet() { 6 | 7 | wallets,_ := NewWallets() 8 | 9 | wallets.CreateNewWallet() 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part69-Merkle-tree/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address) 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | } 14 | 15 | //blocks 16 | //utxoTable -------------------------------------------------------------------------------- /part69-Merkle-tree/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists() { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets() 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part69-Merkle-tree/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | blockchain := BlockchainObject() 11 | defer blockchain.DB.Close() 12 | 13 | utxoSet := &UTXOSet{blockchain} 14 | 15 | amount := utxoSet.GetBalance(address) 16 | 17 | fmt.Printf("%s一共有%d个Token\n",address,amount) 18 | 19 | } 20 | -------------------------------------------------------------------------------- /part69-Merkle-tree/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) printchain() { 9 | 10 | if DBExists() == false { 11 | fmt.Println("数据不存在.......") 12 | os.Exit(1) 13 | } 14 | 15 | blockchain := BlockchainObject() 16 | 17 | defer blockchain.DB.Close() 18 | 19 | blockchain.Printchain() 20 | 21 | } -------------------------------------------------------------------------------- /part69-Merkle-tree/BLC/CLI_send.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // 转账 9 | func (cli *CLI) send(from []string,to []string,amount []string) { 10 | 11 | 12 | if DBExists() == false { 13 | fmt.Println("数据不存在.......") 14 | os.Exit(1) 15 | } 16 | 17 | blockchain := BlockchainObject() 18 | defer blockchain.DB.Close() 19 | 20 | blockchain.MineNewBlock(from,to,amount) 21 | 22 | utxoSet := &UTXOSet{blockchain} 23 | 24 | //转账成功以后,需要更新一下 25 | utxoSet.Update() 26 | 27 | } 28 | 29 | -------------------------------------------------------------------------------- /part69-Merkle-tree/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "bytes" 4 | 5 | type TXInput struct { 6 | // 1. 交易的Hash 7 | TxHash []byte 8 | // 2. 存储TXOutput在Vout里面的索引 9 | Vout int 10 | 11 | Signature []byte // 数字签名 12 | 13 | PublicKey []byte // 公钥,钱包里面 14 | } 15 | 16 | 17 | 18 | // 判断当前的消费是谁的钱 19 | func (txInput *TXInput) UnLockRipemd160Hash(ripemd160Hash []byte) bool { 20 | 21 | publicKey := Ripemd160Hash(txInput.PublicKey) 22 | 23 | return bytes.Compare(publicKey,ripemd160Hash) == 0 24 | } -------------------------------------------------------------------------------- /part69-Merkle-tree/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part69-Merkle-tree/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "./BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part7-proof-of-work/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "log" 7 | ) 8 | 9 | // 将int64转换为字节数组 10 | func IntToHex(num int64) []byte { 11 | buff := new(bytes.Buffer) 12 | err := binary.Write(buff, binary.BigEndian, num) 13 | if err != nil { 14 | log.Panic(err) 15 | } 16 | 17 | return buff.Bytes() 18 | } 19 | -------------------------------------------------------------------------------- /part70-Merkle-NODE_ID/BLC/CLI_UTXO_Reset.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) resetUTXOSet() { 5 | 6 | blockchain := BlockchainObject() 7 | 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | 14 | } 15 | -------------------------------------------------------------------------------- /part70-Merkle-NODE_ID/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet() { 6 | 7 | wallets,_ := NewWallets() 8 | 9 | wallets.CreateNewWallet() 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part70-Merkle-NODE_ID/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address) 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | } 14 | 15 | //blocks 16 | //utxoTable -------------------------------------------------------------------------------- /part70-Merkle-NODE_ID/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists() { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets() 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part70-Merkle-NODE_ID/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | blockchain := BlockchainObject() 11 | defer blockchain.DB.Close() 12 | 13 | utxoSet := &UTXOSet{blockchain} 14 | 15 | amount := utxoSet.GetBalance(address) 16 | 17 | fmt.Printf("%s一共有%d个Token\n",address,amount) 18 | 19 | } 20 | -------------------------------------------------------------------------------- /part70-Merkle-NODE_ID/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) printchain() { 9 | 10 | if DBExists() == false { 11 | fmt.Println("数据不存在.......") 12 | os.Exit(1) 13 | } 14 | 15 | blockchain := BlockchainObject() 16 | 17 | defer blockchain.DB.Close() 18 | 19 | blockchain.Printchain() 20 | 21 | } -------------------------------------------------------------------------------- /part70-Merkle-NODE_ID/BLC/CLI_send.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // 转账 9 | func (cli *CLI) send(from []string,to []string,amount []string) { 10 | 11 | 12 | if DBExists() == false { 13 | fmt.Println("数据不存在.......") 14 | os.Exit(1) 15 | } 16 | 17 | blockchain := BlockchainObject() 18 | defer blockchain.DB.Close() 19 | 20 | blockchain.MineNewBlock(from,to,amount) 21 | 22 | utxoSet := &UTXOSet{blockchain} 23 | 24 | //转账成功以后,需要更新一下 25 | utxoSet.Update() 26 | 27 | } 28 | 29 | -------------------------------------------------------------------------------- /part70-Merkle-NODE_ID/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "bytes" 4 | 5 | type TXInput struct { 6 | // 1. 交易的Hash 7 | TxHash []byte 8 | // 2. 存储TXOutput在Vout里面的索引 9 | Vout int 10 | 11 | Signature []byte // 数字签名 12 | 13 | PublicKey []byte // 公钥,钱包里面 14 | } 15 | 16 | 17 | 18 | // 判断当前的消费是谁的钱 19 | func (txInput *TXInput) UnLockRipemd160Hash(ripemd160Hash []byte) bool { 20 | 21 | publicKey := Ripemd160Hash(txInput.PublicKey) 22 | 23 | return bytes.Compare(publicKey,ripemd160Hash) == 0 24 | } -------------------------------------------------------------------------------- /part70-Merkle-NODE_ID/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part70-Merkle-NODE_ID/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "./BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part71-Net-NODE_ID_CONFIG/BLC/CLI_UTXO_Reset.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) resetUTXOSet(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | 14 | } 15 | -------------------------------------------------------------------------------- /part71-Net-NODE_ID_CONFIG/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet(nodeID string) { 6 | 7 | wallets,_ := NewWallets(nodeID) 8 | 9 | wallets.CreateNewWallet(nodeID) 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part71-Net-NODE_ID_CONFIG/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string,nodeID string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address,nodeID) 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | } 14 | 15 | //blocks 16 | //utxoTable -------------------------------------------------------------------------------- /part71-Net-NODE_ID_CONFIG/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists(nodeID string) { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets(nodeID) 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part71-Net-NODE_ID_CONFIG/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string,nodeID string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | // 获取某一个节点的blockchain对象 11 | blockchain := BlockchainObject(nodeID) 12 | defer blockchain.DB.Close() 13 | 14 | utxoSet := &UTXOSet{blockchain} 15 | 16 | amount := utxoSet.GetBalance(address) 17 | 18 | fmt.Printf("%s一共有%d个Token\n",address,amount) 19 | 20 | } 21 | -------------------------------------------------------------------------------- /part71-Net-NODE_ID_CONFIG/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) printchain(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | blockchain.Printchain() 11 | 12 | } -------------------------------------------------------------------------------- /part71-Net-NODE_ID_CONFIG/BLC/CLI_send.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 转账 5 | func (cli *CLI) send(from []string,to []string,amount []string,nodeID string) { 6 | 7 | 8 | blockchain := BlockchainObject(nodeID) 9 | defer blockchain.DB.Close() 10 | 11 | blockchain.MineNewBlock(from,to,amount,nodeID) 12 | 13 | utxoSet := &UTXOSet{blockchain} 14 | 15 | //转账成功以后,需要更新一下 16 | utxoSet.Update() 17 | 18 | } 19 | 20 | -------------------------------------------------------------------------------- /part71-Net-NODE_ID_CONFIG/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "bytes" 4 | 5 | type TXInput struct { 6 | // 1. 交易的Hash 7 | TxHash []byte 8 | // 2. 存储TXOutput在Vout里面的索引 9 | Vout int 10 | 11 | Signature []byte // 数字签名 12 | 13 | PublicKey []byte // 公钥,钱包里面 14 | } 15 | 16 | 17 | 18 | // 判断当前的消费是谁的钱 19 | func (txInput *TXInput) UnLockRipemd160Hash(ripemd160Hash []byte) bool { 20 | 21 | publicKey := Ripemd160Hash(txInput.PublicKey) 22 | 23 | return bytes.Compare(publicKey,ripemd160Hash) == 0 24 | } -------------------------------------------------------------------------------- /part71-Net-NODE_ID_CONFIG/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part71-Net-NODE_ID_CONFIG/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "./BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part72-Net-mine_cli/BLC/CLI_UTXO_Reset.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) resetUTXOSet(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | 14 | } 15 | -------------------------------------------------------------------------------- /part72-Net-mine_cli/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet(nodeID string) { 6 | 7 | wallets,_ := NewWallets(nodeID) 8 | 9 | wallets.CreateNewWallet(nodeID) 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part72-Net-mine_cli/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string,nodeID string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address,nodeID) 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | } 14 | 15 | //blocks 16 | //utxoTable -------------------------------------------------------------------------------- /part72-Net-mine_cli/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists(nodeID string) { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets(nodeID) 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part72-Net-mine_cli/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string,nodeID string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | // 获取某一个节点的blockchain对象 11 | blockchain := BlockchainObject(nodeID) 12 | defer blockchain.DB.Close() 13 | 14 | utxoSet := &UTXOSet{blockchain} 15 | 16 | amount := utxoSet.GetBalance(address) 17 | 18 | fmt.Printf("%s一共有%d个Token\n",address,amount) 19 | 20 | } 21 | -------------------------------------------------------------------------------- /part72-Net-mine_cli/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) printchain(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | blockchain.Printchain() 11 | 12 | } -------------------------------------------------------------------------------- /part72-Net-mine_cli/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "bytes" 4 | 5 | type TXInput struct { 6 | // 1. 交易的Hash 7 | TxHash []byte 8 | // 2. 存储TXOutput在Vout里面的索引 9 | Vout int 10 | 11 | Signature []byte // 数字签名 12 | 13 | PublicKey []byte // 公钥,钱包里面 14 | } 15 | 16 | 17 | 18 | // 判断当前的消费是谁的钱 19 | func (txInput *TXInput) UnLockRipemd160Hash(ripemd160Hash []byte) bool { 20 | 21 | publicKey := Ripemd160Hash(txInput.PublicKey) 22 | 23 | return bytes.Compare(publicKey,ripemd160Hash) == 0 24 | } -------------------------------------------------------------------------------- /part72-Net-mine_cli/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part72-Net-mine_cli/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "./BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part73-net-tcp/client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liyuechun/blockchain_go_videos/f06c79c07504d6b7a25316ff670368bc600fb8e2/part73-net-tcp/client -------------------------------------------------------------------------------- /part73-net-tcp/client.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | "net" 7 | "bytes" 8 | "log" 9 | 10 | ) 11 | 12 | func main() { 13 | 14 | sendMessage() 15 | } 16 | 17 | func sendMessage() { 18 | fmt.Println("客户端向服务器发送数据......") 19 | conn, err := net.Dial("tcp", "127.0.0.1:8080") 20 | if err != nil { 21 | panic("error") 22 | } 23 | defer conn.Close() 24 | 25 | // 附带要发送的数据 26 | _, err = io.Copy(conn, bytes.NewReader([]byte("version"))) 27 | if err != nil { 28 | log.Panic(err) 29 | } 30 | } -------------------------------------------------------------------------------- /part73-net-tcp/server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liyuechun/blockchain_go_videos/f06c79c07504d6b7a25316ff670368bc600fb8e2/part73-net-tcp/server -------------------------------------------------------------------------------- /part74-Net-TCP/BLC/CLI_UTXO_Reset.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) resetUTXOSet(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | 14 | } 15 | -------------------------------------------------------------------------------- /part74-Net-TCP/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet(nodeID string) { 6 | 7 | wallets,_ := NewWallets(nodeID) 8 | 9 | wallets.CreateNewWallet(nodeID) 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part74-Net-TCP/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string,nodeID string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address,nodeID) 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | } 14 | 15 | //blocks 16 | //utxoTable -------------------------------------------------------------------------------- /part74-Net-TCP/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists(nodeID string) { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets(nodeID) 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part74-Net-TCP/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string,nodeID string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | // 获取某一个节点的blockchain对象 11 | blockchain := BlockchainObject(nodeID) 12 | defer blockchain.DB.Close() 13 | 14 | utxoSet := &UTXOSet{blockchain} 15 | 16 | amount := utxoSet.GetBalance(address) 17 | 18 | fmt.Printf("%s一共有%d个Token\n",address,amount) 19 | 20 | } 21 | -------------------------------------------------------------------------------- /part74-Net-TCP/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) printchain(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | blockchain.Printchain() 11 | 12 | } -------------------------------------------------------------------------------- /part74-Net-TCP/BLC/CLI_startnode.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) startNode(nodeID string,minerAdd string) { 9 | 10 | // 启动服务器 11 | 12 | if minerAdd == "" || IsValidForAdress([]byte(minerAdd)) { 13 | // 启动服务器 14 | fmt.Printf("启动服务器:localhost:%s\n",nodeID) 15 | //startServer(nodeID,minerAdd) 16 | 17 | } else { 18 | 19 | fmt.Println("指定的地址无效....") 20 | os.Exit(0) 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /part74-Net-TCP/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "bytes" 4 | 5 | type TXInput struct { 6 | // 1. 交易的Hash 7 | TxHash []byte 8 | // 2. 存储TXOutput在Vout里面的索引 9 | Vout int 10 | 11 | Signature []byte // 数字签名 12 | 13 | PublicKey []byte // 公钥,钱包里面 14 | } 15 | 16 | 17 | 18 | // 判断当前的消费是谁的钱 19 | func (txInput *TXInput) UnLockRipemd160Hash(ripemd160Hash []byte) bool { 20 | 21 | publicKey := Ripemd160Hash(txInput.PublicKey) 22 | 23 | return bytes.Compare(publicKey,ripemd160Hash) == 0 24 | } -------------------------------------------------------------------------------- /part74-Net-TCP/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part74-Net-TCP/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "./BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part75-Net-Server/BLC/CLI_UTXO_Reset.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) resetUTXOSet(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | 14 | } 15 | -------------------------------------------------------------------------------- /part75-Net-Server/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet(nodeID string) { 6 | 7 | wallets,_ := NewWallets(nodeID) 8 | 9 | wallets.CreateNewWallet(nodeID) 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part75-Net-Server/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string,nodeID string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address,nodeID) 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | } 14 | 15 | //blocks 16 | //utxoTable -------------------------------------------------------------------------------- /part75-Net-Server/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists(nodeID string) { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets(nodeID) 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part75-Net-Server/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string,nodeID string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | // 获取某一个节点的blockchain对象 11 | blockchain := BlockchainObject(nodeID) 12 | defer blockchain.DB.Close() 13 | 14 | utxoSet := &UTXOSet{blockchain} 15 | 16 | amount := utxoSet.GetBalance(address) 17 | 18 | fmt.Printf("%s一共有%d个Token\n",address,amount) 19 | 20 | } 21 | -------------------------------------------------------------------------------- /part75-Net-Server/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) printchain(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | blockchain.Printchain() 11 | 12 | } -------------------------------------------------------------------------------- /part75-Net-Server/BLC/CLI_startnode.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) startNode(nodeID string,minerAdd string) { 9 | 10 | // 启动服务器 11 | 12 | if minerAdd == "" || IsValidForAdress([]byte(minerAdd)) { 13 | // 启动服务器 14 | fmt.Printf("启动服务器:localhost:%s\n",nodeID) 15 | startServer(nodeID,minerAdd) 16 | 17 | } else { 18 | 19 | fmt.Println("指定的地址无效....") 20 | os.Exit(0) 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /part75-Net-Server/BLC/Constant.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | const PROTOCOL = "tcp" 4 | -------------------------------------------------------------------------------- /part75-Net-Server/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "bytes" 4 | 5 | type TXInput struct { 6 | // 1. 交易的Hash 7 | TxHash []byte 8 | // 2. 存储TXOutput在Vout里面的索引 9 | Vout int 10 | 11 | Signature []byte // 数字签名 12 | 13 | PublicKey []byte // 公钥,钱包里面 14 | } 15 | 16 | 17 | 18 | // 判断当前的消费是谁的钱 19 | func (txInput *TXInput) UnLockRipemd160Hash(ripemd160Hash []byte) bool { 20 | 21 | publicKey := Ripemd160Hash(txInput.PublicKey) 22 | 23 | return bytes.Compare(publicKey,ripemd160Hash) == 0 24 | } -------------------------------------------------------------------------------- /part75-Net-Server/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part75-Net-Server/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "./BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part76-Net-Version/BLC/CLI_UTXO_Reset.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) resetUTXOSet(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | 14 | } 15 | -------------------------------------------------------------------------------- /part76-Net-Version/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet(nodeID string) { 6 | 7 | wallets,_ := NewWallets(nodeID) 8 | 9 | wallets.CreateNewWallet(nodeID) 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part76-Net-Version/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string,nodeID string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address,nodeID) 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | } 14 | 15 | //blocks 16 | //utxoTable -------------------------------------------------------------------------------- /part76-Net-Version/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists(nodeID string) { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets(nodeID) 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part76-Net-Version/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string,nodeID string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | // 获取某一个节点的blockchain对象 11 | blockchain := BlockchainObject(nodeID) 12 | defer blockchain.DB.Close() 13 | 14 | utxoSet := &UTXOSet{blockchain} 15 | 16 | amount := utxoSet.GetBalance(address) 17 | 18 | fmt.Printf("%s一共有%d个Token\n",address,amount) 19 | 20 | } 21 | -------------------------------------------------------------------------------- /part76-Net-Version/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) printchain(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | blockchain.Printchain() 11 | 12 | } -------------------------------------------------------------------------------- /part76-Net-Version/BLC/CLI_startnode.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) startNode(nodeID string,minerAdd string) { 9 | 10 | // 启动服务器 11 | 12 | if minerAdd == "" || IsValidForAdress([]byte(minerAdd)) { 13 | // 启动服务器 14 | fmt.Printf("启动服务器:localhost:%s\n",nodeID) 15 | startServer(nodeID,minerAdd) 16 | 17 | } else { 18 | 19 | fmt.Println("指定的地址无效....") 20 | os.Exit(0) 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /part76-Net-Version/BLC/Constant.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | const PROTOCOL = "tcp" 4 | const COMMANDLENGTH = 12 5 | const NODE_VERSION = 1 6 | 7 | 8 | // 命令 9 | const VERSION = "version" 10 | -------------------------------------------------------------------------------- /part76-Net-Version/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "bytes" 4 | 5 | type TXInput struct { 6 | // 1. 交易的Hash 7 | TxHash []byte 8 | // 2. 存储TXOutput在Vout里面的索引 9 | Vout int 10 | 11 | Signature []byte // 数字签名 12 | 13 | PublicKey []byte // 公钥,钱包里面 14 | } 15 | 16 | 17 | 18 | // 判断当前的消费是谁的钱 19 | func (txInput *TXInput) UnLockRipemd160Hash(ripemd160Hash []byte) bool { 20 | 21 | publicKey := Ripemd160Hash(txInput.PublicKey) 22 | 23 | return bytes.Compare(publicKey,ripemd160Hash) == 0 24 | } -------------------------------------------------------------------------------- /part76-Net-Version/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part76-Net-Version/BLC/Version.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | //"version" 5 | 6 | // 7 | 8 | //12 9 | 10 | type Version struct { 11 | Version int // 版本 12 | BestHeight int // 当前节点区块的高度 13 | AddrFrom string //当前节点的地址 14 | } 15 | -------------------------------------------------------------------------------- /part76-Net-Version/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "./BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part77-Net-GetBestHeight/BLC/CLI_UTXO_Reset.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) resetUTXOSet(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | 14 | } 15 | -------------------------------------------------------------------------------- /part77-Net-GetBestHeight/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet(nodeID string) { 6 | 7 | wallets,_ := NewWallets(nodeID) 8 | 9 | wallets.CreateNewWallet(nodeID) 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part77-Net-GetBestHeight/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string,nodeID string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address,nodeID) 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | } 14 | 15 | //blocks 16 | //utxoTable -------------------------------------------------------------------------------- /part77-Net-GetBestHeight/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists(nodeID string) { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets(nodeID) 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part77-Net-GetBestHeight/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string,nodeID string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | // 获取某一个节点的blockchain对象 11 | blockchain := BlockchainObject(nodeID) 12 | defer blockchain.DB.Close() 13 | 14 | utxoSet := &UTXOSet{blockchain} 15 | 16 | amount := utxoSet.GetBalance(address) 17 | 18 | fmt.Printf("%s一共有%d个Token\n",address,amount) 19 | 20 | } 21 | -------------------------------------------------------------------------------- /part77-Net-GetBestHeight/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) printchain(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | blockchain.Printchain() 11 | 12 | } -------------------------------------------------------------------------------- /part77-Net-GetBestHeight/BLC/CLI_startnode.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) startNode(nodeID string,minerAdd string) { 9 | 10 | // 启动服务器 11 | 12 | if minerAdd == "" || IsValidForAdress([]byte(minerAdd)) { 13 | // 启动服务器 14 | fmt.Printf("启动服务器:localhost:%s\n",nodeID) 15 | startServer(nodeID,minerAdd) 16 | 17 | } else { 18 | 19 | fmt.Println("指定的地址无效....") 20 | os.Exit(0) 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /part77-Net-GetBestHeight/BLC/Constant.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | const PROTOCOL = "tcp" 4 | const COMMANDLENGTH = 12 5 | const NODE_VERSION = 1 6 | 7 | 8 | // 命令 9 | const VERSION = "version" 10 | -------------------------------------------------------------------------------- /part77-Net-GetBestHeight/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "bytes" 4 | 5 | type TXInput struct { 6 | // 1. 交易的Hash 7 | TxHash []byte 8 | // 2. 存储TXOutput在Vout里面的索引 9 | Vout int 10 | 11 | Signature []byte // 数字签名 12 | 13 | PublicKey []byte // 公钥,钱包里面 14 | } 15 | 16 | 17 | 18 | // 判断当前的消费是谁的钱 19 | func (txInput *TXInput) UnLockRipemd160Hash(ripemd160Hash []byte) bool { 20 | 21 | publicKey := Ripemd160Hash(txInput.PublicKey) 22 | 23 | return bytes.Compare(publicKey,ripemd160Hash) == 0 24 | } -------------------------------------------------------------------------------- /part77-Net-GetBestHeight/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part77-Net-GetBestHeight/BLC/Version.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | //"version" 5 | 6 | // 7 | 8 | //12 9 | 10 | type Version struct { 11 | Version int64 // 版本 12 | BestHeight int64 // 当前节点区块的高度 13 | AddrFrom string //当前节点的地址 14 | } 15 | -------------------------------------------------------------------------------- /part77-Net-GetBestHeight/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "./BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part78-Net-Conn/BLC/CLI_UTXO_Reset.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) resetUTXOSet(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | 14 | } 15 | -------------------------------------------------------------------------------- /part78-Net-Conn/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet(nodeID string) { 6 | 7 | wallets,_ := NewWallets(nodeID) 8 | 9 | wallets.CreateNewWallet(nodeID) 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part78-Net-Conn/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string,nodeID string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address,nodeID) 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | } 14 | 15 | //blocks 16 | //utxoTable -------------------------------------------------------------------------------- /part78-Net-Conn/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists(nodeID string) { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets(nodeID) 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part78-Net-Conn/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string,nodeID string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | // 获取某一个节点的blockchain对象 11 | blockchain := BlockchainObject(nodeID) 12 | defer blockchain.DB.Close() 13 | 14 | utxoSet := &UTXOSet{blockchain} 15 | 16 | amount := utxoSet.GetBalance(address) 17 | 18 | fmt.Printf("%s一共有%d个Token\n",address,amount) 19 | 20 | } 21 | -------------------------------------------------------------------------------- /part78-Net-Conn/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) printchain(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | blockchain.Printchain() 11 | 12 | } -------------------------------------------------------------------------------- /part78-Net-Conn/BLC/CLI_startnode.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) startNode(nodeID string,minerAdd string) { 9 | 10 | // 启动服务器 11 | 12 | if minerAdd == "" || IsValidForAdress([]byte(minerAdd)) { 13 | // 启动服务器 14 | fmt.Printf("启动服务器:localhost:%s\n",nodeID) 15 | startServer(nodeID,minerAdd) 16 | 17 | } else { 18 | 19 | fmt.Println("指定的地址无效....") 20 | os.Exit(0) 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /part78-Net-Conn/BLC/Constant.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | const PROTOCOL = "tcp" 4 | const COMMANDLENGTH = 12 5 | const NODE_VERSION = 1 6 | 7 | // 命令 8 | const COMMAND_VERSION = "version" 9 | const COMMAND_ADDR = "addr" 10 | const COMMAND_BLOCK = "block" 11 | const COMMAND_INV = "inv" 12 | const COMMAND_GETBLOCKS = "getblocks" 13 | const COMMAND_GETDATA = "getdata" 14 | const COMMAND_TX = "tx" 15 | 16 | -------------------------------------------------------------------------------- /part78-Net-Conn/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "bytes" 4 | 5 | type TXInput struct { 6 | // 1. 交易的Hash 7 | TxHash []byte 8 | // 2. 存储TXOutput在Vout里面的索引 9 | Vout int 10 | 11 | Signature []byte // 数字签名 12 | 13 | PublicKey []byte // 公钥,钱包里面 14 | } 15 | 16 | 17 | 18 | // 判断当前的消费是谁的钱 19 | func (txInput *TXInput) UnLockRipemd160Hash(ripemd160Hash []byte) bool { 20 | 21 | publicKey := Ripemd160Hash(txInput.PublicKey) 22 | 23 | return bytes.Compare(publicKey,ripemd160Hash) == 0 24 | } -------------------------------------------------------------------------------- /part78-Net-Conn/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part78-Net-Conn/BLC/Version.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | //"version" 5 | 6 | // 7 | 8 | //12 9 | 10 | type Version struct { 11 | Version int64 // 版本 12 | BestHeight int64 // 当前节点区块的高度 13 | AddrFrom string //当前节点的地址 14 | } 15 | -------------------------------------------------------------------------------- /part78-Net-Conn/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "./BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part79-Net-HandleVersion/BLC/CLI_UTXO_Reset.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) resetUTXOSet(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | 14 | } 15 | -------------------------------------------------------------------------------- /part79-Net-HandleVersion/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet(nodeID string) { 6 | 7 | wallets,_ := NewWallets(nodeID) 8 | 9 | wallets.CreateNewWallet(nodeID) 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part79-Net-HandleVersion/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string,nodeID string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address,nodeID) 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | } 14 | 15 | //blocks 16 | //utxoTable -------------------------------------------------------------------------------- /part79-Net-HandleVersion/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists(nodeID string) { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets(nodeID) 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part79-Net-HandleVersion/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string,nodeID string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | // 获取某一个节点的blockchain对象 11 | blockchain := BlockchainObject(nodeID) 12 | defer blockchain.DB.Close() 13 | 14 | utxoSet := &UTXOSet{blockchain} 15 | 16 | amount := utxoSet.GetBalance(address) 17 | 18 | fmt.Printf("%s一共有%d个Token\n",address,amount) 19 | 20 | } 21 | -------------------------------------------------------------------------------- /part79-Net-HandleVersion/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) printchain(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | blockchain.Printchain() 11 | 12 | } -------------------------------------------------------------------------------- /part79-Net-HandleVersion/BLC/CLI_startnode.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) startNode(nodeID string,minerAdd string) { 9 | 10 | // 启动服务器 11 | 12 | if minerAdd == "" || IsValidForAdress([]byte(minerAdd)) { 13 | // 启动服务器 14 | fmt.Printf("启动服务器:localhost:%s\n",nodeID) 15 | startServer(nodeID,minerAdd) 16 | 17 | } else { 18 | 19 | fmt.Println("指定的地址无效....") 20 | os.Exit(0) 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /part79-Net-HandleVersion/BLC/Constant.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | const PROTOCOL = "tcp" 4 | const COMMANDLENGTH = 12 5 | const NODE_VERSION = 1 6 | 7 | // 命令 8 | const COMMAND_VERSION = "version" 9 | const COMMAND_ADDR = "addr" 10 | const COMMAND_BLOCK = "block" 11 | const COMMAND_INV = "inv" 12 | const COMMAND_GETBLOCKS = "getblocks" 13 | const COMMAND_GETDATA = "getdata" 14 | const COMMAND_TX = "tx" 15 | 16 | -------------------------------------------------------------------------------- /part79-Net-HandleVersion/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "bytes" 4 | 5 | type TXInput struct { 6 | // 1. 交易的Hash 7 | TxHash []byte 8 | // 2. 存储TXOutput在Vout里面的索引 9 | Vout int 10 | 11 | Signature []byte // 数字签名 12 | 13 | PublicKey []byte // 公钥,钱包里面 14 | } 15 | 16 | 17 | 18 | // 判断当前的消费是谁的钱 19 | func (txInput *TXInput) UnLockRipemd160Hash(ripemd160Hash []byte) bool { 20 | 21 | publicKey := Ripemd160Hash(txInput.PublicKey) 22 | 23 | return bytes.Compare(publicKey,ripemd160Hash) == 0 24 | } -------------------------------------------------------------------------------- /part79-Net-HandleVersion/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part79-Net-HandleVersion/BLC/Version.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | //"version" 5 | 6 | // 7 | 8 | //12 9 | 10 | type Version struct { 11 | Version int64 // 版本 12 | BestHeight int64 // 当前节点区块的高度 13 | AddrFrom string //当前节点的地址 14 | } 15 | -------------------------------------------------------------------------------- /part79-Net-HandleVersion/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "./BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part8-proof-of-work/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "log" 7 | ) 8 | 9 | // 将int64转换为字节数组 10 | func IntToHex(num int64) []byte { 11 | buff := new(bytes.Buffer) 12 | err := binary.Write(buff, binary.BigEndian, num) 13 | if err != nil { 14 | log.Panic(err) 15 | } 16 | 17 | return buff.Bytes() 18 | } 19 | -------------------------------------------------------------------------------- /part80-Net-sendGetBlocks/BLC/CLI_UTXO_Reset.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) resetUTXOSet(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | 14 | } 15 | -------------------------------------------------------------------------------- /part80-Net-sendGetBlocks/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet(nodeID string) { 6 | 7 | wallets,_ := NewWallets(nodeID) 8 | 9 | wallets.CreateNewWallet(nodeID) 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part80-Net-sendGetBlocks/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string,nodeID string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address,nodeID) 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | } 14 | 15 | //blocks 16 | //utxoTable -------------------------------------------------------------------------------- /part80-Net-sendGetBlocks/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists(nodeID string) { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets(nodeID) 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part80-Net-sendGetBlocks/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string,nodeID string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | // 获取某一个节点的blockchain对象 11 | blockchain := BlockchainObject(nodeID) 12 | defer blockchain.DB.Close() 13 | 14 | utxoSet := &UTXOSet{blockchain} 15 | 16 | amount := utxoSet.GetBalance(address) 17 | 18 | fmt.Printf("%s一共有%d个Token\n",address,amount) 19 | 20 | } 21 | -------------------------------------------------------------------------------- /part80-Net-sendGetBlocks/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) printchain(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | blockchain.Printchain() 11 | 12 | } -------------------------------------------------------------------------------- /part80-Net-sendGetBlocks/BLC/CLI_startnode.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) startNode(nodeID string,minerAdd string) { 9 | 10 | // 启动服务器 11 | 12 | if minerAdd == "" || IsValidForAdress([]byte(minerAdd)) { 13 | // 启动服务器 14 | fmt.Printf("启动服务器:localhost:%s\n",nodeID) 15 | startServer(nodeID,minerAdd) 16 | 17 | } else { 18 | 19 | fmt.Println("指定的地址无效....") 20 | os.Exit(0) 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /part80-Net-sendGetBlocks/BLC/Constant.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | const PROTOCOL = "tcp" 4 | const COMMANDLENGTH = 12 5 | const NODE_VERSION = 1 6 | 7 | // 命令 8 | const COMMAND_VERSION = "version" 9 | const COMMAND_ADDR = "addr" 10 | const COMMAND_BLOCK = "block" 11 | const COMMAND_INV = "inv" 12 | const COMMAND_GETBLOCKS = "getblocks" 13 | const COMMAND_GETDATA = "getdata" 14 | const COMMAND_TX = "tx" 15 | 16 | // 类型 17 | const BLOCK_TYPE = "block" 18 | const TX_TYPE = "tx" -------------------------------------------------------------------------------- /part80-Net-sendGetBlocks/BLC/Server_GetData.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type GetData struct { 4 | AddrFrom string 5 | Type string 6 | Hash []byte 7 | } 8 | -------------------------------------------------------------------------------- /part80-Net-sendGetBlocks/BLC/Server_Inv.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type Inv struct { 4 | AddrFrom string //自己的地址 5 | Type string //类型 block tx 6 | Items [][]byte //hash二维数组 7 | } 8 | -------------------------------------------------------------------------------- /part80-Net-sendGetBlocks/BLC/Server_Version.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | //"version" 5 | 6 | // 7 | 8 | //12 9 | 10 | type Version struct { 11 | Version int64 // 版本 12 | BestHeight int64 // 当前节点区块的高度 13 | AddrFrom string //当前节点的地址 14 | } 15 | -------------------------------------------------------------------------------- /part80-Net-sendGetBlocks/BLC/Server_getblocks.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | type GetBlocks struct { 5 | AddrFrom string 6 | } -------------------------------------------------------------------------------- /part80-Net-sendGetBlocks/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "bytes" 4 | 5 | type TXInput struct { 6 | // 1. 交易的Hash 7 | TxHash []byte 8 | // 2. 存储TXOutput在Vout里面的索引 9 | Vout int 10 | 11 | Signature []byte // 数字签名 12 | 13 | PublicKey []byte // 公钥,钱包里面 14 | } 15 | 16 | 17 | 18 | // 判断当前的消费是谁的钱 19 | func (txInput *TXInput) UnLockRipemd160Hash(ripemd160Hash []byte) bool { 20 | 21 | publicKey := Ripemd160Hash(txInput.PublicKey) 22 | 23 | return bytes.Compare(publicKey,ripemd160Hash) == 0 24 | } -------------------------------------------------------------------------------- /part80-Net-sendGetBlocks/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part80-Net-sendGetBlocks/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "./BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part81-Net-sendGetBlocks/BLC/CLI_UTXO_Reset.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) resetUTXOSet(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | 14 | } 15 | -------------------------------------------------------------------------------- /part81-Net-sendGetBlocks/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet(nodeID string) { 6 | 7 | wallets,_ := NewWallets(nodeID) 8 | 9 | wallets.CreateNewWallet(nodeID) 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part81-Net-sendGetBlocks/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string,nodeID string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address,nodeID) 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | } 14 | 15 | //blocks 16 | //utxoTable -------------------------------------------------------------------------------- /part81-Net-sendGetBlocks/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists(nodeID string) { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets(nodeID) 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part81-Net-sendGetBlocks/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string,nodeID string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | // 获取某一个节点的blockchain对象 11 | blockchain := BlockchainObject(nodeID) 12 | defer blockchain.DB.Close() 13 | 14 | utxoSet := &UTXOSet{blockchain} 15 | 16 | amount := utxoSet.GetBalance(address) 17 | 18 | fmt.Printf("%s一共有%d个Token\n",address,amount) 19 | 20 | } 21 | -------------------------------------------------------------------------------- /part81-Net-sendGetBlocks/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) printchain(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | blockchain.Printchain() 11 | 12 | } -------------------------------------------------------------------------------- /part81-Net-sendGetBlocks/BLC/CLI_startnode.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) startNode(nodeID string,minerAdd string) { 9 | 10 | // 启动服务器 11 | 12 | if minerAdd == "" || IsValidForAdress([]byte(minerAdd)) { 13 | // 启动服务器 14 | fmt.Printf("启动服务器:localhost:%s\n",nodeID) 15 | startServer(nodeID,minerAdd) 16 | 17 | } else { 18 | 19 | fmt.Println("指定的地址无效....") 20 | os.Exit(0) 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /part81-Net-sendGetBlocks/BLC/Constant.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | const PROTOCOL = "tcp" 4 | const COMMANDLENGTH = 12 5 | const NODE_VERSION = 1 6 | 7 | 8 | //12个字节 + 结构体序列化的字节数组 9 | 10 | 11 | // 命令 12 | const COMMAND_VERSION = "version" 13 | const COMMAND_ADDR = "addr" 14 | const COMMAND_BLOCK = "block" 15 | const COMMAND_INV = "inv" 16 | const COMMAND_GETBLOCKS = "getblocks" 17 | const COMMAND_GETDATA = "getdata" 18 | const COMMAND_TX = "tx" 19 | 20 | // 类型 21 | const BLOCK_TYPE = "block" 22 | const TX_TYPE = "tx" -------------------------------------------------------------------------------- /part81-Net-sendGetBlocks/BLC/Server_GetData.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type GetData struct { 4 | AddrFrom string 5 | Type string 6 | Hash []byte 7 | } 8 | -------------------------------------------------------------------------------- /part81-Net-sendGetBlocks/BLC/Server_Inv.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type Inv struct { 4 | AddrFrom string //自己的地址 5 | Type string //类型 block tx 6 | Items [][]byte //hash二维数组 7 | } 8 | -------------------------------------------------------------------------------- /part81-Net-sendGetBlocks/BLC/Server_Version.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | //"version" 5 | 6 | // 7 | 8 | //12 9 | 10 | type Version struct { 11 | Version int64 // 版本 12 | BestHeight int64 // 当前节点区块的高度 13 | AddrFrom string //当前节点的地址 14 | } 15 | -------------------------------------------------------------------------------- /part81-Net-sendGetBlocks/BLC/Server_block.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type BlockData struct { 4 | AddrFrom string 5 | Block *Block 6 | } 7 | -------------------------------------------------------------------------------- /part81-Net-sendGetBlocks/BLC/Server_getblocks.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | type GetBlocks struct { 5 | AddrFrom string 6 | } -------------------------------------------------------------------------------- /part81-Net-sendGetBlocks/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part81-Net-sendGetBlocks/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "./BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part82-Net-blockhash_handle/BLC/CLI_UTXO_Reset.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) resetUTXOSet(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | 14 | } 15 | -------------------------------------------------------------------------------- /part82-Net-blockhash_handle/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet(nodeID string) { 6 | 7 | wallets,_ := NewWallets(nodeID) 8 | 9 | wallets.CreateNewWallet(nodeID) 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part82-Net-blockhash_handle/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string,nodeID string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address,nodeID) 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | } 14 | 15 | //blocks 16 | //utxoTable -------------------------------------------------------------------------------- /part82-Net-blockhash_handle/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists(nodeID string) { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets(nodeID) 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part82-Net-blockhash_handle/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string,nodeID string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | // 获取某一个节点的blockchain对象 11 | blockchain := BlockchainObject(nodeID) 12 | defer blockchain.DB.Close() 13 | 14 | utxoSet := &UTXOSet{blockchain} 15 | 16 | amount := utxoSet.GetBalance(address) 17 | 18 | fmt.Printf("%s一共有%d个Token\n",address,amount) 19 | 20 | } 21 | -------------------------------------------------------------------------------- /part82-Net-blockhash_handle/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) printchain(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | blockchain.Printchain() 11 | 12 | } -------------------------------------------------------------------------------- /part82-Net-blockhash_handle/BLC/CLI_startnode.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) startNode(nodeID string,minerAdd string) { 9 | 10 | // 启动服务器 11 | 12 | if minerAdd == "" || IsValidForAdress([]byte(minerAdd)) { 13 | // 启动服务器 14 | fmt.Printf("启动服务器:localhost:%s\n",nodeID) 15 | startServer(nodeID,minerAdd) 16 | 17 | } else { 18 | 19 | fmt.Println("指定的地址无效....") 20 | os.Exit(0) 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /part82-Net-blockhash_handle/BLC/Constant.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | const PROTOCOL = "tcp" 4 | const COMMANDLENGTH = 12 5 | const NODE_VERSION = 1 6 | 7 | 8 | //12个字节 + 结构体序列化的字节数组 9 | 10 | 11 | // 命令 12 | const COMMAND_VERSION = "version" 13 | const COMMAND_ADDR = "addr" 14 | const COMMAND_BLOCK = "block" 15 | const COMMAND_INV = "inv" 16 | const COMMAND_GETBLOCKS = "getblocks" 17 | const COMMAND_GETDATA = "getdata" 18 | const COMMAND_TX = "tx" 19 | 20 | // 类型 21 | const BLOCK_TYPE = "block" 22 | const TX_TYPE = "tx" -------------------------------------------------------------------------------- /part82-Net-blockhash_handle/BLC/Server_GetData.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type GetData struct { 4 | AddrFrom string 5 | Type string 6 | Hash []byte 7 | } 8 | -------------------------------------------------------------------------------- /part82-Net-blockhash_handle/BLC/Server_Inv.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type Inv struct { 4 | AddrFrom string //自己的地址 5 | Type string //类型 block tx 6 | Items [][]byte //hash二维数组 7 | } 8 | -------------------------------------------------------------------------------- /part82-Net-blockhash_handle/BLC/Server_Version.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | //"version" 5 | 6 | // 7 | 8 | //12 9 | 10 | type Version struct { 11 | Version int64 // 版本 12 | BestHeight int64 // 当前节点区块的高度 13 | AddrFrom string //当前节点的地址 14 | } 15 | -------------------------------------------------------------------------------- /part82-Net-blockhash_handle/BLC/Server_block.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type BlockData struct { 4 | AddrFrom string 5 | Block *Block 6 | } 7 | -------------------------------------------------------------------------------- /part82-Net-blockhash_handle/BLC/Server_getblocks.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | type GetBlocks struct { 5 | AddrFrom string 6 | } -------------------------------------------------------------------------------- /part82-Net-blockhash_handle/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part82-Net-blockhash_handle/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "./BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part83-Net-handleblock/BLC/CLI_UTXO_Reset.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) resetUTXOSet(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | 14 | } 15 | -------------------------------------------------------------------------------- /part83-Net-handleblock/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet(nodeID string) { 6 | 7 | wallets,_ := NewWallets(nodeID) 8 | 9 | wallets.CreateNewWallet(nodeID) 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part83-Net-handleblock/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string,nodeID string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address,nodeID) 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | } 14 | 15 | //blocks 16 | //utxoTable -------------------------------------------------------------------------------- /part83-Net-handleblock/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists(nodeID string) { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets(nodeID) 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part83-Net-handleblock/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string,nodeID string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | // 获取某一个节点的blockchain对象 11 | blockchain := BlockchainObject(nodeID) 12 | defer blockchain.DB.Close() 13 | 14 | utxoSet := &UTXOSet{blockchain} 15 | 16 | amount := utxoSet.GetBalance(address) 17 | 18 | fmt.Printf("%s一共有%d个Token\n",address,amount) 19 | 20 | } 21 | -------------------------------------------------------------------------------- /part83-Net-handleblock/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) printchain(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | blockchain.Printchain() 11 | 12 | } -------------------------------------------------------------------------------- /part83-Net-handleblock/BLC/CLI_startnode.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) startNode(nodeID string,minerAdd string) { 9 | 10 | // 启动服务器 11 | 12 | if minerAdd == "" || IsValidForAdress([]byte(minerAdd)) { 13 | // 启动服务器 14 | fmt.Printf("启动服务器:localhost:%s\n",nodeID) 15 | startServer(nodeID,minerAdd) 16 | 17 | } else { 18 | 19 | fmt.Println("指定的地址无效....") 20 | os.Exit(0) 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /part83-Net-handleblock/BLC/Constant.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | const PROTOCOL = "tcp" 4 | const COMMANDLENGTH = 12 5 | const NODE_VERSION = 1 6 | 7 | 8 | //12个字节 + 结构体序列化的字节数组 9 | 10 | 11 | // 命令 12 | const COMMAND_VERSION = "version" 13 | const COMMAND_ADDR = "addr" 14 | const COMMAND_BLOCK = "block" 15 | const COMMAND_INV = "inv" 16 | const COMMAND_GETBLOCKS = "getblocks" 17 | const COMMAND_GETDATA = "getdata" 18 | const COMMAND_TX = "tx" 19 | 20 | // 类型 21 | const BLOCK_TYPE = "block" 22 | const TX_TYPE = "tx" -------------------------------------------------------------------------------- /part83-Net-handleblock/BLC/Server_GetData.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type GetData struct { 4 | AddrFrom string 5 | Type string 6 | Hash []byte 7 | } 8 | -------------------------------------------------------------------------------- /part83-Net-handleblock/BLC/Server_Inv.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type Inv struct { 4 | AddrFrom string //自己的地址 5 | Type string //类型 block tx 6 | Items [][]byte //hash二维数组 7 | } 8 | -------------------------------------------------------------------------------- /part83-Net-handleblock/BLC/Server_Version.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | //"version" 5 | 6 | // 7 | 8 | //12 9 | 10 | type Version struct { 11 | Version int64 // 版本 12 | BestHeight int64 // 当前节点区块的高度 13 | AddrFrom string //当前节点的地址 14 | } 15 | -------------------------------------------------------------------------------- /part83-Net-handleblock/BLC/Server_block.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type BlockData struct { 4 | AddrFrom string 5 | Block *Block 6 | } 7 | -------------------------------------------------------------------------------- /part83-Net-handleblock/BLC/Server_getblocks.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | type GetBlocks struct { 5 | AddrFrom string 6 | } -------------------------------------------------------------------------------- /part83-Net-handleblock/BLC/Server_var.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | //存储节点全局变量 5 | 6 | 7 | //localhost:3000 主节点的地址 8 | var knowNodes = []string{"localhost:3000"} 9 | var nodeAddress string //全局变量,节点地址 10 | // 存储hash值 11 | var transactionArray [][]byte -------------------------------------------------------------------------------- /part83-Net-handleblock/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part83-Net-handleblock/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "./BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part84-Net-validation/BLC/CLI_UTXO_Reset.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) resetUTXOSet(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | 14 | } 15 | -------------------------------------------------------------------------------- /part84-Net-validation/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet(nodeID string) { 6 | 7 | wallets,_ := NewWallets(nodeID) 8 | 9 | wallets.CreateNewWallet(nodeID) 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part84-Net-validation/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string,nodeID string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address,nodeID) 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | } 14 | 15 | //blocks 16 | //utxoTable -------------------------------------------------------------------------------- /part84-Net-validation/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists(nodeID string) { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets(nodeID) 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part84-Net-validation/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string,nodeID string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | // 获取某一个节点的blockchain对象 11 | blockchain := BlockchainObject(nodeID) 12 | defer blockchain.DB.Close() 13 | 14 | utxoSet := &UTXOSet{blockchain} 15 | 16 | amount := utxoSet.GetBalance(address) 17 | 18 | fmt.Printf("%s一共有%d个Token\n",address,amount) 19 | 20 | } 21 | -------------------------------------------------------------------------------- /part84-Net-validation/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) printchain(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | blockchain.Printchain() 11 | 12 | } -------------------------------------------------------------------------------- /part84-Net-validation/BLC/CLI_startnode.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) startNode(nodeID string,minerAdd string) { 9 | 10 | // 启动服务器 11 | 12 | if minerAdd == "" || IsValidForAdress([]byte(minerAdd)) { 13 | // 启动服务器 14 | fmt.Printf("启动服务器:localhost:%s\n",nodeID) 15 | startServer(nodeID,minerAdd) 16 | 17 | } else { 18 | 19 | fmt.Println("指定的地址无效....") 20 | os.Exit(0) 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /part84-Net-validation/BLC/Constant.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | const PROTOCOL = "tcp" 4 | const COMMANDLENGTH = 12 5 | const NODE_VERSION = 1 6 | 7 | 8 | //12个字节 + 结构体序列化的字节数组 9 | 10 | 11 | // 命令 12 | const COMMAND_VERSION = "version" 13 | const COMMAND_ADDR = "addr" 14 | const COMMAND_BLOCK = "block" 15 | const COMMAND_INV = "inv" 16 | const COMMAND_GETBLOCKS = "getblocks" 17 | const COMMAND_GETDATA = "getdata" 18 | const COMMAND_TX = "tx" 19 | 20 | // 类型 21 | const BLOCK_TYPE = "block" 22 | const TX_TYPE = "tx" -------------------------------------------------------------------------------- /part84-Net-validation/BLC/Server_GetData.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type GetData struct { 4 | AddrFrom string 5 | Type string 6 | Hash []byte 7 | } 8 | -------------------------------------------------------------------------------- /part84-Net-validation/BLC/Server_Inv.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type Inv struct { 4 | AddrFrom string //自己的地址 5 | Type string //类型 block tx 6 | Items [][]byte //hash二维数组 7 | } 8 | -------------------------------------------------------------------------------- /part84-Net-validation/BLC/Server_Version.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | //"version" 5 | 6 | // 7 | 8 | //12 9 | 10 | type Version struct { 11 | Version int64 // 版本 12 | BestHeight int64 // 当前节点区块的高度 13 | AddrFrom string //当前节点的地址 14 | } 15 | -------------------------------------------------------------------------------- /part84-Net-validation/BLC/Server_block.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type BlockData struct { 4 | AddrFrom string 5 | Block *Block 6 | } 7 | -------------------------------------------------------------------------------- /part84-Net-validation/BLC/Server_getblocks.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | type GetBlocks struct { 5 | AddrFrom string 6 | } -------------------------------------------------------------------------------- /part84-Net-validation/BLC/Server_var.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | //存储节点全局变量 5 | 6 | 7 | //localhost:3000 主节点的地址 8 | var knowNodes = []string{"localhost:3000"} 9 | var nodeAddress string //全局变量,节点地址 10 | // 存储hash值 11 | var transactionArray [][]byte -------------------------------------------------------------------------------- /part84-Net-validation/BLC/Transaction_TXInput.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "bytes" 4 | 5 | type TXInput struct { 6 | // 1. 交易的Hash 7 | TxHash []byte 8 | // 2. 存储TXOutput在Vout里面的索引 9 | Vout int 10 | 11 | Signature []byte // 数字签名 12 | 13 | PublicKey []byte // 公钥,钱包里面 14 | } 15 | 16 | 17 | 18 | // 判断当前的消费是谁的钱 19 | func (txInput *TXInput) UnLockRipemd160Hash(ripemd160Hash []byte) bool { 20 | 21 | publicKey := Ripemd160Hash(txInput.PublicKey) 22 | 23 | return bytes.Compare(publicKey,ripemd160Hash) == 0 24 | } -------------------------------------------------------------------------------- /part84-Net-validation/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part84-Net-validation/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "./BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part85-Net-validation-fix-bug/BLC/CLI_UTXO_Reset.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) resetUTXOSet(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | 14 | } 15 | -------------------------------------------------------------------------------- /part85-Net-validation-fix-bug/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet(nodeID string) { 6 | 7 | wallets,_ := NewWallets(nodeID) 8 | 9 | wallets.CreateNewWallet(nodeID) 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part85-Net-validation-fix-bug/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string,nodeID string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address,nodeID) 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | } 14 | 15 | //blocks 16 | //utxoTable -------------------------------------------------------------------------------- /part85-Net-validation-fix-bug/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists(nodeID string) { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets(nodeID) 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part85-Net-validation-fix-bug/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string,nodeID string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | // 获取某一个节点的blockchain对象 11 | blockchain := BlockchainObject(nodeID) 12 | defer blockchain.DB.Close() 13 | 14 | utxoSet := &UTXOSet{blockchain} 15 | 16 | amount := utxoSet.GetBalance(address) 17 | 18 | fmt.Printf("%s一共有%d个Token\n",address,amount) 19 | 20 | } 21 | -------------------------------------------------------------------------------- /part85-Net-validation-fix-bug/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) printchain(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | blockchain.Printchain() 11 | 12 | } -------------------------------------------------------------------------------- /part85-Net-validation-fix-bug/BLC/CLI_startnode.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) startNode(nodeID string,minerAdd string) { 9 | 10 | // 启动服务器 11 | 12 | if minerAdd == "" || IsValidForAdress([]byte(minerAdd)) { 13 | // 启动服务器 14 | fmt.Printf("启动服务器:localhost:%s\n",nodeID) 15 | startServer(nodeID,minerAdd) 16 | 17 | } else { 18 | 19 | fmt.Println("指定的地址无效....") 20 | os.Exit(0) 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /part85-Net-validation-fix-bug/BLC/Constant.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | const PROTOCOL = "tcp" 4 | const COMMANDLENGTH = 12 5 | const NODE_VERSION = 1 6 | 7 | 8 | //12个字节 + 结构体序列化的字节数组 9 | 10 | 11 | // 命令 12 | const COMMAND_VERSION = "version" 13 | const COMMAND_ADDR = "addr" 14 | const COMMAND_BLOCK = "block" 15 | const COMMAND_INV = "inv" 16 | const COMMAND_GETBLOCKS = "getblocks" 17 | const COMMAND_GETDATA = "getdata" 18 | const COMMAND_TX = "tx" 19 | 20 | // 类型 21 | const BLOCK_TYPE = "block" 22 | const TX_TYPE = "tx" -------------------------------------------------------------------------------- /part85-Net-validation-fix-bug/BLC/Server_GetData.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type GetData struct { 4 | AddrFrom string 5 | Type string 6 | Hash []byte 7 | } 8 | -------------------------------------------------------------------------------- /part85-Net-validation-fix-bug/BLC/Server_Inv.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type Inv struct { 4 | AddrFrom string //自己的地址 5 | Type string //类型 block tx 6 | Items [][]byte //hash二维数组 7 | } 8 | -------------------------------------------------------------------------------- /part85-Net-validation-fix-bug/BLC/Server_Version.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | //"version" 5 | 6 | // 7 | 8 | //12 9 | 10 | type Version struct { 11 | Version int64 // 版本 12 | BestHeight int64 // 当前节点区块的高度 13 | AddrFrom string //当前节点的地址 14 | } 15 | -------------------------------------------------------------------------------- /part85-Net-validation-fix-bug/BLC/Server_block.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type BlockData struct { 4 | AddrFrom string 5 | Block []byte 6 | } 7 | -------------------------------------------------------------------------------- /part85-Net-validation-fix-bug/BLC/Server_getblocks.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | type GetBlocks struct { 5 | AddrFrom string 6 | } -------------------------------------------------------------------------------- /part85-Net-validation-fix-bug/BLC/Server_var.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | //存储节点全局变量 5 | 6 | 7 | //localhost:3000 主节点的地址 8 | var knowNodes = []string{"localhost:3000"} 9 | var nodeAddress string //全局变量,节点地址 10 | // 存储hash值 11 | var transactionArray [][]byte -------------------------------------------------------------------------------- /part85-Net-validation-fix-bug/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part85-Net-validation-fix-bug/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "./BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part86-Net-send-logcal/BLC/CLI_UTXO_Reset.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) resetUTXOSet(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | 14 | } 15 | -------------------------------------------------------------------------------- /part86-Net-send-logcal/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet(nodeID string) { 6 | 7 | wallets,_ := NewWallets(nodeID) 8 | 9 | wallets.CreateNewWallet(nodeID) 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part86-Net-send-logcal/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string,nodeID string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address,nodeID) 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | } 14 | 15 | //blocks 16 | //utxoTable -------------------------------------------------------------------------------- /part86-Net-send-logcal/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists(nodeID string) { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets(nodeID) 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part86-Net-send-logcal/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string,nodeID string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | // 获取某一个节点的blockchain对象 11 | blockchain := BlockchainObject(nodeID) 12 | defer blockchain.DB.Close() 13 | 14 | utxoSet := &UTXOSet{blockchain} 15 | 16 | amount := utxoSet.GetBalance(address) 17 | 18 | fmt.Printf("%s一共有%d个Token\n",address,amount) 19 | 20 | } 21 | -------------------------------------------------------------------------------- /part86-Net-send-logcal/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) printchain(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | blockchain.Printchain() 11 | 12 | } -------------------------------------------------------------------------------- /part86-Net-send-logcal/BLC/CLI_startnode.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) startNode(nodeID string,minerAdd string) { 9 | 10 | // 启动服务器 11 | 12 | if minerAdd == "" || IsValidForAdress([]byte(minerAdd)) { 13 | // 启动服务器 14 | fmt.Printf("启动服务器:localhost:%s\n",nodeID) 15 | startServer(nodeID,minerAdd) 16 | 17 | } else { 18 | 19 | fmt.Println("指定的地址无效....") 20 | os.Exit(0) 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /part86-Net-send-logcal/BLC/Constant.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | const PROTOCOL = "tcp" 4 | const COMMANDLENGTH = 12 5 | const NODE_VERSION = 1 6 | 7 | 8 | //12个字节 + 结构体序列化的字节数组 9 | 10 | 11 | // 命令 12 | const COMMAND_VERSION = "version" 13 | const COMMAND_ADDR = "addr" 14 | const COMMAND_BLOCK = "block" 15 | const COMMAND_INV = "inv" 16 | const COMMAND_GETBLOCKS = "getblocks" 17 | const COMMAND_GETDATA = "getdata" 18 | const COMMAND_TX = "tx" 19 | 20 | // 类型 21 | const BLOCK_TYPE = "block" 22 | const TX_TYPE = "tx" -------------------------------------------------------------------------------- /part86-Net-send-logcal/BLC/Server_GetData.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type GetData struct { 4 | AddrFrom string 5 | Type string 6 | Hash []byte 7 | } 8 | -------------------------------------------------------------------------------- /part86-Net-send-logcal/BLC/Server_Inv.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type Inv struct { 4 | AddrFrom string //自己的地址 5 | Type string //类型 block tx 6 | Items [][]byte //hash二维数组 7 | } 8 | -------------------------------------------------------------------------------- /part86-Net-send-logcal/BLC/Server_Tx.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type Tx struct { 4 | AddrFrom string 5 | Tx *Transaction 6 | } 7 | 8 | -------------------------------------------------------------------------------- /part86-Net-send-logcal/BLC/Server_Version.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | //"version" 5 | 6 | // 7 | 8 | //12 9 | 10 | type Version struct { 11 | Version int64 // 版本 12 | BestHeight int64 // 当前节点区块的高度 13 | AddrFrom string //当前节点的地址 14 | } 15 | -------------------------------------------------------------------------------- /part86-Net-send-logcal/BLC/Server_block.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type BlockData struct { 4 | AddrFrom string 5 | Block []byte 6 | } 7 | -------------------------------------------------------------------------------- /part86-Net-send-logcal/BLC/Server_getblocks.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | type GetBlocks struct { 5 | AddrFrom string 6 | } -------------------------------------------------------------------------------- /part86-Net-send-logcal/BLC/Server_var.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | //存储节点全局变量 5 | 6 | 7 | //localhost:3000 主节点的地址 8 | var knowNodes = []string{"localhost:3000"} 9 | var nodeAddress string //全局变量,节点地址 10 | // 存储hash值 11 | var transactionArray [][]byte 12 | var minerAddress string 13 | var memoryTxPool = make(map[string]*Transaction) 14 | -------------------------------------------------------------------------------- /part86-Net-send-logcal/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part86-Net-send-logcal/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "./BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part87-Net-send-logcal/BLC/CLI_UTXO_Reset.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) resetUTXOSet(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | 14 | } 15 | -------------------------------------------------------------------------------- /part87-Net-send-logcal/BLC/CLI_createWallet.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | func (cli *CLI) createWallet(nodeID string) { 6 | 7 | wallets,_ := NewWallets(nodeID) 8 | 9 | wallets.CreateNewWallet(nodeID) 10 | 11 | fmt.Println(len(wallets.WalletsMap)) 12 | } 13 | -------------------------------------------------------------------------------- /part87-Net-send-logcal/BLC/CLI_createblockchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | // 创建创世区块 5 | func (cli *CLI) createGenesisBlockchain(address string,nodeID string) { 6 | 7 | blockchain := CreateBlockchainWithGenesisBlock(address,nodeID) 8 | defer blockchain.DB.Close() 9 | 10 | utxoSet := &UTXOSet{blockchain} 11 | 12 | utxoSet.ResetUTXOSet() 13 | } 14 | 15 | //blocks 16 | //utxoTable -------------------------------------------------------------------------------- /part87-Net-send-logcal/BLC/CLI_getaddresslists.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 打印所有的钱包地址 6 | func (cli *CLI) addressLists(nodeID string) { 7 | 8 | fmt.Println("打印所有的钱包地址:") 9 | 10 | wallets,_ := NewWallets(nodeID) 11 | 12 | for address,_ := range wallets.WalletsMap { 13 | 14 | fmt.Println(address) 15 | } 16 | } -------------------------------------------------------------------------------- /part87-Net-send-logcal/BLC/CLI_getbalance.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import "fmt" 4 | 5 | // 先用它去查询余额 6 | func (cli *CLI) getBalance(address string,nodeID string) { 7 | 8 | fmt.Println("地址:" + address) 9 | 10 | // 获取某一个节点的blockchain对象 11 | blockchain := BlockchainObject(nodeID) 12 | defer blockchain.DB.Close() 13 | 14 | utxoSet := &UTXOSet{blockchain} 15 | 16 | amount := utxoSet.GetBalance(address) 17 | 18 | fmt.Printf("%s一共有%d个Token\n",address,amount) 19 | 20 | } 21 | -------------------------------------------------------------------------------- /part87-Net-send-logcal/BLC/CLI_printchain.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | func (cli *CLI) printchain(nodeID string) { 5 | 6 | blockchain := BlockchainObject(nodeID) 7 | 8 | defer blockchain.DB.Close() 9 | 10 | blockchain.Printchain() 11 | 12 | } -------------------------------------------------------------------------------- /part87-Net-send-logcal/BLC/CLI_startnode.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func (cli *CLI) startNode(nodeID string,minerAdd string) { 9 | 10 | // 启动服务器 11 | 12 | if minerAdd == "" || IsValidForAdress([]byte(minerAdd)) { 13 | // 启动服务器 14 | fmt.Printf("启动服务器:localhost:%s\n",nodeID) 15 | startServer(nodeID,minerAdd) 16 | 17 | } else { 18 | 19 | fmt.Println("指定的地址无效....") 20 | os.Exit(0) 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /part87-Net-send-logcal/BLC/Constant.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | const PROTOCOL = "tcp" 4 | const COMMANDLENGTH = 12 5 | const NODE_VERSION = 1 6 | 7 | 8 | //12个字节 + 结构体序列化的字节数组 9 | 10 | 11 | // 命令 12 | const COMMAND_VERSION = "version" 13 | const COMMAND_ADDR = "addr" 14 | const COMMAND_BLOCK = "block" 15 | const COMMAND_INV = "inv" 16 | const COMMAND_GETBLOCKS = "getblocks" 17 | const COMMAND_GETDATA = "getdata" 18 | const COMMAND_TX = "tx" 19 | 20 | // 类型 21 | const BLOCK_TYPE = "block" 22 | const TX_TYPE = "tx" -------------------------------------------------------------------------------- /part87-Net-send-logcal/BLC/Server_GetData.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type GetData struct { 4 | AddrFrom string 5 | Type string 6 | Hash []byte 7 | } 8 | -------------------------------------------------------------------------------- /part87-Net-send-logcal/BLC/Server_Inv.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type Inv struct { 4 | AddrFrom string //自己的地址 5 | Type string //类型 block tx 6 | Items [][]byte //hash二维数组 7 | } 8 | -------------------------------------------------------------------------------- /part87-Net-send-logcal/BLC/Server_Tx.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type Tx struct { 4 | AddrFrom string 5 | Tx *Transaction 6 | } 7 | 8 | -------------------------------------------------------------------------------- /part87-Net-send-logcal/BLC/Server_Version.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | //"version" 5 | 6 | // 7 | 8 | //12 9 | 10 | type Version struct { 11 | Version int64 // 版本 12 | BestHeight int64 // 当前节点区块的高度 13 | AddrFrom string //当前节点的地址 14 | } 15 | -------------------------------------------------------------------------------- /part87-Net-send-logcal/BLC/Server_block.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type BlockData struct { 4 | AddrFrom string 5 | Block []byte 6 | } 7 | -------------------------------------------------------------------------------- /part87-Net-send-logcal/BLC/Server_getblocks.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | type GetBlocks struct { 5 | AddrFrom string 6 | } -------------------------------------------------------------------------------- /part87-Net-send-logcal/BLC/Server_var.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | 4 | //存储节点全局变量 5 | 6 | 7 | //localhost:3000 主节点的地址 8 | var knowNodes = []string{"localhost:3000"} 9 | var nodeAddress string //全局变量,节点地址 10 | // 存储hash值 11 | var transactionArray [][]byte 12 | var minerAddress string 13 | var memoryTxPool = make(map[string]*Transaction) 14 | -------------------------------------------------------------------------------- /part87-Net-send-logcal/BLC/Transaction_UTXO.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | type UTXO struct { 4 | TxHash []byte 5 | Index int 6 | Output *TXOutput 7 | } 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part87-Net-send-logcal/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | 5 | "./BLC" 6 | ) 7 | 8 | func main() { 9 | 10 | cli := BLC.CLI{} 11 | cli.Run() 12 | } -------------------------------------------------------------------------------- /part9-Serialize-DeserializeBlock/BLC/utils.go: -------------------------------------------------------------------------------- 1 | package BLC 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "log" 7 | ) 8 | 9 | // 将int64转换为字节数组 10 | func IntToHex(num int64) []byte { 11 | buff := new(bytes.Buffer) 12 | err := binary.Write(buff, binary.BigEndian, num) 13 | if err != nil { 14 | log.Panic(err) 15 | } 16 | 17 | return buff.Bytes() 18 | } 19 | --------------------------------------------------------------------------------