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

qbft: add max_clock_drift to client state #280

Merged
merged 3 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
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
126 changes: 63 additions & 63 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,73 +1,73 @@
IBCMockAppTest:testHandshake() (gas: 3537520)
IBCMockAppTest:testHandshakeBetweenDifferentPorts() (gas: 2644408)
IBCMockAppTest:testPacketRelay() (gas: 9584548)
IBCMockAppTest:testPacketTimeout() (gas: 2981201)
IBCTest:testBenchmarkCreateMockClient() (gas: 209410)
IBCTest:testBenchmarkLCUpdateMockClient() (gas: 39945)
IBCTest:testBenchmarkRecvPacket() (gas: 133353)
IBCTest:testBenchmarkSendPacket() (gas: 80964)
IBCTest:testBenchmarkUpdateMockClient() (gas: 137373)
IBCMockAppTest:testHandshake() (gas: 4420400)
IBCMockAppTest:testHandshakeBetweenDifferentPorts() (gas: 3334308)
IBCMockAppTest:testPacketRelay() (gas: 13931868)
IBCMockAppTest:testPacketTimeout() (gas: 4279269)
IBCTest:testBenchmarkCreateMockClient() (gas: 233366)
IBCTest:testBenchmarkLCUpdateMockClient() (gas: 62005)
IBCTest:testBenchmarkRecvPacket() (gas: 158921)
IBCTest:testBenchmarkSendPacket() (gas: 128424)
IBCTest:testBenchmarkUpdateMockClient() (gas: 160229)
IBCTest:testToUint128((uint64,uint64)) (runs: 256, μ: 947, ~: 947)
TestICS02:testCreateClient() (gas: 33302626)
TestICS02:testInvalidCreateClient() (gas: 33152264)
TestICS02:testInvalidUpdateClient() (gas: 33154327)
TestICS02:testRegisterClient() (gas: 32897266)
TestICS02:testRegisterClientDuplicatedClientType() (gas: 32880491)
TestICS02:testRegisterClientInvalidClientType() (gas: 32864530)
TestICS02:testUpdateClient() (gas: 33319687)
TestICS03Handshake:testConnOpenAck() (gas: 1631658)
TestICS03Handshake:testConnOpenConfirm() (gas: 1766077)
TestICS03Handshake:testConnOpenInit() (gas: 1279190)
TestICS03Handshake:testConnOpenTry() (gas: 2160696)
TestICS03Handshake:testInvalidConnOpenAck() (gas: 2031548)
TestICS03Handshake:testInvalidConnOpenConfirm() (gas: 2095798)
TestICS03Handshake:testInvalidConnOpenInit() (gas: 666951)
TestICS03Handshake:testInvalidConnOpenTry() (gas: 2101089)
TestICS02:testCreateClient() (gas: 36328221)
TestICS02:testInvalidCreateClient() (gas: 36225735)
TestICS02:testInvalidUpdateClient() (gas: 36224438)
TestICS02:testRegisterClient() (gas: 35880037)
TestICS02:testRegisterClientDuplicatedClientType() (gas: 35865346)
TestICS02:testRegisterClientInvalidClientType() (gas: 35894673)
TestICS02:testUpdateClient() (gas: 36392638)
TestICS03Handshake:testConnOpenAck() (gas: 1858318)
TestICS03Handshake:testConnOpenConfirm() (gas: 2054253)
TestICS03Handshake:testConnOpenInit() (gas: 1429882)
TestICS03Handshake:testConnOpenTry() (gas: 2398932)
TestICS03Handshake:testInvalidConnOpenAck() (gas: 2422020)
TestICS03Handshake:testInvalidConnOpenConfirm() (gas: 2535722)
TestICS03Handshake:testInvalidConnOpenInit() (gas: 783147)
TestICS03Handshake:testInvalidConnOpenTry() (gas: 2342609)
TestICS03Version:testCopyVersions() (gas: 558658)
TestICS03Version:testFindSupportedVersion() (gas: 19400)
TestICS03Version:testIsSupportedVersion() (gas: 7864)
TestICS03Version:testPickVersion() (gas: 25327)
TestICS03Version:testVerifyProposedVersion() (gas: 11777)
TestICS03Version:testVerifySupportedFeature() (gas: 4153)
TestICS04Handshake:testBindPort() (gas: 37502)
TestICS04Handshake:testChanClose() (gas: 8864072)
TestICS04Handshake:testChanOpenAck() (gas: 2988216)
TestICS04Handshake:testChanOpenConfirm() (gas: 3170513)
TestICS04Handshake:testChanOpenInit() (gas: 2274533)
TestICS04Handshake:testChanOpenTry() (gas: 2759774)
TestICS04Handshake:testInvalidChanOpenAck() (gas: 2115402)
TestICS04Handshake:testInvalidChanOpenConfirm() (gas: 2171859)
TestICS04Handshake:testInvalidChanOpenInit() (gas: 1299857)
TestICS04Handshake:testInvalidChanOpenTry() (gas: 1378320)
TestICS04Packet:testAcknowledgementPacket() (gas: 2393006)
TestICS04Packet:testInvalidSendPacket() (gas: 2385887)
TestICS04Packet:testRecvPacket() (gas: 7941501)
TestICS04Packet:testRecvPacketTimeoutHeight() (gas: 2411677)
TestICS04Packet:testRecvPacketTimeoutTimestamp() (gas: 2421551)
TestICS04Packet:testSendPacket() (gas: 5160796)
TestICS04Packet:testTimeoutOnClose() (gas: 2654567)
TestICS04Upgrade:testUpgradeAuthorityCancel() (gas: 32899652)
TestICS04Upgrade:testUpgradeCannotCancelWithOldErrorReceipt() (gas: 2648059)
TestICS04Upgrade:testUpgradeCannotRecvNextUpgradePacket() (gas: 3322208)
TestICS04Upgrade:testUpgradeCounterpartyAdvanceNextSequenceBeforeOpen() (gas: 3402742)
TestICS04Upgrade:testUpgradeCrossingHelloIncompatibleProposals() (gas: 3312033)
TestICS04Upgrade:testUpgradeFull() (gas: 34308220)
TestICS04Upgrade:testUpgradeInit() (gas: 2362479)
TestICS04Upgrade:testUpgradeNoChanges() (gas: 1950920)
TestICS04Upgrade:testUpgradeOutOfSync() (gas: 3051844)
TestICS04Upgrade:testUpgradeRelaySuccessAtCounterpartyFlushComplete() (gas: 3325729)
TestICS04Upgrade:testUpgradeRelaySuccessAtFlushing() (gas: 3522672)
TestICS04Upgrade:testUpgradeSendPacketFailAtFlushingOrFlushComplete() (gas: 2743014)
TestICS04Upgrade:testUpgradeTimeoutAbortAck() (gas: 10509336)
TestICS04Upgrade:testUpgradeTimeoutAbortConfirm() (gas: 11912012)
TestICS04Upgrade:testUpgradeTimeoutUpgrade() (gas: 31027545)
TestICS04Upgrade:testUpgradeToOrdered() (gas: 36895593)
TestICS04Upgrade:testUpgradeToUnordered() (gas: 29370382)
TestICS04UpgradeApp:testUpgradeAuthorizationChanneNotFound() (gas: 37604)
TestICS04UpgradeApp:testUpgradeAuthorizationRePropose() (gas: 2034676)
TestICS04UpgradeApp:testUpgradeAuthorizationRemove() (gas: 1859258)
TestICS20:testAddressToHex(address) (runs: 256, μ: 22676, ~: 22804)
TestICS04Handshake:testBindPort() (gas: 124350)
TestICS04Handshake:testChanClose() (gas: 12938668)
TestICS04Handshake:testChanOpenAck() (gas: 3459596)
TestICS04Handshake:testChanOpenConfirm() (gas: 3770909)
TestICS04Handshake:testChanOpenInit() (gas: 2543717)
TestICS04Handshake:testChanOpenTry() (gas: 3100002)
TestICS04Handshake:testInvalidChanOpenAck() (gas: 2439814)
TestICS04Handshake:testInvalidChanOpenConfirm() (gas: 2517403)
TestICS04Handshake:testInvalidChanOpenInit() (gas: 1758661)
TestICS04Handshake:testInvalidChanOpenTry() (gas: 1773244)
TestICS04Packet:testAcknowledgementPacket() (gas: 3351538)
TestICS04Packet:testInvalidSendPacket() (gas: 3551759)
TestICS04Packet:testRecvPacket() (gas: 10996525)
TestICS04Packet:testRecvPacketTimeoutHeight() (gas: 3260141)
TestICS04Packet:testRecvPacketTimeoutTimestamp() (gas: 3279291)
TestICS04Packet:testSendPacket() (gas: 6413300)
TestICS04Packet:testTimeoutOnClose() (gas: 3553791)
TestICS04Upgrade:testUpgradeAuthorityCancel() (gas: 46694868)
TestICS04Upgrade:testUpgradeCannotCancelWithOldErrorReceipt() (gas: 3455607)
TestICS04Upgrade:testUpgradeCannotRecvNextUpgradePacket() (gas: 5244036)
TestICS04Upgrade:testUpgradeCounterpartyAdvanceNextSequenceBeforeOpen() (gas: 5213202)
TestICS04Upgrade:testUpgradeCrossingHelloIncompatibleProposals() (gas: 4405965)
TestICS04Upgrade:testUpgradeFull() (gas: 57590258)
TestICS04Upgrade:testUpgradeInit() (gas: 3068755)
TestICS04Upgrade:testUpgradeNoChanges() (gas: 2471908)
TestICS04Upgrade:testUpgradeOutOfSync() (gas: 3902216)
TestICS04Upgrade:testUpgradeRelaySuccessAtCounterpartyFlushComplete() (gas: 5215801)
TestICS04Upgrade:testUpgradeRelaySuccessAtFlushing() (gas: 5589764)
TestICS04Upgrade:testUpgradeSendPacketFailAtFlushingOrFlushComplete() (gas: 4070972)
TestICS04Upgrade:testUpgradeTimeoutAbortAck() (gas: 17679417)
TestICS04Upgrade:testUpgradeTimeoutAbortConfirm() (gas: 21317049)
TestICS04Upgrade:testUpgradeTimeoutUpgrade() (gas: 44126259)
TestICS04Upgrade:testUpgradeToOrdered() (gas: 56352929)
TestICS04Upgrade:testUpgradeToUnordered() (gas: 44985196)
TestICS04UpgradeApp:testUpgradeAuthorizationChanneNotFound() (gas: 61712)
TestICS04UpgradeApp:testUpgradeAuthorizationRePropose() (gas: 2565532)
TestICS04UpgradeApp:testUpgradeAuthorizationRemove() (gas: 2473970)
TestICS20:testAddressToHex(address) (runs: 256, μ: 22726, ~: 22867)
TestICS20:testHexToAddress(string) (runs: 256, μ: 4776, ~: 4734)
TestICS20:testIsEscapedString() (gas: 48979)
TestICS20:testMarshaling() (gas: 148517)
TestICS20:testParseAmount(uint256) (runs: 256, μ: 27026, ~: 21930)
TestICS20:testParseAmount(uint256) (runs: 256, μ: 26449, ~: 24175)
8 changes: 4 additions & 4 deletions chains/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
- 8645:8545
- 8646:8546
healthcheck:
test: [ "CMD-SHELL", "grep \"00000000:2161 00000000:0000 0A\" /proc/net/tcp || exit 1" ]
test: [ "CMD-SHELL", "grep \"00000000:2161 00000000:0000 0A\" /proc/net/tcp || grep \"00000000000000000000000000000000:2161 00000000000000000000000000000000:0000 0A\" /proc/net/tcp6 || exit 1" ]
interval: 100ms
timeout: 1s
retries: 100
Expand All @@ -15,7 +15,7 @@ services:
- 8745:8545
- 8746:8546
healthcheck:
test: [ "CMD-SHELL", "grep \"00000000:2161 00000000:0000 0A\" /proc/net/tcp || exit 1" ]
test: [ "CMD-SHELL", "grep \"00000000:2161 00000000:0000 0A\" /proc/net/tcp || grep \"00000000000000000000000000000000:2161 00000000000000000000000000000000:0000 0A\" /proc/net/tcp6 || exit 1" ]
interval: 100ms
timeout: 1s
retries: 100
Expand All @@ -26,7 +26,7 @@ services:
- 8645:8545
- 8646:8546
healthcheck:
test: [ "CMD-SHELL", "grep \"00000000:2161 00000000:0000 0A\" /proc/net/tcp || exit 1" ]
test: [ "CMD-SHELL", "grep \"00000000:2161 00000000:0000 0A\" /proc/net/tcp || grep \"00000000000000000000000000000000:2161 00000000000000000000000000000000:0000 0A\" /proc/net/tcp6 || exit 1" ]
interval: 100ms
timeout: 1s
retries: 100
Expand All @@ -36,7 +36,7 @@ services:
- 8745:8545
- 8746:8546
healthcheck:
test: [ "CMD-SHELL", "grep \"00000000:2161 00000000:0000 0A\" /proc/net/tcp || exit 1" ]
test: [ "CMD-SHELL", "grep \"00000000:2161 00000000:0000 0A\" /proc/net/tcp || grep \"00000000000000000000000000000000:2161 00000000000000000000000000000000:0000 0A\" /proc/net/tcp6 || exit 1" ]
interval: 100ms
timeout: 1s
retries: 100
5 changes: 5 additions & 0 deletions contracts/clients/qbft/QBFTClient.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ contract QBFTClient is ILightClient, ILightClientErrors {
error InsuffientUntrustedValidatorsSeals(uint256 actual, uint256 threshold);
/// @param length length of the signature
error InvalidECDSASignatureLength(uint256 length);
/// @dev An error indicating that the header is from the future
error HeaderFromFuture();

string internal constant HEADER_TYPE_URL = "/ibc.lightclients.qbft.v1.Header";
string internal constant CLIENT_STATE_TYPE_URL = "/ibc.lightclients.qbft.v1.ClientState";
Expand Down Expand Up @@ -208,6 +210,9 @@ contract QBFTClient is ILightClient, ILightClientErrors {
) {
revert LightClientConsensusStateExpired();
}
if (block.timestamp + clientState.max_clock_drift < parsedHeader.time) {
revert HeaderFromFuture();
}

bytes[] memory validators = verify(trustedConsensusState.validators, parsedHeader);
if (validators.length == 0) {
Expand Down
36 changes: 36 additions & 0 deletions contracts/proto/QBFT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ library IbcLightclientsQbftV1ClientState {
bytes ibc_store_address;
Height.Data latest_height;
uint64 trusting_period;
uint64 max_clock_drift;
}

// Decoder section
Expand Down Expand Up @@ -72,6 +73,9 @@ library IbcLightclientsQbftV1ClientState {
if (fieldId == 4) {
pointer += _read_trusting_period(pointer, bs, r);
} else
if (fieldId == 5) {
pointer += _read_max_clock_drift(pointer, bs, r);
} else
{
pointer += ProtoBufRuntime._skip_field_decode(wireType, pointer, bs);
}
Expand Down Expand Up @@ -150,6 +154,23 @@ library IbcLightclientsQbftV1ClientState {
return sz;
}

/**
* @dev The decoder for reading a field
* @param p The offset of bytes array to start decode
* @param bs The bytes array to be decoded
* @param r The in-memory struct
* @return The number of bytes decoded
*/
function _read_max_clock_drift(
uint256 p,
bytes memory bs,
Data memory r
) internal pure returns (uint) {
(uint64 x, uint256 sz) = ProtoBufRuntime._decode_uint64(p, bs);
r.max_clock_drift = x;
return sz;
}

// struct decoder
/**
* @dev The decoder for reading a inner struct field
Expand Down Expand Up @@ -239,6 +260,15 @@ library IbcLightclientsQbftV1ClientState {
);
pointer += ProtoBufRuntime._encode_uint64(r.trusting_period, pointer, bs);
}
if (r.max_clock_drift != 0) {
pointer += ProtoBufRuntime._encode_key(
5,
ProtoBufRuntime.WireType.Varint,
pointer,
bs
);
pointer += ProtoBufRuntime._encode_uint64(r.max_clock_drift, pointer, bs);
}
return pointer - offset;
}
// nested encoder
Expand Down Expand Up @@ -286,6 +316,7 @@ library IbcLightclientsQbftV1ClientState {
e += 1 + ProtoBufRuntime._sz_lendelim(r.ibc_store_address.length);
e += 1 + ProtoBufRuntime._sz_lendelim(Height._estimate(r.latest_height));
e += 1 + ProtoBufRuntime._sz_uint64(r.trusting_period);
e += 1 + ProtoBufRuntime._sz_uint64(r.max_clock_drift);
return e;
}
// empty checker
Expand All @@ -306,6 +337,10 @@ library IbcLightclientsQbftV1ClientState {
return false;
}

if (r.max_clock_drift != 0) {
return false;
}

return true;
}

Expand All @@ -321,6 +356,7 @@ library IbcLightclientsQbftV1ClientState {
output.ibc_store_address = input.ibc_store_address;
Height.store(input.latest_height, output.latest_height);
output.trusting_period = input.trusting_period;
output.max_clock_drift = input.max_clock_drift;

}

Expand Down
Loading