OSDN Git Service

add the list-peers auto refresh
[bytom/bytom-electron.git] / src / features / core / reducers.js
1 import { combineReducers } from 'redux'
2 import { testnetUrl } from 'utility/environment'
3 import moment from 'moment'
4 import { DeltaSampler } from 'utility/time'
5
6 const LONG_TIME_FORMAT = 'YYYY-MM-DD, h:mm:ss a'
7
8 const coreConfigReducer = (key, state, defaultState, action) => {
9   if (action.type == 'UPDATE_CORE_INFO') {
10     return action.param.data[key] || defaultState
11   }
12
13   return state || defaultState
14 }
15
16 const buildConfigReducer = (key, state, defaultState, action) => {
17   // if (action.type == 'UPDATE_CORE_INFO') {
18         // return action.param.buildConfig[key] || defaultState
19   // }
20
21   return state || defaultState
22 }
23
24 const configKnown = (state = false, action) => {
25   if (action.type == 'UPDATE_CORE_INFO') {
26     return true
27   }
28   return state
29 }
30
31 export const configured = (state, action) =>
32   coreConfigReducer('isConfigured', state, false, action)
33 export const configuredAt = (state, action) => {
34   let value = coreConfigReducer('configuredAt', state, '', action)
35   if (action.type == 'UPDATE_CORE_INFO' && value != '') {
36     value = moment(value).format(LONG_TIME_FORMAT)
37   }
38   return value
39 }
40
41 // export const mockhsm = (state, action) =>
42 //   buildConfigReducer('isMockhsm', state, false, action)
43 // export const localhostAuth = (state, action) =>
44 //   buildConfigReducer('isLocalhostAuth', state, false, action)
45 // export const reset = (state, action) =>
46 //   buildConfigReducer('isReset', state, false, action)
47 // export const httpOk = (state, action) =>
48 //   buildConfigReducer('isHttpOk', state, false, action)
49 export const blockHeight = (state, action) =>
50   coreConfigReducer('highestBlock', state, 0, action)
51 export const currentBlockHeight = (state, action) =>
52   coreConfigReducer('currentBlock', state, 0, action)
53 export const peerCount = (state, action) =>
54   coreConfigReducer('peerCount', state, 0, action)
55 export const generatorBlockHeight = (state, action) => {
56   if (action.type == 'UPDATE_CORE_INFO') {
57     if (action.param.generatorBlockHeight == 0) return '???'
58   }
59
60   return coreConfigReducer('generatorBlockHeight', state, 0, action)
61 }
62 export const signer = (state, action) =>
63   coreConfigReducer('isSigner', state, false, action)
64 export const generator = (state, action) =>
65   coreConfigReducer('isGenerator', state, false, action)
66 export const generatorUrl = (state, action) =>
67   coreConfigReducer('generatorUrl', state, false, action)
68 export const generatorAccessToken = (state, action) =>
69   coreConfigReducer('generatorAccessToken', state, false, action)
70 export const blockchainId = (state, action) =>
71   coreConfigReducer('blockchainId', state, 0, action)
72 // export const crosscoreRpcVersion = (state, action) =>
73 //   coreConfigReducer('crosscoreRpcVersion', state, 0, action)
74 //
75 // export const coreType = (state = '', action) => {
76 //   if (action.type == 'UPDATE_CORE_INFO') {
77 //     if (action.param.isGenerator) return 'Generator'
78 //     if (action.param.isSigner) return 'Signer'
79 //     return 'Participant'
80 //   }
81 //   return state
82 // }
83 //
84 // export const replicationLag = (state = null, action) => {
85 //   if (action.type == 'UPDATE_CORE_INFO') {
86 //     if (action.param.generatorBlockHeight == 0) {
87 //       return null
88 //     }
89 //     return action.param.generatorBlockHeight - action.param.blockHeight
90 //   }
91 //
92 //   return state
93 // }
94
95 // let syncSamplers = null
96 // const resetSyncSamplers = () => {
97 //   syncSamplers = {
98 //     snapshot: new DeltaSampler({sampleTtl: 10 * 1000}),
99 //     replicationLag: new DeltaSampler({sampleTtl: 10 * 1000}),
100 //   }
101 // }
102 //
103 // export const syncEstimates = (state = {}, action) => {
104 //   switch (action.type) {
105 //     case 'UPDATE_CORE_INFO': {
106 //       if (!syncSamplers) {
107 //         resetSyncSamplers()
108 //       }
109 //
110 //       const {
111 //         snapshot,
112 //         generatorBlockHeight,
113 //         blockHeight,
114 //       } = action.param
115 //
116 //       const estimates = {}
117 //
118 //       if (snapshot && snapshot.inProgress) {
119 //         const speed = syncSamplers.snapshot.sample(snapshot.downloaded)
120 //
121 //         if (speed != 0) {
122 //           estimates.snapshot = (snapshot.size - snapshot.downloaded) / speed
123 //         }
124 //       } else if (generatorBlockHeight > 0) {
125 //         const replicationLag = generatorBlockHeight - blockHeight
126 //         const speed = syncSamplers.replicationLag.sample(replicationLag)
127 //         if (speed != 0) {
128 //           const duration = -1 * replicationLag / speed
129 //           if (duration > 0) {
130 //             estimates.replicationLag = duration
131 //           }
132 //         }
133 //       }
134 //
135 //       return estimates
136 //     }
137 //
138 //     case 'CORE_DISCONNECT':
139 //       resetSyncSamplers()
140 //       return {}
141 //
142 //     default:
143 //       return state
144 //   }
145 // }
146 //
147 // export const replicationLagClass = (state = null, action) => {
148 //   if (action.type == 'UPDATE_CORE_INFO') {
149 //     if (action.param.generatorBlockHeight == 0) {
150 //       return 'red'
151 //     } else {
152 //       let lag = action.param.generatorBlockHeight - action.param.blockHeight
153 //       if (lag < 5) {
154 //         return 'green'
155 //       } else if (lag < 10) {
156 //         return 'yellow'
157 //       } else {
158 //         return 'red'
159 //       }
160 //     }
161 //   }
162 //
163 //   return state
164 // }
165
166 // export const onTestnet = (state = false, action) => {
167 //   if (action.type == 'UPDATE_CORE_INFO') {
168 //     return (action.param.generatorUrl || '').indexOf(testnetUrl) >= 0
169 //   }
170 //
171 //   return state
172 // }
173
174 export const requireClientToken = (state = false, action) => {
175   if (action.type == 'ERROR' && action.payload.status == 401) return true
176
177   return state
178 }
179
180 export const clientToken = (state = '', action) => {
181   if      (action.type == 'SET_CLIENT_TOKEN') return action.token
182   else if (action.type == 'ERROR' &&
183            action.payload.status == 401)      return ''
184
185   return state
186 }
187
188 export const validToken = (state = false, action) => {
189   if      (action.type == 'SET_CLIENT_TOKEN') return false
190   else if (action.type == 'USER_LOG_IN')      return true
191   else if (action.type == 'ERROR' &&
192            action.payload.status == 401)      return false
193
194   return state
195 }
196
197 export const connected = (state = true, action) => {
198   if      (action.type == 'UPDATE_CORE_INFO') return true
199   else if (action.type == 'CORE_DISCONNECT')  return false
200
201   return state
202 }
203
204 export const btmAmountUnit = (state = 'BTM' , action) => {
205   if (action.type == 'UPDATE_BTM_AMOUNT_UNIT') {
206     return action.param
207   }
208   return state
209 }
210
211 const mingStatus = (state = false, action) => {
212   if (action.type == 'UPDATE_CORE_INFO') {
213     return action.param.data.mining
214   }
215   return state
216 }
217
218 const coreData = (state = null, action) => {
219   if (action.type == 'UPDATE_CORE_INFO') {
220     return action.param.data || null
221   }
222   return state
223 }
224
225 const accountInit = (state = false, action) => {
226   if (action.type == 'CREATE_REGISTER_ACCOUNT') {
227     return true
228   }
229   return state
230 }
231
232 const snapshot = (state = null, action) => {
233   if (action.type == 'UPDATE_CORE_INFO') {
234     return action.param.snapshot || null // snapshot may be undefined, which Redux doesn't like.
235   }
236   return state
237 }
238
239 const version = (state = 'N/A', action) => {
240   if (action.type == 'UPDATE_CORE_INFO') {
241     return action.param.data.versionInfo.version
242   }
243   return state
244 }
245
246 const newVersionCode = (state = 'N/A', action) => {
247   if (action.type == 'UPDATE_CORE_INFO') {
248     return action.param.data.versionInfo.newVersion
249   }
250   return state
251 }
252
253 const update = (state = false, action) => {
254   if (action.type == 'UPDATE_CORE_INFO') {
255     return action.param.data.versionInfo.update > 0
256   }
257   return state
258 }
259
260
261 export default combineReducers({
262   accountInit,
263   blockchainId,
264   blockHeight,
265   connected,
266   clientToken,
267   configKnown,
268   configured,
269   configuredAt,
270   // coreType,
271   coreData,
272   currentBlockHeight,
273   generator,
274   generatorAccessToken,
275   generatorBlockHeight,
276   generatorUrl,
277   // localhostAuth,
278   // newVersionCode,
279   // mockhsm,
280   // mingStatus,
281   // crosscoreRpcVersion,
282   // onTestnet,
283   // httpOk,
284   peerCount,
285   // replicationLag,
286   // replicationLagClass,
287   requireClientToken,
288   // reset,
289   signer,
290   snapshot,
291   // syncEstimates,
292   update,
293   validToken,
294   version,
295   btmAmountUnit
296 })