├── .gitattributes ├── .soliumignore ├── .gitignore ├── .gitmodules ├── migrations ├── 1_initial_migration.js └── 2_genesis_contracts_deploy.js ├── contracts ├── IStateReceiver.sol ├── test │ ├── TestStateReceiver.sol │ ├── TestBorValidatorSet.sol │ ├── TestSystem.sol │ └── TestCommitState.sol ├── System.sol ├── Migrations.sol ├── ValidatorVerifier.sol ├── ECVerify.sol ├── StateReceiver.sol ├── IterableMapping.sol ├── ValidatorSet.sol ├── BorValidatorSet.sol └── BorValidatorSet.template ├── .soliumrc.json ├── validators.json ├── validators.js.backup ├── scripts └── run-test.sh ├── generate.sh ├── package.json ├── .github └── workflows │ └── ci.yml ├── README.md ├── generate-borvalidatorset.js ├── genesis-template.json ├── generate-genesis.js ├── truffle-config.js └── test ├── BorValidatorSet.test.js └── StateReceiver.test.js /.gitattributes: -------------------------------------------------------------------------------- 1 | *.sol linguist-language=Solidity 2 | -------------------------------------------------------------------------------- /.soliumignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | contracts/Migrations.sol 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | build/ 3 | contracts/BorValidatorSet.sol 4 | 5 | pids 6 | logs 7 | genesis.json 8 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "bttc-contracts"] 2 | path = bttc-contracts 3 | url = https://github.com/bttcprotocol/contracts.git 4 | branch = /stake 5 | 6 | -------------------------------------------------------------------------------- /migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | const Migrations = artifacts.require("Migrations"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations); 5 | }; 6 | -------------------------------------------------------------------------------- /contracts/IStateReceiver.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.5.11; 2 | 3 | // IStateReceiver represents interface to receive state 4 | interface IStateReceiver { 5 | function onStateReceive(uint256 stateId, bytes calldata data) external; 6 | } -------------------------------------------------------------------------------- /.soliumrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "solium:recommended", 3 | "plugins": ["security"], 4 | "rules": { 5 | "quotes": ["error", "double"], 6 | "indentation": ["error", 2], 7 | "linebreak-style": ["error", "unix"] 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /contracts/test/TestStateReceiver.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.5.11; 2 | pragma experimental ABIEncoderV2; 3 | 4 | import {StateReceiver} from "../StateReceiver.sol"; 5 | import {TestSystem} from "./TestSystem.sol"; 6 | 7 | contract TestStateReceiver is StateReceiver, TestSystem {} 8 | -------------------------------------------------------------------------------- /contracts/test/TestBorValidatorSet.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.5.11; 2 | pragma experimental ABIEncoderV2; 3 | 4 | import {BorValidatorSet} from "../BorValidatorSet.sol"; 5 | import {TestSystem} from "./TestSystem.sol"; 6 | 7 | contract TestBorValidatorSet is BorValidatorSet, TestSystem {} 8 | -------------------------------------------------------------------------------- /contracts/System.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.5.11; 2 | 3 | contract System { 4 | address public constant SYSTEM_ADDRESS = 0xffffFFFfFFffffffffffffffFfFFFfffFFFfFFfE; 5 | 6 | modifier onlySystem() { 7 | require(msg.sender == SYSTEM_ADDRESS, "Not System Addess!"); 8 | _; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /contracts/test/TestSystem.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.5.11; 2 | 3 | contract TestSystem { 4 | address public dummySystem; 5 | 6 | function setSystemAddress(address _system) public { 7 | dummySystem = _system; 8 | } 9 | 10 | modifier onlySystem() { 11 | require(msg.sender == dummySystem, "Not System Addess!"); 12 | _; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /validators.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "address": "0xfA841eAAcf03598bAadF0266eF6097C654DE5465", 4 | "stake": 1, 5 | "balance": 300000000 6 | }, 7 | { 8 | "address": "0x80CFb197Be875eE45294AC31406E6483e3eAb02E", 9 | "stake": 1, 10 | "balance": 300000000 11 | }, 12 | { 13 | "address": "0x0aB3ab4542ED5FA2A67B2B8DAbC82C42162853A6", 14 | "stake": 1, 15 | "balance": 300000000 16 | } 17 | ] -------------------------------------------------------------------------------- /contracts/test/TestCommitState.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.5.11; 2 | pragma experimental ABIEncoderV2; 3 | 4 | 5 | contract TestCommitState { 6 | 7 | uint256 public id; 8 | bytes public data; 9 | 10 | function onStateReceive( 11 | uint256 _id, /* id */ 12 | bytes calldata _data 13 | ) external { 14 | (,,,uint256 num) = abi.decode(_data, (address, address, uint256, uint256)); 15 | // dummy loop 16 | for (uint256 i = 0; i < num; i++) { 17 | id = i; 18 | } 19 | id = _id; 20 | data = _data; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /validators.js.backup: -------------------------------------------------------------------------------- 1 | const validators = [ 2 | { 3 | address: "0x6c468CF8c9879006E22EC4029696E005C2319C9D", 4 | stake: 40, // without 10^18 5 | balance: 300000000 // without 10^18 6 | }, 7 | // { 8 | // address: "0x48aA8D4AF32551892FCF08Ad63Be7dD206D46F65", 9 | // stake: 30, 10 | // balance: 0 11 | // }, 12 | // { 13 | // address: "0x61083121D4b6ae002aF0CAD52359ae163e183Ccc", 14 | // stake: 20, 15 | // balance: 0 16 | // }, 17 | // { 18 | // address: "0xaFA4EE2EB4707e51Be14dcf182a03e0C9302BA2C", 19 | // stake: 10, 20 | // balance: 0 21 | // } 22 | ] 23 | 24 | exports = module.exports = validators 25 | -------------------------------------------------------------------------------- /contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.5.2; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint256 public last_completed_migration; 6 | 7 | modifier restricted() { 8 | if (msg.sender == owner) _; 9 | } 10 | 11 | constructor() public { 12 | owner = msg.sender; 13 | } 14 | 15 | function setCompleted(uint256 completed) public restricted { 16 | last_completed_migration = completed; 17 | } 18 | 19 | function upgrade(address new_address) public restricted { 20 | Migrations upgraded = Migrations(new_address); 21 | upgraded.setCompleted(last_completed_migration); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /scripts/run-test.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Exit script as soon as a command fails. 4 | set -o errexit 5 | 6 | # Executes cleanup function at script exit. 7 | trap cleanup EXIT 8 | 9 | # get current directory 10 | PWD=$(pwd) 11 | 12 | cleanup() { 13 | echo "Cleaning up" 14 | pkill -f ganache-cli 15 | echo "Done" 16 | } 17 | 18 | start_testrpc() { 19 | npm run testrpc > /dev/null & 20 | sleep 5 21 | } 22 | 23 | 24 | echo "Starting our own testrpc instance" 25 | start_testrpc 26 | 27 | npm run truffle:migrate "$@" 28 | 29 | if [ "$SOLIDITY_COVERAGE" = true ]; then 30 | npm run truffle:coverage "$@" 31 | else 32 | npm run truffle:test "$@" 33 | fi 34 | -------------------------------------------------------------------------------- /generate.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # Usage: 4 | # generate.sh 1029 delivery-1029 5 | 6 | set -x #echo on 7 | 8 | if [ -z "$1" ] 9 | then 10 | echo "Bttc chain id is required first argument" 11 | exit 1 12 | fi 13 | 14 | if [ -z "$2" ] 15 | then 16 | echo "Delivery chain id is required as second argument" 17 | exit 1 18 | fi 19 | 20 | npm install 21 | npm run truffle:compile 22 | git submodule init 23 | git submodule update 24 | cd bttc-contracts 25 | npm install 26 | node scripts/process-templates.js --bttc-chain-id $1 27 | npm run truffle:compile 28 | cd .. 29 | node generate-borvalidatorset.js --bttc-chain-id $1 --delivery-chain-id $2 30 | npm run truffle:compile 31 | node generate-genesis.js --bttc-chain-id $1 --delivery-chain-id $2 32 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "genesis-contracts", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "testrpc": "ganache-cli -p 8545 --networkId 15001 --mnemonic 'clock radar mass judge dismiss just intact mind resemble fringe diary casino' --gasLimit 13000000", 8 | "truffle": "truffle", 9 | "truffle:test": "truffle test", 10 | "truffle:compile": "truffle compile", 11 | "truffle:migrate": "truffle migrate", 12 | "test": "truffle test", 13 | "test:ci": "bash scripts/run-test.sh", 14 | "lint": "npm run lint:sol && npm run lint:js", 15 | "lint:js": "eslint test/**/*.js", 16 | "lint:sol:fix": "solium -d contracts/ --fix", 17 | "lint:sol": "solium -d contracts/" 18 | }, 19 | "author": "", 20 | "license": "MIT", 21 | "dependencies": { 22 | "bluebird": "^3.5.5", 23 | "chai": "4.2.0", 24 | "chai-as-promised": "^7.1.1", 25 | "commander": "^3.0.1", 26 | "nunjucks": "^3.2.0", 27 | "openzeppelin-solidity": "2.2.0", 28 | "solidity-rlp": "^2.0.0", 29 | "truffle": "^5.0.34", 30 | "web3": "^1.2.4", 31 | "ganache-cli": "^6.8.0-istanbul.0" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: 6 | - develop 7 | - master 8 | pull_request: 9 | branches: 10 | - develop 11 | - master 12 | 13 | jobs: 14 | build: 15 | runs-on: ubuntu-latest 16 | steps: 17 | - uses: actions/checkout@v2 18 | - name: Update Path 19 | run: echo "::add-path::$RUNNER_WORKSPACE/$(basename $GITHUB_REPOSITORY)" # Make it accessible from runner 20 | env: 21 | ACTIONS_ALLOW_UNSECURE_COMMANDS: 'true' 22 | - name: Install solc 23 | run: | 24 | set -x 25 | wget -c https://github.com/ethereum/solidity/releases/download/v0.5.12/solc-static-linux 26 | mv solc-static-linux solc 27 | chmod +x solc 28 | solc --version 29 | - name: Setup Node.js environment 30 | uses: actions/setup-node@v1.4.2 31 | env: 32 | ACTIONS_ALLOW_UNSECURE_COMMANDS: 'true' 33 | - name: Generate genesis file 34 | run: bash generate.sh 199 delivery-199 35 | env: 36 | ACTIONS_ALLOW_UNSECURE_COMMANDS: 'true' 37 | - name: Run tests 38 | run: npm run test:ci 39 | env: 40 | ACTIONS_ALLOW_UNSECURE_COMMANDS: 'true' 41 | -------------------------------------------------------------------------------- /contracts/ValidatorVerifier.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.5.11; 2 | 3 | import { ValidatorSet } from "./ValidatorSet.sol"; 4 | 5 | 6 | contract ValidatorVerifier { 7 | address constant public validatorSet = 0x0000000000000000000000000000000000001000; 8 | 9 | /** 10 | * @dev Throws if called by any account other than the validator set. 11 | */ 12 | modifier onlyValidatorSetContract() { 13 | require(isValidatorSetContract(), "Verifiable: caller is not the verifiable contract"); 14 | _; 15 | } 16 | 17 | constructor () public { 18 | // Doesn't work since contract is in genesis 19 | } 20 | 21 | /** 22 | * @dev Returns true if the caller is the current validator set contract. 23 | */ 24 | function isValidatorSetContract() public view returns (bool) { 25 | return msg.sender == validatorSet; 26 | } 27 | 28 | // check if signer is validator 29 | function isValidator(address signer) public view returns (bool) { 30 | return ValidatorSet(validatorSet).isCurrentValidator(signer); 31 | } 32 | 33 | // check if signer is producer 34 | function isProducer(address signer) public view returns (bool) { 35 | return ValidatorSet(validatorSet).isCurrentProducer(signer); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # genesis-contracts 2 | 3 | #### Setup genesis 4 | 5 | Setup genesis whenever contracts get changed 6 | ### 1. Install dependencies and submodules 7 | ```bash 8 | $ npm install 9 | $ git submodule init 10 | $ git submodule update 11 | ``` 12 | 13 | ### 2. Compile Bttc contracts 14 | ```bash 15 | $ cd bttc-contracts 16 | $ npm install 17 | $ node scripts/process-templates.js --bttc-chain-id 18 | $ npm run truffle:compile 19 | $ cd .. 20 | ``` 21 | 22 | ### 3. Generate Bttc validator set sol file 23 | 24 | Following command will generate `BorValidatorSet.sol` file from `BorValidatorSet.template` file. 25 | 26 | ```bash 27 | # Generate bttc validator set using stake and balance 28 | # Modify validators.json before as per your need 29 | $ node generate-borvalidatorset.js --bttc-chain-id --delivery-chain-id 30 | ``` 31 | 32 | ### 4. Compile contracts 33 | ```bash 34 | $ npm run truffle:compile 35 | ``` 36 | 37 | ### 5. Generate genesis file 38 | 39 | Following command will generate `genesis.json` file from `genesis-template.json` file. 40 | 41 | ```bash 42 | # Generate genesis file 43 | $ node generate-genesis.js --bttc-chain-id --delivery-chain-id 44 | ``` 45 | 46 | ### 6. Run Tests 47 | ```bash 48 | $ npm run testrpc 49 | $ npm test 50 | ``` 51 | -------------------------------------------------------------------------------- /generate-borvalidatorset.js: -------------------------------------------------------------------------------- 1 | const program = require("commander") 2 | const fs = require("fs") 3 | const nunjucks = require("nunjucks") 4 | const web3 = require("web3") 5 | const validators = require("./validators") 6 | 7 | program.version("0.0.1") 8 | program.option("--bttc-chain-id ", "Bttc chain id", "1029") 9 | program.option( 10 | "--delivery-chain-id ", 11 | "Delivery chain id", 12 | "delivery-P5rXwg" 13 | ) 14 | program.option( 15 | "--first-end-block ", 16 | "End block for first span", 17 | "255" 18 | ) 19 | program.option( 20 | "-o, --output ", 21 | "BorValidatorSet.sol", 22 | "./contracts/BorValidatorSet.sol" 23 | ) 24 | program.option( 25 | "-t, --template