1 package blockchain.basic;
2
3 public class Transaction {
4
5 private String sender;
6 private String recipient;
7
private int amount;
8
9
10 public Transaction(String sender, String recipient, int amount)
11{
12 this.sender = sender;
13 this.recipient = recipient;
14 this.amount = amount;
15 }
16
17 public String getSender() {
18 return sender;
19 }
20
21 public String getRecipient() {
22 return recipient;
23 }
24
25 public int getAmount() {
26
return amount;
27 }
28
29}
Keyinchalik, blokga tranzaktsiyalarni qo'shish usuli kerak. Buning uchun
newTransaction() usuli javobgar bo'ladi:
1 /**
2 *
3 * Направляє нову транзакцію в наступний блок
4 *
5 *
6 * @param sender Адреса відправника
7 * @param recipient Адреса отримувача
8 * @param amount Сума
9 * @return Індекса блока, що буде зберігати цю транзакцію
10 */
11
12 public int newTransaction(String sender, String recipient, int
amount) {
13 this.currentTransactions.add(new Transaction(sender, recipient,
amount));
14 return this.chain.size();
15 }
newTransaction() tranzaksiyani roʻyxatga qoʻshgandan soʻng,
tranzaksiya
qoʻshiladigan blok indeksini, yaʼni almashtiriladigan keyingi blokni qaytaradi. Bu
kelajakda tranzaktsiyani yuboradigan foydalanuvchi uchun foydali bo'ladi.
Yangi bloklarni yaratish
Biz yangi Block sinfini yaratamiz va unga kerakli maydonlarni,
ular uchun
oluvchilarni va konstruktorni qo'shamiz:
1 package blockchain.basic;
2
3 Import java.util.List;
4
5 public class Block {
6
7 private int index;
8 private long timestamp;
9
private List transactions;
10 private int proof;
11 private String previousHash;
12
13 public Block(int index, int proof, String previousHash,
List transactions) {
14 this.index = index;
15 this.proof = proof;
16 this.previousHash = previousHash;
17 this.transactions = transactions;
18 this.timestamp = System.currentTimeMillis();
19 }
20
21 public int getIndex() {
22 return index;
23 }
24
25 public long getTimestamp() {
26 return timestamp;
27 }
28
29 public List getTransactions() {
30 return transactions;
31 }
32 public int getProof() {
33 return proof;
34 }
35
36 public String getPreviousHash() {
37 return previousHash;
38 }
39 }