Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add test for verify attestation #135

Merged
merged 4 commits into from
Jan 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/test/QuantumGravityBridge.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "../lib/openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "../Constants.sol";
import "../DataRootTuple.sol";
import "../QuantumGravityBridge.sol";
import "../lib/tree/binary/BinaryMerkleProof.sol";

import "ds-test/test.sol";

Expand Down Expand Up @@ -93,6 +94,61 @@ contract RelayerTest is DSTest {
assertEq(bridge.state_dataRootTupleRoots(nonce), newTupleRoot);
}

/*
the values used in the verify attestation test are in the format `<height padded to 32 bytes || data root>`, which
represent an encoded `abi.encode(DataRootTuple)`:

0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0x00000000000000000000000000000000000000000000000000000000000000010101010101010101010101010101010101010101010101010101010101010101
0x00000000000000000000000000000000000000000000000000000000000000020202020202020202020202020202020202020202020202020202020202020202
0x00000000000000000000000000000000000000000000000000000000000000030303030303030303030303030303030303030303030303030303030303030303
*/
function testVerifyAttestation() public {
rach-id marked this conversation as resolved.
Show resolved Hide resolved
uint256 initialVelsetNonce = 0;
// data root tuple root nonce.
uint256 nonce = 1;
// commitment to a set of roots.
rach-id marked this conversation as resolved.
Show resolved Hide resolved
// these values were generated using the tendermint implementation of binary merkle trees:
// https://github.com/celestiaorg/celestia-core/blob/60310e7aa554bb76b735a010847a6613bcfe06e8/crypto/merkle/proof.go#L33-L48
bytes32 newTupleRoot = 0x82dc1607d84557d3579ce602a45f5872e821c36dbda7ec926dfa17ebc8d5c013;
adlerjohn marked this conversation as resolved.
Show resolved Hide resolved
// a data root committed to by the above tuple root.
bytes32 newTuple = 0x0101010101010101010101010101010101010101010101010101010101010101;
// the height of the data root.
uint256 height = 1;
// the binary merkle proof of the data root to the data root tuple root.
bytes32[] memory sideNodes = new bytes32[](2);
sideNodes[0] = 0x98ce42deef51d40269d542f5314bef2c7468d401ad5d85168bfab4c0108f75f7;
sideNodes[1] = 0x575664048c9e64260eca2304d177b11d1566d0c954f1417fc76a4f9f27350063;
BinaryMerkleProof memory newTupleProof;
newTupleProof.sideNodes = sideNodes;
newTupleProof.key = 1;
newTupleProof.numLeaves = 4;

bytes32 newDataRootTupleRoot = domainSeparateDataRootTupleRoot(nonce, newTupleRoot);

// Signature for the update.
Signature[] memory sigs = new Signature[](1);
bytes32 digest_eip191 = ECDSA.toEthSignedMessageHash(newDataRootTupleRoot);
(uint8 v, bytes32 r, bytes32 s) = cheats.sign(testPriv1, digest_eip191);
sigs[0] = Signature(v, r, s);

Validator[] memory valSet = new Validator[](1);
valSet[0] = Validator(cheats.addr(testPriv1), votingPower);

bridge.submitDataRootTupleRoot(nonce, initialVelsetNonce, newTupleRoot, valSet, sigs);

assertEq(bridge.state_eventNonce(), nonce);
assertEq(bridge.state_dataRootTupleRoots(nonce), newTupleRoot);

DataRootTuple memory t;
t.height = height;
t.dataRoot = newTuple;

// verify that the tuple was committed to
bool committedTo = bridge.verifyAttestation(nonce, t, newTupleProof);
assertTrue(committedTo);
}

function computeValidatorSetHash(Validator[] memory _validators) private pure returns (bytes32) {
return keccak256(abi.encode(_validators));
}
Expand Down