Java Blockchain: Handling Coinbase Transactions and Transaction Verification Issues

I’m currently developing a java implementation of bitcoin, and I’m facing challenges related to handling coinbases transactions and ensuring the verification of these transactions within the blockchain. The coinbase transactions are there to reward the miners for successfully mining the blocks. These coinbase transactions generate new coins, which are then added to the miner’s wallet. However, I’m countering difficulties in correctly identifying and processing these transactions.

public static void main(String[] args) {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        Wallet walletA = new Wallet();
        Wallet walletB = new Wallet();
        coinbase = new Wallet();

        Block genesis = new Block("0");
        genesisTransaction = mintBlock(genesis, walletA);

        Block block1 = new Block(blockchain.getLast().hash);
        block1.addTransaction(walletA.sendFunds(walletB.publicKey, 40f));
        mintBlock(block1, walletA);

        Block block2 = new Block(blockchain.getLast().hash);
        block2.addTransaction(walletA.sendFunds(walletB.publicKey, 1000f));
        mintBlock(block2, walletA);

        Block block3 = new Block(blockchain.getLast().hash);
        block3.addTransaction(walletB.sendFunds(walletA.publicKey, 20f));
        block3.addTransaction(walletB.sendFunds(walletA.publicKey, 10f));
        mintBlock(block3, walletB);

        for (int i = 0; i < 2; i++) {
            mintBlock(new Block(blockchain.getLast().hash), walletB);
        }
        System.out.println("n[*] Blockchain is valid: " + chainValidity());
}

public static Transaction mintBlock(Block newBlock, Wallet minerAddress) {
        Transaction coinbaseTx = new Transaction(coinbase.publicKey, minerAddress.publicKey, 100f, null);
        coinbaseTx.generateSignature(coinbase.privateKey);
        coinbaseTx.transactionID = "COINBASE_" + newBlock.hashCode();
        coinbaseTx.outputs.add(new TransOutput(coinbaseTx.recipient, coinbaseTx.value, coinbaseTx.transactionID));
        UTXOs.put(coinbaseTx.outputs.getFirst().ID, coinbaseTx.outputs.getFirst());
        
        newBlock.addTransaction(coinbaseTx);
        newBlock.mineBlock(difficulty);
        blockchain.add(newBlock);
        return coinbaseTx; // genesisTransaction
}

These are some of the tests I wrote to check whether the code is working, and numerically all the data works. As in each wallet has the expected transactions with the correct rewards, but there always runs into an error saying Transaction inputs too small; there might be an issue with the transaction inputs or how I’m handling the coinbase results. Below is the transaction class.

public class Transaction {
    public String transactionID;
    public PublicKey sender;
    public PublicKey recipient;
    public float value;
    public byte[] signature;

    public List<TransInput> inputs = new ArrayList<>();
    public List<TransOutput> outputs = new ArrayList<>();
    public static int sequence = 0;

    public Transaction(PublicKey from, PublicKey to, float value, List<TransInput> inputs) {
        this.sender = from;
        this.recipient = to;
        this.value = value;
        this.inputs = inputs == null ? new ArrayList<>() : inputs;
    }
    .. code excluded...
    public boolean processTransaction() {
        if (!this.verifySignature()) {
            System.out.println("[!] Transaction Signature failed to verify.");
            return false;
        }

        for (TransInput i : inputs) {
            i.UTXO = AevumChain.UTXOs.get(i.transOutputID);
        }

        if (this.getInputsValue() < AevumChain.minTransaction) {
            System.out.println("[!] Transaction Inputs too small: " + this.getInputsValue());
            return false;
        }

        float leftOver = this.getInputsValue() - this.value;
        this.transactionID = this.calculateHash();
        this.outputs.add(new TransOutput(this.recipient, value, transactionID));
        if (leftOver > 0) {
            this.outputs.add(new TransOutput(this.sender, leftOver, transactionID)); // send the left over 'change' back to sender
        }

        for (TransOutput o : outputs) {
            AevumChain.UTXOs.put(o.ID, o);
        }

        for (TransInput i : inputs) {
            if (i.UTXO == null) continue;
            AevumChain.UTXOs.remove(i.UTXO.ID);
        }
        return true;
    }

This is the output that I keep getting. Although the transaction go through correctly, there is a transaction inputs too small error everytime the transaction for the coinbase goes through. This is only an issue with the coinbase because the error only occurs during that instance.

[*] Creating and Mining Genesis block...
[+] Block mined: 000002ff3d5c92f121cdf0ebb9f79afa3c1f1c4a9c430b7107e1213d97ce4fee
[*] WalletA's balance is: 100.0
[*] WalletB's balance is: 0.0

[*] WalletA is attempting to send funds (40) to WalletB...
[!] Transaction Inputs too small: 0.0
[!] Transaction failed to process. Discarded.
[+] Block mined: 000008c35d6dd48ff3827eec2031083889ab4373b94998b385fa70e523c7b208
[*] WalletA's balance is: 160.0
[*] WalletB's balance is: 40.0

[*] WalletA is attempting to send more funds (1000) than it has...
[!] Not enough funds to send transaction. Transaction Discarded.
[!] Transaction Inputs too small: 0.0
[!] Transaction failed to process. Discarded.
[+] Block mined: 00000f56d8fe0428aa21bc012045f99f9e494b6bc3935e25c5287a4abde77a3c
[*] WalletA's balance is: 260.0
[*] WalletB's balance is: 40.0

[*] WalletB is attempting to send funds (20) to WalletA...
[*] WalletB is attempting to send funds (10) to WalletA...
[!] Transaction Inputs too small: 0.0
[!] Transaction failed to process. Discarded.
[+] Block mined: 000001f560bfd1b70363b40193ab8a977a94693d83e3533ecaacf43ea2e14521
[*] WalletA's balance is: 290.0
[*] WalletB's balance is: 110.0

[*] WalletB runs a mining algorithm for 10 coins...
[!] Transaction Inputs too small: 0.0
[!] Transaction failed to process. Discarded.
[+] Block mined: 00000a2f483d210b37f656c72c902f29f6b08af5832513f210b029b075a69c3e
[!] Transaction Inputs too small: 0.0
[!] Transaction failed to process. Discarded.
[+] Block mined: 00000b52437c5279e0dd6bde9a1031277e8604278b43ac0fb21cab3b111f02f4
[*] WalletA's balance is: 290.0
[*] WalletB's balance is: 310.0

[*] Blockchain is valid: true

Apologies that I am not able to paste all my code in here, but most of the code is very similar if not the same as mentioned in https://medium.com/programmers-blockchain/create-simple-blockchain-java-tutorial-from-scratch-6eeed3cb03fa. The implementation is the same except I’m trying to reward the miners with 100 coins for actually mining.

New contributor

CuriousAvenger is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật