Skip to content

Commit

Permalink
Adapt client verification and update to fit java score
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonAndell committed Mar 24, 2023
1 parent d577833 commit ead6708
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 311 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,15 @@ public String _createClient(MsgCreateClient msg) {
btpNetworkId.set(clientId, msg.getBtpNetworkId());
ILightClient client = getClient(clientId);
UpdateClientResponse response = client.createClient(clientId, msg.getClientState(), msg.getConsensusState());
Context.require(response.isOk());

byte[] clientKey = IBCCommitment.clientStateCommitmentKey(clientId);
Height updateHeight = Height.decode(response.getUpdate().getHeight());
Height updateHeight = Height.decode(response.getHeight());
byte[] consensusKey = IBCCommitment.consensusStateCommitmentKey(clientId,
updateHeight.getRevisionNumber(),
updateHeight.getRevisionHeight());

sendBTPMessage(clientId, ByteUtil.join(clientKey, response.getClientStateCommitment()));
sendBTPMessage(clientId, ByteUtil.join(consensusKey, response.getUpdate().getConsensusStateCommitment()));
sendBTPMessage(clientId, ByteUtil.join(consensusKey, response.getConsensusStateCommitment()));

return clientId;
}
Expand All @@ -56,19 +55,18 @@ public byte[] _updateClient(MsgUpdateClient msg) {
ILightClient client = getClient(clientId);

UpdateClientResponse response = client.updateClient(clientId, msg.getClientMessage());
Context.require(response.isOk());

byte[] clientKey = IBCCommitment.clientStateCommitmentKey(clientId);

Height updateHeight = Height.decode(response.getUpdate().getHeight());
Height updateHeight = Height.decode(response.getHeight());
byte[] consensusKey = IBCCommitment.consensusStateCommitmentKey(clientId,
updateHeight.getRevisionNumber(),
updateHeight.getRevisionHeight());

sendBTPMessage(clientId, ByteUtil.join(clientKey, response.getClientStateCommitment()));
sendBTPMessage(clientId, ByteUtil.join(consensusKey, response.getUpdate().getConsensusStateCommitment()));
sendBTPMessage(clientId, ByteUtil.join(consensusKey, response.getConsensusStateCommitment()));

return response.getUpdate().getHeight();
return response.getHeight();
}

private String generateClientIdentifier(String clientType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public byte[] _connectionOpenConfirm(MsgConnectionOpenConfirm msg) {
private void verifyClientState(ConnectionEnd connection, byte[] height, byte[] path, byte[] proof,
byte[] clientStatebytes) {
ILightClient client = getClient(connection.getClientId());
boolean ok = client.verifyMembership(
client.verifyMembership(
connection.getClientId(),
height,
BigInteger.ZERO,
Expand All @@ -201,7 +201,6 @@ private void verifyClientState(ConnectionEnd connection, byte[] height, byte[] p
connection.getCounterparty().getPrefix().getKeyPrefix(),
path,
clientStatebytes);
Context.require(ok, "failed to verify clientState");
}

private void verifyClientConsensusState(ConnectionEnd connection, byte[] height, Height consensusHeight,
Expand All @@ -211,7 +210,7 @@ private void verifyClientConsensusState(ConnectionEnd connection, byte[] height,
consensusHeight.getRevisionHeight());

ILightClient client = getClient(connection.getClientId());
boolean ok = client.verifyMembership(
client.verifyMembership(
connection.getClientId(),
height,
BigInteger.ZERO,
Expand All @@ -220,14 +219,12 @@ private void verifyClientConsensusState(ConnectionEnd connection, byte[] height,
connection.getCounterparty().getPrefix().getKeyPrefix(),
consensusPath,
consensusStateBytes);
Context.require(ok, "failed to verify consensus state");

}

private void verifyConnectionState(ConnectionEnd connection, byte[] height, byte[] proof, String connectionId,
ConnectionEnd counterpartyConnection) {
ILightClient client = getClient(connection.getClientId());
boolean ok = client.verifyMembership(
client.verifyMembership(
connection.getClientId(),
height,
BigInteger.ZERO,
Expand All @@ -236,7 +233,6 @@ private void verifyConnectionState(ConnectionEnd connection, byte[] height, byte
connection.getCounterparty().getPrefix().getKeyPrefix(),
IBCCommitment.connectionPath(connectionId),
counterpartyConnection.encode());
Context.require(ok, "failed to verify connection state");
}

/* Internal functions */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ private void verifyChannelState(
String channelId,
Channel channel) {
ILightClient client = getClient(connection.getClientId());
boolean ok = client.verifyMembership(
client.verifyMembership(
connection.getClientId(),
height,
BigInteger.ZERO,
Expand All @@ -273,7 +273,6 @@ private void verifyChannelState(
connection.getCounterparty().getPrefix().getKeyPrefix(),
IBCCommitment.channelPath(portId, channelId),
channel.encode());
Context.require(ok, "failed to verify channel state");

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ private void verifyPacketCommitment(
byte[] path,
byte[] commitmentBytes) {
ILightClient client = getClient(connection.getClientId());
boolean ok = client.verifyMembership(
client.verifyMembership(
connection.getClientId(),
height,
connection.getDelayPeriod(),
Expand All @@ -333,7 +333,6 @@ private void verifyPacketCommitment(
connection.getCounterparty().getPrefix().getKeyPrefix(),
path,
commitmentBytes);
Context.require(ok, "failed to verify packet commitment");
}

private void verifyPacketAcknowledgement(
Expand All @@ -343,7 +342,7 @@ private void verifyPacketAcknowledgement(
byte[] path,
byte[] acknowledgementCommitmentBytes) {
ILightClient client = getClient(connection.getClientId());
boolean ok = client.verifyMembership(
client.verifyMembership(
connection.getClientId(),
height,
connection.getDelayPeriod(),
Expand All @@ -352,7 +351,6 @@ private void verifyPacketAcknowledgement(
connection.getCounterparty().getPrefix().getKeyPrefix(),
path,
acknowledgementCommitmentBytes);
Context.require(ok, "failed to verify packet acknowledgement commitment");
}

private void verifyNextSequenceRecv(
Expand All @@ -362,7 +360,7 @@ private void verifyNextSequenceRecv(
byte[] path,
byte[] commitmentBytes) {
ILightClient client = getClient(connection.getClientId());
boolean ok = client.verifyMembership(
client.verifyMembership(
connection.getClientId(),
height,
connection.getDelayPeriod(),
Expand All @@ -371,7 +369,6 @@ private void verifyNextSequenceRecv(
connection.getCounterparty().getPrefix().getKeyPrefix(),
path,
commitmentBytes);
Context.require(ok, "failed to verify next sequence");
}

private void verifyPacketReceiptAbsence(
Expand All @@ -380,15 +377,14 @@ private void verifyPacketReceiptAbsence(
byte[] proof,
byte[] path) {
ILightClient client = getClient(connection.getClientId());
boolean ok = client.verifyNonMembership(
client.verifyNonMembership(
connection.getClientId(),
height,
connection.getDelayPeriod(),
calcBlockDelay(connection.getDelayPeriod()),
proof,
connection.getCounterparty().getPrefix().getKeyPrefix(),
path);
Context.require(ok, "failed to verify receipt absence");
}

/* Internal functions */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import ibc.icon.interfaces.ILightClientScoreInterface;
import ibc.icon.score.util.ByteUtil;
import ibc.icon.interfaces.ILightClient;
import ibc.icon.structs.messages.ConsensusStateUpdate;
import ibc.icon.structs.messages.MsgCreateClient;
import ibc.icon.structs.messages.MsgUpdateClient;
import ibc.icon.structs.messages.UpdateClientResponse;
Expand Down Expand Up @@ -97,8 +96,7 @@ void createClient() {
Height consensusHeight = Height.newBuilder()
.setRevisionHeight(1)
.setRevisionNumber(2).build();
ConsensusStateUpdate update = new ConsensusStateUpdate(consensusStateCommitment, consensusHeight.toByteArray());
UpdateClientResponse response = new UpdateClientResponse(clientStateCommitment, update, true);
UpdateClientResponse response = new UpdateClientResponse(clientStateCommitment, consensusStateCommitment, consensusHeight.toByteArray());
when(lightClient.mock.createClient(msg.getClientType() + "-" + BigInteger.ZERO, msg.getClientState(),
msg.getConsensusState())).thenReturn(response);

Expand Down Expand Up @@ -148,9 +146,7 @@ public void updateClient() {
.setRevisionHeight(1)
.setRevisionNumber(2).build();

ConsensusStateUpdate update = new ConsensusStateUpdate(consensusStateCommitment, consensusHeight.toByteArray());

UpdateClientResponse response = new UpdateClientResponse(clientStateCommitment, update, true);
UpdateClientResponse response = new UpdateClientResponse(clientStateCommitment, consensusStateCommitment, consensusHeight.toByteArray());

when(lightClient.mock.updateClient(msg.getClientId(), msg.getClientMessage())).thenReturn(response);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,65 +182,6 @@ void connectionOpenTry_failedConnectionStateVerification() {
assertTrue(e.getMessage().contains(expectedErrorMessage));
}

@Test
void connectionOpenTry_invalidStates() {
// Arrange
MsgConnectionOpenTry msg = new MsgConnectionOpenTry();
msg.setClientId(clientId);
msg.setCounterparty(counterparty.toByteArray());
msg.setDelayPeriod(delayPeriod);
msg.setClientStateBytes(new byte[1]);
msg.setCounterpartyVersions(new byte[][] { version.toByteArray() });
msg.setProofInit(new byte[2]);
msg.setProofClient(new byte[3]);
msg.setProofConsensus(new byte[4]);
msg.setProofHeight(proofHeight.toByteArray());
msg.setConsensusHeight(consensusHeight.toByteArray());

Counterparty expectedCounterparty = Counterparty.newBuilder()
.setClientId(msg.getClientId())
.setConnectionId("")
.setPrefix(prefix).build();

ConnectionEnd counterpartyConnection = ConnectionEnd.newBuilder()
.setClientId(counterparty.getClientId())
.addVersions(0, version)
.setState(ConnectionEnd.State.STATE_INIT)
.setDelayPeriod(msg.getDelayPeriod().longValue())
.setCounterparty(expectedCounterparty).build();

// verifyConnectionState
byte[] connectionPath = IBCCommitment.connectionPath(msg.getCounterparty().getConnectionId());
when(lightClient.mock.verifyMembership(msg.getClientId(),
msg.getProofHeightRaw(), BigInteger.ZERO,
BigInteger.ZERO, msg.getProofInit(), prefix.getKeyPrefix().toByteArray(), connectionPath,
counterpartyConnection.toByteArray()))
.thenReturn(false).thenReturn(true);

// verifyClientState
byte[] clientStatePath = IBCCommitment.clientStatePath(msg.getCounterparty().getClientId());
when(lightClient.mock.verifyMembership(msg.getClientId(), msg.getProofHeightRaw(), BigInteger.ZERO,
BigInteger.ZERO, msg.getProofClient(), prefix.getKeyPrefix().toByteArray(), clientStatePath,
msg.getClientStateBytes()))
.thenReturn(false);

// Act & Assert
String expectedErrorMessage = "failed to verify connection state";
Executable clientVerificationFailed = () -> connection.invoke(owner,
"_connectionOpenTry", msg);
AssertionError e = assertThrows(AssertionError.class,
clientVerificationFailed);
assertTrue(e.getMessage().contains(expectedErrorMessage));

expectedErrorMessage = "failed to verify clientState";
Executable stateVerificationFailed = () -> connection.invoke(owner,
"_connectionOpenTry", msg);
e = assertThrows(AssertionError.class,
stateVerificationFailed);
assertTrue(e.getMessage().contains(expectedErrorMessage));

}

@Test
void connectionOpenTry() {
// Arrange
Expand Down Expand Up @@ -270,28 +211,26 @@ void connectionOpenTry() {

String expectedConnectionId = "connection-0";

// Act
connection.invoke(owner, "_connectionOpenTry", msg);

// Assert
// verifyConnectionState
byte[] connectionPath = IBCCommitment.connectionPath(msg.getCounterparty().getConnectionId());
when(lightClient.mock.verifyMembership(msg.getClientId(),
verify(lightClient.mock).verifyMembership(msg.getClientId(),
msg.getProofHeightRaw(), BigInteger.ZERO,
BigInteger.ZERO, msg.getProofInit(), prefix.getKeyPrefix().toByteArray(), connectionPath,
counterpartyConnection.toByteArray())).thenReturn(true);
counterpartyConnection.toByteArray());

// verifyClientState
byte[] clientStatePath = IBCCommitment.clientStatePath(msg.getCounterparty().getClientId());
when(lightClient.mock.verifyMembership(msg.getClientId(), msg.getProofHeightRaw(), BigInteger.ZERO,
verify(lightClient.mock).verifyMembership(msg.getClientId(), msg.getProofHeightRaw(), BigInteger.ZERO,
BigInteger.ZERO, msg.getProofClient(), prefix.getKeyPrefix().toByteArray(), clientStatePath,
msg.getClientStateBytes()))
.thenReturn(true);

// Act
connection.invoke(owner, "_connectionOpenTry", msg);
msg.getClientStateBytes());

// Assert
ConnectionEnd expectedConnection = ConnectionEnd.newBuilder(baseConnection)
.setState(ConnectionEnd.State.STATE_TRYOPEN).build();


byte[] connectionKey = IBCCommitment.connectionCommitmentKey(expectedConnectionId);
verify(connectionSpy)
.sendBTPMessage(
Expand Down Expand Up @@ -365,23 +304,21 @@ void connectionOpenAck() {
.setDelayPeriod(delayPeriod.longValue())
.setCounterparty(expectedCounterparty).build();

// Act
connection.invoke(owner, "_connectionOpenAck", msg);

// Assert
// verifyConnectionState
byte[] connectionPath = IBCCommitment.connectionPath(msg.getCounterpartyConnectionID());
when(lightClient.mock.verifyMembership(clientId, msg.getProofHeightRaw(), BigInteger.ZERO, BigInteger.ZERO,
verify(lightClient.mock).verifyMembership(clientId, msg.getProofHeightRaw(), BigInteger.ZERO, BigInteger.ZERO,
msg.getProofTry(), prefix.getKeyPrefix().toByteArray(), connectionPath,
counterpartyConnection.toByteArray()))
.thenReturn(true);
counterpartyConnection.toByteArray());

// verifyClientState
byte[] clientStatePath = IBCCommitment.clientStatePath(counterparty.getClientId());
when(lightClient.mock.verifyMembership(clientId, msg.getProofHeightRaw(), BigInteger.ZERO, BigInteger.ZERO,
msg.getProofClient(), prefix.getKeyPrefix().toByteArray(), clientStatePath, msg.getClientStateBytes()))
.thenReturn(true);

// Act
connection.invoke(owner, "_connectionOpenAck", msg);
verify(lightClient.mock).verifyMembership(clientId, msg.getProofHeightRaw(), BigInteger.ZERO, BigInteger.ZERO,
msg.getProofClient(), prefix.getKeyPrefix().toByteArray(), clientStatePath, msg.getClientStateBytes());

// Assert
Counterparty counterparty = Counterparty.newBuilder(baseConnection.getCounterparty())
.setConnectionId(msg.getCounterpartyConnectionID()).build();
ConnectionEnd expectedConnection = ConnectionEnd.newBuilder(baseConnection)
Expand Down Expand Up @@ -438,17 +375,16 @@ void connectionOpenConfirm() {
.setDelayPeriod(delayPeriod.longValue())
.setCounterparty(expectedCounterparty).build();

// verifyConnectionState
byte[] connectionPath = IBCCommitment.connectionPath(counterparty.getConnectionId());
when(lightClient.mock.verifyMembership(clientId, msg.getProofHeightRaw(), BigInteger.ZERO, BigInteger.ZERO,
msg.getProofAck(), prefix.getKeyPrefix().toByteArray(), connectionPath,
counterpartyConnection.toByteArray()))
.thenReturn(true);

// Act
connection.invoke(owner, "_connectionOpenConfirm", msg);

// Assert
// verifyConnectionState
byte[] connectionPath = IBCCommitment.connectionPath(counterparty.getConnectionId());
verify(lightClient.mock).verifyMembership(clientId, msg.getProofHeightRaw(), BigInteger.ZERO, BigInteger.ZERO,
msg.getProofAck(), prefix.getKeyPrefix().toByteArray(), connectionPath,
counterpartyConnection.toByteArray());

ConnectionEnd expectedConnection = ConnectionEnd.newBuilder(baseConnection)
.setState(ConnectionEnd.State.STATE_OPEN).build();

Expand Down
Loading

0 comments on commit ead6708

Please sign in to comment.