You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. const { ShaMap } = require('../src/shamap')
  2. const { binary, HashPrefix } = require('../src/coretypes')
  3. const { coreTypes } = require('../src/types')
  4. const { loadFixture } = require('./utils')
  5. const { Buffer } = require('buffer/')
  6. function now() {
  7. return Number(Date.now()) / 1000
  8. }
  9. const ZERO = '0000000000000000000000000000000000000000000000000000000000000000'
  10. function makeItem(indexArg) {
  11. let str = indexArg
  12. while (str.length < 64) {
  13. str += '0'
  14. }
  15. const index = coreTypes.Hash256.from(str)
  16. const item = {
  17. toBytesSink(sink) {
  18. index.toBytesSink(sink)
  19. },
  20. hashPrefix() {
  21. return Buffer.from([1, 3, 3, 7])
  22. },
  23. }
  24. return [index, item]
  25. }
  26. describe('ShaMap', () => {
  27. now()
  28. test('hashes to zero when empty', () => {
  29. const map = new ShaMap()
  30. expect(map.hash().toHex()).toBe(ZERO)
  31. })
  32. test('creates the same hash no matter which order items are added', () => {
  33. let map = new ShaMap()
  34. const items = [
  35. '0',
  36. '1',
  37. '11',
  38. '7000DE445E22CB9BB7E1717589FA858736BAA5FD192310E20000000000000000',
  39. '7000DE445E22CB9BB7E1717589FA858736BAA5FD192310E21000000000000000',
  40. '7000DE445E22CB9BB7E1717589FA858736BAA5FD192310E22000000000000000',
  41. '7000DE445E22CB9BB7E1717589FA858736BAA5FD192310E23000000000000000',
  42. '12',
  43. '122',
  44. ]
  45. items.forEach((i) => map.addItem(...makeItem(i)))
  46. const h1 = map.hash()
  47. expect(h1.eq(h1)).toBe(true)
  48. map = new ShaMap()
  49. items.reverse().forEach((i) => map.addItem(...makeItem(i)))
  50. expect(map.hash()).toStrictEqual(h1)
  51. })
  52. function factory(fixture) {
  53. test(`recreate account state hash from ${fixture}`, () => {
  54. const map = new ShaMap()
  55. const ledger = loadFixture(fixture)
  56. // const t = now();
  57. const leafNodePrefix = HashPrefix.accountStateEntry
  58. ledger.accountState
  59. .map((e, i) => {
  60. if ((i > 1000) & (i % 1000 === 0)) {
  61. console.log(e.index)
  62. console.log(i)
  63. }
  64. const bytes = binary.serializeObject(e)
  65. return {
  66. index: coreTypes.Hash256.from(e.index),
  67. hashPrefix() {
  68. return leafNodePrefix
  69. },
  70. toBytesSink(sink) {
  71. sink.put(bytes)
  72. },
  73. }
  74. })
  75. .forEach((so) => map.addItem(so.index, so))
  76. expect(map.hash().toHex()).toBe(ledger.account_hash)
  77. // console.log('took seconds: ', (now() - t));
  78. })
  79. }
  80. factory('ledger-full-38129.json')
  81. factory('ledger-full-40000.json')
  82. // factory('ledger-4320277.json');
  83. // factory('14280680.json');
  84. })