Skip to content

Commit

Permalink
fix: architecture updated (#214)
Browse files Browse the repository at this point in the history
* fix: architecture updated

* fix: use connectio sn

* style: formatting code
  • Loading branch information
gcranju authored Dec 18, 2023
1 parent 6d2ec07 commit aec797f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
29 changes: 15 additions & 14 deletions contracts/evm/contracts/adapters/CentralizedConnection.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import "openzeppelin-contracts-upgradeable/contracts/proxy/utils/Initializable.s
import "@xcall/utils/Types.sol";
import "@xcall/contracts/xcall/interfaces/IConnection.sol";
import "@iconfoundation/btp2-solidity-library/interfaces/ICallService.sol";
pragma solidity ^0.8.0;

contract CentralizedConnection is Initializable, IConnection {
mapping(string => uint256) private messageFees;
mapping(string => uint256) private responseFees;
mapping(string => mapping(uint256 => bool)) receipts;
address private xCall;
address private adminAddress;
uint256 public connSn;

event Message(string targetNetwork, int256 sn, bytes _msg);
event Message(string targetNetwork, uint256 sn, bytes _msg);

modifier onlyAdmin() {
require(msg.sender == this.admin(), "OnlyRelayer");
Expand Down Expand Up @@ -65,7 +65,7 @@ contract CentralizedConnection is Initializable, IConnection {
@param sn : positive for two-way message, zero for one-way message, negative for response
@param to String ( Network Id of destination network )
@param svc String ( name of the service )
@param sn Integer ( serial number of the message )
@param sn Integer ( serial number of the xcall message )
@param _msg Bytes ( serialized bytes of Service Message )
*/
function sendMessage(
Expand All @@ -76,28 +76,29 @@ contract CentralizedConnection is Initializable, IConnection {
) external payable override {
require(msg.sender == xCall, "Only Xcall can call sendMessage");
uint256 fee;
if (sn >= 0) {
if (sn > 0) {
fee = this.getFee(to, true);
} else {
} else if (sn == 0) {
fee = this.getFee(to, false);
}
require(msg.value >= fee, "Fee is not Sufficient");
emit Message(to, sn, _msg);
connSn++;
emit Message(to, connSn, _msg);
}

/**
@notice Sends the message to a xCall.
@param srcNetwork String ( Network Id )
@param sn Integer ( serial number of the message )
@param _connSn Integer ( connection message sn )
@param _msg Bytes ( serialized bytes of Service Message )
*/
function recvMessage(
string memory srcNetwork,
uint256 sn,
uint256 _connSn,
bytes calldata _msg
) public onlyAdmin {
require(!receipts[srcNetwork][sn], "Duplicate Message");
receipts[srcNetwork][sn] = true;
require(!receipts[srcNetwork][_connSn], "Duplicate Message");
receipts[srcNetwork][_connSn] = true;
ICallService(xCall).handleMessage(srcNetwork, _msg);
}

Expand All @@ -111,7 +112,7 @@ contract CentralizedConnection is Initializable, IConnection {

/**
@notice Revert a messages, used in special cases where message can't just be dropped
@param sn Integer ( serial number of the message )
@param sn Integer ( serial number of the xcall message )
*/
function revertMessage(uint256 sn) public onlyAdmin {
ICallService(xCall).handleError(sn);
Expand All @@ -120,14 +121,14 @@ contract CentralizedConnection is Initializable, IConnection {
/**
@notice Gets a message receipt
@param srcNetwork String ( Network Id )
@param sn Integer ( serial number of the message )
@param _connSn Integer ( connection message sn )
@return boolean if is has been recived or not
*/
function getReceipt(
string memory srcNetwork,
uint256 sn
uint256 _connSn
) public view returns (bool) {
return receipts[srcNetwork][sn];
return receipts[srcNetwork][_connSn];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
public class CentralizedConnection {
protected final VarDB<Address> xCall = Context.newVarDB("callService", Address.class);
protected final VarDB<Address> adminAddress = Context.newVarDB("relayer", Address.class);
private final VarDB<BigInteger> connSn = Context.newVarDB("connSn", BigInteger.class);

protected final DictDB<String, BigInteger> messageFees = Context.newDictDB("messageFees", BigInteger.class);
protected final DictDB<String, BigInteger> responseFees = Context.newDictDB("responseFees", BigInteger.class);
Expand All @@ -41,11 +42,12 @@ public CentralizedConnection(Address _relayer, Address _xCall) {
if (xCall.get() == null) {
xCall.set(_xCall);
adminAddress.set(_relayer);
connSn.set(BigInteger.ZERO);
}
}

@EventLog(indexed = 2)
public void Message(String targetNetwork, BigInteger sn, byte[] msg) {
public void Message(String targetNetwork, BigInteger connSn, byte[] msg) {
}

/**
Expand Down Expand Up @@ -92,9 +94,9 @@ public void setFee(String networkId, BigInteger messageFee, BigInteger responseF
*/
@External(readonly = true)
public BigInteger getFee(String to, boolean response) {
BigInteger messageFee = messageFees.get(to);
BigInteger messageFee = messageFees.getOrDefault(to, BigInteger.ZERO);
if (response) {
BigInteger responseFee = responseFees.get(to);
BigInteger responseFee = responseFees.getOrDefault(to, BigInteger.ZERO);
return messageFee.add(responseFee);
}
return messageFee;
Expand All @@ -106,44 +108,48 @@ public BigInteger getFee(String to, boolean response) {
* @param to Network Id of destination network
* @param svc name of the service
* @param sn positive for two-way message, zero for one-way message, negative
* for response
* for response(for xcall message)
* @param msg serialized bytes of Service Message
*/
@Payable
@External
public void sendMessage(String to, String svc, BigInteger sn, byte[] msg) {
Context.require(Context.getCaller().equals(xCall.get()), "Only xCall can send messages");
BigInteger fee = BigInteger.ZERO;
if (sn.compareTo(BigInteger.ZERO) >= 0) {
if (sn.compareTo(BigInteger.ZERO) > 0) {
fee = getFee(to, true);
} else {
} else if (sn.equals(BigInteger.ZERO)) {
fee = getFee(to, false);
}

BigInteger nextConnSn = connSn.get().add(BigInteger.ONE);
connSn.set(nextConnSn);

Context.require(Context.getValue().compareTo(fee) >= 0, "Insufficient balance");
Message(to, sn, msg);
Message(to, nextConnSn, msg);
}

/**
* Receives a message from a source network.
*
* @param srcNetwork the source network id from which the message is received
* @param sn the serial number of the message
* @param _connSn the serial number of the connection message
* @param msg serialized bytes of Service Message
*/
@Payable
@External
public void recvMessage(String srcNetwork, BigInteger sn, byte[] msg) {
public void recvMessage(String srcNetwork, BigInteger _connSn, byte[] msg) {
OnlyAdmin();
Context.require(!receipts.at(srcNetwork).getOrDefault(sn, false), "Duplicate Message");
receipts.at(srcNetwork).set(sn, true);
Context.require(!receipts.at(srcNetwork).getOrDefault(_connSn, false), "Duplicate Message");
receipts.at(srcNetwork).set(_connSn, true);
Context.call(xCall.get(), "handleMessage", srcNetwork, msg);
}

/**
* Reverts a message.
*
* @param sn the serial number of message representing the message to revert
* @param sn the serial number of xcall message representing the message to
* revert
*/
@External
public void revertMessage(BigInteger sn) {
Expand All @@ -165,12 +171,12 @@ public void claimFees() {
* Get the receipts for a given source network and serial number.
*
* @param srcNetwork the source network id
* @param sn the serial number of message
* @param _connSn the serial number of connection message
* @return the receipt if is has been recived or not
*/
@External(readonly = true)
public boolean getReceipts(String srcNetwork, BigInteger sn) {
return receipts.at(srcNetwork).getOrDefault(sn, false);
public boolean getReceipts(String srcNetwork, BigInteger _connSn) {
return receipts.at(srcNetwork).getOrDefault(_connSn, false);
}

/**
Expand Down

0 comments on commit aec797f

Please sign in to comment.