Skip to content

Commit

Permalink
Get and apply latest changes from global account
Browse files Browse the repository at this point in the history
  • Loading branch information
jintukumardas committed Jan 16, 2025
1 parent 682cf4e commit 49e8777
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions src/shardeum/allowedArchiversManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,19 @@ class AllowedArchiversManager {
}

public setGlobalAccountConfig(allowedSigners: { [key: string]: DevSecurityLevel }, minSigRequired: number): void {
this.globalAccountAllowedSigners = allowedSigners
this.globalAccountMinSigRequired = minSigRequired
this.useGlobalAccount = true
// Set initial values
this.globalAccountAllowedSigners = allowedSigners;
this.globalAccountMinSigRequired = minSigRequired;
this.useGlobalAccount = true;

// Get and apply any updates from the global account
const globalAccount = getGlobalNetworkAccount(false);
if (globalAccount) {
this.applyLatestGlobalAccountChanges(globalAccount);
}
}

private getSignerConfig(): {
private getArchiverWhitelistConfig(): {
allowedAccounts: { [key: string]: DevSecurityLevel }, minSigRequired: number, signatures: Sign[], counter: number, allowedArchivers: { ip: string, port: number, publicKey: string }[]
} {
try {
Expand All @@ -91,13 +98,11 @@ class AllowedArchiversManager {

private loadAndVerifyConfig(): void {
try {

const getArchiverConfig = this.getSignerConfig()
const getArchiverConfig = this.getArchiverWhitelistConfig()
const payload = {
allowedArchivers: getArchiverConfig.allowedArchivers,
counter: getArchiverConfig.counter
}

const isValidList = verifyMultiSigs(
payload,
getArchiverConfig.signatures,
Expand Down Expand Up @@ -146,6 +151,31 @@ class AllowedArchiversManager {
archiver => archiver.publicKey === publicKey
)
}

private applyLatestGlobalAccountChanges(globalAccountData: any): void {
try {
const changes = globalAccountData.data.listOfChanges;
if (!changes || changes.length === 0) return;

// Find the latest change
const latestChange = changes.reduce((prev, current) => {
return (current.cycle > prev.cycle) ? current : prev;
});

// Check if the latest change affects multisigKeys or archiverWhitelistMinSigRequired
if (latestChange.change?.debug) {
if (latestChange.change?.debug?.multisigKeys && latestChange.change?.debug?.multisigKeys.length > 0) {
this.globalAccountAllowedSigners = latestChange.change.debug.multisigKeys;
}
if (latestChange.change?.debug?.minSigRequiredForArchiverWhitelist !== undefined) {
this.globalAccountMinSigRequired = latestChange.change.debug.minSigRequiredForArchiverWhitelist;
}
}
} catch (error) {
Logger.mainLogger.error('Error applying latest global account changes:', error)
}

}
}

export const allowedArchiversManager = new AllowedArchiversManager()

0 comments on commit 49e8777

Please sign in to comment.