From 48207bf551f3b5abc9f15ebd5d8705a2e04b44ad Mon Sep 17 00:00:00 2001 From: Ezerous Date: Wed, 20 Mar 2019 12:54:37 +0200 Subject: [PATCH] Orbit improvements --- .gitignore | 1 + app/package.json | 1 + app/src/components/Post.js | 2 +- app/src/components/Topic.js | 5 ++--- app/src/config/ipfsOptions.js | 3 +++ app/src/redux/actions/orbitActions.js | 2 ++ app/src/redux/reducers/orbitReducer.js | 14 ++++++++++++-- app/src/redux/sagas/orbitSaga.js | 24 ++++++++++++++++++++---- app/src/utils/orbitUtils.js | 2 ++ 9 files changed, 44 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 1dd4d96..21c570d 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ yarn.lock /build /src/build /app/src/build +/app/build # Contract Artifacts /app/src/contracts diff --git a/app/package.json b/app/package.json index af810b1..e480194 100644 --- a/app/package.json +++ b/app/package.json @@ -11,6 +11,7 @@ "drizzle": "1.3.3", "history": "4.9.0", "ipfs": "github:ipfs/js-ipfs#e849dbcab4a313f7ffc1532a389097ee76344067", + "lodash.isequal": "4.5.0", "orbit-db": "0.19.9", "orbit-db-keystore": "0.1.0", "prop-types": "15.7.2", diff --git a/app/src/components/Post.js b/app/src/components/Post.js index abd8b39..be886e4 100644 --- a/app/src/components/Post.js +++ b/app/src/components/Post.js @@ -82,7 +82,7 @@ class Post extends Component { orbitPostData = orbitDB.postsDB.get(postID); } else { const fullAddress = `/orbitdb/${postData.value[0]}/posts`; - const store = await orbitDB.orbitdb.keyvalue(fullAddress); + const store = await orbitDB.orbitdb.open(fullAddress, {type: 'keyvalue'}); await store.load(); const localOrbitData = store.get(postID); diff --git a/app/src/components/Topic.js b/app/src/components/Topic.js index e48bcb2..73a4cf7 100644 --- a/app/src/components/Topic.js +++ b/app/src/components/Topic.js @@ -54,9 +54,8 @@ class Topic extends Component { const orbitData = orbitDB.topicsDB.get(topicID); topicSubject = orbitData.subject; } else { - const fullAddress = `/orbitdb/${topicData.value[0] - }/topics`; - const store = await orbitDB.orbitdb.keyvalue(fullAddress); + const fullAddress = `/orbitdb/${topicData.value[0]}/topics`; + const store = await orbitDB.orbitdb.open(fullAddress, {type: 'keyvalue'}); await store.load(); const localOrbitData = store.get(topicID); diff --git a/app/src/config/ipfsOptions.js b/app/src/config/ipfsOptions.js index 706a11c..ed1e642 100644 --- a/app/src/config/ipfsOptions.js +++ b/app/src/config/ipfsOptions.js @@ -13,6 +13,9 @@ const ipfsOptions = { '/ip4/127.0.0.1/tcp/9090/ws/p2p-websocket-star' ] } + }, + preload: { + enabled: false } }; diff --git a/app/src/redux/actions/orbitActions.js b/app/src/redux/actions/orbitActions.js index 9d1f55e..2e2005f 100644 --- a/app/src/redux/actions/orbitActions.js +++ b/app/src/redux/actions/orbitActions.js @@ -2,6 +2,7 @@ const IPFS_INITIALIZED = 'IPFS_INITIALIZED'; const DATABASES_CREATED = 'DATABASES_CREATED'; const DATABASES_LOADED = 'DATABASES_LOADED'; const DATABASES_NOT_READY = 'DATABASES_NOT_READY'; +const UPDATE_PEERS = 'UPDATE_PEERS'; function updateDatabases(type, orbitdb, topicsDB, postsDB) { return { @@ -17,4 +18,5 @@ export { DATABASES_CREATED, DATABASES_LOADED, DATABASES_NOT_READY, IPFS_INITIALIZED, + UPDATE_PEERS, updateDatabases }; diff --git a/app/src/redux/reducers/orbitReducer.js b/app/src/redux/reducers/orbitReducer.js index 77a4ea1..8374047 100644 --- a/app/src/redux/reducers/orbitReducer.js +++ b/app/src/redux/reducers/orbitReducer.js @@ -1,7 +1,9 @@ -import { DATABASES_CREATED, +import { + DATABASES_CREATED, DATABASES_LOADED, DATABASES_NOT_READY, - IPFS_INITIALIZED } from '../actions/orbitActions'; + IPFS_INITIALIZED, UPDATE_PEERS +} from '../actions/orbitActions'; const initialState = { ipfs: null, @@ -10,6 +12,8 @@ const initialState = { orbitdb: null, topicsDB: null, postsDB: null, + topicsDBPeers: [], + postsDBPeers: [], id: null }; @@ -48,6 +52,12 @@ const orbitReducer = (state = initialState, action) => { postsDB: null, id: null }; + case UPDATE_PEERS: + return { + ...state, + topicsDBPeers: action.topicsDBPeers, + postsDBPeers: action.postsDBPeers + }; default: return state; } diff --git a/app/src/redux/sagas/orbitSaga.js b/app/src/redux/sagas/orbitSaga.js index b785c6a..3bd9f92 100644 --- a/app/src/redux/sagas/orbitSaga.js +++ b/app/src/redux/sagas/orbitSaga.js @@ -1,8 +1,9 @@ -import { all, call, put, take, takeLatest } from 'redux-saga/effects'; +import { all, call, put, select, take, takeEvery, takeLatest } from 'redux-saga/effects'; +import isEqual from 'lodash.isequal'; import { contract, getCurrentAccount } from './drizzleUtilsSaga'; import { loadDatabases } from '../../utils/orbitUtils'; import { DRIZZLE_UTILS_SAGA_INITIALIZED } from '../actions/drizzleUtilsActions'; -import { DATABASES_NOT_READY, IPFS_INITIALIZED } from '../actions/orbitActions'; +import { DATABASES_NOT_READY, IPFS_INITIALIZED, UPDATE_PEERS } from '../actions/orbitActions'; let latestAccount; @@ -19,8 +20,6 @@ function* getOrbitDBInfo() { address: account }); if (callResult) { - // console.log("Deleting local storage.."); - // localStorage.clear(); const txObj2 = yield call(contract.methods.getOrbitIdentityInfo, ...[account]); const orbitIdentityInfo = yield call(txObj2.call, { @@ -51,12 +50,29 @@ function* getOrbitDBInfo() { } } +function* updatePeersState() { + const orbit = yield select(state => state.orbit); + if(orbit.ready){ + const topicsDBAddress = orbit.topicsDB.address.toString(); + const postsDBAddress = orbit.postsDB.address.toString(); + const topicsDBPeers = yield call(orbit.ipfs.pubsub.peers, topicsDBAddress); + const postsDBPeers = yield call(orbit.ipfs.pubsub.peers, postsDBAddress); + if(!isEqual(topicsDBPeers.sort(), orbit.topicsDBPeers.sort()) || + !isEqual(postsDBPeers.sort(), orbit.postsDBPeers.sort())){ + yield put({ + type: UPDATE_PEERS, topicsDBPeers, postsDBPeers + }); + } + } +} + function* orbitSaga() { yield all([ take(DRIZZLE_UTILS_SAGA_INITIALIZED), take(IPFS_INITIALIZED) ]); yield takeLatest('ACCOUNT_CHANGED', getOrbitDBInfo); + yield takeEvery('ACCOUNTS_FETCHED', updatePeersState); } export default orbitSaga; diff --git a/app/src/utils/orbitUtils.js b/app/src/utils/orbitUtils.js index f3a099f..a4422e5 100644 --- a/app/src/utils/orbitUtils.js +++ b/app/src/utils/orbitUtils.js @@ -17,6 +17,8 @@ function initIPFS() { } async function createDatabases() { + console.log("Deleting local storage..."); // Else we are in danger of reusing an existing orbit + localStorage.clear(); // Perhaps not needed at all when orbit ids are used in Orbit 0.20.x+ console.log('Creating databases...'); const ipfs = getIPFS(); const orbitdb = await new OrbitDB(ipfs);