ProHoster > Blog > Tsamaiso > Etsa litšebelisano tsa sechaba le tsa poraefete ho JPMorgan Quorum blockchain u sebelisa Web3
Etsa litšebelisano tsa sechaba le tsa poraefete ho JPMorgan Quorum blockchain u sebelisa Web3
Quorum ke blockchain e thehiloeng ho Ethereum e ntlafalitsoeng ke JPMorgan 'me haufinyane tjena e bile sethala sa pele sa leja se ajoang se fanoang ke Microsoft Azure.
Quorum e ts'ehetsa litšebelisano tsa poraefete le tsa sechaba 'me e na le linyeoe tse ngata tsa tšebeliso ea khoebo.
Sehloohong sena, re tla hlahloba e 'ngoe ea boemo bo joalo - ho tsamaisoa ha marang-rang a li-ledger pakeng tsa lebenkele le mong'a ntlo ea polokelo ho fana ka tlhahisoleseding ea morao-rao mabapi le mocheso oa ntlo ea polokelo.
ho tsamaisoa ha marangrang a Quorum ho sebelisoa Chainstack;
Khoamo ea litšebelisano tsa sechaba;
Quorum poraefete ea lekunutu.
Ho etsa mohlala, re sebelisa boemo ba ho beha leihlo mocheso matlong a polokelo a litho tsa marang-rang a Quorum ka har'a Marang-rang a Lintho (IoT).
Mohopolo
Sehlopha sa lik'hamphani tsa polokelo ea thepa se kopana ka kopanelo ho boloka tlhahisoleseling le ho iketsetsa lits'ebetso ho blockchain. Bakeng sa sena, lik'hamphani li nkile qeto ea ho sebelisa Quorum. Sehloohong sena re tla bua ka maemo a mabeli: litšebelisano tsa sechaba le tsa poraefete.
Litšebelisano li etsoa ke barupeluoa ba fapaneng ho sebelisana le khonsortium eo ba leng ho eona. Ts'ebetso e 'ngoe le e' ngoe e sebelisa konteraka kapa e bitsa mosebetsi ho konteraka ho kenya data ho marang-rang. Liketso tsena li phetoa ho li-node tsohle tsa marang-rang.
Litšebelisano tsa sechaba li teng hore li bonoe ke bankakarolo bohle ba consortium. Litšebelisano tsa poraefete li eketsa lekunutu 'me li fumaneha feela ho bankakarolo ba nang le litokelo tsa ho etsa joalo.
Bakeng sa maemo ana ka bobeli, re sebelisa konteraka e tšoanang ho hlakisa.
Konteraka e bohlale
Ka tlase ke konteraka e bohlale e etselitsoeng boemo ba rona. E na le phetoho ea sechaba temperature, e ka fetoloang ho sebelisoa set le ho amohela ka mokhoa get.
pragma solidity ^0.4.25;
contract TemperatureMonitor {
int8 public temperature;
function set(int8 temp) public {
temperature = temp;
}
function get() view public returns (int8) {
return temperature;
}
}
E le hore konteraka e sebetse le web3.js, e tlameha ho fetoleloa ka mokhoa oa ABI le ka bytecode. Ho sebelisa ts'ebetso formatContractka tlase o bokella konteraka e sebelisang Solc-js.
Kaha joale konteraka e se e loketse, re tla romela marang-rang le ho tsamaisa konteraka.
Tšebeliso ea li-node
Ho tsamaisa node ho ka ba boima haholo mme ts'ebetso ena e ka nkeloa sebaka ke ho sebelisa ts'ebeletso Chainstack.
Ka tlase ke ts'ebetso ea ho tsamaisa marang-rang a Quorum ka tumellano ea Raft le li-node tse tharo.
Taba ea pele, ha re theheng projeke ebe re e bitsa Quorum Project:
Ha re thehe marangrang a Quorum ka tumellano ea Raft ho Google Cloud Platform:
Ha re kenye li-node tse ling tse peli ho node e seng e entsoe ka boiketsetso:
Li-node tse tharo tse sebetsang:
Leqephe la lintlha tsa node le bonts'a pheletso ea RPC, senotlolo sa sechaba, joalo-joalo.
Marang-rang a sebelisoa. Joale ha re sebeliseng likonteraka tse bohlale 'me re etse transaction re sebelisa web3.js.
Litšebelisano tsa sechaba
Mohopolo
Thempereichara ea polokelo ea thepa e bohlokoa haholo ho fokotsa litšenyehelo, haholo-holo bakeng sa lihlahisoa tse reretsoeng ho bolokoa tlas'a mocheso o tlase ho zero.
Ka ho lumella lik'hamphani ho arolelana mocheso o ka ntle oa sebaka sa bona sa libaka ka nako ea sebele le ho e ngola bukeng e sa fetoheng, barupeluoa ba marang-rang ba fokotsa litšenyehelo le nako.
Re tla etsa mesebetsi e meraro, e bontšitsoeng setšoantšong:
Re tla tsamaisa konteraka ka Node 1:
const contractAddress = await deployContract(raft1Node);
console.log(`Contract address after deployment: ${contractAddress}`);
Beha mocheso ka Node 2 ka likhato tse 3:
const status = await setTemperature(raft2Node, contractAddress, 3);
console.log(`Transaction status: ${status}`);
Node 3 e tla fumana tlhahisoleseding ho tswa ho konteraka e bohlale. Konteraka e tla khutlisa boleng ba likhato tse 3:
Ka mor'a moo, re tla sheba mokhoa oa ho etsa khoebo ea sechaba ka marang-rang a Quorum web3.js.
Re qala mohlala ka RPC bakeng sa li-node tse tharo:
const raft1Node = new Web3(
new Web3.providers.HttpProvider(process.env.RPC1), null, {
transactionConfirmationBlocks: 1,
},
);
const raft2Node = new Web3(
new Web3.providers.HttpProvider(process.env.RPC2), null, {
transactionConfirmationBlocks: 1,
},
);
const raft3Node = new Web3(
new Web3.providers.HttpProvider(process.env.RPC3), null, {
transactionConfirmationBlocks: 1,
},
);
Ha re sebeliseng konteraka e bohlale:
// returns the default account from the Web3 instance initiated previously
function getAddress(web3) {
return web3.eth.getAccounts().then(accounts => accounts[0]);
}
// Deploys the contract using contract's interface and node's default address
async function deployContract(web3) {
const address = await getAddress(web3);
// initiate contract with contract's interface
const contract = new web3.eth.Contract(
temperatureMonitor.interface
);
return contract.deploy({
// deploy contract with contract's bytecode
data: temperatureMonitor.bytecode,
})
.send({
from: address,
gas: '0x2CD29C0',
})
.on('error', console.error)
.then((newContractInstance) => {
// returns deployed contract address
return newContractInstance.options.address;
});
}
web3.js e fana ka mekhoa e 'meli ea ho sebelisana le konteraka: call и send.
Ha re nchafatse mocheso oa konteraka ka ho phethahatsa set ho sebelisa mokhoa oa web3 send.
// get contract deployed previously
async function getContract(web3, contractAddress) {
const address = await getAddress(web3);
return web3.eth.Contract(
temperatureMonitor.interface,
contractAddress, {
defaultAccount: address,
}
);
}
// calls contract set method to update contract's temperature
async function setTemperature(web3, contractAddress, temp) {
const myContract = await getContract(web3, contractAddress);
return myContract.methods.set(temp).send({}).then((receipt) => {
return receipt.status;
});
}
Ka mor'a moo re sebelisa mokhoa oa web3 call ho fumana mocheso oa konteraka. Ka kopo hlokomela hore mokhoa call e etsoa ka node ea lehae mme transaction e ke ke ea etsoa ho blockchain.
// calls contract get method to retrieve contract's temperature
async function getTemperature(web3, contractAddress) {
const myContract = await getContract(web3, contractAddress);
return myContract.methods.get().call().then(result => result);
}
Joale u ka matha public.js ho fumana sephetho se latelang:
// Execute public script
node public.js
Contract address after deployment: 0xf46141Ac7D6D6E986eFb2321756b5d1e8a25008F
Transaction status: true
Retrieved contract Temperature 3
Ka mor'a moo, re ka sheba likenyelletso tsa Quorum explorer ho phanele ea Chainstack, joalo ka ha ho bonts'itsoe ka tlase.
Li-node tse tharo li ile tsa sebelisana 'me litšebelisano tsa ntlafatsoa:
Ts'ebetso ea pele e ile ea tsamaisa konteraka.
Ts'ebetso ea bobeli e behile mocheso oa konteraka ho likhato tse 3.
Mocheso o amoheloa ka node ea sebaka seo, kahoo ha ho na transaction e entsoeng.
Litšebelisano tsa poraefete
Mohopolo
Tlhokahalo e tloaelehileng ea mekhatlo ke ts'ireletso ea data. Ka mohlala, nahana ka boemo boo ho bona Supamakete e hirisang sebaka sa polokelo bakeng sa ho boloka lijo tsa leoatleng sebakeng se arohaneng Morekisi:
Morekisi e sebelisa li-sensor tsa IoT, e bala boleng ba mocheso metsotsoana e meng le e meng e 30 ebe oa e fetisetsa Ho ea lebenkeleng;
litekanyetso tsena li lokela ho ba teng feela Ho morekisi и Ho ea lebenkeleng, e hokahane le khokahanyo.
Re tla phetha mesebetsi e mene e bontšitsoeng setšoantšong se ka holimo.
Re sebelisa li-node tse tharo tse tšoanang ho tloha boemong bo fetileng ho bonts'a litšebelisano tsa poraefete:
Supamakete e tsamaisa konteraka e bohlale eo e leng lekunutu ho Supamakete и Morekisi.
Lehlakore la boraro ha a na tokelo ea ho fihlella konteraka e bohlale.
Re tla bitsa mekhoa get и set sebakeng sa Supamakete и Morekisi ho bontša tšebetso ea Quorum ea poraefete.
Re tla romela konteraka ea poraefete bakeng sa bankakarolo Supamakete и Morekisi ka monkakarolo Supamakete:
Ha re behe mocheso ho tloha Moamehi e mong (node ea kantle) 'me u fumane boleng ba mocheso:
// Attempts to set Contract temperature to 10, this will not mutate contract's temperature
await setTemperature(
raft3Node,
contractAddress,
process.env.PK1,
10,
);
// This returns null
const temp = await getTemperature(raft3Node, contractAddress);
console.log(`[Node3] temp retrieved after updating contract from external nodes: ${temp}`);
Ha re behe mocheso ho tloha Morekisi (node ea ka hare) 'me u fumane boleng ba mocheso:
Thempereichara sebakeng sena e lokela ho khutlisa boleng ba 12 ho tsoa ho konteraka e bohlale. Ka kopo hlokomela hore Morekisi mona ho na le tumello ea ho fihlella konteraka e bohlale.
// Updated Contract temperature to 12 degrees
await setTemperature(
raft2Node,
contractAddress,
process.env.PK1,
12,
);
// This returns 12
const temp2 = await getTemperature(raft2Node, contractAddress);
console.log(`[Node2] temp retrieved after updating contract from internal nodes: ${temp2}`);
Re fumana mocheso ho tloha Moamehi e mong (node ea kantle):
Mohato oa 3 mocheso o ne o behiloe ho 12, empa Lehlakore la boraro ha e na phihlello ho konteraka e bohlale. Ka hona boleng ba ho khutlisa bo tlameha ho ba lefeela.
// This returns null
const temp3 = await getTemperature(raft3Node, contractAddress);
console.log(`[Node3] temp retrieved from external nodes after update ${temp}`);
Ka mor'a moo, re tla shebisisa ho etsa litšebelisano tsa poraefete ho netweke ea Quorum ka web3.js. Kaha boholo ba khoutu e ts'oana le litšebelisano tsa sechaba, re tla totobatsa feela likarolo tse fapaneng bakeng sa litšebelisano tsa botho.
Hlokomela hore konteraka e kentsoeng marang-rang e ke ke ea fetoha, kahoo ho fumana tumello ho tlameha ho fuoa li-node tse loketseng ka ho nolofalletsa konteraka ea sechaba nakong eo konteraka e sebelisoang, eseng ka morao.
async function deployContract(web3, publicKey) {
const address = await getAddress(web3);
const contract = new web3.eth.Contract(
temperatureMonitor.interface,
);
return contract.deploy({
data: temperatureMonitor.bytecode,
})
.send({
from: address,
gas: ‘0x2CD29C0’,
// Grant Permission to Contract by including nodes public keys
privateFor: [publicKey],
})
.then((contract) => {
return contract.options.address;
});
}
Litšebelisano tsa poraefete li etsoa ka mokhoa o ts'oanang - ka ho kenyelletsa senotlolo sa sechaba sa barupeluoa nakong ea ho bolaoa.
async function setTemperature(web3, contractAddress, publicKey, temp) {
const address = await getAddress(web3);
const myContract = await getContract(web3, contractAddress);
return myContract.methods.set(temp).send({
from: address,
// Grant Permission by including nodes public keys
privateFor: [publicKey],
}).then((receipt) => {
return receipt.status;
});
}
Joale re ka matha private.js ka liphetho tse latelang:
node private.js
Contract address after deployment: 0x85dBF88B4dfa47e73608b33454E4e3BA2812B21D
[Node3] temp retrieved after updating contract from external nodes: null
[Node2] temp retrieved after updating contract from internal nodes: 12
[Node3] temp retrieved from external nodes after update null
Mofuputsi oa Quorum in Chainstack o tla bontša tse latelang:
phepelo ya konteraka ho tswa ho monkakarolo Supamakete;
Ho phethahala SetTemperature от Moamehi e mong;
Ho phethahala SetTemperature ho tsoa ho monkakarolo Morekisi.
Joalokaha u ka bona, litšebelisano ka bobeli li phethiloe, empa ke ts'ebetso feela e tsoang ho motho ea nkang karolo Morekisi ntjhafatswa mocheso konteraka. Kahoo, litšebelisano tsa poraefete li fana ka ho se fetohe, empa ka nako e ts'oanang ha li senole data ho motho oa boraro.
fihlela qeto e
Re ile ra sheba nyeoe ea ts'ebeliso ea khoebo bakeng sa Quorum ho fana ka tlhaiso-leseling ea morao-rao ea mocheso sebakeng sa polokelo ka ho kenya marang-rang lipakeng tsa mekha e 'meli - lebenkele le leholo le mong'a ntlo ea polokelo.
Re bonts'itse hore na litaba tsa mocheso oa morao-rao li ka bolokoa joang ka litšebelisano tsa sechaba le tsa poraefete.
Ho ka ba le maemo a mangata a kopo mme, joalo ka ha u bona, ha ho thata ho hang.