mirror of https://gitlab.com/ecentrics/concordia
Apostolos Fanakis
4 years ago
14 changed files with 1732 additions and 1566 deletions
@ -1,5 +1,11 @@ |
|||||
export const FORUM_CONTRACT = 'Forum'; |
export const FORUM_CONTRACT = 'Forum'; |
||||
export const POST_VOTING_CONTRACT = 'PostVoting'; |
export const POST_VOTING_CONTRACT = 'PostVoting'; |
||||
export const VOTING_CONTRACT = 'Voting'; |
export const VOTING_CONTRACT = 'Voting'; |
||||
export const NUMBER_OF_CONTRACTS = 4; |
|
||||
export const NUMBER_OF_CONTRACTS = 4; |
const CONTRACTS = [ |
||||
|
FORUM_CONTRACT, |
||||
|
POST_VOTING_CONTRACT, |
||||
|
VOTING_CONTRACT, |
||||
|
]; |
||||
|
|
||||
|
export default CONTRACTS; |
||||
|
@ -1,47 +1,62 @@ |
|||||
// const downloadContractArtifacts = async () => {
|
import { |
||||
import { NUMBER_OF_CONTRACTS } from '../constants/contracts/ContractNames'; |
REACT_APP_CONTRACTS_SUPPLIER_HOST_DEFAULT, |
||||
|
REACT_APP_CONTRACTS_SUPPLIER_PORT_DEFAULT, REACT_APP_CONTRACTS_VERSION_HASH_DEFAULT, |
||||
|
} from '../constants/configuration/defaults'; |
||||
|
import CONTRACTS from '../constants/contracts/ContractNames'; |
||||
|
|
||||
|
function getContractsDownloadRequest() { |
||||
|
const CONTRACTS_SUPPLIER_HOST = process.env.REACT_APP_CONTRACTS_SUPPLIER_HOST |
||||
|
|| REACT_APP_CONTRACTS_SUPPLIER_HOST_DEFAULT; |
||||
|
const CONTRACTS_SUPPLIER_PORT = process.env.REACT_APP_CONTRACTS_SUPPLIER_PORT |
||||
|
|| REACT_APP_CONTRACTS_SUPPLIER_PORT_DEFAULT; |
||||
|
const CONTRACTS_VERSION_HASH = process.env.REACT_APP_CONTRACTS_VERSION_HASH |
||||
|
|| REACT_APP_CONTRACTS_VERSION_HASH_DEFAULT; |
||||
|
|
||||
export const downloadContractArtifacts = async () => { |
const xhrRequest = new XMLHttpRequest(); |
||||
const headers = new Headers(); |
|
||||
headers.append('Access-Control-Allow-Origin', 'http://localhost:7000'); |
|
||||
headers.append('Access-Control-Allow-Credentials', 'true'); |
|
||||
|
|
||||
const requestOptions = { |
xhrRequest.open('GET', |
||||
method: 'GET', |
`http://${CONTRACTS_SUPPLIER_HOST}:${CONTRACTS_SUPPLIER_PORT}/contracts/${CONTRACTS_VERSION_HASH}`, |
||||
redirect: 'follow', |
false); |
||||
headers, |
xhrRequest.setRequestHeader('Access-Control-Allow-Origin', 'http://localhost:7000'); |
||||
}; |
xhrRequest.setRequestHeader('Access-Control-Allow-Credentials', 'true'); |
||||
|
|
||||
const remoteContracts = await fetch('http://127.0.0.1:8400/contracts/asdf', requestOptions) |
return xhrRequest; |
||||
.then((response) => response.text()) |
} |
||||
.then((contractsRawData) => JSON.parse(contractsRawData)); |
|
||||
|
|
||||
if (remoteContracts.length !== NUMBER_OF_CONTRACTS) { |
function validateRemoteContracts(remoteContracts) { |
||||
|
if (remoteContracts.length !== CONTRACTS.length) { |
||||
throw new Error(`Version mismatch detected. Artifacts brought ${remoteContracts.length} contracts but app
|
throw new Error(`Version mismatch detected. Artifacts brought ${remoteContracts.length} contracts but app
|
||||
expected ${NUMBER_OF_CONTRACTS}`);
|
expected ${CONTRACTS.length}`);
|
||||
} |
} |
||||
|
|
||||
return remoteContracts; |
const contractsPresentStatus = CONTRACTS.map((contract) => ({ |
||||
}; |
contract, |
||||
|
present: remoteContracts.contains((remoteContract) => remoteContract.contractName === contract), |
||||
|
})); |
||||
|
|
||||
|
if (contractsPresentStatus.reduce((accumulator, contract) => accumulator && contract.present, true)) { |
||||
|
throw new Error(`Contracts missing from artifacts. Supplier didn't bring ${contractsPresentStatus |
||||
|
.filter((contractPresentStatus) => contractPresentStatus.present === false) |
||||
|
.map((contractPresentStatus) => contractPresentStatus.contract) |
||||
|
.join(', ')}.`);
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
const downloadContractArtifactsSync = () => { |
||||
|
const xhrRequest = getContractsDownloadRequest(); |
||||
|
|
||||
export const downloadContractArtifactsSync = () => { |
|
||||
const xhrRequest = new XMLHttpRequest(); |
|
||||
// xhrRequest.withCredentials = true;
|
|
||||
xhrRequest.open('GET', 'http://127.0.0.1:8400/contracts/asdf', false); |
|
||||
xhrRequest.setRequestHeader('Access-Control-Allow-Origin', 'http://localhost:7000'); |
|
||||
xhrRequest.setRequestHeader('Access-Control-Allow-Credentials', 'true'); |
|
||||
xhrRequest.send(null); |
xhrRequest.send(null); |
||||
|
|
||||
if (xhrRequest.status === 200) { |
if (xhrRequest.status === 200) { |
||||
const contractsRawData = xhrRequest.responseText; |
const contractsRawData = xhrRequest.responseText; |
||||
const remoteContracts = JSON.parse(contractsRawData); |
const remoteContracts = JSON.parse(contractsRawData); |
||||
|
|
||||
if (remoteContracts.length !== NUMBER_OF_CONTRACTS) { |
validateRemoteContracts(remoteContracts); |
||||
throw new Error(`Version mismatch detected. Artifacts brought ${remoteContracts.length} contracts but app
|
|
||||
expected ${NUMBER_OF_CONTRACTS}`);
|
|
||||
} |
|
||||
|
|
||||
return remoteContracts; |
return remoteContracts; |
||||
} |
} |
||||
|
|
||||
throw new Error(`Remote contract artifacts download request failed!\n${xhrRequest.responseText}`); |
throw new Error(`Remote contract artifacts download request failed!\n${xhrRequest.responseText}`); |
||||
}; |
}; |
||||
|
|
||||
|
export default downloadContractArtifactsSync; |
||||
|
@ -1,3 +1,11 @@ |
|||||
export const API_HOST = '127.0.0.1'; |
import path from 'path'; |
||||
export const API_PORT = '8400'; |
|
||||
export const UPLOADED_CONTRACTS_DIR = './contracts-uploads/'; |
const PROVIDER_PORT = '8400'; |
||||
|
const UPLOAD_CONTRACTS_DIRECTORY = path.join(__dirname, '..', 'contracts-uploads'); |
||||
|
const CORS_ALLOWED_ORIGINS = ['localhost:7000', '127.0.0.1:7000']; |
||||
|
|
||||
|
export default { |
||||
|
port: PROVIDER_PORT, |
||||
|
uploadsDirectory: UPLOAD_CONTRACTS_DIRECTORY, |
||||
|
corsAllowedOrigins: CORS_ALLOWED_ORIGINS, |
||||
|
}; |
||||
|
@ -0,0 +1,10 @@ |
|||||
|
import path from 'path'; |
||||
|
import constants from '../constants'; |
||||
|
|
||||
|
const getStorageLocation = (hash) => { |
||||
|
const UPLOADS_DIRECTORY = process.env.UPLOAD_CONTRACTS_DIRECTORY || constants.uploadsDirectory; |
||||
|
|
||||
|
return path.join(UPLOADS_DIRECTORY, hash); |
||||
|
}; |
||||
|
|
||||
|
export default getStorageLocation; |
@ -0,0 +1,31 @@ |
|||||
|
const path = require('path'); |
||||
|
const unirest = require('unirest'); |
||||
|
const { contracts } = require('../index'); |
||||
|
const defaults = require('../constants/config/defaults'); |
||||
|
|
||||
|
const uploadContractsToProviderUnirest = (versionHash, tag) => { |
||||
|
const CONTRACTS_PROVIDER_HOST = process.env.CONTRACTS_PROVIDER_HOST || defaults.contractsProviderHost; |
||||
|
const CONTRACTS_PROVIDER_PORT = process.env.CONTRACTS_PROVIDER_PORT || defaults.contractsProviderPort; |
||||
|
|
||||
|
const uploadPath = `http://${CONTRACTS_PROVIDER_HOST}:${CONTRACTS_PROVIDER_PORT}/contracts/${versionHash}`; |
||||
|
const req = unirest('POST', uploadPath); |
||||
|
|
||||
|
contracts |
||||
|
.forEach((contract) => req |
||||
|
.attach('contracts', path.join(__dirname, '../', 'build/', `${contract.contractName}.json`))); |
||||
|
|
||||
|
console.log(`Uploading to ${uploadPath}`); |
||||
|
req.end((res) => { |
||||
|
if (res.error) { |
||||
|
throw new Error(`Failed to upload contracts to provider: ${res.error}`); |
||||
|
} |
||||
|
|
||||
|
console.log('Contracts uploaded to provider.'); |
||||
|
}); |
||||
|
}; |
||||
|
|
||||
|
const main = () => { |
||||
|
uploadContractsToProviderUnirest(process.argv[2], process.argv[3]); |
||||
|
}; |
||||
|
|
||||
|
main(); |
File diff suppressed because it is too large
Loading…
Reference in new issue