Skip to content

Commit 732f269

Browse files
authored
Merge pull request #280 from hyperledger-labs/qbft-max-clock-drift
qbft: add `max_clock_drift` to client state Signed-off-by: Jun Kimura <[email protected]>
2 parents eb7767b + 66f5fa3 commit 732f269

File tree

7 files changed

+195
-108
lines changed

7 files changed

+195
-108
lines changed

.gas-snapshot

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,73 @@
1-
IBCMockAppTest:testHandshake() (gas: 3537520)
2-
IBCMockAppTest:testHandshakeBetweenDifferentPorts() (gas: 2644408)
3-
IBCMockAppTest:testPacketRelay() (gas: 9584548)
4-
IBCMockAppTest:testPacketTimeout() (gas: 2981201)
5-
IBCTest:testBenchmarkCreateMockClient() (gas: 209410)
6-
IBCTest:testBenchmarkLCUpdateMockClient() (gas: 39945)
7-
IBCTest:testBenchmarkRecvPacket() (gas: 133353)
8-
IBCTest:testBenchmarkSendPacket() (gas: 80964)
9-
IBCTest:testBenchmarkUpdateMockClient() (gas: 137373)
1+
IBCMockAppTest:testHandshake() (gas: 4420400)
2+
IBCMockAppTest:testHandshakeBetweenDifferentPorts() (gas: 3334308)
3+
IBCMockAppTest:testPacketRelay() (gas: 13931868)
4+
IBCMockAppTest:testPacketTimeout() (gas: 4279269)
5+
IBCTest:testBenchmarkCreateMockClient() (gas: 233366)
6+
IBCTest:testBenchmarkLCUpdateMockClient() (gas: 62005)
7+
IBCTest:testBenchmarkRecvPacket() (gas: 158921)
8+
IBCTest:testBenchmarkSendPacket() (gas: 128424)
9+
IBCTest:testBenchmarkUpdateMockClient() (gas: 160229)
1010
IBCTest:testToUint128((uint64,uint64)) (runs: 256, μ: 947, ~: 947)
11-
TestICS02:testCreateClient() (gas: 33302626)
12-
TestICS02:testInvalidCreateClient() (gas: 33152264)
13-
TestICS02:testInvalidUpdateClient() (gas: 33154327)
14-
TestICS02:testRegisterClient() (gas: 32897266)
15-
TestICS02:testRegisterClientDuplicatedClientType() (gas: 32880491)
16-
TestICS02:testRegisterClientInvalidClientType() (gas: 32864530)
17-
TestICS02:testUpdateClient() (gas: 33319687)
18-
TestICS03Handshake:testConnOpenAck() (gas: 1631658)
19-
TestICS03Handshake:testConnOpenConfirm() (gas: 1766077)
20-
TestICS03Handshake:testConnOpenInit() (gas: 1279190)
21-
TestICS03Handshake:testConnOpenTry() (gas: 2160696)
22-
TestICS03Handshake:testInvalidConnOpenAck() (gas: 2031548)
23-
TestICS03Handshake:testInvalidConnOpenConfirm() (gas: 2095798)
24-
TestICS03Handshake:testInvalidConnOpenInit() (gas: 666951)
25-
TestICS03Handshake:testInvalidConnOpenTry() (gas: 2101089)
11+
TestICS02:testCreateClient() (gas: 36328221)
12+
TestICS02:testInvalidCreateClient() (gas: 36225735)
13+
TestICS02:testInvalidUpdateClient() (gas: 36224438)
14+
TestICS02:testRegisterClient() (gas: 35880037)
15+
TestICS02:testRegisterClientDuplicatedClientType() (gas: 35865346)
16+
TestICS02:testRegisterClientInvalidClientType() (gas: 35894673)
17+
TestICS02:testUpdateClient() (gas: 36392638)
18+
TestICS03Handshake:testConnOpenAck() (gas: 1858318)
19+
TestICS03Handshake:testConnOpenConfirm() (gas: 2054253)
20+
TestICS03Handshake:testConnOpenInit() (gas: 1429882)
21+
TestICS03Handshake:testConnOpenTry() (gas: 2398932)
22+
TestICS03Handshake:testInvalidConnOpenAck() (gas: 2422020)
23+
TestICS03Handshake:testInvalidConnOpenConfirm() (gas: 2535722)
24+
TestICS03Handshake:testInvalidConnOpenInit() (gas: 783147)
25+
TestICS03Handshake:testInvalidConnOpenTry() (gas: 2342609)
2626
TestICS03Version:testCopyVersions() (gas: 558658)
2727
TestICS03Version:testFindSupportedVersion() (gas: 19400)
2828
TestICS03Version:testIsSupportedVersion() (gas: 7864)
2929
TestICS03Version:testPickVersion() (gas: 25327)
3030
TestICS03Version:testVerifyProposedVersion() (gas: 11777)
3131
TestICS03Version:testVerifySupportedFeature() (gas: 4153)
32-
TestICS04Handshake:testBindPort() (gas: 37502)
33-
TestICS04Handshake:testChanClose() (gas: 8864072)
34-
TestICS04Handshake:testChanOpenAck() (gas: 2988216)
35-
TestICS04Handshake:testChanOpenConfirm() (gas: 3170513)
36-
TestICS04Handshake:testChanOpenInit() (gas: 2274533)
37-
TestICS04Handshake:testChanOpenTry() (gas: 2759774)
38-
TestICS04Handshake:testInvalidChanOpenAck() (gas: 2115402)
39-
TestICS04Handshake:testInvalidChanOpenConfirm() (gas: 2171859)
40-
TestICS04Handshake:testInvalidChanOpenInit() (gas: 1299857)
41-
TestICS04Handshake:testInvalidChanOpenTry() (gas: 1378320)
42-
TestICS04Packet:testAcknowledgementPacket() (gas: 2393006)
43-
TestICS04Packet:testInvalidSendPacket() (gas: 2385887)
44-
TestICS04Packet:testRecvPacket() (gas: 7941501)
45-
TestICS04Packet:testRecvPacketTimeoutHeight() (gas: 2411677)
46-
TestICS04Packet:testRecvPacketTimeoutTimestamp() (gas: 2421551)
47-
TestICS04Packet:testSendPacket() (gas: 5160796)
48-
TestICS04Packet:testTimeoutOnClose() (gas: 2654567)
49-
TestICS04Upgrade:testUpgradeAuthorityCancel() (gas: 32899652)
50-
TestICS04Upgrade:testUpgradeCannotCancelWithOldErrorReceipt() (gas: 2648059)
51-
TestICS04Upgrade:testUpgradeCannotRecvNextUpgradePacket() (gas: 3322208)
52-
TestICS04Upgrade:testUpgradeCounterpartyAdvanceNextSequenceBeforeOpen() (gas: 3402742)
53-
TestICS04Upgrade:testUpgradeCrossingHelloIncompatibleProposals() (gas: 3312033)
54-
TestICS04Upgrade:testUpgradeFull() (gas: 34308220)
55-
TestICS04Upgrade:testUpgradeInit() (gas: 2362479)
56-
TestICS04Upgrade:testUpgradeNoChanges() (gas: 1950920)
57-
TestICS04Upgrade:testUpgradeOutOfSync() (gas: 3051844)
58-
TestICS04Upgrade:testUpgradeRelaySuccessAtCounterpartyFlushComplete() (gas: 3325729)
59-
TestICS04Upgrade:testUpgradeRelaySuccessAtFlushing() (gas: 3522672)
60-
TestICS04Upgrade:testUpgradeSendPacketFailAtFlushingOrFlushComplete() (gas: 2743014)
61-
TestICS04Upgrade:testUpgradeTimeoutAbortAck() (gas: 10509336)
62-
TestICS04Upgrade:testUpgradeTimeoutAbortConfirm() (gas: 11912012)
63-
TestICS04Upgrade:testUpgradeTimeoutUpgrade() (gas: 31027545)
64-
TestICS04Upgrade:testUpgradeToOrdered() (gas: 36895593)
65-
TestICS04Upgrade:testUpgradeToUnordered() (gas: 29370382)
66-
TestICS04UpgradeApp:testUpgradeAuthorizationChanneNotFound() (gas: 37604)
67-
TestICS04UpgradeApp:testUpgradeAuthorizationRePropose() (gas: 2034676)
68-
TestICS04UpgradeApp:testUpgradeAuthorizationRemove() (gas: 1859258)
69-
TestICS20:testAddressToHex(address) (runs: 256, μ: 22676, ~: 22804)
32+
TestICS04Handshake:testBindPort() (gas: 124350)
33+
TestICS04Handshake:testChanClose() (gas: 12938668)
34+
TestICS04Handshake:testChanOpenAck() (gas: 3459596)
35+
TestICS04Handshake:testChanOpenConfirm() (gas: 3770909)
36+
TestICS04Handshake:testChanOpenInit() (gas: 2543717)
37+
TestICS04Handshake:testChanOpenTry() (gas: 3100002)
38+
TestICS04Handshake:testInvalidChanOpenAck() (gas: 2439814)
39+
TestICS04Handshake:testInvalidChanOpenConfirm() (gas: 2517403)
40+
TestICS04Handshake:testInvalidChanOpenInit() (gas: 1758661)
41+
TestICS04Handshake:testInvalidChanOpenTry() (gas: 1773244)
42+
TestICS04Packet:testAcknowledgementPacket() (gas: 3351538)
43+
TestICS04Packet:testInvalidSendPacket() (gas: 3551759)
44+
TestICS04Packet:testRecvPacket() (gas: 10996525)
45+
TestICS04Packet:testRecvPacketTimeoutHeight() (gas: 3260141)
46+
TestICS04Packet:testRecvPacketTimeoutTimestamp() (gas: 3279291)
47+
TestICS04Packet:testSendPacket() (gas: 6413300)
48+
TestICS04Packet:testTimeoutOnClose() (gas: 3553791)
49+
TestICS04Upgrade:testUpgradeAuthorityCancel() (gas: 46694868)
50+
TestICS04Upgrade:testUpgradeCannotCancelWithOldErrorReceipt() (gas: 3455607)
51+
TestICS04Upgrade:testUpgradeCannotRecvNextUpgradePacket() (gas: 5244036)
52+
TestICS04Upgrade:testUpgradeCounterpartyAdvanceNextSequenceBeforeOpen() (gas: 5213202)
53+
TestICS04Upgrade:testUpgradeCrossingHelloIncompatibleProposals() (gas: 4405965)
54+
TestICS04Upgrade:testUpgradeFull() (gas: 57590258)
55+
TestICS04Upgrade:testUpgradeInit() (gas: 3068755)
56+
TestICS04Upgrade:testUpgradeNoChanges() (gas: 2471908)
57+
TestICS04Upgrade:testUpgradeOutOfSync() (gas: 3902216)
58+
TestICS04Upgrade:testUpgradeRelaySuccessAtCounterpartyFlushComplete() (gas: 5215801)
59+
TestICS04Upgrade:testUpgradeRelaySuccessAtFlushing() (gas: 5589764)
60+
TestICS04Upgrade:testUpgradeSendPacketFailAtFlushingOrFlushComplete() (gas: 4070972)
61+
TestICS04Upgrade:testUpgradeTimeoutAbortAck() (gas: 17679417)
62+
TestICS04Upgrade:testUpgradeTimeoutAbortConfirm() (gas: 21317049)
63+
TestICS04Upgrade:testUpgradeTimeoutUpgrade() (gas: 44126259)
64+
TestICS04Upgrade:testUpgradeToOrdered() (gas: 56352929)
65+
TestICS04Upgrade:testUpgradeToUnordered() (gas: 44985196)
66+
TestICS04UpgradeApp:testUpgradeAuthorizationChanneNotFound() (gas: 61712)
67+
TestICS04UpgradeApp:testUpgradeAuthorizationRePropose() (gas: 2565532)
68+
TestICS04UpgradeApp:testUpgradeAuthorizationRemove() (gas: 2473970)
69+
TestICS20:testAddressToHex(address) (runs: 256, μ: 22726, ~: 22867)
7070
TestICS20:testHexToAddress(string) (runs: 256, μ: 4776, ~: 4734)
7171
TestICS20:testIsEscapedString() (gas: 48979)
7272
TestICS20:testMarshaling() (gas: 148517)
73-
TestICS20:testParseAmount(uint256) (runs: 256, μ: 27026, ~: 21930)
73+
TestICS20:testParseAmount(uint256) (runs: 256, μ: 26449, ~: 24175)

chains/compose.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ services:
55
- 8645:8545
66
- 8646:8546
77
healthcheck:
8-
test: [ "CMD-SHELL", "grep \"00000000:2161 00000000:0000 0A\" /proc/net/tcp || exit 1" ]
8+
test: [ "CMD-SHELL", "grep \"00000000:2161 00000000:0000 0A\" /proc/net/tcp || grep \"00000000000000000000000000000000:2161 00000000000000000000000000000000:0000 0A\" /proc/net/tcp6 || exit 1" ]
99
interval: 100ms
1010
timeout: 1s
1111
retries: 100
@@ -15,7 +15,7 @@ services:
1515
- 8745:8545
1616
- 8746:8546
1717
healthcheck:
18-
test: [ "CMD-SHELL", "grep \"00000000:2161 00000000:0000 0A\" /proc/net/tcp || exit 1" ]
18+
test: [ "CMD-SHELL", "grep \"00000000:2161 00000000:0000 0A\" /proc/net/tcp || grep \"00000000000000000000000000000000:2161 00000000000000000000000000000000:0000 0A\" /proc/net/tcp6 || exit 1" ]
1919
interval: 100ms
2020
timeout: 1s
2121
retries: 100
@@ -26,7 +26,7 @@ services:
2626
- 8645:8545
2727
- 8646:8546
2828
healthcheck:
29-
test: [ "CMD-SHELL", "grep \"00000000:2161 00000000:0000 0A\" /proc/net/tcp || exit 1" ]
29+
test: [ "CMD-SHELL", "grep \"00000000:2161 00000000:0000 0A\" /proc/net/tcp || grep \"00000000000000000000000000000000:2161 00000000000000000000000000000000:0000 0A\" /proc/net/tcp6 || exit 1" ]
3030
interval: 100ms
3131
timeout: 1s
3232
retries: 100
@@ -36,7 +36,7 @@ services:
3636
- 8745:8545
3737
- 8746:8546
3838
healthcheck:
39-
test: [ "CMD-SHELL", "grep \"00000000:2161 00000000:0000 0A\" /proc/net/tcp || exit 1" ]
39+
test: [ "CMD-SHELL", "grep \"00000000:2161 00000000:0000 0A\" /proc/net/tcp || grep \"00000000000000000000000000000000:2161 00000000000000000000000000000000:0000 0A\" /proc/net/tcp6 || exit 1" ]
4040
interval: 100ms
4141
timeout: 1s
4242
retries: 100

contracts/clients/qbft/QBFTClient.sol

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ contract QBFTClient is ILightClient, ILightClientErrors {
5858
error InsuffientUntrustedValidatorsSeals(uint256 actual, uint256 threshold);
5959
/// @param length length of the signature
6060
error InvalidECDSASignatureLength(uint256 length);
61+
/// @dev An error indicating that the header is from the future
62+
error HeaderFromFuture();
6163

6264
string internal constant HEADER_TYPE_URL = "/ibc.lightclients.qbft.v1.Header";
6365
string internal constant CLIENT_STATE_TYPE_URL = "/ibc.lightclients.qbft.v1.ClientState";
@@ -208,6 +210,9 @@ contract QBFTClient is ILightClient, ILightClientErrors {
208210
) {
209211
revert LightClientConsensusStateExpired();
210212
}
213+
if (block.timestamp + clientState.max_clock_drift < parsedHeader.time) {
214+
revert HeaderFromFuture();
215+
}
211216

212217
bytes[] memory validators = verify(trustedConsensusState.validators, parsedHeader);
213218
if (validators.length == 0) {

contracts/proto/QBFT.sol

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ library IbcLightclientsQbftV1ClientState {
1313
bytes ibc_store_address;
1414
Height.Data latest_height;
1515
uint64 trusting_period;
16+
uint64 max_clock_drift;
1617
}
1718

1819
// Decoder section
@@ -72,6 +73,9 @@ library IbcLightclientsQbftV1ClientState {
7273
if (fieldId == 4) {
7374
pointer += _read_trusting_period(pointer, bs, r);
7475
} else
76+
if (fieldId == 5) {
77+
pointer += _read_max_clock_drift(pointer, bs, r);
78+
} else
7579
{
7680
pointer += ProtoBufRuntime._skip_field_decode(wireType, pointer, bs);
7781
}
@@ -150,6 +154,23 @@ library IbcLightclientsQbftV1ClientState {
150154
return sz;
151155
}
152156

157+
/**
158+
* @dev The decoder for reading a field
159+
* @param p The offset of bytes array to start decode
160+
* @param bs The bytes array to be decoded
161+
* @param r The in-memory struct
162+
* @return The number of bytes decoded
163+
*/
164+
function _read_max_clock_drift(
165+
uint256 p,
166+
bytes memory bs,
167+
Data memory r
168+
) internal pure returns (uint) {
169+
(uint64 x, uint256 sz) = ProtoBufRuntime._decode_uint64(p, bs);
170+
r.max_clock_drift = x;
171+
return sz;
172+
}
173+
153174
// struct decoder
154175
/**
155176
* @dev The decoder for reading a inner struct field
@@ -239,6 +260,15 @@ library IbcLightclientsQbftV1ClientState {
239260
);
240261
pointer += ProtoBufRuntime._encode_uint64(r.trusting_period, pointer, bs);
241262
}
263+
if (r.max_clock_drift != 0) {
264+
pointer += ProtoBufRuntime._encode_key(
265+
5,
266+
ProtoBufRuntime.WireType.Varint,
267+
pointer,
268+
bs
269+
);
270+
pointer += ProtoBufRuntime._encode_uint64(r.max_clock_drift, pointer, bs);
271+
}
242272
return pointer - offset;
243273
}
244274
// nested encoder
@@ -286,6 +316,7 @@ library IbcLightclientsQbftV1ClientState {
286316
e += 1 + ProtoBufRuntime._sz_lendelim(r.ibc_store_address.length);
287317
e += 1 + ProtoBufRuntime._sz_lendelim(Height._estimate(r.latest_height));
288318
e += 1 + ProtoBufRuntime._sz_uint64(r.trusting_period);
319+
e += 1 + ProtoBufRuntime._sz_uint64(r.max_clock_drift);
289320
return e;
290321
}
291322
// empty checker
@@ -306,6 +337,10 @@ library IbcLightclientsQbftV1ClientState {
306337
return false;
307338
}
308339

340+
if (r.max_clock_drift != 0) {
341+
return false;
342+
}
343+
309344
return true;
310345
}
311346

@@ -321,6 +356,7 @@ library IbcLightclientsQbftV1ClientState {
321356
output.ibc_store_address = input.ibc_store_address;
322357
Height.store(input.latest_height, output.latest_height);
323358
output.trusting_period = input.trusting_period;
359+
output.max_clock_drift = input.max_clock_drift;
324360

325361
}
326362

0 commit comments

Comments
 (0)