PHP WebShell

Текущая директория: /usr/lib/node_modules/bitgo/node_modules/@celo/contractkit/lib/wrappers

Просмотр файла: DowntimeSlasher.js

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DowntimeSlasherWrapper = void 0;
var BaseSlasher_1 = require("./BaseSlasher");
var BaseWrapper_1 = require("./BaseWrapper");
var unpackInterval = function (interval) { return [interval.start, interval.end]; };
/**
 * Contract handling slashing for Validator downtime using intervals.
 */
var DowntimeSlasherWrapper = /** @class */ (function (_super) {
    __extends(DowntimeSlasherWrapper, _super);
    function DowntimeSlasherWrapper() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        /**
         * Returns slashable downtime in blocks.
         * @return The number of consecutive blocks before a Validator missing from IBFT consensus
         * can be slashed.
         */
        _this.slashableDowntime = (0, BaseWrapper_1.proxyCall)(_this.contract.methods.slashableDowntime, undefined, BaseWrapper_1.valueToInt);
        /**
         * Calculates and returns the signature bitmap for the specified interval.
         * Similar to the parentSealBitmap of every block (where you have which validators were
         * able to sign the previous block), this bitmap shows for that specific interval which
         * validators signed at least one block
         * @param interval First and last block of the interval.
         * @return (string) The signature uptime bitmap for the specified interval.
         * @dev startBlock and endBlock must be in the same epoch.
         * @dev The getParentSealBitmap precompile requires that startBlock must be within 4 epochs of
         * the current block.
         */
        _this.getBitmapForInterval = (0, BaseWrapper_1.proxyCall)(_this.contract.methods.getBitmapForInterval, unpackInterval, BaseWrapper_1.solidityBytesToString);
        /**
         * Calculates and sets the signature bitmap for the specified interval.
         * @param interval First and last block of the interval.
         * @dev interval.start and interval.end must be in the same epoch.
         * @return The signature bitmap for the specified interval.
         */
        _this.setBitmapForInterval = (0, BaseWrapper_1.proxySend)(_this.connection, _this.contract.methods.setBitmapForInterval, unpackInterval);
        /**
         * Shows if the user already called the `setBitmapForInterval` for
         * the specific interval.
         * @param interval First and last block of the interval.
         * @return True if the user already called the `setBitmapForInterval` for
         * the specific interval.
         */
        _this.isBitmapSetForInterval = (0, BaseWrapper_1.proxyCall)(_this.contract.methods.isBitmapSetForInterval, unpackInterval);
        _this.lastSlashedBlock = (0, BaseWrapper_1.proxyCall)(_this.contract.methods.lastSlashedBlock, undefined, BaseWrapper_1.valueToInt);
        return _this;
    }
    /**
     * Returns current configuration parameters.
     */
    DowntimeSlasherWrapper.prototype.getConfig = function () {
        return __awaiter(this, void 0, void 0, function () {
            var res;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, Promise.all([this.slashableDowntime(), this.slashingIncentives()])];
                    case 1:
                        res = _a.sent();
                        return [2 /*return*/, {
                                slashableDowntime: res[0],
                                slashingIncentives: res[1],
                            }];
                }
            });
        });
    };
    /**
     * @dev Returns human readable configuration of the downtime slasher contract
     * @return DowntimeSlasherConfig object
     */
    DowntimeSlasherWrapper.prototype.getHumanReadableConfig = function () {
        return __awaiter(this, void 0, void 0, function () {
            var config;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.getConfig()];
                    case 1:
                        config = _a.sent();
                        return [2 /*return*/, __assign(__assign({}, config), { slashableDowntime: (0, BaseWrapper_1.blocksToDurationString)(config.slashableDowntime) })];
                }
            });
        });
    };
    /**
     * Calculates intervals which span `slashableDowntime` before provided block.
     * @param block Block number to build intervals before.
     * @param maximumLength Maximum length for any interval (limited by gas limit).
     * @dev if block is undefined, latest will be used
     * @return The signature bitmap for the specified interval.
     */
    DowntimeSlasherWrapper.prototype.slashableDowntimeIntervalsBefore = function (block, maximumLength) {
        if (maximumLength === void 0) { maximumLength = 4000; }
        return __awaiter(this, void 0, void 0, function () {
            var _a, window, blockchainParamsWrapper, end, intervals, epochNumber, firstBlock, start;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0: return [4 /*yield*/, Promise.all([
                            this.getSlashableDowntimeWindow(undefined, block),
                            this.contracts.getBlockchainParameters(),
                        ])];
                    case 1:
                        _a = _b.sent(), window = _a[0], blockchainParamsWrapper = _a[1];
                        end = window.end;
                        intervals = [];
                        _b.label = 2;
                    case 2:
                        if (!(end > window.start)) return [3 /*break*/, 5];
                        return [4 /*yield*/, blockchainParamsWrapper.getEpochNumberOfBlock(end)];
                    case 3:
                        epochNumber = _b.sent();
                        return [4 /*yield*/, blockchainParamsWrapper.getFirstBlockNumberForEpoch(epochNumber)];
                    case 4:
                        firstBlock = _b.sent();
                        start = Math.max(window.start, end - maximumLength, firstBlock);
                        intervals.push({ start: start, end: end });
                        end = start - 1;
                        return [3 /*break*/, 2];
                    case 5: return [2 /*return*/, intervals.reverse()];
                }
            });
        });
    };
    /**
     * Shows if the user already called the `setBitmapForInterval` for intervals.
     * @param intervals First and last block of the interval.
     * @return True if the user already called the `setBitmapForInterval` for intervals.
     */
    DowntimeSlasherWrapper.prototype.isBitmapSetForIntervals = function (intervals) {
        return __awaiter(this, void 0, void 0, function () {
            var setArray;
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, Promise.all(intervals.map(function (interval) { return _this.isBitmapSetForInterval(interval); }))];
                    case 1:
                        setArray = _a.sent();
                        return [2 /*return*/, !setArray.some(function (set) { return !set; })];
                }
            });
        });
    };
    /**
     * Tests if the given validator or signer did not sign any blocks in the interval.
     * @param address Address of the validator account or signer.
     * @param interval First and last block of the interval.
     */
    DowntimeSlasherWrapper.prototype.wasValidatorDownForInterval = function (address, interval) {
        return __awaiter(this, void 0, void 0, function () {
            var startSignerIndex;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.signerIndexAtBlock(address, interval.start)];
                    case 1:
                        startSignerIndex = _a.sent();
                        return [2 /*return*/, this.contract.methods
                                .wasDownForInterval(interval.start, interval.end, startSignerIndex)
                                .call()];
                }
            });
        });
    };
    /**
     * Returns true if the validator did not sign any blocks for the specified overlapping or adjacent
     * intervals.
     * @param address Address of the validator account or signer.
     * @param intervals
     * @return True if the validator signature does not appear in any block within the window.
     */
    DowntimeSlasherWrapper.prototype.wasValidatorDownForIntervals = function (address, intervals) {
        return __awaiter(this, void 0, void 0, function () {
            var downArray;
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, Promise.all(intervals.map(function (interval) { return _this.wasValidatorDownForInterval(address, interval); }))];
                    case 1:
                        downArray = _a.sent();
                        return [2 /*return*/, downArray.every(function (down) { return down; })];
                }
            });
        });
    };
    /**
     * Returns true if the validator did not sign any blocks for the specified overlapping or adjacent
     * intervals.
     * @param address Address of the validator account or signer.
     * @param intervals A list of ordered intervals for which signature bitmaps have already been set.
     */
    DowntimeSlasherWrapper.prototype.slashValidator = function (address, intervals) {
        return __awaiter(this, void 0, void 0, function () {
            var blockchainParamsWrapper, signerIndices, prevEpochNumber, _i, intervals_1, interval, epochNumber, signerIndex, _a, _b, _c, _d;
            return __generator(this, function (_e) {
                switch (_e.label) {
                    case 0:
                        if (intervals.length === 0) {
                            throw new Error('intervals array should have at least one element');
                        }
                        return [4 /*yield*/, this.contracts.getBlockchainParameters()];
                    case 1:
                        blockchainParamsWrapper = _e.sent();
                        signerIndices = [];
                        prevEpochNumber = -1;
                        _i = 0, intervals_1 = intervals;
                        _e.label = 2;
                    case 2:
                        if (!(_i < intervals_1.length)) return [3 /*break*/, 6];
                        interval = intervals_1[_i];
                        return [4 /*yield*/, blockchainParamsWrapper.getFirstBlockNumberForEpoch(interval.start)];
                    case 3:
                        epochNumber = _e.sent();
                        if (!(epochNumber !== prevEpochNumber)) return [3 /*break*/, 5];
                        return [4 /*yield*/, this.signerIndexAtBlock(address, interval.start)];
                    case 4:
                        signerIndex = _e.sent();
                        signerIndices.push(signerIndex);
                        prevEpochNumber = epochNumber;
                        _e.label = 5;
                    case 5:
                        _i++;
                        return [3 /*break*/, 2];
                    case 6:
                        _b = (_a = this.slash).apply;
                        _c = [this];
                        _d = [[intervals.map(function (interval) { return interval.start; }),
                                intervals.map(function (interval) { return interval.end; }),
                                signerIndices]];
                        return [4 /*yield*/, this.trailingSlashArgs(address, intervals[0].start)];
                    case 7: return [2 /*return*/, _b.apply(_a, _c.concat([__spreadArray.apply(void 0, _d.concat([(_e.sent()), false]))]))];
                }
            });
        });
    };
    /**
     * Calculate the slashable downtime window with respect to a provided start/end block numbers and length.
     * @param startBlock First block of the downtime. Determined from endBlock if not provided.
     * @param endBlock Last block of the downtime. Determined from startBlock or grandparent of latest block if not provided.
     * @param length Length of downtime. Determined from minimum slashable downtime if not provided.
     */
    DowntimeSlasherWrapper.prototype.getSlashableDowntimeWindow = function (startBlock, endBlock, length) {
        return __awaiter(this, void 0, void 0, function () {
            var _a;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        if (!!length) return [3 /*break*/, 2];
                        return [4 /*yield*/, this.slashableDowntime()];
                    case 1:
                        length = _b.sent();
                        _b.label = 2;
                    case 2:
                        if (!!endBlock) return [3 /*break*/, 6];
                        if (!startBlock) return [3 /*break*/, 3];
                        _a = startBlock + length - 1;
                        return [3 /*break*/, 5];
                    case 3: return [4 /*yield*/, this.connection.getBlockNumber()];
                    case 4:
                        _a = (_b.sent()) - 2; // latest grandparent
                        _b.label = 5;
                    case 5:
                        endBlock = _a; // latest grandparent
                        _b.label = 6;
                    case 6: return [2 /*return*/, {
                            start: startBlock !== null && startBlock !== void 0 ? startBlock : endBlock - length + 1,
                            end: endBlock,
                        }];
                }
            });
        });
    };
    return DowntimeSlasherWrapper;
}(BaseSlasher_1.BaseSlasher));
exports.DowntimeSlasherWrapper = DowntimeSlasherWrapper;
//# sourceMappingURL=DowntimeSlasher.js.map

Выполнить команду


Для локальной разработки. Не используйте в интернете!