Skip to content

Commit

Permalink
update val set
Browse files Browse the repository at this point in the history
  • Loading branch information
tkernell committed Dec 14, 2023
1 parent 1d7f1f9 commit 7ab766b
Show file tree
Hide file tree
Showing 13 changed files with 285 additions and 131 deletions.

This file was deleted.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../../build-info/7276be3ce648e1521c3508e7027f1f7d.json"
"buildInfo": "../../../build-info/ad3ff5239657765be100e630e19a9bad.json"
}
88 changes: 86 additions & 2 deletions evm/artifacts/contracts/bridge/BlobstreamO.sol/BlobstreamO.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion evm/artifacts/contracts/bridge/ECDSA.sol/ECDSA.dbg.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../../build-info/a4acdfc059c1288ca6c919f20c646bb8.json"
"buildInfo": "../../../build-info/7857f7e22a11a76528415da615845c5c.json"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../../../../build-info/a4acdfc059c1288ca6c919f20c646bb8.json"
"buildInfo": "../../../../../build-info/7857f7e22a11a76528415da615845c5c.json"
}
4 changes: 4 additions & 0 deletions evm/artifacts/hardhat/console.sol/console.dbg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../build-info/ad3ff5239657765be100e630e19a9bad.json"
}
10 changes: 10 additions & 0 deletions evm/artifacts/hardhat/console.sol/console.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"_format": "hh-sol-artifact-1",
"contractName": "console",
"sourceName": "hardhat/console.sol",
"abi": [],
"bytecode": "0x60566050600b82828239805160001a6073146043577f4e487b7100000000000000000000000000000000000000000000000000000000600052600060045260246000fd5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600080fdfea26469706673582212208acc3345f79544773a2ac70d67040fdb23f23e6fe7dfc8b56dc3265f4d377c1464736f6c63430008160033",
"deployedBytecode": "0x73000000000000000000000000000000000000000030146080604052600080fdfea26469706673582212208acc3345f79544773a2ac70d67040fdb23f23e6fe7dfc8b56dc3265f4d377c1464736f6c63430008160033",
"linkReferences": {},
"deployedLinkReferences": {}
}
45 changes: 41 additions & 4 deletions evm/cache/solidity-files-cache.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"_format": "hh-sol-cache-2",
"files": {
"/Users/timothykernell/Documents/tellor/cosmos/layer/evm/contracts/bridge/BlobstreamO.sol": {
"lastModificationDate": 1702476753091,
"contentHash": "e1e0a992f973c0eaafdbaffa991b650f",
"lastModificationDate": 1702526944428,
"contentHash": "00c5f3c861597e8be3bca344a9d45c80",
"sourceName": "contracts/bridge/BlobstreamO.sol",
"solcConfig": {
"version": "0.8.22",
Expand Down Expand Up @@ -34,7 +34,8 @@
"./Constants.sol",
"./DataRootTuple.sol",
"./lib/tree/binary/BinaryMerkleProof.sol",
"./lib/tree/binary/BinaryMerkleTree.sol"
"./lib/tree/binary/BinaryMerkleTree.sol",
"hardhat/console.sol"
],
"versionPragmas": [
"^0.8.22"
Expand Down Expand Up @@ -226,7 +227,7 @@
]
},
"/Users/timothykernell/Documents/tellor/cosmos/layer/evm/contracts/bridge/ECDSA.sol": {
"lastModificationDate": 1702056262457,
"lastModificationDate": 1702488060271,
"contentHash": "3a594d32a18782cfb8fa0eb8811d05b3",
"sourceName": "contracts/bridge/ECDSA.sol",
"solcConfig": {
Expand Down Expand Up @@ -751,6 +752,42 @@
"artifacts": [
"BridgeProxyMock"
]
},
"/Users/timothykernell/Documents/tellor/cosmos/layer/evm/node_modules/hardhat/console.sol": {
"lastModificationDate": 1699026530409,
"contentHash": "896f68942c3a20e9ae3c3c5bfd3add54",
"sourceName": "hardhat/console.sol",
"solcConfig": {
"version": "0.8.22",
"settings": {
"evmVersion": "paris",
"optimizer": {
"enabled": false,
"runs": 200
},
"outputSelection": {
"*": {
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers",
"metadata"
],
"": [
"ast"
]
}
}
}
},
"imports": [],
"versionPragmas": [
">=0.4.22 <0.9.0"
],
"artifacts": [
"console"
]
}
}
}
14 changes: 14 additions & 0 deletions evm/contracts/bridge/BlobstreamO.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "./Constants.sol";
import "./DataRootTuple.sol";
import "./lib/tree/binary/BinaryMerkleProof.sol";
import "./lib/tree/binary/BinaryMerkleTree.sol";
import "hardhat/console.sol";

struct Validator {
address addr;
Expand Down Expand Up @@ -150,13 +151,18 @@ contract BlobstreamO is ECDSA {
}

bytes32 newCheckpoint = domainSeparateValidatorSetHash(validatorNonce, _newPowerThreshold, _newValidatorSetHash);
console.logBytes32(newCheckpoint);
checkValidatorSignatures(_currentValidatorSet, _sigs, newCheckpoint, powerThreshold);
lastValidatorSetCheckpoint = newCheckpoint;
powerThreshold = _newPowerThreshold;
validatorNonce++;
emit ValidatorSetUpdated(validatorNonce, _newPowerThreshold, _newValidatorSetHash);
}

function deleteThisInputValSet(Validator[] calldata _currentValidatorSet) external pure returns(address) {
return _currentValidatorSet[0].addr;
}

function verifyOracleData(
OracleAttestationData calldata _attest,
Validator[] calldata _currentValidatorSet,
Expand Down Expand Up @@ -198,4 +204,12 @@ contract BlobstreamO is ECDSA {
);
}

function toEthSignedMessageHashPublic(bytes32 _hash) public pure returns (bytes32) {
return ECDSA.toEthSignedMessageHash(_hash);
}

function tryRecoverPublic(bytes32 _hash, bytes memory signature) public pure returns (address, ECDSA.RecoverError, bytes32) {
return ECDSA.tryRecover(_hash, signature);
}

}
181 changes: 61 additions & 120 deletions evm/test/BridgeTestManualDelete.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,25 @@ const { prependOnceListener } = require("process");
const BN = ethers.BigNumber.from
const abiCoder = new ethers.utils.AbiCoder();

describe("BlobstreamO Manual Delete - Function Tests", function () {
describe("BlobstreamO Manual - Function Tests", function () {

let bridge, validatorHash, valPower, accounts, validators, powers;
let startHeight = 0;
let bridge, valPower, accounts, validators, powers, initialValAddrs, initialPowers, nonce, threshold, valCheckpoint;
const UNBONDING_PERIOD = 86400 * 7 * 3; // 3 weeks
const CHAIN_ID = "layer"

beforeEach(async function () {
// const Bridge = await ethers.getContractFactory("BlobstreamO");
// if(startHeight == 0) {
// startHeight = await h.getLatestBlockNumber()
// valsResponse = await h.getValidatorSet(startHeight)
// validators = valsResponse[0]
// powers = valsResponse[1]
// }
// for(i = 0; i< powers.length; i++){
// valPower += powers[i]
// }
// let enc = ethers.utils.defaultAbiCoder.encode(["address[]"], validators)
// let validatorHash = web3.utils.keccak256(enc);
// bridge = await Bridge.deploy(startHeight,valPower * 2/3,validatorHash) ;
accounts = await ethers.getSigners();
initialValAddrs = [accounts[1].address, accounts[2].address]
initialPowers = [1, 2]
nonce = 1
threshold = 2
valCheckpoint = h.calculateValCheckpoint(initialValAddrs, initialPowers, nonce, threshold)

const Bridge = await ethers.getContractFactory("BlobstreamO");
bridge = await Bridge.deploy(nonce, threshold, valCheckpoint);
await bridge.deployed();
});

it.only("init test", async function () {
it("init test", async function () {
valSet = [accounts[0].address, accounts[1].address]
powers = [1, 2]
nonce = 1
Expand All @@ -40,112 +34,59 @@ describe("BlobstreamO Manual Delete - Function Tests", function () {
console.log("valCheckpoint: ", valCheckpoint)
})

it("Should be able to set the bridge address", async function () {
assert.equal(1, 1)
height = await h.getLatestBlockNumber()
console.log("height: " + height)
let [vals, powers] = await h.getValidatorSet(height)
console.log("vals: " + vals)
console.log("powers: " + powers)
// await bridge.setBridgeAddress(accounts[0].address);
// expect(await bridge.bridgeAddress()).to.equal(accounts[0].address);
it.only("updateValidatorSet", async function() {
newValAddrs = [accounts[1].address, accounts[2].address, accounts[3].address]
newPowers = [1, 2, 3]
newNonce = 2
newThreshold = 4
newValHash = await h.calculateValHash(newValAddrs, newPowers)
newValCheckpoint = h.calculateValCheckpoint(newValAddrs, newPowers, nonce, newThreshold)
newDigest = await h.getEthSignedMessageHash(newValCheckpoint)
valSetArray = await h.getValSetStructArray(initialValAddrs, initialPowers)
sig1 = await accounts[1].signMessage(ethers.utils.arrayify(newValCheckpoint))
sig2 = await accounts[2].signMessage(ethers.utils.arrayify(newValCheckpoint))
sigStructArray = await h.getSigStructArray([sig1, sig2])
console.log("sig1: ", sig1)
console.log("sig2: ", sig2)
// resp = await bridge.deleteThisInputValSet([{addr: accounts[1].address, power: 1}, {addr: accounts[2].address, power: 2}])
tryRecoverResp = await bridge.tryRecoverPublic(newDigest, sig1)
console.log("tryRecoverResp: ", tryRecoverResp)
console.log("addr1: ", accounts[1].address)
console.log("testNewCheckpoint: ", newValCheckpoint)
await bridge.updateValidatorSet(newValHash, newThreshold, valSetArray, sigStructArray);
})

it("test verifyBlockHeader", async function() {
multistoreData = await h.getMultistore()
console.log("muultistore: ", multistoreData)
merkleParts = await h.getBlockHeaderMerkleParts(startHeight)
console.log("merkleParts: ", merkleParts)
commonParts = await h.getCommonEncodedVoteParts(startHeight)
console.log("commonParts: ", commonParts)
tmSig = await h.getTmSig(startHeight)
console.log("tmSig: ", tmSig)
// result = await bridge.verifyBlockHeader(multistoreData, merkleParts, commonParts, tmSig)
// result = await bridge.readMultistoreData(multistoreData)
a = BigInt("100")
b = BigInt("200")
// encode as struct
// struct FakeStruct {
// uint a;
// uint b;
// }
// fakeEnc = abiCoder.encode(["tuple(uint256, uint256)"], [[a, b]])
// fakeEnc = abiCoder.encode(["uint256", "uint256"], [a, b])
// result = await bridge.readFakeStruct(fakeEnc)

// try web3 for encoding fake struct
// web3.eth.abi.encodeParameter(
// {
// "ParentStruct": {
// "propertyOne": 'uint256',
// "propertyTwo": 'uint256',
// "childStruct": {
// "propertyOne": 'uint256',
// "propertyTwo": 'uint256'
// }
// }
// },
// {
// "propertyOne": 42,
// "propertyTwo": 56,
// "childStruct": {
// "propertyOne": 45,
// "propertyTwo": 78
// }
// }
// );
fakeEnc = web3.eth.abi.encodeParameter(
{
"FakeStruct": {
"a": 'uint256',
"b": 'uint256'
}
},
{
"a": a,
"b": b
}
);
// result = await bridge.readFakeStruct(fakeEnc)
// dec = ethers.utils.defaultAbiCoder.decode(["uint256", "uint256"], fakeEnc)

types = ["uint256", "uint256"]
vals = [a, b]
fakeEnc = ethers.utils.defaultAbiCoder.encode(types, vals)
// fakeEnc = ethers.utils.defaultAbiCoder.encode(["uint256", "uint256"], [[a, b]])
// result = await bridge.readFakeStructBytes(fakeEnc)

// bridge.FakeStruct()

aEnc = abiCoder.encode(["uint256"], [a])
bEnc = abiCoder.encode(["uint256"], [b])
fakeEnc = {
a: aEnc,
b: bEnc
}

result = await bridge.readFakeStruct(fakeEnc)

multistoreData = await h.getMultistore()
console.log("multistoreData: ", multistoreData)
result = await bridge.readMultistoreData(multistoreData)
console.log("result: ", result)

it("sigs", async function() {
const signer = accounts[1]
message0 = 'Hello, world!'
messageHash0 = ethers.utils.hashMessage(message0)
mysig = await signer.signMessage(message0)
console.log("mysig: ", mysig)
tryRecoverResp = await bridge.tryRecoverPublic(messageHash0, mysig)
console.log("tryRecoverResp: ", tryRecoverResp)
console.log("addr1: ", accounts[1].address)

console.log("\n\nBREAK\n\n")
const message = 'Hello, world!';
const messageHash = ethers.utils.hashMessage(message);
console.log("messageHash: ", messageHash)
console.log("message type: ", typeof(message))
console.log("messageHash type: ", typeof(messageHash))
const signature = await signer.signMessage(message);
console.log("sig type: ", typeof(signature))
console.log("signature: ", signature)

// Pass the hashed message and signature to tryRecover
const recoveredAddress = await bridge.tryRecoverPublic(messageHash, signature);

console.log('Original address:', signer.address);
console.log('Recovered address:', recoveredAddress);


})

it("test verifyBlockHeader", async function() {
multistoreData = await h.getMultistore(startHeight)
merklePartsData = await h.getBlockHeaderMerkleParts(startHeight)
commonPartsData = await h.getCommonEncodedVoteParts(startHeight)
tmSigData = await h.getTmSig(startHeight)
result = await bridge.verifyBlockHeader(multistoreData, merklePartsData, commonPartsData, tmSigData)

console.log("multistore: ", multistoreData)
console.log("merkleParts: ", merklePartsData)
console.log("commonParts: ", commonPartsData)
console.log("tmSig: ", tmSigData)


assert.equal(result, true)
})


})
Loading

0 comments on commit 7ab766b

Please sign in to comment.