evernode-js-client 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. package/index.js +97 -44
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -11846,29 +11846,29 @@ class BaseEvernodeClient {
11846
11846
  }
11847
11847
 
11848
11848
  /**
11849
- * Get the moment from the given XRP ledger index. (1 Moment - 1190 XRP ledgers).
11850
- * @param {number} ledgerIndex [Optional] Ledger index to get the moment value.
11851
- * @returns The moment of the given XPR ledger index as 'number'. Returns current moment if XRP ledger index is not given.
11852
- */
11853
- async getMoment(ledgerIndex = null) {
11854
- const lv = ledgerIndex || this.xrplApi.ledgerIndex;
11855
- const m = Math.floor((lv - this.config.momentBaseIdx) / this.config.momentSize);
11856
-
11857
- await Promise.resolve(); // Awaiter placeholder for future async requirements.
11849
+ * Get the moment from the given index (timestamp).
11850
+ * @param {number} index [Optional] Index (timestamp) to get the moment value.
11851
+ * @returns The moment of the given index (timestamp) as 'number'. Returns current moment if index (timestamp) is not given.
11852
+ */
11853
+ async getMoment(index = null) {
11854
+ const i = index || UtilHelpers.getCurrentUnixTime();
11855
+ const m = this.config.momentBaseInfo.baseTransitionMoment + Math.floor((i - this.config.momentBaseInfo.baseIdx) / this.config.momentSize);
11856
+ await Promise.resolve();
11858
11857
  return m;
11859
11858
  }
11860
11859
 
11861
11860
  /**
11862
- * Get start XRP ledger index of the moment (of the given XRPL index).
11863
- * @param {number} ledgerIndex [Optional] Ledger index to get the moment value.
11864
- * @returns The XRP ledger index of the moment (of the given XRPL index) as a 'number'. Returns the current moment's start XRP ledger index if ledger index parameter is not given.
11861
+ * Get start index (timestamp) of the moment.
11862
+ * @param {number} index [Optional] Index (timestamp) to get the moment value.
11863
+ * @returns The index (timestamp) of the moment as a 'number'. Returns the current moment's start index (timestamp) if ledger index parameter is not given.
11865
11864
  */
11866
- async getMomentStartIndex(ledgerIndex = null) {
11867
- const lv = ledgerIndex || this.xrplApi.ledgerIndex;
11868
- const m = Math.floor((lv - this.config.momentBaseIdx) / this.config.momentSize);
11865
+ async getMomentStartIndex(index = null) {
11866
+ const i = index || UtilHelpers.getCurrentUnixTime();
11867
+
11868
+ const m = Math.floor((i - this.config.momentBaseInfo.baseIdx) / this.config.momentSize);
11869
11869
 
11870
11870
  await Promise.resolve(); // Awaiter placeholder for future async requirements.
11871
- return this.config.momentBaseIdx + (m * this.config.momentSize);
11871
+ return this.config.momentBaseInfo.baseIdx + (m * this.config.momentSize);
11872
11872
  }
11873
11873
 
11874
11874
  /**
@@ -11883,19 +11883,23 @@ class BaseEvernodeClient {
11883
11883
  hostRegFee: HookStateKeys.HOST_REG_FEE,
11884
11884
  momentSize: HookStateKeys.MOMENT_SIZE,
11885
11885
  hostHeartbeatFreq: HookStateKeys.HOST_HEARTBEAT_FREQ,
11886
- momentBaseIdx: HookStateKeys.MOMENT_BASE_IDX,
11886
+ momentBaseInfo: HookStateKeys.MOMENT_BASE_INFO,
11887
11887
  purchaserTargetPrice: HookStateKeys.PURCHASER_TARGET_PRICE,
11888
11888
  leaseAcquireWindow: HookStateKeys.LEASE_ACQUIRE_WINDOW,
11889
11889
  rewardInfo: HookStateKeys.REWARD_INFO,
11890
- rewardConfiguaration: HookStateKeys.REWARD_CONFIGURATION,
11891
- hostCount: HookStateKeys.HOST_COUNT
11890
+ rewardConfiguration: HookStateKeys.REWARD_CONFIGURATION,
11891
+ hostCount: HookStateKeys.HOST_COUNT,
11892
+ momentTransitInfo: HookStateKeys.MOMENT_TRANSIT_INFO
11892
11893
  }
11893
11894
  let config = {};
11894
11895
  for (const [key, value] of Object.entries(configStateKeys)) {
11895
11896
  const stateKey = Buffer.from(value, 'hex');
11896
- const stateData = Buffer.from(UtilHelpers.getStateData(states, value), 'hex');
11897
- const decoded = StateHelpers.decodeStateData(stateKey, stateData);
11898
- config[key] = decoded.value;
11897
+ const stateDataBin = StateHelpers.getStateData(states, value);
11898
+ if (stateDataBin) {
11899
+ const stateData = Buffer.from(StateHelpers.getStateData(states, value), 'hex');
11900
+ const decoded = StateHelpers.decodeStateData(stateKey, stateData);
11901
+ config[key] = decoded.value;
11902
+ }
11899
11903
  }
11900
11904
  return config;
11901
11905
  }
@@ -12159,9 +12163,9 @@ class BaseEvernodeClient {
12159
12163
  if (addrStateData) {
12160
12164
  const addrStateDecoded = StateHelpers.decodeHostAddressState(Buffer.from(addrStateKey, 'hex'), Buffer.from(addrStateData, 'hex'));
12161
12165
  const curMomentStartIdx = await this.getMomentStartIndex();
12162
- addrStateDecoded.active = (addrStateDecoded.lastHeartbeatLedger > (this.config.hostHeartbeatFreq * this.config.momentSize) ?
12163
- (addrStateDecoded.lastHeartbeatLedger >= (curMomentStartIdx - (this.config.hostHeartbeatFreq * this.config.momentSize))) :
12164
- (addrStateDecoded.lastHeartbeatLedger > 0))
12166
+ addrStateDecoded.active = (addrStateDecoded.lastHeartbeatIndex > (this.config.hostHeartbeatFreq * this.config.momentSize) ?
12167
+ (addrStateDecoded.lastHeartbeatIndex >= (curMomentStartIdx - (this.config.hostHeartbeatFreq * this.config.momentSize))) :
12168
+ (addrStateDecoded.lastHeartbeatIndex > 0))
12165
12169
 
12166
12170
  const nftIdStatekey = StateHelpers.generateTokenIdStateKey(addrStateDecoded.nfTokenId);
12167
12171
  const nftIdStateIndex = StateHelpers.getHookStateIndex(this.registryAddress, nftIdStatekey);
@@ -12195,9 +12199,9 @@ class BaseEvernodeClient {
12195
12199
  const curMomentStartIdx = await this.getMomentStartIndex();
12196
12200
  // Populate the host active status.
12197
12201
  (hosts.nextPageToken ? hosts.data : hosts).forEach(h => {
12198
- h.active = (h.lastHeartbeatLedger > (this.config.hostHeartbeatFreq * this.config.momentSize) ?
12199
- (h.lastHeartbeatLedger >= (curMomentStartIdx - (this.config.hostHeartbeatFreq * this.config.momentSize))) :
12200
- (h.lastHeartbeatLedger > 0))
12202
+ h.active = (h.lastHeartbeatIndex > (this.config.hostHeartbeatFreq * this.config.momentSize) ?
12203
+ (h.lastHeartbeatIndex >= (curMomentStartIdx - (this.config.hostHeartbeatFreq * this.config.momentSize))) :
12204
+ (h.lastHeartbeatIndex > 0))
12201
12205
  });
12202
12206
  return hosts;
12203
12207
  }
@@ -13434,10 +13438,11 @@ const HookStateKeys = {
13434
13438
  LEASE_ACQUIRE_WINDOW: "4556520100000000000000000000000000000000000000000000000000000008",
13435
13439
  REWARD_CONFIGURATION: "4556520100000000000000000000000000000000000000000000000000000009",
13436
13440
  MAX_TOLERABLE_DOWNTIME: "455652010000000000000000000000000000000000000000000000000000000A",
13441
+ MOMENT_TRANSIT_INFO: "455652010000000000000000000000000000000000000000000000000000000B",
13437
13442
 
13438
13443
  // Singleton
13439
13444
  HOST_COUNT: "4556523200000000000000000000000000000000000000000000000000000000",
13440
- MOMENT_BASE_IDX: "4556523300000000000000000000000000000000000000000000000000000000",
13445
+ MOMENT_BASE_INFO: "4556523300000000000000000000000000000000000000000000000000000000",
13441
13446
  HOST_REG_FEE: "4556523400000000000000000000000000000000000000000000000000000000",
13442
13447
  MAX_REG: "4556523500000000000000000000000000000000000000000000000000000000",
13443
13448
  REWARD_INFO: "4556523600000000000000000000000000000000000000000000000000000000",
@@ -13876,6 +13881,14 @@ const FIRST_EPOCH_REWARD_QUOTA_OFFSET = 1;
13876
13881
  const EPOCH_REWARD_AMOUNT_OFFSET = 5;
13877
13882
  const REWARD_START_MOMENT_OFFSET = 9;
13878
13883
 
13884
+ const TRANSIT_IDX_OFFSET = 0;
13885
+ const TRANSIT_MOMENT_SIZE_OFFSET = 8;
13886
+ const TRANSIT_MOMENT_TYPE_OFFSET = 10;
13887
+
13888
+ const MOMENT_BASE_POINT_OFFSET = 0;
13889
+ const MOMENT_AT_TRANSITION_OFFSET = 8;
13890
+ const MOMENT_TYPE_OFFSET = 12;
13891
+
13879
13892
  const HOST_TOKEN_ID_OFFSET = 0;
13880
13893
  const HOST_COUNTRY_CODE_OFFSET = 32;
13881
13894
  const HOST_RESERVED_OFFSET = 34;
@@ -13886,6 +13899,7 @@ const HOST_TOT_INS_COUNT_OFFSET = 84;
13886
13899
  const HOST_ACT_INS_COUNT_OFFSET = 88;
13887
13900
  const HOST_HEARTBEAT_LEDGER_IDX_OFFSET = 92;
13888
13901
  const HOST_VERSION_OFFSET = 100;
13902
+ const HOST_REG_TIMESTAMP_OFFSET = 103;
13889
13903
 
13890
13904
  const HOST_ADDRESS_OFFSET = 0;
13891
13905
  const HOST_CPU_MODEL_NAME_OFFSET = 20;
@@ -13900,6 +13914,11 @@ const STATE_KEY_TYPES = {
13900
13914
  HOST_ADDR: 3
13901
13915
  }
13902
13916
 
13917
+ const MOMENT_TYPES = {
13918
+ LEDGER: 0,
13919
+ TIMESTAMP: 1
13920
+ }
13921
+
13903
13922
  const EVERNODE_PREFIX = 'EVR';
13904
13923
  const HOST_ADDR_KEY_ZERO_COUNT = 8;
13905
13924
  const HOOK_STATE_LEDGER_TYPE_PREFIX = 118; // Decimal value of ASCII 'v'
@@ -13912,8 +13931,20 @@ class StateHelpers {
13912
13931
  CONFIGURATION: 'configuration'
13913
13932
  }
13914
13933
 
13934
+ static timeLines = {
13935
+ SEC: "SEC"
13936
+ }
13937
+
13938
+ static getStateData(states, key) {
13939
+ const state = states.find(s => key === s.key);
13940
+ if (!state)
13941
+ return null;
13942
+
13943
+ return state.data;
13944
+ }
13945
+
13915
13946
  static decodeHostAddressState(stateKeyBuf, stateDataBuf) {
13916
- return {
13947
+ let data = {
13917
13948
  address: codec.encodeAccountID(stateKeyBuf.slice(12)),
13918
13949
  nfTokenId: stateDataBuf.slice(HOST_TOKEN_ID_OFFSET, HOST_COUNTRY_CODE_OFFSET).toString('hex').toUpperCase(),
13919
13950
  countryCode: stateDataBuf.slice(HOST_COUNTRY_CODE_OFFSET, HOST_RESERVED_OFFSET).toString(),
@@ -13922,9 +13953,12 @@ class StateHelpers {
13922
13953
  registrationFee: Number(stateDataBuf.readBigUInt64BE(HOST_REG_FEE_OFFSET)),
13923
13954
  maxInstances: stateDataBuf.readUInt32BE(HOST_TOT_INS_COUNT_OFFSET),
13924
13955
  activeInstances: stateDataBuf.readUInt32BE(HOST_ACT_INS_COUNT_OFFSET),
13925
- lastHeartbeatLedger: Number(stateDataBuf.readBigUInt64BE(HOST_HEARTBEAT_LEDGER_IDX_OFFSET)),
13926
- version: `${stateDataBuf.readUInt8(HOST_VERSION_OFFSET)}.${stateDataBuf.readUInt8(HOST_VERSION_OFFSET + 1)}.${stateDataBuf.readUInt8(HOST_VERSION_OFFSET + 2)}`
13956
+ lastHeartbeatIndex: Number(stateDataBuf.readBigUInt64BE(HOST_HEARTBEAT_LEDGER_IDX_OFFSET)),
13957
+ version: `${stateDataBuf.readUInt8(HOST_VERSION_OFFSET)}.${stateDataBuf.readUInt8(HOST_VERSION_OFFSET + 1)}.${stateDataBuf.readUInt8(HOST_VERSION_OFFSET + 2)}`,
13927
13958
  }
13959
+ if (stateDataBuf.length > HOST_REG_TIMESTAMP_OFFSET)
13960
+ data.registrationTimestamp = Number(stateDataBuf.readBigUInt64BE(HOST_REG_TIMESTAMP_OFFSET));
13961
+ return data;
13928
13962
  }
13929
13963
 
13930
13964
  static decodeTokenIdState(stateDataBuf) {
@@ -13967,11 +14001,15 @@ class StateHelpers {
13967
14001
  value: stateData.readUInt32BE()
13968
14002
  }
13969
14003
  }
13970
- else if (Buffer.from(HookStateKeys.MOMENT_BASE_IDX, 'hex').compare(stateKey) === 0) {
14004
+ else if (Buffer.from(HookStateKeys.MOMENT_BASE_INFO, 'hex').compare(stateKey) === 0) {
13971
14005
  return {
13972
14006
  type: this.StateTypes.SIGLETON,
13973
14007
  key: hexKey,
13974
- value: Number(stateData.readBigInt64BE())
14008
+ value: {
14009
+ baseIdx: Number(stateData.readBigUInt64BE(MOMENT_BASE_POINT_OFFSET)),
14010
+ baseTransitionMoment: stateData.length > MOMENT_AT_TRANSITION_OFFSET ? stateData.readUInt32BE(MOMENT_AT_TRANSITION_OFFSET) : 0,
14011
+ momentType: (stateData.length <= MOMENT_TYPE_OFFSET || stateData.readUInt8(MOMENT_TYPE_OFFSET) === MOMENT_TYPES.LEDGER) ? 'ledger' : 'timestamp'
14012
+ }
13975
14013
  }
13976
14014
  }
13977
14015
  else if (Buffer.from(HookStateKeys.HOST_REG_FEE, 'hex').compare(stateKey) === 0 || Buffer.from(HookStateKeys.MAX_REG, 'hex').compare(stateKey) === 0) {
@@ -14045,6 +14083,18 @@ class StateHelpers {
14045
14083
  value: stateData.readUInt16BE()
14046
14084
  }
14047
14085
  }
14086
+ else if (Buffer.from(HookStateKeys.MOMENT_TRANSIT_INFO, 'hex').compare(stateKey) === 0) {
14087
+ Buffer.alloc(1).readUInt8()
14088
+ return {
14089
+ type: this.StateTypes.CONFIGURATION,
14090
+ key: hexKey,
14091
+ value: {
14092
+ transitionIndex: Number(stateData.readBigInt64BE(TRANSIT_IDX_OFFSET)),
14093
+ momentSize: stateData.readUInt16BE(TRANSIT_MOMENT_SIZE_OFFSET),
14094
+ momentType: stateData.readUInt8(TRANSIT_MOMENT_TYPE_OFFSET) === MOMENT_TYPES.LEDGER ? 'ledger' : 'timestamp'
14095
+ }
14096
+ }
14097
+ }
14048
14098
  else
14049
14099
  throw { type: 'Validation Error', message: 'Invalid state key.' };
14050
14100
  }
@@ -14064,7 +14114,7 @@ class StateHelpers {
14064
14114
  };
14065
14115
  }
14066
14116
  else if (Buffer.from(HookStateKeys.HOST_COUNT, 'hex').compare(stateKey) === 0 ||
14067
- Buffer.from(HookStateKeys.MOMENT_BASE_IDX, 'hex').compare(stateKey) === 0 ||
14117
+ Buffer.from(HookStateKeys.MOMENT_BASE_INFO, 'hex').compare(stateKey) === 0 ||
14068
14118
  Buffer.from(HookStateKeys.HOST_REG_FEE, 'hex').compare(stateKey) === 0 ||
14069
14119
  Buffer.from(HookStateKeys.MAX_REG, 'hex').compare(stateKey) === 0 ||
14070
14120
  Buffer.from(HookStateKeys.REWARD_INFO, 'hex').compare(stateKey) === 0) {
@@ -14082,7 +14132,8 @@ class StateHelpers {
14082
14132
  Buffer.from(HookStateKeys.FIXED_REG_FEE, 'hex').compare(stateKey) === 0 ||
14083
14133
  Buffer.from(HookStateKeys.LEASE_ACQUIRE_WINDOW, 'hex').compare(stateKey) === 0 ||
14084
14134
  Buffer.from(HookStateKeys.REWARD_CONFIGURATION, 'hex').compare(stateKey) === 0 ||
14085
- Buffer.from(HookStateKeys.MAX_TOLERABLE_DOWNTIME, 'hex').compare(stateKey) === 0) {
14135
+ Buffer.from(HookStateKeys.MAX_TOLERABLE_DOWNTIME, 'hex').compare(stateKey) === 0 ||
14136
+ Buffer.from(HookStateKeys.MOMENT_TRANSIT_INFO, 'hex').compare(stateKey) === 0) {
14086
14137
  return {
14087
14138
  key: hexKey,
14088
14139
  type: this.STATE_TYPES.CONFIGURATION
@@ -14221,14 +14272,6 @@ const { EvernodeConstants, ErrorReasons } = __nccwpck_require__(9849);
14221
14272
  // Utility helper functions.
14222
14273
  class UtilHelpers {
14223
14274
 
14224
- static getStateData(states, key) {
14225
- const state = states.find(s => key === s.key);
14226
- if (!state)
14227
- throw { code: ErrorReasons.NO_STATE_KEY, error: `State key '${key}' not found.` };
14228
-
14229
- return state.data;
14230
- }
14231
-
14232
14275
  static readUInt(buf, base = 32, isBE = true) {
14233
14276
  buf = Buffer.from(buf);
14234
14277
  switch (base) {
@@ -14257,6 +14300,16 @@ class UtilHelpers {
14257
14300
  leaseAmount: parseFloat(XflHelpers.toString(uriBuf.readBigInt64BE(prefixLen + 2 + halfToSLen)))
14258
14301
  }
14259
14302
  }
14303
+
14304
+ static getCurrentUnixTime(format = "sec") {
14305
+ const time = Date.now();
14306
+ switch (format) {
14307
+ case "sec":
14308
+ return Math.floor(time / 1000);
14309
+ default:
14310
+ return time;
14311
+ }
14312
+ }
14260
14313
  }
14261
14314
 
14262
14315
  module.exports = {
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  ],
7
7
  "homepage": "https://github.com/HotPocketDev/evernode-js-client",
8
8
  "license": "MIT",
9
- "version": "0.5.2",
9
+ "version": "0.5.3",
10
10
  "dependencies": {
11
11
  "elliptic": "6.5.4",
12
12
  "ripple-address-codec": "4.2.0",