Home Reference Source

packages/oo7-substrate/src/srml/session.js

  1. const { Bond, TransformBond } = require('oo7')
  2. const { ss58Encode } = require('../ss58')
  3.  
  4. function augment (runtime, chain) {
  5. let timestamp = runtime.timestamp
  6. let session = runtime.session
  7. if (session._extras) {
  8. return
  9. } else {
  10. session._extras = true
  11. }
  12.  
  13. session.blocksRemaining = Bond // 1..60
  14. .all([chain.height, session.lastLengthChange, session.sessionLength])
  15. .map(([h, c, l]) => {
  16. c = (c || 0);
  17. return l - (h - c + l) % l;
  18. });
  19. session.lateness = Bond
  20. .all([
  21. timestamp.blockPeriod,
  22. timestamp.now,
  23. session.blocksRemaining,
  24. session.sessionLength,
  25. session.currentStart,
  26. ]).map(([p, n, r, l, s]) => (n.number + p.number * r - s.number) / (p.number * l));
  27. session.percentLate = session.lateness.map(l => Math.round(l * 100 - 100));
  28. session.validatorIndexOf = id =>
  29. new TransformBond((i, id) => {
  30. let ss58 = ss58Encode(id);
  31. return i.findIndex(a => ss58Encode(a) === ss58);
  32. }, [session.validators, id])
  33. }
  34.  
  35. module.exports = { augment }