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

Use the upstream version of yui-ibc-solidity via NPM, instead of copying sources #24

Open
wants to merge 19 commits into
base: optimized
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
41766b8
fix package.json to add dependency on @hyperledger-labs/yui-ibc-solidity
siburu Jan 19, 2022
5efc5f6
delete files copied from the old version of yui-ibc-solidity
siburu Jan 19, 2022
270cb01
regenerate .sol from .proto using the latest solidity-protobuf
siburu Jan 28, 2022
5241fd3
regenerate .sol from .proto using solidity-protobuf with allow_reserv…
siburu Jan 28, 2022
02edc1d
reimport ics23 files
siburu Jan 28, 2022
e618769
use @hyperledger-labs/yui-ibc-solidity in .sol files
siburu Jan 28, 2022
c1c97d6
use Height struct instead of integer number
siburu Jan 31, 2022
05af0aa
add devDependencies on the latest versions of truffle and ganache-cli
siburu Jan 31, 2022
ed1c1dd
fix truffle configuration and migration script to deploy contracts su…
siburu Jan 31, 2022
3b49431
fix Makefile not to assume that truffle is installed globally
siburu Mar 1, 2022
15c72c5
use the latest upstream version of yui-ibc-solidity
siburu Mar 1, 2022
b8cb32b
use the version of solidity-protobuf, which supports the ".solidity.f…
siburu Mar 1, 2022
ff727db
fix TendermintLight.proto to use integer instead of Height for struct…
siburu Mar 1, 2022
4c49cb6
fix import_ics23.sh to use the absolute path starting from @hyperledg…
siburu Mar 1, 2022
6d0e3cc
fix test to correctly resolve proto files using protobuf.js
siburu Mar 1, 2022
d9fe9b5
fix to pass TendermintLightClient.test.js by using Height instead of …
siburu Mar 1, 2022
f7df6a4
remove import_ibc.sh
siburu Mar 2, 2022
f337d96
delete verbose console.log in testing
siburu Mar 2, 2022
0252923
delete unnecessary modification on truffle-config.js
siburu Mar 2, 2022
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
23 changes: 12 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
.PHONY: clean-protoc build-protoc proto sol-lint js-lint sol-format import-ibc test demo

clean-protoc:
rm -rf ./solidity-protobuf 2>/dev/null

build-protoc: | clean-protoc
git clone https://github.com/mkaczanowski/solidity-protobuf

.PHONY: proto
proto:
./scripts/protobuf_compile.sh

.PHONY: sol-lint
sol-lint:
solhint 'contracts/{utils,mocks}/**/*.sol'

.PHONY: js-lint
js-lint:
eslint test/utlis *.js

.PHONY: sol-format
sol-format:
npx prettier --write 'contracts/{utils,mocks}/*.sol' 'contracts/proto/{Encoder.sol,TendermintHelper.sol}'

import-ibc:
./scripts/import_ibc.sh
.PHONY: import-ics23
import-ics23:
./scripts/import_ics23.sh

.PHONY: config
config:
export CONF_TPL="./test/demo/src/consts.rs:./scripts/template/contract.rs.tpl" && truffle exec ./scripts/confgen.js --network=$(NETWORK)

.PHONY: test
test:
truffle test --network tests
npx --no-install truffle test --network tests

.PHONY: demo
demo:
# gas-price: 0.5 gwei = 500000000 wei
cd test/demo && cargo run -- --max-headers 3 --celo-gas-price 500000000 --celo-usd-price 5.20

.PHONY: deploy
deploy:
# gas-price: 0.5 gwei = 500000000 wei
./scripts/deploy_with_stats.sh 500000000 5.20
2 changes: 1 addition & 1 deletion contracts/Migrations.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: TBD
pragma solidity >=0.4.25 <=0.8.2;
pragma solidity >=0.4.25 <0.9.0;

contract Migrations {
address public owner;
Expand Down
71 changes: 37 additions & 34 deletions contracts/TendermintLightClient.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import {
InnerOp
} from "./proto/proofs.sol";
import "./proto/TendermintHelper.sol";
import {GoogleProtobufAny as Any} from "./proto/GoogleProtobufAny.sol";
import "./ibc/IClient.sol";
import "./ibc/IBCHost.sol";
import "./ibc/IBCMsgs.sol";
import "./ibc/IBCIdentifier.sol";
import {GoogleProtobufAny as Any} from "@hyperledger-labs/yui-ibc-solidity/contracts/core/types/GoogleProtobufAny.sol";
import "@hyperledger-labs/yui-ibc-solidity/contracts/core/IClient.sol";
import "@hyperledger-labs/yui-ibc-solidity/contracts/core/IBCHost.sol";
import "@hyperledger-labs/yui-ibc-solidity/contracts/core/IBCMsgs.sol";
import "@hyperledger-labs/yui-ibc-solidity/contracts/core/IBCIdentifier.sol";
import "@hyperledger-labs/yui-ibc-solidity/contracts/core/IBCHeight.sol";
import "@hyperledger-labs/yui-ibc-solidity/contracts/core/types/Client.sol";
import "./utils/Bytes.sol";
import "./utils/Tendermint.sol";
import "./ics23/ics23.sol";
Expand All @@ -33,6 +35,7 @@ contract TendermintLightClient is IClient {
using TendermintHelper for TmHeader.Data;
using TendermintHelper for ConsensusState.Data;
using TendermintHelper for ValidatorSet.Data;
using IBCHeight for Height.Data;

struct ProtoTypes {
bytes32 clientState;
Expand Down Expand Up @@ -75,7 +78,7 @@ contract TendermintLightClient is IClient {
function getTimestampAtHeight(
IBCHost host,
string memory clientId,
uint64 height
Height.Data memory height
) public override view returns (uint64, bool) {
(ConsensusState.Data memory consensusState, bool found) = getConsensusState(host, clientId, height);
if (!found) {
Expand All @@ -91,12 +94,12 @@ contract TendermintLightClient is IClient {
function getLatestHeight(
IBCHost host,
string memory clientId
) public override view returns (uint64, bool) {
) public override view returns (Height.Data memory, bool) {
(ClientState.Data memory clientState, bool found) = getClientState(host, clientId);
if (!found) {
return (0, false);
return (Height.Data(0, 0), false);
}
return (uint64(clientState.latest_height), true);
return (clientState.latest_height, true);
}

/**
Expand All @@ -107,7 +110,7 @@ contract TendermintLightClient is IClient {
string memory clientId,
bytes memory clientStateBytes,
bytes memory headerBytes
) public override view returns (bytes memory newClientStateBytes, bytes memory newConsensusStateBytes, uint64 height) {
) public override view returns (bytes memory newClientStateBytes, bytes memory newConsensusStateBytes, Height.Data memory height) {
TmHeader.Data memory tmHeader;
ClientState.Data memory clientState;
ConsensusState.Data memory trustedConsensusState;
Expand All @@ -121,19 +124,19 @@ contract TendermintLightClient is IClient {
// Check if the Client store already has a consensus state for the header's height
// If the consensus state exists, and it matches the header then we return early
// since header has already been submitted in a previous UpdateClient.
(prevConsState, ok) = getConsensusState(host, clientId, uint64(tmHeader.signed_header.header.height));
(prevConsState, ok) = getConsensusState(host, clientId, tmHeader.getHeight());
if (ok) {
// This header has already been submitted and the necessary state is already stored
// in client store, thus we can return early without further validation.
if (prevConsState.isEqual(tmHeader.toConsensusState())) {
return (clientStateBytes, marshalConsensusState(prevConsState), uint64(tmHeader.signed_header.header.height));
return (clientStateBytes, marshalConsensusState(prevConsState), tmHeader.getHeight());
}
// A consensus state already exists for this height, but it does not match the provided header.
// Thus, we must check that this header is valid, and if so we will freeze the client.
conflictingHeader = true;
}

(trustedConsensusState, ok) = getConsensusState(host, clientId, uint64(tmHeader.trusted_height));
(trustedConsensusState, ok) = getConsensusState(host, clientId, tmHeader.trusted_height);
require(ok, "LC: consensusState not found at trusted height");

(clientState, ok) = unmarshalClientState(clientStateBytes);
Expand All @@ -143,22 +146,22 @@ contract TendermintLightClient is IClient {

// Header is different from existing consensus state and also valid, so freeze the client and return
if (conflictingHeader) {
clientState.frozen_height = tmHeader.signed_header.header.height;
clientState.frozen_height = tmHeader.getHeight();
return (
marshalClientState(clientState),
marshalConsensusState(tmHeader.toConsensusState()),
uint64(tmHeader.signed_header.header.height)
tmHeader.getHeight()
);
}

// TODO: check consensus state monotonicity

// update the consensus state from a new header and set processed time metadata
if (tmHeader.signed_header.header.height > clientState.latest_height) {
clientState.latest_height = tmHeader.signed_header.header.height;
if (tmHeader.getHeight().gt(clientState.latest_height)) {
clientState.latest_height = tmHeader.getHeight();
}

return (marshalClientState(clientState), marshalConsensusState(tmHeader.toConsensusState()), uint64(clientState.latest_height));
return (marshalClientState(clientState), marshalConsensusState(tmHeader.toConsensusState()), clientState.latest_height);
}

// checkValidity checks if the Tendermint header is valid.
Expand All @@ -170,13 +173,13 @@ contract TendermintLightClient is IClient {
) private view {
// assert header height is newer than consensus state
require(
tmHeader.signed_header.header.height > tmHeader.trusted_height,
tmHeader.getHeight().gt(tmHeader.trusted_height),
"LC: header height consensus state height"
);

LightHeader.Data memory lc;
lc.chain_id = clientState.chain_id;
lc.height = tmHeader.trusted_height;
lc.height = int64(tmHeader.trusted_height.revision_height);
lc.time = trustedConsensusState.timestamp;
lc.next_validators_hash = trustedConsensusState.next_validators_hash;

Expand Down Expand Up @@ -204,7 +207,7 @@ contract TendermintLightClient is IClient {
function verifyConnectionState(
IBCHost host,
string memory clientId,
uint64 height,
Height.Data memory height,
bytes memory prefix,
bytes memory proof,
string memory connectionId,
Expand All @@ -231,7 +234,7 @@ contract TendermintLightClient is IClient {
function verifyChannelState(
IBCHost host,
string memory clientId,
uint64 height,
Height.Data memory height,
bytes memory prefix,
bytes memory proof,
string memory portId,
Expand Down Expand Up @@ -259,7 +262,7 @@ contract TendermintLightClient is IClient {
function verifyPacketCommitment(
IBCHost host,
string memory clientId,
uint64 height,
Height.Data memory height,
uint64 delayPeriodTime,
uint64 delayPeriodBlocks,
bytes memory prefix,
Expand Down Expand Up @@ -293,7 +296,7 @@ contract TendermintLightClient is IClient {
function verifyPacketAcknowledgement(
IBCHost host,
string memory clientId,
uint64 height,
Height.Data memory height,
uint64 delayPeriodTime,
uint64 delayPeriodBlocks,
bytes memory prefix,
Expand All @@ -319,7 +322,7 @@ contract TendermintLightClient is IClient {
function verifyClientState(
IBCHost host,
string memory clientId,
uint64 height,
Height.Data memory height,
bytes memory prefix,
string memory counterpartyClientIdentifier,
bytes memory proof,
Expand All @@ -346,9 +349,9 @@ contract TendermintLightClient is IClient {
function verifyClientConsensusState(
IBCHost host,
string memory clientId,
uint64 height,
Height.Data memory height,
string memory counterpartyClientIdentifier,
uint64 consensusHeight,
Height.Data memory consensusHeight,
bytes memory prefix,
bytes memory proof,
bytes memory consensusStateBytes // serialized with pb
Expand All @@ -371,8 +374,8 @@ contract TendermintLightClient is IClient {
return verifyMembership(proof, consensusState.merkle_root_hash.toBytes32(), prefix, IBCIdentifier.consensusStateCommitmentSlot(counterpartyClientIdentifier, consensusHeight), keccak256(consensusStateBytes));
}

function validateArgs(ClientState.Data memory cs, uint64 height, bytes memory prefix, bytes memory proof) internal pure returns (bool) {
if (cs.latest_height < int64(height)) {
function validateArgs(ClientState.Data memory cs, Height.Data memory height, bytes memory prefix, bytes memory proof) internal pure returns (bool) {
if (cs.latest_height.lt(height)) {
return false;
} else if (prefix.length == 0) {
return false;
Expand All @@ -382,7 +385,7 @@ contract TendermintLightClient is IClient {
return true;
}

function validateDelayPeriod(IBCHost host, string memory clientId, uint64 height, uint64 delayPeriodTime, uint64 delayPeriodBlocks) private view returns (bool) {
function validateDelayPeriod(IBCHost host, string memory clientId, Height.Data memory height, uint64 delayPeriodTime, uint64 delayPeriodBlocks) private view returns (bool) {
uint64 currentTime = uint64(block.timestamp * 1000 * 1000 * 1000);
uint64 validTime = mustGetProcessedTime(host, clientId, height) + delayPeriodTime;
if (currentTime < validTime) {
Expand All @@ -404,19 +407,19 @@ contract TendermintLightClient is IClient {
}

// NOTE: this is a workaround to avoid the error `Stack too deep` in caller side
function mustGetConsensusState(IBCHost host, string memory clientId, uint64 height) internal view returns (ConsensusState.Data memory) {
function mustGetConsensusState(IBCHost host, string memory clientId, Height.Data memory height) internal view returns (ConsensusState.Data memory) {
(ConsensusState.Data memory consensusState, bool found) = getConsensusState(host, clientId, height);
require(found, "LC: consensus state not found");
return consensusState;
}

function mustGetProcessedTime(IBCHost host, string memory clientId, uint64 height) internal view returns (uint64) {
function mustGetProcessedTime(IBCHost host, string memory clientId, Height.Data memory height) internal view returns (uint64) {
(uint256 processedTime, bool found) = host.getProcessedTime(clientId, height);
require(found, "LC: processed time not found");
return uint64(processedTime) * 1000 * 1000 * 1000;
}

function mustGetProcessedHeight(IBCHost host, string memory clientId, uint64 height) internal view returns (uint64) {
function mustGetProcessedHeight(IBCHost host, string memory clientId, Height.Data memory height) internal view returns (uint64) {
(uint256 processedHeight, bool found) = host.getProcessedHeight(clientId, height);
require(found, "LC: processed height not found");
return uint64(processedHeight);
Expand All @@ -431,7 +434,7 @@ contract TendermintLightClient is IClient {
return (ClientState.decode(Any.decode(clientStateBytes).value), true);
}

function getConsensusState(IBCHost host, string memory clientId, uint64 height) public view returns (ConsensusState.Data memory consensusState, bool found) {
function getConsensusState(IBCHost host, string memory clientId, Height.Data memory height) public view returns (ConsensusState.Data memory consensusState, bool found) {
bytes memory consensusStateBytes;
(consensusStateBytes, found) = host.getConsensusState(clientId, height);
if (!found) {
Expand Down
Loading