Guest User

Untitled

a guest
Mar 22nd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. import findIndex from 'lodash/findIndex';
  2. import Redux from 'redux';
  3.  
  4. const initialState = {
  5.  
  6. // Initialization
  7. dbLoaded: false,
  8. lastBlock: null,
  9. allPeers: [ ], // list of { ip: String, port: Number }
  10. version: 1,
  11. difficulty: 0,
  12. numBlocks: 0,
  13.  
  14. // Mempool
  15. memoryPool: [ ], // list of valid transactions (txs)
  16. pendingBlockTxs: [ ],
  17. unfetchedHeaders: new Set(),
  18. loadingHeaders: new Set(),
  19. orphanTransactions: new Set(),
  20.  
  21. // Mining
  22. isMining: false,
  23. };
  24.  
  25. let newUnfetchedHeaders, newLoadingHeaders, peerIdx, newMemoryPool, tempMempool;
  26.  
  27. const reducer = (state = initialState, action) => {
  28. switch(action.type) {
  29. case 'SET_INITIAL_BLOCK_COUNT':
  30. return {
  31. ...state,
  32. dbLoaded: true,
  33. lastBlock: action.lastBlock,
  34. numBlocks: action.numBlocks,
  35. };
  36. case 'ADD_BLOCK':
  37. return {
  38. ...state,
  39. lastBlock: action.block,
  40. numBlocks: state.numBlocks + 1,
  41. };
  42. case 'SET_DIFFICULTY':
  43. return {
  44. ...state,
  45. difficulty: action.difficulty,
  46. };
  47. case 'SET_PEERS':
  48. return { ...state, allPeers: action.allPeers };
  49. case 'CONNECT_PEER':
  50. peerIdx = findIndex(state.allPeers, ({ ip }) => ip === action.ip);
  51. return {
  52. ...state,
  53. allPeers: peerIdx === -1 ? state.allPeers : [
  54. ...state.allPeers.slice(0, peerIdx),
  55. { ip: action.ip, client: action.client, synced: false, connected: true },
  56. ...state.allPeers.slice(peerIdx + 1),
  57. ],
  58. }
  59. case 'ADD_UNFETCHED_HEADERS':
  60. return {
  61. ...state,
  62. unfetchedHeaders: new Set([
  63. ...Array.from(state.unfetchedHeaders),
  64. ...action.headers,
  65. ]),
  66. };
  67. case 'LOADING_BLOCK':
  68. newUnfetchedHeaders = state.unfetchedHeaders;
  69. newUnfetchedHeaders.delete(action.header);
  70. newLoadingHeaders = state.loadingHeaders;
  71. newLoadingHeaders.add(action.header);
  72. return {
  73. ...state,
  74. unfetchedHeaders: newUnfetchedHeaders,
  75. loadingHeaders: newLoadingHeaders,
  76. };
  77. case 'NEW_BLOCK':
  78. newUnfetchedHeaders = state.unfetchedHeaders;
  79. newUnfetchedHeaders.delete(action.header);
  80. newLoadingHeaders = state.loadingHeaders;
  81. newLoadingHeaders.delete(action.header);
  82. return {
  83. ...state,
  84. lastBlock: action.block,
  85. numBlocks: state.numBlocks + 1,
  86. unfetchedHeaders: newUnfetchedHeaders,
  87. loadingHeaders: newLoadingHeaders,
  88. };
  89. case 'SYNC_PEER':
  90. peerIdx = findIndex(state.allPeers, ({ ip }) => ip === action.ip);
  91. return {
  92. ...state,
  93. allPeers: peerIdx === -1 ? state.allPeers : [
  94. ...state.allPeers.slice(0, peerIdx),
  95. { ...state.allPeers[peerIdx], synced: true, connected: true },
  96. ...state.allPeers.slice(peerIdx + 1),
  97. ],
  98. };
  99. case 'NEW_TX':
  100. return {
  101. ...state,
  102. memoryPool: [ ...state.memoryPool, action.tx ],
  103. };
  104. case 'START_MINING':
  105. return { ...state, isMining: true };
  106. case 'STOP_MINING':
  107. tempMempool = state.memoryPool;
  108. return { ...state, isMining: false, memoryPool: [], pendingBlockTxs: tempMempool };
  109. default:
  110. return state;
  111. }
  112. };
  113.  
  114. const store = Redux.createStore(reducer);
  115.  
  116. store.dispatch = addLoggingToDispatch(store);
  117.  
  118. function addLoggingToDispatch(store) {
  119. const rawDispatch = store.dispatch;
  120. return (action) => {
  121. console.log(`> Action: ${action.type}, (Keys: ${Object.keys(action).join(', ')})`);
  122. const returnValue = rawDispatch(action);
  123. return returnValue;
  124. }
  125. }
  126.  
  127. export default store;
Add Comment
Please, Sign In to add comment