Skip to content

Commit

Permalink
feat: intent contracts for icon
Browse files Browse the repository at this point in the history
  • Loading branch information
0ptimizerr committed Nov 11, 2024
1 parent c7dae72 commit 5f070b0
Show file tree
Hide file tree
Showing 28 changed files with 2,412 additions and 0 deletions.
9 changes: 9 additions & 0 deletions contracts/javascore/Intent_Contracts/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# https://help.github.com/articles/dealing-with-line-endings/
#
# Linux start script should use lf
/gradlew text eol=lf

# These are Windows script files and should use crlf
*.bat text eol=crlf

5 changes: 5 additions & 0 deletions contracts/javascore/Intent_Contracts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Ignore Gradle project-specific cache directory
.gradle

# Ignore Gradle build output directory
build
40 changes: 40 additions & 0 deletions contracts/javascore/Intent_Contracts/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
dependencies {
compileOnly 'foundation.icon:javaee-api:0.9.6'
implementation 'com.github.sink772:javaee-tokens:0.6.4'
testImplementation 'foundation.icon:javaee-unittest:0.10.0'
testImplementation 'org.mockito:mockito-core:4.8.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
}

optimizedJar {
mainClassName = 'network.icon.intent.Intent'
archivesBaseName = "intent" + 1 + ".jar"
from {
configurations.runtimeClasspath.collect {it.isDirectory() ? it : zipTree(it) }
}

}

deployJar {
endpoints {
local {
uri = 'http://localhost:9080/api/v3'
nid = 0x3
}
}

keystore = rootProject.hasProperty('keystoreName') ? "$keystoreName" : ''
password = rootProject.hasProperty('keystorePass') ? "$keystorePass" : ''
}

repositories {
mavenCentral()
}


test {
useJUnitPlatform()
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package network.icon.intent;

import java.math.BigInteger;

import network.icon.intent.constants.Constant;
import score.Address;
import score.BranchDB;
import score.Context;
import score.DictDB;
import score.VarDB;
import score.annotation.EventLog;
import score.annotation.External;

public class GeneralizedConnection {
private final BranchDB<String, DictDB<BigInteger, Boolean>> receipts = Context.newBranchDB(Constant.RECEIPTS,
Boolean.class);
protected final VarDB<Address> relayAddress = Context.newVarDB(Constant.RELAY_ADDRESS, Address.class);
private final VarDB<BigInteger> connSn = Context.newVarDB(Constant.CONN_SN, BigInteger.class);

@EventLog(indexed = 3)
public void Message(String targetNetwork, BigInteger sn, byte[] _msg) {
}

protected void _sendMessage(
String to,
byte[] _msg) {
connSn.set(connSn.getOrDefault(BigInteger.ZERO).add(BigInteger.ONE));
Message(to, connSn.get(), _msg);
}

protected void _recvMessage(String srcNetwork, BigInteger _connSn) {
onlyRelay();
Context.require(receipts.at(srcNetwork).getOrDefault(_connSn, false).equals(false), "Duplicate Message");
receipts.at(srcNetwork).set(_connSn, true);
}

@External
public void setAdmin(Address _address) {
onlyRelay();
relayAddress.set(_address);
}

@External
public boolean getReceipt(
String srcNetwork,
BigInteger _connSn) {
return receipts.at(srcNetwork).getOrDefault(_connSn, false);
}

@External
public Address admin() {
return relayAddress.get();
}

public BigInteger getConnSn() {
return connSn.getOrDefault(BigInteger.ZERO);
}

void onlyRelay() {
Context.require(Context.getCaller().equals(relayAddress.get()), "OnlyRelayer");
}

}
Loading

0 comments on commit 5f070b0

Please sign in to comment.