@neo-one/node-blockchain-esnext-esm
Version:
NEO•ONE NEO blockchain implementation.
129 lines (127 loc) • 5.23 kB
JavaScript
import { common, utils } from '@neo-one/client-common-esnext-esm';
import { Account, Validator, ValidatorsCount } from '@neo-one/node-core-esnext-esm';
import _ from 'lodash';
import { toArray } from 'rxjs/operators';
export class ValidatorCache {
constructor(blockchain) {
this.blockchain = blockchain;
this.mutableAccounts = {};
this.mutableValidators = {};
}
async getAccount(hash) {
let account = this.mutableAccounts[common.uInt160ToHex(hash)];
if (account === undefined) {
account = await this.blockchain.account.tryGet({ hash });
}
if (account === undefined) {
account = new Account({ hash });
}
this.mutableAccounts[common.uInt160ToHex(hash)] = account;
return account;
}
async updateAccountBalance(hash, asset, value) {
const assetHex = common.uInt256ToHex(asset);
await this.getAccount(hash);
const hashHex = common.uInt160ToHex(hash);
const account = this.mutableAccounts[hashHex];
const balance = account.balances[assetHex];
this.mutableAccounts[hashHex] = account.update({
balances: {
...account.balances,
[assetHex]: value.add(balance === undefined ? utils.ZERO : balance),
},
});
}
async getValidator(publicKey) {
const publicKeyHex = common.ecPointToHex(publicKey);
let validator = this.mutableValidators[publicKeyHex];
if (validator === undefined) {
validator = await this.blockchain.validator.tryGet({ publicKey });
}
if (validator === undefined) {
validator = new Validator({ publicKey });
}
this.mutableValidators[publicKeyHex] = validator;
return validator;
}
async addValidator(validator) {
this.mutableValidators[common.ecPointToHex(validator.publicKey)] = validator;
}
async deleteValidator(publicKey) {
this.mutableValidators[common.ecPointToHex(publicKey)] = undefined;
}
async updateValidatorVotes(publicKey, value) {
await this.getValidator(publicKey);
const publicKeyHex = common.ecPointToHex(publicKey);
const validator = this.mutableValidators[publicKeyHex];
if (validator === undefined) {
throw new Error('For Flow');
}
this.mutableValidators[publicKeyHex] = validator.update({
votes: validator.votes.add(value),
});
}
async updateValidator(publicKey, update) {
await this.getValidator(publicKey);
const publicKeyHex = common.ecPointToHex(publicKey);
const validator = this.mutableValidators[publicKeyHex];
if (validator === undefined) {
throw new Error('For Flow');
}
const newValidator = validator.update(update);
this.mutableValidators[publicKeyHex] = newValidator;
return newValidator;
}
async getAllValidators() {
const validators = await this.blockchain.validator.all$.pipe(toArray()).toPromise();
const mutablePublicKeyToValidator = _.fromPairs(validators.map((validator) => [common.ecPointToHex(validator.publicKey), validator]));
Object.entries(this.mutableValidators).forEach(([publicKey, validator]) => {
const publicKeyHex = common.ecPointToHex(publicKey);
if (validator === undefined) {
delete mutablePublicKeyToValidator[publicKeyHex];
}
else {
mutablePublicKeyToValidator[publicKeyHex] = validator;
}
});
return Object.values(mutablePublicKeyToValidator);
}
async getValidatorsCount() {
let validatorsCount = this.mutableValidatorsCount;
if (validatorsCount === undefined) {
validatorsCount = await this.blockchain.validatorsCount.tryGet();
}
if (validatorsCount === undefined) {
validatorsCount = new ValidatorsCount();
}
this.mutableValidatorsCount = validatorsCount;
return validatorsCount;
}
async updateValidatorsCountVotes(index, value) {
await this.getValidatorsCount();
const validatorsCount = this.mutableValidatorsCount;
if (validatorsCount === undefined) {
throw new Error('For Flow');
}
const votes = validatorsCount.votes[index];
this.mutableValidatorsCount = validatorsCount.update({
votes: validatorsCount.votes
.slice(0, index)
.concat((votes === undefined ? utils.ZERO : votes).add(value))
.concat(validatorsCount.votes.slice(index + 1)),
});
}
async addValidatorsCount(validatorsCount) {
this.mutableValidatorsCount = validatorsCount;
}
async updateValidatorsCount(update) {
await this.getValidatorsCount();
const validatorsCount = this.mutableValidatorsCount;
if (validatorsCount === undefined) {
throw new Error('For Flow');
}
this.mutableValidatorsCount = validatorsCount.update(update);
return this.mutableValidatorsCount;
}
}
//# sourceMappingURL=ValidatorCache.js.map