Skip to content

Commit

Permalink
feat: electionManager use witness slots
Browse files Browse the repository at this point in the history
  • Loading branch information
vaultec81 committed Mar 2, 2024
1 parent ee28cc0 commit 519584d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/services/new/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { P2PService } from './p2pService';
import { AddrRecord } from './types';
import { ContractEngineV2 } from './contractEngineV2';
import { VersionManager } from './witness/versionManager';
import { ElectionManager } from './witness/electionManager';

const CONSTANTS = {
//Reset block records
Expand All @@ -39,6 +40,7 @@ export class NewCoreService {
contractEngine: ContractEngineV2;
miscDb: Collection;
versionManager: VersionManager
electionManager: ElectionManager
nonceMap: Collection;

constructor() {
Expand All @@ -59,6 +61,7 @@ export class NewCoreService {
this.transactionPool = new TransactionPoolV2(this)
this.contractEngine = new ContractEngineV2(this)
this.versionManager = new VersionManager(this)
this.electionManager = new ElectionManager(this)
}

async init(oldService) {
Expand All @@ -83,6 +86,7 @@ export class NewCoreService {
await this.witness.init();
await this.transactionPool.init()
await this.contractEngine.init()
await this.electionManager.init()

}

Expand All @@ -91,6 +95,7 @@ export class NewCoreService {
console.log('running here')
await this.nodeIdentity.start()
await this.witness.start()
await this.electionManager.start()
}

async stop() {
Expand Down
19 changes: 11 additions & 8 deletions src/services/new/witness/electionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,16 @@ function calcVotingWeight(drift: number) {
export class ElectionManager {
self: NewCoreService;
electionDb: Collection<ElectionResult>
epochLength: number;
constructor(self: NewCoreService) {
this.self = self;

this.btHoldElection = this.btHoldElection.bind(this)
this.btIndexElection = this.btIndexElection.bind(this)
this.handlePeerMsg = this.handlePeerMsg.bind(this)

//Every 6 hours
this.epochLength = 20 * 60 * 6
}

/**
Expand Down Expand Up @@ -274,11 +278,13 @@ export class ElectionManager {
}

async btHoldElection({data:block}) {
// const mod = 20 * 60 * 6;
const mod = 40;
const blk = block.key
if(blk % mod === 0 && this.self.chainBridge.parseLag < 5) {
this.holdElection(blk)
const witnessSchedule = await this.self.witness.roundCheck(blk)
const scheduleSlot = witnessSchedule.find(e => e.bn >= blk)
if(scheduleSlot && scheduleSlot.account === process.env.HIVE_ACCOUNT) {
if(blk % this.epochLength === 0 && this.self.chainBridge.parseLag < 5) {
this.holdElection(blk)
}
}
}

Expand Down Expand Up @@ -355,10 +361,7 @@ export class ElectionManager {
epoch: election.epoch,
net_id: this.self.config.get('network.id')
})).bytes;
console.log('signRaw', signRaw, {
data: (await this.self.ipfs.dag.put(election)).toString(),
epoch: election.epoch
})

const sigData = await this.self.consensusKey.signRaw(signRaw)
data.drain.push(sigData)
}
Expand Down

0 comments on commit 519584d

Please sign in to comment.