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

feat(javascore): Adapt ibc core to icon lightclient #141

Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions contracts/javascore/ibc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dependencies {
implementation project(':lib')
implementation project(':score-util')

testImplementation 'com.google.protobuf:protobuf-javalite:3.13.0'
testImplementation 'foundation.icon:javaee-rt:0.9.3'
testImplementation("org.mockito:mockito-core:$mockitoCoreVersion")
testImplementation("org.mockito:mockito-inline:$mockitoCoreVersion")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ibc.ics02.client;

import ibc.icon.interfaces.IIBCClient;
import ibc.icon.interfaces.ILightClient;
import ibc.icon.score.util.ByteUtil;
import ibc.icon.score.util.Logger;
Expand All @@ -10,68 +9,66 @@
import ibc.icon.structs.messages.UpdateClientResponse;
import ibc.ics24.host.IBCCommitment;
import ibc.ics24.host.IBCHost;
import icon.proto.core.client.Height;
import score.Address;
import score.Context;

import java.math.BigInteger;

public class IBCClient extends IBCHost implements IIBCClient {
public class IBCClient extends IBCHost {

Logger logger = new Logger("ibc-core");
static Logger logger = new Logger("ibc-core");

public void registerClient(String clientType, Address lightClient) {
Context.require(clientRegistry.get(clientType) == null, "Already registered.");
clientRegistry.set(clientType, lightClient);
}

public String createClient(MsgCreateClient msg) {
String clientType = msg.clientType;
public String _createClient(MsgCreateClient msg) {
String clientType = msg.getClientType();
Address lightClientAddr = clientRegistry.get(clientType);
NullChecker.requireNotNull(lightClientAddr, "Register client before creation.");

String clientId = generateClientIdentifier(clientType);
logger.println("Create Client: ", " clientId: ", clientId);

clientTypes.set(clientId, msg.clientType);
clientTypes.set(clientId, msg.getClientType());
clientImplementations.set(clientId, lightClientAddr);
btpNetworkId.set(clientId, msg.getBtpNetworkId());
ILightClient client = getClient(clientId);
UpdateClientResponse response = client.createClient(clientId, msg.clientState, msg.consensusState);
Context.require(response.ok);
UpdateClientResponse response = client.createClient(clientId, msg.getClientState(), msg.getConsensusState());
Context.require(response.isOk());

byte[] clientKey = IBCCommitment.clientStateCommitmentKey(clientId);
// commitments.set(IBCCommitment.clientStateCommitmentKey(clientId),
// response.clientStateCommitment);
Height updateHeight = Height.decode(response.getUpdate().getHeight());
byte[] consensusKey = IBCCommitment.consensusStateCommitmentKey(clientId,
response.update.height.getRevisionNumber(),
response.update.height.getRevisionHeight());
// commitments.set(consensusKey, response.update.consensusStateCommitment);
updateHeight.getRevisionNumber(),
updateHeight.getRevisionHeight());

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

return clientId;
}

public void updateClient(MsgUpdateClient msg) {
String clientId = msg.clientId;
public byte[] _updateClient(MsgUpdateClient msg) {
String clientId = msg.getClientId();
ILightClient client = getClient(clientId);

// Should be required on client side
// Context.require(commitments.get(IBCCommitment.clientStateCommitmentKey(clientId))
// != null);
UpdateClientResponse response = client.updateClient(clientId, msg.clientMessage);
Context.require(response.ok);
UpdateClientResponse response = client.updateClient(clientId, msg.getClientMessage());
Context.require(response.isOk());

byte[] clientKey = IBCCommitment.clientStateCommitmentKey(clientId);
// commitments.set(IBCCommitment.clientStateCommitmentKey(clientId),
// response.clientStateCommitment);

Height updateHeight = Height.decode(response.getUpdate().getHeight());
byte[] consensusKey = IBCCommitment.consensusStateCommitmentKey(clientId,
response.update.height.getRevisionNumber(),
response.update.height.getRevisionHeight());
// commitments.set(consensusKey, response.update.consensusStateCommitment);
updateHeight.getRevisionNumber(),
updateHeight.getRevisionHeight());

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

sendBTPMessage(ByteUtil.join(clientKey, response.clientStateCommitment));
sendBTPMessage(ByteUtil.join(consensusKey, response.update.consensusStateCommitment));
return response.getUpdate().getHeight();
}

private String generateClientIdentifier(String clientType) {
Expand Down
Loading