###### tags: `console-emulator`
# Консольный скрипт эмулятор консенсуса
````js
var qaz = new (function emulator() {
var __stop = false
var isInit = false
this.slot = 0
this.creators = []
this.candidates = []
this.node = 1
this.sleep = function(milliseconds) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}
this.init = function () {
var dagHashes = admin.nodeInfo.protocols.wfdag.dag||[]
this.slot = 0
for (var i=0; i < dagHashes.length; i++) {
var s = Number(eth.getBlock(dagHashes[i]).slot);
if (s > this.slot) this.slot = s;
}
if (!this.slot){
var lastFinBlock = eth.getBlock(eth.blockNumber);
this.slot = Number(lastFinBlock.slot);
}
this.creators = [eth.coinbase]
isInit = true
return this
}
this.nexIncrSlot = function () {
this.slot++
return this
}
this.setNode = function (node) {
this.node = node
console.log("+++ Set node +++", this.node)
return this
}
this.sendTx = function (txCount) {
if (!txCount) {
txCount = 1
}
for (var i = 0; i < txCount; i++) {
if (__stop) return;
if (this.node == 0){
// personal.unlockAccount("0xa7062a2bd7270740f1d15ab70b3dee189a87b6de", "")
eth.sendTransaction({from: "0xa7062a2bd7270740f1d15ab70b3dee189a87b6de",to: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',value: web3.toWei(0.01, "ether"),gas: 21000});
eth.sendTransaction({from: "0xa7062a2bd7270740f1d15ab70b3dee189a87b6de",to: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',value: web3.toWei(0.01, "ether"),gas: 21000});
// personal.unlockAccount("0xa7e558cc6efa1c41270ef4aa227b3dd6b4a3951e", "")
eth.sendTransaction({from: "0xa7e558cc6efa1c41270ef4aa227b3dd6b4a3951e",to: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',value: web3.toWei(0.01, "ether"),gas: 21000});
eth.sendTransaction({from: "0xa7e558cc6efa1c41270ef4aa227b3dd6b4a3951e",to: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',value: web3.toWei(0.01, "ether"),gas: 21000});
// personal.unlockAccount("0xe43bb1b64fc7068d313d24d01d8ccca785b22c72", "WaTerFaLLForEvEr")
eth.sendTransaction({from: "0xe43bb1b64fc7068d313d24d01d8ccca785b22c72",to: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',value: web3.toWei(0.01, "ether"),gas: 21000});
eth.sendTransaction({from: "0xe43bb1b64fc7068d313d24d01d8ccca785b22c72",to: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',value: web3.toWei(0.01, "ether"),gas: 21000});
} else if (this.node == 1){
eth.sendTransaction({from: eth.coinbase,to: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',value: web3.toWei(0.01, "ether"),gas: 21000});
} else {
console.error("+++ Bad node +++", this.node)
}
console.log("+++ send txs +++", i)
}
console.log("+++ send txs +++", txCount)
return this;
}
this.callDagSync = function () {
if (this.node == 0) {
const CREATORS = ["0xa7e558cc6efa1c41270ef4aa227b3dd6b4a3951e", "0xa7062a2bd7270740f1d15ab70b3dee189a87b6de", "0xe43bb1b64fc7068d313d24d01d8ccca785b22c72"]
// this.creators = [ CREATORS[this.slot%CREATORS.length] ]
// this.creators = CREATORS
this.creators = CREATORS
}
// console.log('###################################', JSON.stringify({slot: this.slot, creators: this.creators, finalizing: this.candidates}, null, 2))
var resp = dag.sync({slot: "0x" + this.slot.toString(16), creators: this.creators, finalizing: this.candidates})
console.log("RESP::callDagSync", JSON.stringify(resp, null, 2), {resp})
if (resp.error) console.error("<<<<<<<<<<<< ERROR >>>>>>>>>>>>", resp.error)
// if (resp.candidates) this.candidates = resp.candidates;
return this
}
this.callDagFinalize = function () {
var resp = dag.finalize(this.candidates)
console.log("RESP::callDagFinalize", JSON.stringify(resp, null, 2))
if (resp.error) console.error("<<<<<<<<<<<< ERROR >>>>>>>>>>>>", resp.error)
return this
}
this.callDagGetCandidates = function () {
var cutSlot = this.slot - 2
if (cutSlot < 0) cutSlot = 0
var resp = dag.getCandidates(cutSlot)
console.log("RESP::callDagGetCandidates", JSON.stringify(resp, null, 2))
if (resp.error) console.error("<<<<<<<<<<<< ERROR >>>>>>>>>>>>", resp.error)
if (resp.candidates) this.candidates = resp.candidates;
return this
}
this.do = function () {
if (!isInit) this.init()
console.log("################"," slot:", this.slot)
this.nexIncrSlot().sendTx(1).callDagSync().callDagFinalize().callDagGetCandidates()
// console.log(JSON.stringify(admin.nodeInfo, null, 2))
}
this.run = function () {
var self = this
__stop = false
if (!isInit) this.init()
this.nexIncrSlot().sendTx(1).callDagSync()
setTimeout(function () {
if (__stop) return;
self.run()
}, 5000)
}
this.stop = function () {__stop = true;}
})()
````
- Отправляет транзакцию
- Финализирует блоки
- Создает новый блок
#### Использование
1. Копи-Паст код в консоль gwat
2. выполнить
`qaz.do()`
