Skip to content

Commit

Permalink
NearProver:add scripts&tests for bridge upgradability (#647)
Browse files Browse the repository at this point in the history
* NearProver:add scripts&tests for bridge upgradability
* NearProver: wait receipt for 10 blocks on upgrade
* Add ledger support
* Apply eslint fixes and add a command to Makefile
* Update contracts/eth/nearprover/UPGRADE.md
* Change CLI line endings
* NearBridge: use local `utils` package
* Update yarn.lock files
* NearBridge: update tests `utils` package reference
* NearBridge: add tests for admin upgradability.
* NearBridge: hardhat config allow using empty PK.
* NearBridge: add scripts to upgrade an admin.

Co-authored-by: Marcelo Fornet <[email protected]>
Co-authored-by: Karim <[email protected]>
Co-authored-by: Karim <[email protected]>
  • Loading branch information
4 people authored Dec 18, 2021
1 parent 1a473b7 commit fb38f59
Show file tree
Hide file tree
Showing 19 changed files with 3,346 additions and 3,073 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.env
node_modules
artifacts
cache
coverage
Expand Down
1 change: 1 addition & 0 deletions cli/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node

const path = require('path')
const changeCase = require('change-case')
const { program } = require('commander')
Expand Down
2 changes: 1 addition & 1 deletion contracts/eth/nearbridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"ethereum-waffle": "^3.3.0",
"ethers": "^5.0.31",
"hardhat": "^2.1.1",
"rainbow-bridge-lib": "^2.0.0",
"rainbow-bridge-utils": "file:../../../utils",
"solc": "0.8.3",
"solidity-coverage": "^0.7.16",
"solium": "^1.2.5"
Expand Down
2 changes: 1 addition & 1 deletion contracts/eth/nearbridge/test/NearBridge.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { expect } = require('chai');

const fs = require('fs').promises;
const { borshify, borshifyInitialValidators } = require('rainbow-bridge-lib/rainbow/borsh');
const { borshify, borshifyInitialValidators } = require('rainbow-bridge-utils');

async function increaseTime(time) {
await network.provider.send('evm_increaseTime', [time]);
Expand Down
4 changes: 2 additions & 2 deletions contracts/eth/nearbridge/test/NearBridge2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');

const { borshify, borshifyInitialValidators } = require('rainbow-bridge-lib/rainbow/borsh')
const { borshify, borshifyInitialValidators } = require('rainbow-bridge-utils')

async function increaseTime(time) {
await network.provider.send('evm_increaseTime', [time]);
Expand Down Expand Up @@ -144,4 +144,4 @@ it('After challenge prev should be revert to prev epoch of latest valid block',

await NearBridge.challenge(ethers.constants.AddressZero, 0)
expect((await NearBridge.lastValidAt())).to.be.equal(0);
});
});
90 changes: 70 additions & 20 deletions contracts/eth/nearbridge/test/NearBridge3.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,77 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');

const { borshify, borshifyInitialValidators } = require('rainbow-bridge-lib/rainbow/borsh')

let Ed25519, NearBridge;

beforeEach(async function () {
Ed25519 = await (await ethers.getContractFactory('Ed25519')).deploy();
NearBridge = await (await ethers.getContractFactory('NearBridge')).deploy(
Ed25519.address,
ethers.BigNumber.from("1000000000000000000"), // 1e18
ethers.BigNumber.from("10"), // lock duration
ethers.BigNumber.from("20000000000"), // replace duration
await (await ethers.getSigners())[0].getAddress(),
0
);
await NearBridge.deposit({ value: ethers.utils.parseEther('1') });
});
const { borshify, borshifyInitialValidators } = require('rainbow-bridge-utils')


describe('New tests', () => {
let Ed25519;
let NearBridge;
let AdminWallet;

beforeEach(async function () {
Ed25519 = await (await ethers.getContractFactory('Ed25519')).deploy();
[AdminWallet] = await ethers.getSigners();
NearBridge = await (await ethers.getContractFactory('NearBridge')).deploy(
Ed25519.address,
ethers.BigNumber.from("1000000000000000000"), // 1e18
ethers.BigNumber.from("10"), // lock duration
ethers.BigNumber.from("20000000000"), // replace duration
await AdminWallet.getAddress(),
0
);
await NearBridge.deposit({ value: ethers.utils.parseEther('1') });
});

it('Verify init with validators V1', async function () {
const initialValidators = borshifyInitialValidators(require('./block_validators_v1_testnet.json').next_bps);
console.log(`Initial validators: ${initialValidators.toString('hex')}`);
it('Verify init with validators V1', async function () {
const initialValidators = borshifyInitialValidators(require('./block_validators_v1_testnet.json').next_bps);

expect(await NearBridge.initWithValidators(borshifyInitialValidators(require('./block_validators_v1_testnet.json').next_bps)));
expect(await NearBridge.initWithValidators(borshifyInitialValidators(require('./block_validators_v1_testnet.json').next_bps)));
});
});


describe('NearBridge with admin access', () => {
const ADMIN_ADDRESS_SLOT = 0;
let nearBridge;
let adminAccount;

beforeEach(async () => {
ed25519 = await (await ethers.getContractFactory('Ed25519')).deploy();
[deployerAccount] = await ethers.getSigners();

// Make the deployer admin
adminAccount = deployerAccount;

nearBridge = await (await ethers.getContractFactory('NearBridge')).deploy(
ed25519.address,
ethers.BigNumber.from("1000000000000000000"), // 1e18
ethers.BigNumber.from("10"), // lock duration
ethers.BigNumber.from("20000000000"), // replace duration
await adminAccount.getAddress(),
0
);
});

describe('Upgradability', async () => {
it('should upgrade the admin address from the provided hex string', async () => {
const initialAdminAddress = await nearBridge.admin();
expect(initialAdminAddress)
.to
.equal(await adminAccount.address);

const newAdminAddress = '0x0123456789abcdefcafedeadbeefbea77a1de456';
expect(newAdminAddress)
.not
.equal(initialAdminAddress);

// Mask matches only on the latest 20 bytes (to store the address)
const mask = ethers.BigNumber.from("0x000000000000000000000000ffffffffffffffffffffffffffffffffffffffff");
nearBridge.adminSstoreWithMask(ADMIN_ADDRESS_SLOT, newAdminAddress, mask);

expect((await nearBridge.admin()).toLowerCase())
.to
.equal(newAdminAddress);
});
});
});
Loading

0 comments on commit fb38f59

Please sign in to comment.