Laboratoriya ishi no tizimda bir nechta tugun kerak bo'lsa, Consensus algoritmidan foydalaning Vazifa


Lokal tarmoqda tugunlarni ro'yxatga olish


Download 58.44 Kb.
Pdf ko'rish
bet3/4
Sana18.06.2023
Hajmi58.44 Kb.
#1591506
1   2   3   4
Bog'liq
5-lab 2 blokchain

4.3 Lokal tarmoqda tugunlarni ro'yxatga olish
tugunni shaxsiy kompyuteringizda, lekin boshqa portda o'rnating va uni ro'yxatdan o'tkazing
Endi siz boshqa kompyuterda o'tirib, turli xil tugunlarni joylashtirishingiz mumkin
joriy tugun yordamida. Shunday qilib, biz ikkita tugunni olamiz:
mahalliy tarmoq. Bundan tashqari, turli xil usullardan foydalangan holda jarayonlarni joylashtirishingiz mumkin
http://localhost:5000 va http://localhost:5001.
Shundan so'ng, ishonch hosil qilish uchun 2-tugunda ikkita yangi blok olamiz
bir xil kompyuterdagi portlar. Keling, boshqasini joylashtirishga harakat qilaylik
Anjir. 1. POST so'rovi http://localhost:5000/node/register-da Postman
43
Machine Translated by Google


chunki zanjir uzunroq bo'ladi. Shundan so'ng biz GET /nodes/resolve ga qo'ng'iroq qilamiz
1-tugunda, bu erda zanjir Consensus algoritmi bilan almashtirildi:
44
Anjir. 2. Postman-da http://localhost:5000/node/resolve so'rovini oling
Machine Translated by Google


Biz tugunlarni ro'yxatga olish usullarini qo'shamiz
Ishni boshlashdan oldin, pe uchun ikkita bunday sinfni yaratish kerak
Yangi tugunlarni ro'yxatdan o'tkazish
Konsensus algoritmini amalga oshirish
Amaliy misol ÿ2
so'rov tanasini qulay shaklga aylantirish:
6 }
1 ta umumiy mantiqiy validChain(List zanjiri) { 2 for (int i = 1; i <
chain.size(); i++) { 3 Blok lastBlock = chain.get(i - 1); Blok joriyBlock =
chain.get(i);
9 }
13
14 } 15 }
4
1 paket blockchain.basic; 2 3 import
java.util.List; 4
1 paket blockchain.basic; 2 3 import
java.util.ArrayList; 4 import java.util.List; 5 6
umumiy sinf ChainResponse { 7 8 Roÿyxat
zanjiri = yangi ArrayList<>(); 9 dyuym uzunligi;
10
1 ta shaxsiy toÿplam tugunlari = yangi HashSet<>(); 2 3
5 umumiy sinf NodesResponse {6
11 ommaviy ChainResponse(Ro'yxat zanjiri, int uzunligi) { 12 this.chain = zanjir;
this.length = length;
4 ommaviy void registerNode(String netloc) { 5 nodes.add(netloc);
7 Ro'yxat tugunlari; 8
45
Machine Translated by Google


}
} catch (IOException e) {
16 ommaviy mantiqiy yechimConflicts() {
29
30
newChain = javob.chain;
BufferedReader in = yangi Buff
34
35
49 qaytish newChain == this.chain;
}
22
19 List newChain = this.chain;
Bufer();
42
32
33
}
10
11
HttpURLConnection con = (HttpURLConnection) url.openConnection();
in.close();
47 this.chain = newChain;
5 agar (!currentBlock.getPreviousHash().teng (xesh(lastBlock))) {
agar (!isProofValid(lastBlock.getProof(), cur
URL url;
content.append(inputLine);
e.printStackTrace();
}
23
24
38
39
50 }
System.out.println("Xesh mos kelmaydi");
20 ta urinib ko'ring {
31
43
System.out.println("Isbot noto'g'ri");
25 con.setRequestMethod("GET");
con.disconnect();
48
40
41
rentBlock.getProof())) {
url = yangi URL (host + "/ zanjir");
}
esa ((inputLine = in.readLine()) !=
44
45
46 if(newChain != this.chain) {
14 haqiqatni qaytaradi;
26
27
yolg'onni qaytarish;
int status = con.getResponseCode();
36 ChainResponse javobi = gson.fromJson(content.toString(), ChainResponse.class);
}
21
6 7 qaytarma noto'g'ri;
17 Gson gson = yangi Gson();
String inputLine;
}
15 }
28
eredReader(yangi InputStreamReader(con.getInputStream()));
maxLen = javob.uzunlik;
8
9
uchun (String xosti: this.nodes) {
null) {
}
12
13
agar (holat == 200) {
37 agar (response.length > maxLen && validChain(response.chain)) {
}
18 int maxLen = this.chain.size();
StringBuffer tarkibi = yangi String
46
Machine Translated by Google


va ikkala serverda ham ro'yxatdan o'ting:

Download 58.44 Kb.

Do'stlaringiz bilan baham:
1   2   3   4




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling