Skip to content

Commit

Permalink
Merge pull request #234 from BrightID/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
abramsymons authored May 30, 2021
2 parents ff13c1b + d3fbb8d commit f6c9735
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 23 deletions.
10 changes: 10 additions & 0 deletions updater/sponsorships.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
variables = db['variables']
contexts = db['contexts']
sponsorships = db['sponsorships']
testblocks = db['testblocks']


def get_events(app):
Expand Down Expand Up @@ -59,6 +60,15 @@ def update():
print('checking sponsored\tapp_name: {0}, context_id: {1}'.format(
app['_key'], context_id))

# remove testblocks if exists
tblocks = testblocks.find({
'contextId': context_id,
'action': 'sponsorship',
'app': app['_key']
}).batch()
for tblock in tblocks:
testblocks.delete(tblock)

tsponsorships = app['totalSponsorships']
usponsorships = sponsorships.find(
{'_to': 'apps/{0}'.format(app['_key'])}).count()
Expand Down
31 changes: 24 additions & 7 deletions updater/test_sponsorships.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import os
os.environ['BN_UPDATER_SP_ADDRESS'] = '0xFB32926d0A1e2082D12426B2854cb0c945AAF7c6'
os.environ['BN_UPDATER_SP_INFURA_URL'] = ''
os.environ['BN_UPDATER_MAINNET_WSS'] = ''
os.environ['BN_UPDATER_IDCHAIN_WSS'] = 'wss://idchain.one/ws/'
os.environ['BN_UPDATER_SEED_VOTING_ADDRESS'] = '0x56741DbC203648983c359A48aaf68f25f5550B6a'
os.environ['BN_UPDATER_SP_ADDRESS_MAINNET'] = '0x0aB346a16ceA1B1363b20430C414eAB7bC179324'
os.environ['BN_UPDATER_SP_ADDRESS_IDCHAIN'] = '0x183C5D2d1E43A3aCC8a977023796996f8AFd2327'
os.environ['BN_UPDATER_SEED_GROUPS_WS_URL'] = 'wss://idchain.one/ws/'
os.environ['BN_ARANGO_PROTOCOL'] = 'http'
os.environ['BN_ARANGO_HOST'] = 'localhost'
os.environ['BN_ARANGO_PORT'] = '8529'

from eth_keys import keys
import unittest
Expand All @@ -10,6 +17,8 @@
import time
from web3 import Web3
from web3.middleware import geth_poa_middleware
import ed25519
import base64


class TestUpdate(unittest.TestCase):
Expand All @@ -19,12 +28,15 @@ def __init__(self, *args, **kwargs):
self.IDS_AS_HEX = True
self.GAS = 500 * 10**3
self.GAS_PRICE = 5 * 10**9
self.SPONSOR_EVENT_CONTRACT = '0xE8572C106662F7Da8D6734F99bADE6BAE33c5DB8'
self.SPONSOR_EVENT_CONTRACT = ''
self.CONTRACT_ABI = '[{"anonymous": false,"inputs": [{"indexed": true,"internalType": "address","name": "addr","type": "address"}],"name": "Sponsor","type": "event"},{"inputs": [{"internalType": "address","name": "addr","type": "address"}],"name": "sponsor","outputs": [],"stateMutability": "nonpayable","type": "function"}]'
self.PRIVATE_KEY = 'EEBED6AE74B73BE44F4706222344E1D90363F64DA2C31B58B29F7A39EB6BFB43'
self.PRIVATE_KEY = ''
self.APP = ''.join(random.choices(string.ascii_uppercase, k=5))
self.USER = 'v7vS3jEqXazNUWj-5QXmrBL8x5XCp3EksF7uVGlijll'
self.WS_PROVIDER = 'wss://rinkeby.infura.io/ws/v3/588bb93634084be69f62f302a279d76f'
private, public = ed25519.create_keypair()
public = base64.b64encode(public.to_bytes()).decode('ascii')
private = base64.b64encode(private.to_bytes()).decode('ascii')
self.USER = public.strip('=').replace('/', '_').replace('+', '-')
self.WS_PROVIDER = ''
self.w3 = Web3(Web3.WebsocketProvider(
self.WS_PROVIDER, websocket_kwargs={'timeout': 60}))
self.w3.middleware_onion.inject(geth_poa_middleware, layer=0)
Expand All @@ -48,6 +60,8 @@ def __init__(self, *args, **kwargs):
'verification': self.APP,
'wsProvider': self.WS_PROVIDER,
'sponsorEventContract': self.SPONSOR_EVENT_CONTRACT,
'sponsorPublicKey': public,
'sponsorPrivateKey': private,
'totalSponsorships': 2,
'idsAsHex': self.IDS_AS_HEX
}
Expand Down Expand Up @@ -123,7 +137,7 @@ def send_transaction(self, func):
raw_transaction = signed.rawTransaction.hex()
tx_hash = self.w3.eth.sendRawTransaction(raw_transaction).hex()
rec = self.w3.eth.waitForTransactionReceipt(tx_hash)
return {'status': rec['status'], 'tx_hash': tx_hash}
print(f"Sponsor Transaction: status: {rec['status']}\ttx_hash: {tx_hash}")

def sponsor(self, context_id):
func = self.contract.functions.sponsor(context_id)
Expand All @@ -141,10 +155,13 @@ def test_sponsors(self):
'value': lb - 1
})
sponsorships.update()
# wait for the operation
time.sleep(60)
self.assertFalse(self.sponsorships.find(
{'_from': 'users/{}'.format(self.USER)}).empty())
self.assertTrue(self.testblocks.find(
{'contextId': self.CONTEXT_ID}).empty())


if __name__ == '__main__':
unittest.main()
Binary file modified web_services/foxx/apply5.zip
Binary file not shown.
3 changes: 2 additions & 1 deletion web_services/foxx/brightid/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,8 @@ function appToDic(app) {
logo: app.logo,
url: app.url,
assignedSponsorships: app.totalSponsorships,
unusedSponsorships: unusedSponsorships(app._key)
unusedSponsorships: unusedSponsorships(app._key),
testing: app.testing
};
}

Expand Down
33 changes: 23 additions & 10 deletions web_services/foxx/brightid/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,11 @@ const handlers = {
const verification = req.param('verification');
const app = db.getApp(appKey);
const context = db.getContext(app.context);
if (context.idsAsHex) {
contextId = contextId.toLowerCase();
}
const testblocks = db.getTestblocks(appKey, contextId);

if (testblocks.includes('link')) {
throw new errors.ContextIdNotFoundError(contextId);
} else if (testblocks.includes('sponsorship')) {
Expand All @@ -230,10 +234,6 @@ const handlers = {
throw new errors.NotVerifiedError(contextId, appKey);
}

if (context.idsAsHex) {
contextId = contextId.toLowerCase();
}

const coll = arango._collection(context.collection);
const user = db.getUserByContextId(coll, contextId);
if (! user) {
Expand Down Expand Up @@ -353,9 +353,7 @@ const handlers = {
},

allAppsGet: function(req, res){
const apps = db.getApps().filter(
app => !app.testing
).map(app => db.appToDic(app));
const apps = db.getApps().map(app => db.appToDic(app));
apps.sort((app1, app2) => {
const used1 = app1.assignedSponsorships - app1.unusedSponsorships;
const unused1 = app1.unusedSponsorships;
Expand All @@ -379,28 +377,43 @@ const handlers = {
testblocksPut: function(req, res){
const appKey = req.param('app');
const action = req.param('action');
const contextId = req.param('contextId');
let contextId = req.param('contextId');
const testingKey = req.param('testingKey');

const app = db.getApp(appKey);
if (app.testingKey != testingKey) {
throw new errors.InvalidTestingKeyError();
}
const context = db.getContext(app.context);
if (context.idsAsHex) {
const re = new RegExp(/^0[xX][A-Fa-f0-9]+$/);
if(!re.test(contextId)) {
throw new errors.InvalidContextIdError(contextId);
}
contextId = contextId.toLowerCase();
}

return db.addTestblock(contextId, action, appKey);
},

testblocksDelete: function(req, res){
const appKey = req.param('app');
const action = req.param('action');
const contextId = req.param('contextId');
let contextId = req.param('contextId');
const testingKey = req.param('testingKey');

const app = db.getApp(appKey);
if (app.testingKey != testingKey) {
throw new errors.InvalidTestingKeyError();
}

const context = db.getContext(app.context);
if (context.idsAsHex) {
const re = new RegExp(/^0[xX][A-Fa-f0-9]+$/);
if(!re.test(contextId)) {
throw new errors.InvalidContextIdError(contextId);
}
contextId = contextId.toLowerCase();
}
return db.removeTestblock(contextId, action, appKey);
},

Expand Down
2 changes: 1 addition & 1 deletion web_services/foxx/brightid/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "BrightID-Node",
"description": "Read and update the anonymous social graph stored on BrightID nodes.",
"license": "ISC",
"version": "5.9.6",
"version": "5.9.7",
"tests": [
"tests/*.js"
],
Expand Down
2 changes: 1 addition & 1 deletion web_services/foxx/brightid/manifest_apply.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"main": "apply.js",
"name": "apply",
"description": "Allows BrightID consensus module to apply operations to the database.",
"version": "5.9.6",
"version": "5.9.7",
"scripts": {
"setup": "initdb.js"
}
Expand Down
4 changes: 2 additions & 2 deletions web_services/foxx/brightid/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion web_services/foxx/brightid/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "brightid-foxx",
"description": "Foxx service for managing BrightID connections",
"version": "5.9.6",
"version": "5.9.7",
"dependencies": {
"base64-js": "^1.3.0",
"crypto-js": "^3.1.9-1",
Expand Down
1 change: 1 addition & 0 deletions web_services/foxx/brightid/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ schemas = Object.assign({
url: joi.string().description('the base url for the app'),
assignedSponsorships: joi.number().integer().description('number of assigned sponsorships'),
unusedSponsorships: joi.number().integer().description('number of unused sponsorships'),
testing: joi.boolean().required().description('true if the app in the testing mode'),
}),
}, schemas);

Expand Down
Binary file modified web_services/foxx/brightid5.zip
Binary file not shown.

0 comments on commit f6c9735

Please sign in to comment.