Skip to content
This repository has been archived by the owner on Oct 20, 2020. It is now read-only.

Commit

Permalink
Merge pull request #16 from near/incident20200909
Browse files Browse the repository at this point in the history
Account for extra approvals in the last header of the epoch
  • Loading branch information
MaksymZavershynskyi authored Sep 11, 2020
2 parents 9953121 + 56c7b68 commit a3968ce
Show file tree
Hide file tree
Showing 23 changed files with 701 additions and 34 deletions.
2 changes: 0 additions & 2 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ steps:
git status
changed_files=$$(git status --porcelain --untracked-files=no | wc -l)
if [ $$changed_files -gt 0 ]; then
yarn --version
git diff
echo 'contract changed, please rebuild contract'
exit 1
fi
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
**/package-lock.json
*.tgz
**/.yarn*
.idea
1 change: 1 addition & 0 deletions nearbridge/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
coverage
coverage.json
build
.idea
3 changes: 2 additions & 1 deletion nearbridge/contracts/NearBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ contract NearBridge is INearBridge {
}

function _checkBp(NearDecoder.LightClientBlock memory nearBlock, BlockProducerInfo storage bpInfo) internal {
require(nearBlock.approvals_after_next.length == bpInfo.bpsLength, "NearBridge: number of BPs should match number of approvals");
require(nearBlock.approvals_after_next.length >= bpInfo.bpsLength, "NearBridge: number of approvals should be at least as large as number of BPs");

uint256 votedFor = 0;
for (uint i = 0; i < bpInfo.bpsLength; i++) {
Expand All @@ -195,6 +195,7 @@ contract NearBridge is INearBridge {
votedFor = votedFor.add(bpInfo.bps[i].stake);
}
}
// Last block in the epoch might contain extra approvals that light client can ignore.

require(votedFor > bpInfo.totalStake.mul(2).div(3), "NearBridge: Less than 2/3 voted by the block after next");
}
Expand Down
2 changes: 1 addition & 1 deletion nearbridge/dist/Borsh.full.bin
Original file line number Diff line number Diff line change
@@ -1 +1 @@
60556023600b82828239805160001a607314601657fe5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600080fdfea265627a7a72315820879e9b0ca7429abc157d7aed4686fe2993cdd1723f511ebdaa06c1cb9431d30964736f6c63430005100032
60556023600b82828239805160001a607314601657fe5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600080fdfea265627a7a72315820088e5b0296813c1e1c29a351db258df5946de2103682725b6985f6ac763bb37e64736f6c63430005100032
2 changes: 1 addition & 1 deletion nearbridge/dist/Borsh.full.sol
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ library Borsh {
}
}

function decodeBytes20(Data memory data) internal pure shift(data, 20) returns(bytes20 value) {
function decodeBytes20(Data memory data) internal pure returns(bytes20 value) {
for (uint i = 0; i < 20; i++) {
value |= bytes20(byte(decodeU8(data)) & 0xFF) >> (i * 8);
}
Expand Down
2 changes: 1 addition & 1 deletion nearbridge/dist/NearBridge.full.bin

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions nearbridge/dist/NearBridge.full.sol
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ library Borsh {
}
}

function decodeBytes20(Data memory data) internal pure shift(data, 20) returns(bytes20 value) {
function decodeBytes20(Data memory data) internal pure returns(bytes20 value) {
for (uint i = 0; i < 20; i++) {
value |= bytes20(byte(decodeU8(data)) & 0xFF) >> (i * 8);
}
Expand Down Expand Up @@ -1998,7 +1998,7 @@ contract NearBridge is INearBridge {
}

function _checkBp(NearDecoder.LightClientBlock memory nearBlock, BlockProducerInfo storage bpInfo) internal {
require(nearBlock.approvals_after_next.length == bpInfo.bpsLength, "NearBridge: number of BPs should match number of approvals");
require(nearBlock.approvals_after_next.length >= bpInfo.bpsLength, "NearBridge: number of approvals should be at least as large as number of BPs");

uint256 votedFor = 0;
for (uint i = 0; i < bpInfo.bpsLength; i++) {
Expand All @@ -2007,6 +2007,7 @@ contract NearBridge is INearBridge {
votedFor = votedFor.add(bpInfo.bps[i].stake);
}
}
// Last block in the epoch might contain extra approvals that light client can ignore.

require(votedFor > bpInfo.totalStake.mul(2).div(3), "NearBridge: Less than 2/3 voted by the block after next");
}
Expand Down
2 changes: 1 addition & 1 deletion nearbridge/dist/NearDecoder.full.bin
Original file line number Diff line number Diff line change
@@ -1 +1 @@
60556023600b82828239805160001a607314601657fe5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600080fdfea265627a7a723158206b7a052d0cc15750d7a71f5934ed674e1a5cae7eede3f495e4572fe7c5a3158764736f6c63430005100032
60556023600b82828239805160001a607314601657fe5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600080fdfea265627a7a72315820bae90120f6a9e44fd916628f84c83280a4338d3ad85a6568c5dd94cc629b699164736f6c63430005100032
2 changes: 1 addition & 1 deletion nearbridge/dist/NearDecoder.full.sol
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ library Borsh {
}
}

function decodeBytes20(Data memory data) internal pure shift(data, 20) returns(bytes20 value) {
function decodeBytes20(Data memory data) internal pure returns(bytes20 value) {
for (uint i = 0; i < 20; i++) {
value |= bytes20(byte(decodeU8(data)) & 0xFF) >> (i * 8);
}
Expand Down
43 changes: 26 additions & 17 deletions nearbridge/test/NearBridge2.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,32 @@ contract('NearBridge2', function ([_, addr1]) {
});
});

//contract('2020-08-18 Example', function ([_, addr1]) {
// beforeEach(async function () {
// this.decoder = await NearDecoder.new();
// this.bridge = await NearBridge.new((await Ed25519.deployed()).address, web3.utils.toBN(1e18), web3.utils.toBN(10));
// await this.bridge.deposit({ value: web3.utils.toWei('1') });
// });
//
// it('should be ok', async function () {
// const block_12640118 = borshify(require('./block_12640118.json'));
// const block_12640218 = borshify(require('./block_12640218.json'));
//
// await this.bridge.initWithValidators(borshifyInitialValidators(require('./init_validators_12640118.json').next_bps));
// await this.bridge.initWithBlock(block_12640118);
// await this.bridge.blockHashes(12640118);
// await this.bridge.addLightClientBlock(block_12640218);
// });
//});
contract('2020-09-09 Example', function ([_, addr1]) {
beforeEach(async function () {
this.decoder = await NearDecoder.new();
this.bridge = await NearBridge.new((await Ed25519.deployed()).address, web3.utils.toBN(1e18), web3.utils.toBN(10));
await this.bridge.deposit({ value: web3.utils.toWei('1') });
});

it('should be ok', async function () {
const block_15178713 = borshify(require('./block_15178713.json'));
const block_15178760 = borshify(require('./block_15178760.json'));
const block_15204402 = borshify(require('./block_15204402.json'));
const block_15248583 = borshify(require('./block_15248583.json'));

await this.bridge.initWithValidators(borshifyInitialValidators(require('./init_validators_15178713.json')));
await this.bridge.initWithBlock(block_15178713);
let now = await time.latest();
await timeIncreaseTo(now.add(time.duration.seconds(3600)));
await this.bridge.addLightClientBlock(block_15178760);
now = await time.latest();
await timeIncreaseTo(now.add(time.duration.seconds(3600)));
await this.bridge.addLightClientBlock(block_15204402);
now = await time.latest();
await timeIncreaseTo(now.add(time.duration.seconds(3600)));
await this.bridge.addLightClientBlock(block_15248583);
});
});

contract('Add second block in first epoch should be verifiable', function ([_, addr1]) {
beforeEach(async function () {
Expand Down
236 changes: 236 additions & 0 deletions nearbridge/test/block_15178713.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
{
"prev_block_hash":"4GxgMTWB9wixdWPPTLrfXH9wZWmRF8kaozrnjedgT3yN",
"next_block_inner_hash":"2tZib5wzkbFuGkRpj7z3TZ3sy6k8uPE5KUhvTNUDJ44c",
"inner_lite":{
"height":15178713,
"epoch_id":"3Y2LeXHxrsYoXiNSs1YTLiir7XUyrXqCy5RZWVpMjj2f",
"next_epoch_id":"F6Kte1BopdxesfLSx2C4qX5D2pfHJLK2tPwAfPBjW7yb",
"prev_state_root":"23H98AdLAuWJ45xueJ3uce7ppqJhPaJmkHRmzXAH2dhG",
"outcome_root":"7tkzFg8RHBmMw1ncRJZCCZAizgq4rwCftTKYLce8RU8t",
"timestamp":1599610800786885400,
"timestamp_nanosec":"1599610800786885484",
"next_bp_hash":"3mpWxraadKzTFnPx7oNDpTS6TiXiG7xLrXkEJ2rW5ipR",
"block_merkle_root":"DBSrnjke2qqWoRnRFBNgEkxTM1z3QQFJxJVXR5GyHTNd"
},
"inner_rest_hash":"4u48cKyeVQyVbFWHcgwY7nZQrSxCeott6vwLf1sWMYe2",
"next_bps":[
{
"account_id":"alexandruast.pool.f863973.m0",
"public_key":"ed25519:A3XJ3uVGxSi9o2gnG2r8Ra3fqqodRpL4iuLTc6fNdGUj",
"stake":"107173191597663585523525746269"
},
{
"account_id":"dsrvlabs.pool.f863973.m0",
"public_key":"ed25519:61ei2efmmLkeDR1CG6JDEC2U3oZCUuC2K1X16Vmxrud9",
"stake":"119451453112709816384635131094"
},
{
"account_id":"zainy.pool.f863973.m0",
"public_key":"ed25519:CnYuTtsUsmYM8WxQiC3UMAbdVnapHtwLT2S7WBFKhD7M",
"stake":"72852799958430931761534272218"
},
{
"account_id":"inotel.pool.f863973.m0",
"public_key":"ed25519:C55jH1MCHYGa3tzUyZZdGrJmmCLP22Aa4v88KYpn2xwZ",
"stake":"123432727217727761212405759400"
},
{
"account_id":"bisontrails.pool.f863973.m0",
"public_key":"ed25519:8g4P5EXyp2b2pfVMHY1QLfkRcY59hjPfWrFCKUWX3RmR",
"stake":"336176656044126300277146541359"
},
{
"account_id":"node2",
"public_key":"ed25519:GkDv7nSMS3xcqA45cpMvFmfV1o4fRF6zYo1JRR6mNqg5",
"stake":"1268346241475720087568057387055"
},
{
"account_id":"pool_easy2stake.pool.f863973.m0",
"public_key":"ed25519:8nzKxvmyeauQRehWkby8GfWNLgqPiF5FCRFSD75M1Rwh",
"stake":"122397138234589506779466946084"
},
{
"account_id":"top.pool.f863973.m0",
"public_key":"ed25519:FR5qxAsP8GgXDN96pappLtWMywiqWsPVqT3HLE3YaUx",
"stake":"119898836066057324986841673742"
},
{
"account_id":"fresh_lockup.pool.f863973.m0",
"public_key":"ed25519:7CMFLtEohojtxBkmj9Jb6AGgbphb1zvxymHzpzuyCjfG",
"stake":"139619718154621021957788001435"
},
{
"account_id":"node1",
"public_key":"ed25519:6DSjZ8mvsRZDvFqFxo8tCKePG96omXW7eVYVSySmDk8e",
"stake":"1313753104055069329020024981283"
},
{
"account_id":"node0",
"public_key":"ed25519:7PGseFbWxvYVgZ89K1uTJKYoKetWs7BJtbyXDzfbAcqX",
"stake":"1313928341565675877507313830790"
},
{
"account_id":"kronos.pool.f863973.m0",
"public_key":"ed25519:3i2pertqzF8xqkJ4BrE4t4r67YiYYrUKCktbqvDgjzuQ",
"stake":"102753264019650005274509265618"
},
{
"account_id":"figment.pool.f863973.m0",
"public_key":"ed25519:5vyPYDsCsxfJvgremrL1cRPfuFqgm62AsyC4AZYJM85w",
"stake":"91606290289796633983270788197"
},
{
"account_id":"01node.pool.f863973.m0",
"public_key":"ed25519:3iNqnvBgxJPXCxu6hNdvJso1PEAc1miAD35KQMBCA3aL",
"stake":"121106555067036708161091240204"
},
{
"account_id":"sl1sub.pool.f863973.m0",
"public_key":"ed25519:3URBpNUjNAMzugQH1rdSKMtwFM8AwHaJgZk5Z6YtnfFL",
"stake":"109278830231560542166170239817"
},
{
"account_id":"aquarius.pool.f863973.m0",
"public_key":"ed25519:8NfEarjStDYjJTwKUgQGy7Z7UTGsZaPhTUsExheQN3r1",
"stake":"88471539602392116738808466388"
},
{
"account_id":"bazilik.pool.f863973.m0",
"public_key":"ed25519:3pDJwDQ6Y5B9QeW1jz8KunhZH4D4GQG86reTmrRfdD7c",
"stake":"120679415892907117111390134328"
},
{
"account_id":"moonlet.pool.f863973.m0",
"public_key":"ed25519:3e1nVCVGNS3yr6CcUvpDAs3BhiWtyM9uTBWkyVR5Xn3K",
"stake":"98652874088329893947021598807"
},
{
"account_id":"staked.pool.6fb1358",
"public_key":"ed25519:684rMbuVYYgL2CkmYgC1weLh3erd2bwrmtQtJJhWzPwj",
"stake":"199056759807572200425508675037"
},
{
"account_id":"certusone.pool.f863973.m0",
"public_key":"ed25519:CKW7f41Kn8YCDPzaGLs1MrPb9h3BjQmHhbei6Ff6nRRF",
"stake":"125660363012937384021192901573"
},
{
"account_id":"nodeasy.pool.f863973.m0",
"public_key":"ed25519:25Dhg8NBvQhsVTuugav3t1To1X1zKiomDmnh8yN9hHMb",
"stake":"90828895032286577298859651309"
},
{
"account_id":"staked.pool.f863973.m0",
"public_key":"ed25519:D2afKYVaKQ1LGiWbMAZRfkKLgqimTR74wvtESvjx5Ft2",
"stake":"105830213003055269037660818310"
},
{
"account_id":"orangeclub.pool.f863973.m0",
"public_key":"ed25519:HezFeSzcwuR5wvkqccgMCMnpf1eQkVCfk52tXZEdKZHz",
"stake":"208202022929424471252944494731"
},
{
"account_id":"thepassivetrust.pool.f863973.m0",
"public_key":"ed25519:4NccD2DNJpBkDmWeJ2GbqPoivQ93qcKiR4PHALJKCTod",
"stake":"115110695650470114565756454966"
},
{
"account_id":"lunanova.pool.f863973.m0",
"public_key":"ed25519:2fZ59qfo9QHNLijoht9cwUb9enSNcnRmXbQn1gKZxvkw",
"stake":"118259887838174750120242604774"
},
{
"account_id":"dokia.pool.f863973.m0",
"public_key":"ed25519:935JMz1vLcJxFApG3TY4MA4RHhvResvoGwCrQoJxHPn9",
"stake":"167432217130375811242413280228"
},
{
"account_id":"bitcat.pool.f863973.m0",
"public_key":"ed25519:9mtnwPQyyap1QNH9ag6r4the7Jkkpdyt9HUF5G1dWxKx",
"stake":"101101830043116301060736686489"
},
{
"account_id":"zpool.pool.f863973.m0",
"public_key":"ed25519:ETFRFNHfvd6fpj74MGYYQp3diY8WB4bFmWMxjTB2yY4V",
"stake":"99512891516314260089253778858"
},
{
"account_id":"blazenet.pool.f863973.m0",
"public_key":"ed25519:DiogP36wBXKFpFeqirrxN8G2Mq9vnakgBvgnHdL9CcN3",
"stake":"112255207308548035105975728558"
},
{
"account_id":"masternode24.pool.f863973.m0",
"public_key":"ed25519:9E3JvrQN6VGDGg1WJ3TjBsNyfmrU6kncBcDvvJLj6qHr",
"stake":"113584978915478143979523867994"
},
{
"account_id":"iosg.pool.f863973.m0",
"public_key":"ed25519:ENp2MvEsT4kVDRdyScSDJZeCMovVPPoodSfHVes1r43M",
"stake":"95085410110477417768019170089"
},
{
"account_id":"jazza.pool.f863973.m0",
"public_key":"ed25519:85cPMNVrqUz8N7oWbbvWbUuamHcJNe49uRbaSzftLCz9",
"stake":"129183474509308730407487785636"
},
{
"account_id":"moonlet.pool.6fb1358",
"public_key":"ed25519:3e1nVCVGNS3yr6CcUvpDAs3BhiWtyM9uTBWkyVR5Xn3K",
"stake":"122874033898208491354065066976"
},
{
"account_id":"sparkpool.pool.f863973.m0",
"public_key":"ed25519:D8ByHdRhPAfRQNgVj1Pri8P2A5P1jthbyqYha38MtyBb",
"stake":"101926228632174257198533848344"
},
{
"account_id":"syncnode.pool.f863973.m0",
"public_key":"ed25519:FUAVDkmLhuTbKYv4GWuWv9ogjKzRatLd5ZBMKXRy7WqE",
"stake":"92687242075644971666484304883"
},
{
"account_id":"stakin.pool.f863973.m0",
"public_key":"ed25519:GvddxjaxBCqGGB4kMNWNFtvozU1EEZ2jrnggKZW8LaU4",
"stake":"93304393186632432779639241730"
}
],
"approvals_after_next":[
"ed25519:2aAUxF6maA1FzvgrRbBTRY39MNZSWB56wp2uXMswwMmfinHVKXC99Bbx6Nqn5fRcQ6UDksowhMn8wNCQAS37K89H",
null,
"ed25519:4t7eyZfGFLB5y1s2f6cTRKABswBb1YGvYapy8UfeHv94Dpt8gmxd4vKznHYVPa66UBxhmtrhihpRP6CPRgQj6JN",
"ed25519:65Vh5ky75H18UGRQpmtrAJEdsrcroQGthQSA8pCcqXm2G53ogcQU6HzPsskXmLaCYPJx1C5CPae9zTVbM7SrRvWN",
null,
"ed25519:2gcjpmXzqtw14thzFR2B3wiLf7m5e5Uwrq3wxFTGgEptweo931X6gqS3govKe44EKgu9x53ZNWq4Ay43d2JYEuXN",
"ed25519:2uN21heKG3n2tEVnw9ghckQPyRPpuLG5GTDXSQ3rYY6gCiAxT3aqBYnL6rWe92VZ9zTdwW5ye6cPv7TDbGqj2zoD",
"ed25519:362Q18K1gaQmPye19Hq6Xfi4hcjXYk2YUDEVvAP4siSg38yQVf4ud9D9TsRq3uZQM7axx1NsSDV3EeF3FUociwFm",
"ed25519:2aDdU7Tvw7eRXLA3sjDPNbLJZozBZcxEg5U9f64kPsB28kFAbLh2Tufux9NNakZyMLv1Qnso1CiozMVkDf5fXiAn",
"ed25519:5TnYqnCn4XVdYaceEbgkBR3pgTdMK8NPsPTj8kfMC83jcBdXbhL2bgbiYEwjJcdBhFh2HewrQMZHvjQzerk6apZJ",
"ed25519:5pHwCSy2b6bzH3Cn1ucRV8wDURdw7cmkYNbXv5urvdsXuS4JBVgdBfKho1prXrBVsQgRFYrpNzcAktXYfrF8ng5h",
"ed25519:4oLiywoqoggk5953QNfw7ybYmPYHgyC2KgSD3fsn7xwnwL2Ht2wY5dTHC6HcrzCRU4z5hL3gwrktXXMeXwMW2MYu",
"ed25519:5or3Zm12rFthdT93BuLLaMtsPxjUwyRrwZ8SHVAJAY7TG2asNXMDAywkbPKs4aqYnRYyA5oz1ihR5cVpouXxmKsQ",
"ed25519:5vgZWRRZWV9ujr8kmF31VWhN1LPDt1313DMVeZXEEpaEV6GC6feDzusfqmHFdj4v2pzh3oWwVzWWuzUbRyakqW68",
null,
"ed25519:5bbqAjxMTXxxPUgWxfJMfFiA6trunQW3JDXeCBAFSJCpwLWYymbghSoxuJAV8rccddiQA11y9Wu784TMU9bLRaD2",
"ed25519:4pTD7BbFe87uQBHUGzResTw4wss9idC1wePpqniyYRZak5G6itF3oF9JeeyxZ4DEcHZQ9HwT4JKkSFbqnqkQoTQh",
null,
null,
null,
null,
"ed25519:4QwthbHHvBjEB6JYx7tFUu7WHmjeQQMK6rKRkTEir6jD6p1RbYZn5gDKM33Dwis8igumrsVb92hjifLwdk7oVTRk",
null,
"ed25519:3zBBUBSKCkGUaSVTt3HNrYMwPFTEZnhRY3zEy5kr5xtdMdguc6FwK3iNiazK5G5WyeLufxLJoqZbuCKgVkZsw8fX",
null,
null,
"ed25519:5EDLUEZhkUbeAeu2Txd8kSo3YCCVgstWQPEQNdsCNT3pcBWLdcfT4hUxfXvzpBzYRjM8jWgmk1ivKxANcFVYJpZD",
null,
null,
"ed25519:4VBVmJijexgCfrYpSCr9K2rVk84DVNqjfzH8digA6xf2QVRbzVZcRiFWhnSaEJYHYzUuxEQVsToXiJVwp9eVjGqb",
null,
"ed25519:38p9Yr7Ec8XdHGcefK14zMh15YECrniELUZtttRuK2SZ6hLZcn2oTaW5p3ZuGKQzGNZEyVxvdFtVjoVPEE3rudHZ",
null,
"ed25519:5aYiTVUBNvwLuVyYUJ598Tcvent2E7hyw6MuTFioVW1PLCpb7h16SMr58QB74fFAs86znzmohdP5vjTYvTb5Dg6G",
null,
"ed25519:221Sa21DeVZEfyKTgGSYtguFeqdpHqfssH1yvSxn4JReQ439cQkmBzZwi9hS1JXD97UMrjmYX1Qp9iC35iKCK81D"
]
}
Loading

0 comments on commit a3968ce

Please sign in to comment.