Skip to content

Commit

Permalink
Merge pull request #9 from tgrospic/master
Browse files Browse the repository at this point in the history
Cleanup JS code, added Docker configuration, added read example
  • Loading branch information
tgrospic authored Dec 22, 2021
2 parents bf7ab7d + 304acbf commit b5c9a34
Show file tree
Hide file tree
Showing 29 changed files with 4,586 additions and 1,541 deletions.
29 changes: 29 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# RNode configuration (used with Docker compose and scripts)
# ---------------------------------------------------------------------------------------------------

# RNode Docker image

RNODE_IMAGE=rchain/rnode:v0.12.4

# RNode host address

MY_NET_IP=127.0.0.1

# Bootstrap node

VALIDATOR_BOOT_PRIVATE=bb6f30056d1981b98e729cef72a82920e6242a4395e500bd24bd6c6e6a65c36c
VALIDATOR_BOOT_ADDRESS="rnode://ebffd419dea60220734ccea8875e86d87bac10a7@boot?protocol=40400&discovery=40404"

# Zulip config

POSTGRES_PASSWORD="12345"
MEMCACHED_PASSWORD="12345"
RABBITMQ_PASSWORD="12345"
REDIS_PASSWORD="12345"
ZULIP_SECRET_KEY="12345"

# Fill in URI result from `npm run iddb-deploy` e.g. "rho:id:6cb36epk1fuhg9gws8c95d3cyeju63a83r8qeste5jw17oda477srf"
IDDB_CONTRACT_URI=""

# Fill in URI result from `npm run myzulipdb-deploy` e.g. "rho:id:6cb36epk1fuhg9gws8c95d3cyeju63a83r8qeste5jw17oda477srf"
DB_CONTRACT_URI=""
113 changes: 6 additions & 107 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,107 +1,6 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# emacs backups
*~
node_modules
dist/
.parcel-cache/
.gen
rnode-grpc-*
data
94 changes: 94 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# RChat - RChain support to decentralize chat (Zulip)

Show case example how to make chat applications like Zulip decentralized with on-chain data.

## Goal

1. Extract chat messages from Zulip and store it on-chain.
1. Read on-chain tables, keys and messages.
1. _TODO: Import messages to Zulip_.

## Install nodejs dependencies

```sh
npm install
```

## Run local Zulip server with RChain nodes

Control of the whole process is defined with `npm` scripts in [package.json](package.json).

The first step is to start Zulip with its dependencies and RNode instances which will store on-chain data. Starting from fresh it will take a few minutes to complete. Check _docker-compose_ logs and web access for more info.

_TODO: Read-only RNode is defined in configuration but not currently used. Additional info should be provided how to read exported on-chain data._

```sh
npm run start-docker
```

To control docker-compose services which includes both [docker-compose.yml](docker-compose.yml) and [docker-compose-zulip.yml](docker-compose-zulip.yml) this npm script can be used.

```sh
# Get logs from all containers
npm run dc -- logs -f

# Get logs only from bootstrap RNode
npm run dc -- logs -f boot
```

### Check when Zulip and RNode are started from the browser

Zulip: [https://localhost:1443/](https://localhost:1443/).
RNode: [http://localhost:40403/status](http://localhost:40403/status).

# Configure initial Zulip account (organization)

To login to a locally started Zulip instance it's necessary to follow the Zulip procedure and create an organization with an initial account.

This command will print a temporary link to access the initial Zulip configuration. Follow the link, create your account and login to Zulip server.

```sh
npm run zulip-gen-org-link
```

## Configure smart contract to store chat messages

This step is necessary to execute only once when RChain nodes are started from a fresh state.

Creates a main contract with operations to insert DB data on-chain.

**Result of these deploys will produce URI output which must be updated in [**.env**](.env) file.**

```sh
npm run iddb-deploy
```

After __.env__ file is updated with __IDDB_CONTRACT_URI__ execute command to create a contract for Zulip DB.

```sh
npm run myzulipdb-deploy
```

Update __DB_CONTRACT_URI__ in __.env__ file which is used as part of private channel for insert operations ([chain_replica.mjs](chain_replica.mjs)) and to get read-only channel for read operations ([read-db.mjs](read-db.mjs)).

_NOTE: Whenever `iddb-deploy` and `myzulipdb-deploy` are deployed again the corresponding record in __.env__ file must be updated._

## Start JS script to process new messages from Zulip and import on-chain

```sh
npm start
```

## Stop containers and clean data

Stop and remove all containers (data stored on disk will not be touched).

```sh
npm run dc -- down
```

Delete all data on disk. `sudo` is needed because RNode uses _root_ user inside the container, so the mounted folder will be owned by _root_.

```sh
sudo rm -R data
```
1 change: 1 addition & 0 deletions data-init/genesis/bonds.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
048c024adca3706ed6460a895ec7608b4217a1b1d080dd5259e41d9c55e21a875aa10b438e144b679ef202a350861011418fa6bee3128300d9b4cbdc4971a207fa 1234567890123
1 change: 1 addition & 0 deletions data-init/genesis/wallets.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
11113y7AfYj7hShN49oAHHd3KiWxZRsodesdBi8QwSrPR5Veyh77S,100000000000,0 # Start network with this REV balances - only used on genesis
10 changes: 10 additions & 0 deletions data-init/node.certificate.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-----BEGIN CERTIFICATE-----
MIIBXjCCAQKgAwIBAgIIRgmpOyKqJiIwDAYIKoZIzj0EAwIFADAzMTEwLwYDVQQD
EyhlYmZmZDQxOWRlYTYwMjIwNzM0Y2NlYTg4NzVlODZkODdiYWMxMGE3MB4XDTE5
MTExNjE0MTcwMVoXDTIwMTExNTE0MTcwMVowMzExMC8GA1UEAxMoZWJmZmQ0MTlk
ZWE2MDIyMDczNGNjZWE4ODc1ZTg2ZDg3YmFjMTBhNzBZMBMGByqGSM49AgEGCCqG
SM49AwEHA0IABOMsflowoPMdm4WV5E/sjWVUwQZ0TcKBJNqbMzBwFTHIeTtXfjkz
+OkeMZa1gK7tNm+9XkTa2eaoCiGF8lsgXQkwDAYIKoZIzj0EAwIFAANIADBFAiBO
38RcQxpi0UZ+UlEJGbjiBNMkwOENmP0vKxF54+4skAIhAIHr7dMrvev5Fd/tESmi
VMI7KJh06qdcafM0sx8MSYLr
-----END CERTIFICATE-----
4 changes: 4 additions & 0 deletions data-init/node.key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-----BEGIN PRIVATE KEY-----
MEECAQAwEwYHKoZIzj0CAQYIKoZIzj0DAQcEJzAlAgEBBCC964NyCbeGDW3INKBd
ML9dTLwZxaFw3N74g9VV5PPckw==
-----END PRIVATE KEY-----
100 changes: 100 additions & 0 deletions docker-compose-zulip.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
version: '2.3'

services:
database:
image: "zulip/zulip-postgresql:10"
environment:
POSTGRES_DB: "zulip"
POSTGRES_USER: "zulip"
# Note that you need to do a manual `ALTER ROLE` query if you
# change this on a system after booting the postgres container
# the first time on a host. Instructions are available in README.md.
POSTGRES_PASSWORD: $POSTGRES_PASSWORD
volumes:
- ./data/postgresql:/var/lib/postgresql/data:rw
ports:
- 5432:5432

memcached:
image: "memcached:alpine"
command:
- "sh"
- "-euc"
- |
echo 'mech_list: plain' > "$$SASL_CONF_PATH"
echo "zulip@$$HOSTNAME:$$MEMCACHED_PASSWORD" > "$$MEMCACHED_SASL_PWDB"
echo "zulip@localhost:$$MEMCACHED_PASSWORD" >> "$$MEMCACHED_SASL_PWDB"
exec memcached -S
environment:
SASL_CONF_PATH: "/home/memcache/memcached.conf"
MEMCACHED_SASL_PWDB: "/home/memcache/memcached-sasl-db"
MEMCACHED_PASSWORD: $MEMCACHED_PASSWORD
restart: always

rabbitmq:
image: "rabbitmq:3.7.7"
restart: always
environment:
RABBITMQ_DEFAULT_USER: "zulip"
RABBITMQ_DEFAULT_PASS: $RABBITMQ_PASSWORD
volumes:
- ./data/rabbitmq:/var/lib/rabbitmq:rw

redis:
image: "redis:alpine"
command:
- "sh"
- "-euc"
- |
echo "requirepass '$$REDIS_PASSWORD'" > /etc/redis.conf
exec redis-server /etc/redis.conf
environment:
REDIS_PASSWORD: $REDIS_PASSWORD
volumes:
- ./data/redis:/data:rw

zulip:
image: "zulip/docker-zulip:4.7-0"
# build:
# context: .
# args:
# # Change these if you want to build zulip from a different repo/branch
# ZULIP_GIT_URL: https://github.com/zulip/zulip.git
# ZULIP_GIT_REF: "4.7"
# # Set this up if you plan to use your own CA certificate bundle for building
# # CUSTOM_CA_CERTIFICATES:
ports:
- 1443:443
environment:
DB_HOST: "database"
DB_HOST_PORT: "5432"
DB_USER: "zulip"
SSL_CERTIFICATE_GENERATION: "self-signed"
SETTING_MEMCACHED_LOCATION: "memcached:11211"
SETTING_RABBITMQ_HOST: "rabbitmq"
SETTING_REDIS_HOST: "redis"
SECRETS_email_password: "123456789"
# These should match RABBITMQ_DEFAULT_PASS, POSTGRES_PASSWORD,
# MEMCACHED_PASSWORD, and REDIS_PASSWORD above.
SECRETS_rabbitmq_password: $RABBITMQ_PASSWORD
SECRETS_postgres_password: $POSTGRES_PASSWORD
SECRETS_memcached_password: $MEMCACHED_PASSWORD
SECRETS_redis_password: $REDIS_PASSWORD
SECRETS_secret_key: $ZULIP_SECRET_KEY
SETTING_EXTERNAL_HOST: "localhost:1443"
SETTING_ZULIP_ADMINISTRATOR: "[email protected]"
SETTING_EMAIL_HOST: "" # e.g. smtp.example.com
SETTING_EMAIL_HOST_USER: "[email protected]"
SETTING_EMAIL_PORT: "587"
# It seems that the email server needs to use ssl or tls and can't be used without it
SETTING_EMAIL_USE_SSL: "False"
SETTING_EMAIL_USE_TLS: "True"
ZULIP_AUTH_BACKENDS: "EmailAuthBackend"
# Uncomment this when configuring the mobile push notifications service
# SETTING_PUSH_NOTIFICATION_BOUNCER_URL: 'https://push.zulipchat.com'
volumes:
- ./data/zulip:/data:rw
ulimits:
nofile:
soft: 1000000
hard: 1048576
Loading

0 comments on commit b5c9a34

Please sign in to comment.