-
Notifications
You must be signed in to change notification settings - Fork 454
Add new auxiliary functions for CCU #7743
Changes from 1 commit
ade8b92
d65e18d
ec2f2ed
34e8e08
466ffc1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ import { | |
CCMApplyContext, | ||
TerminateChainContext, | ||
CreateTerminatedStateAccountContext, | ||
CrossChainUpdateTransactionParams, | ||
} from './types'; | ||
import { getCCMSize, getIDAsKeyForStore } from './utils'; | ||
import { | ||
|
@@ -54,6 +55,10 @@ import { TerminatedOutboxAccount, TerminatedOutboxStore } from './stores/termina | |
import { ChainAccountUpdatedEvent } from './events/chain_account_updated'; | ||
import { TerminatedStateCreatedEvent } from './events/terminated_state_created'; | ||
import { BaseInternalMethod } from '../BaseInternalMethod'; | ||
import { ChainValidatorsStore } from './stores/chain_validators'; | ||
import { MethodContext } from '../../state_machine'; | ||
import { certificateSchema } from '../../engine/consensus/certificate_generation/schema'; | ||
import { Certificate } from '../../engine/consensus/certificate_generation/types'; | ||
|
||
export abstract class BaseInteroperabilityInternalMethod extends BaseInternalMethod { | ||
public readonly context: StoreGetter; | ||
|
@@ -363,6 +368,40 @@ export abstract class BaseInteroperabilityInternalMethod extends BaseInternalMet | |
await ccCommand.execute(ccCommandExecuteContext); | ||
} | ||
|
||
public async updateValidators( | ||
context: MethodContext, | ||
ccu: CrossChainUpdateTransactionParams, | ||
): Promise<void> { | ||
await this.stores.get(ChainValidatorsStore).updateValidators(context, ccu.sendingChainID, { | ||
activeValidators: ccu.activeValidatorsUpdate, | ||
certificateThreshold: ccu.newCertificateThreshold, | ||
}); | ||
} | ||
|
||
public async updateCertificate( | ||
context: MethodContext, | ||
ccu: CrossChainUpdateTransactionParams, | ||
): Promise<void> { | ||
const certificate = codec.decode<Certificate>(certificateSchema, ccu.certificate); | ||
const updatedAccountStore = await this.stores | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here are a few observations:
OR (preferable if we are updating certificate from multiple places e.g. test case)
|
||
.get(ChainAccountStore) | ||
.updateLastCertificate(context, ccu.sendingChainID, certificate); | ||
this.events.get(ChainAccountUpdatedEvent).log(context, ccu.sendingChainID, updatedAccountStore); | ||
} | ||
|
||
public async updatePartnerChainOutboxRoot( | ||
context: MethodContext, | ||
ccu: CrossChainUpdateTransactionParams, | ||
): Promise<void> { | ||
await this.stores | ||
.get(ChannelDataStore) | ||
.updatePartnerChainOutboxRoot( | ||
context, | ||
ccu.sendingChainID, | ||
ccu.inboxUpdate.messageWitnessHashes, | ||
); | ||
} | ||
|
||
// Different in mainchain and sidechain so to be implemented in each module store separately | ||
public abstract isLive(chainID: Buffer, timestamp?: number): Promise<boolean>; | ||
public abstract sendInternal(sendContext: SendInternalContext): Promise<boolean>; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
* | ||
* Removal or modification of this copyright notice is prohibited. | ||
*/ | ||
import { BaseStore } from '../../base_store'; | ||
import { BaseStore, StoreGetter } from '../../base_store'; | ||
import { ActiveValidator } from '../types'; | ||
import { BLS_PUBLIC_KEY_LENGTH } from '../constants'; | ||
|
||
|
@@ -53,6 +53,47 @@ export const chainValidatorsSchema = { | |
}, | ||
}; | ||
|
||
// TODO: Fix with #7742 - In order to avoid circular dependency temporally, move to this file | ||
export const updateActiveValidators = ( | ||
sitetester marked this conversation as resolved.
Show resolved
Hide resolved
|
||
activeValidators: ActiveValidator[], | ||
activeValidatorsUpdate: ActiveValidator[], | ||
): ActiveValidator[] => { | ||
for (const updatedValidator of activeValidatorsUpdate) { | ||
const currentValidator = activeValidators.find(v => v.blsKey.equals(updatedValidator.blsKey)); | ||
if (currentValidator) { | ||
currentValidator.bftWeight = updatedValidator.bftWeight; | ||
} else { | ||
activeValidators.push(updatedValidator); | ||
activeValidators.sort((v1, v2) => v1.blsKey.compare(v2.blsKey)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
} | ||
|
||
for (const currentValidator of activeValidators) { | ||
if (currentValidator.bftWeight === BigInt(0)) { | ||
const index = activeValidators.findIndex(v => v.blsKey.equals(currentValidator.blsKey)); | ||
activeValidators.splice(index, 1); | ||
} | ||
} | ||
Comment on lines
+71
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can also be like this
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't use |
||
|
||
return activeValidators; | ||
}; | ||
|
||
export class ChainValidatorsStore extends BaseStore<ChainValidators> { | ||
public schema = chainValidatorsSchema; | ||
|
||
public async updateValidators( | ||
context: StoreGetter, | ||
chainID: Buffer, | ||
chainValidators: ChainValidators, | ||
): Promise<void> { | ||
const currentValidators = await this.get(context, chainID); | ||
|
||
currentValidators.certificateThreshold = chainValidators.certificateThreshold; | ||
currentValidators.activeValidators = updateActiveValidators( | ||
currentValidators.activeValidators, | ||
chainValidators.activeValidators, | ||
); | ||
|
||
await this.set(context, chainID, currentValidators); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency
newCertificateThreshold
could have been calledcertificateThresholdUpdate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is type definition/schema definition we have now