Skip to content

Commit

Permalink
refactor(protocol): signal service (#12944)
Browse files Browse the repository at this point in the history
* initial

* more

* fix tests

* fix tests

* fix tests

* fix tests

* more

* more

* more

* more

* more

* more

* more

* more

* Update LibTrieProof.test.ts

* Update LibTrieProof.test.ts

* fix: fix workflow errors

* feat(deployment): deploy SignalService on L1

* fix(protocol): fix an occantional error in `test:tokenomics`

* Update packages/protocol/contracts/common/AddressResolver.sol

Co-authored-by: David <david@taiko.xyz>

* Create SignalService.test.ts

* Update SignalService.test.ts

* Update SignalService.test.ts

* fix sendMessage which was expecting signal to be returned but isntead is now msgHash

Co-authored-by: David <david@taiko.xyz>
Co-authored-by: jeff <113397187+cyberhorsey@users.noreply.github.com>
Co-authored-by: Jeffery Walsh <cyberhorsey@gmail.com>
Co-authored-by: dave | d1onys1us <13951458+d1onys1us@users.noreply.github.com>
  • Loading branch information
5 people authored Jan 18, 2023
1 parent fde76dd commit 53adc98
Show file tree
Hide file tree
Showing 42 changed files with 753 additions and 598 deletions.
2 changes: 1 addition & 1 deletion packages/protocol/contracts/L1/libs/LibProposing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ library LibProposing {
{
uint256 fee;
(newFeeBase, fee, deposit) = getBlockFee(state, config);
TkoToken(resolver.resolve("tko_token")).burn(
TkoToken(resolver.resolve("tko_token", false)).burn(
msg.sender,
fee + deposit
);
Expand Down
11 changes: 6 additions & 5 deletions packages/protocol/contracts/L1/libs/LibProving.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ library LibProving {
);
require(_tx.txType == 0, "L1:anchor:type");
require(
_tx.destination == resolver.resolve(config.chainId, "taiko"),
_tx.destination ==
resolver.resolve(config.chainId, "taiko", false),
"L1:anchor:dest"
);
require(
Expand All @@ -107,7 +108,7 @@ library LibProving {
}

IProofVerifier proofVerifier = IProofVerifier(
resolver.resolve("proof_verifier")
resolver.resolve("proof_verifier", false)
);

// Check anchor tx is the 1st tx in the block
Expand Down Expand Up @@ -175,7 +176,7 @@ library LibProving {
);

IProofVerifier proofVerifier = IProofVerifier(
resolver.resolve("proof_verifier")
resolver.resolve("proof_verifier", false)
);

// Check the event is the first one in the throw-away block
Expand All @@ -200,7 +201,7 @@ library LibProving {
LibReceiptDecoder.Log memory log = receipt.logs[0];
require(
log.contractAddress ==
resolver.resolve(config.chainId, "taiko"),
resolver.resolve(config.chainId, "taiko", false),
"L1:receipt:addr"
);
require(log.data.length == 0, "L1:receipt:data");
Expand Down Expand Up @@ -249,7 +250,7 @@ library LibProving {
require(
proofVerifier.verifyZKP({
verificationKey: ConfigManager(
resolver.resolve("config_manager")
resolver.resolve("config_manager", false)
).getValue(string(abi.encodePacked("zk_vkey_", i))),
zkproof: evidence.proofs[i],
blockHash: blockHash,
Expand Down
4 changes: 3 additions & 1 deletion packages/protocol/contracts/L1/libs/LibVerifying.sol
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ library LibVerifying {
proposedAt: target.proposedAt
});

TkoToken tkoToken = TkoToken(resolver.resolve("tko_token"));
TkoToken tkoToken = TkoToken(
resolver.resolve("tko_token", false)
);

_rewardProvers(fc, reward, tkoToken);
_refundProposerDeposit(target, tRelBp, tkoToken);
Expand Down
60 changes: 16 additions & 44 deletions packages/protocol/contracts/bridge/Bridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import "./libs/LibBridgeData.sol";
import "./libs/LibBridgeProcess.sol";
import "./libs/LibBridgeRetry.sol";
import "./libs/LibBridgeSend.sol";
import "./libs/LibBridgeSignal.sol";
import "./libs/LibBridgeStatus.sol";

/**
Expand All @@ -39,7 +38,7 @@ contract Bridge is EssentialContract, IBridge {
*********************/

event MessageStatusChanged(
bytes32 indexed signal,
bytes32 indexed msgHash,
LibBridgeStatus.MessageStatus status
);

Expand All @@ -59,7 +58,7 @@ contract Bridge is EssentialContract, IBridge {

function sendMessage(
Message calldata message
) external payable nonReentrant returns (bytes32 signal) {
) external payable nonReentrant returns (bytes32 msgHash) {
return
LibBridgeSend.sendMessage({
state: state,
Expand All @@ -68,11 +67,6 @@ contract Bridge is EssentialContract, IBridge {
});
}

function sendSignal(bytes32 signal) external override {
LibBridgeSignal.sendSignal(msg.sender, signal);
emit SignalSent(msg.sender, signal);
}

function processMessage(
Message calldata message,
bytes calldata proof
Expand Down Expand Up @@ -103,54 +97,28 @@ contract Bridge is EssentialContract, IBridge {
* Public Functions *
*********************/

function isMessageSent(bytes32 signal) public view virtual returns (bool) {
return LibBridgeSignal.isSignalSent(address(this), signal);
function isMessageSent(bytes32 msgHash) public view virtual returns (bool) {
return LibBridgeSend.isMessageSent(AddressResolver(this), msgHash);
}

function isMessageReceived(
bytes32 signal,
uint256 srcChainId,
bytes calldata proof
) public view virtual override returns (bool) {
address srcBridge = resolve(srcChainId, "bridge");
return
LibBridgeSignal.isSignalReceived({
resolver: AddressResolver(this),
srcBridge: srcBridge,
sender: srcBridge,
signal: signal,
proof: proof
});
}

function isSignalSent(
address sender,
bytes32 signal
) public view virtual override returns (bool) {
return LibBridgeSignal.isSignalSent(sender, signal);
}

function isSignalReceived(
bytes32 signal,
bytes32 msgHash,
uint256 srcChainId,
address sender,
bytes calldata proof
) public view virtual override returns (bool) {
address srcBridge = resolve(srcChainId, "bridge");
return
LibBridgeSignal.isSignalReceived({
LibBridgeSend.isMessageReceived({
resolver: AddressResolver(this),
srcBridge: srcBridge,
sender: sender,
signal: signal,
msgHash: msgHash,
srcChainId: srcChainId,
proof: proof
});
}

function getMessageStatus(
bytes32 signal
bytes32 msgHash
) public view virtual returns (LibBridgeStatus.MessageStatus) {
return LibBridgeStatus.getMessageStatus(signal);
return LibBridgeStatus.getMessageStatus(msgHash);
}

function context() public view returns (Context memory) {
Expand All @@ -162,9 +130,13 @@ contract Bridge is EssentialContract, IBridge {
LibBridgeSend.isDestChainEnabled(AddressResolver(this), _chainId);
}

function hashMessage(Message memory message) public pure returns (bytes32) {
return LibBridgeData.hashMessage(message);
}

function getMessageStatusSlot(
bytes32 signal
bytes32 msgHash
) public pure returns (bytes32) {
return LibBridgeStatus.getMessageStatusSlot(signal);
return LibBridgeStatus.getMessageStatusSlot(msgHash);
}
}
36 changes: 8 additions & 28 deletions packages/protocol/contracts/bridge/IBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,50 +31,30 @@ interface IBridge {
}

struct Context {
bytes32 signal; // messageHash
bytes32 msgHash; // messageHash
address sender;
uint256 srcChainId;
}

event SignalSent(address sender, bytes32 signal);
event SignalSent(address sender, bytes32 msgHash);

event MessageSent(bytes32 indexed signal, Message message);
event MessageSent(bytes32 indexed msgHash, Message message);

/// Sends a message to the destination chain and takes custody
/// of Ether required in this contract. All extra Ether will be refunded.
function sendMessage(
Message memory message
) external payable returns (bytes32 signal);
) external payable returns (bytes32 msgHash);

/// Stores a signal on the bridge contract and emits an event for the
/// relayer to pick up.
function sendSignal(bytes32 signal) external;

/// Checks if a signal has been stored on the bridge contract by the
/// Checks if a msgHash has been stored on the bridge contract by the
/// current address.
function isMessageSent(bytes32 signal) external view returns (bool);
function isMessageSent(bytes32 msgHash) external view returns (bool);

/// Checks if a signal has been received on the destination chain and
/// Checks if a msgHash has been received on the destination chain and
/// sent by the src chain.
function isMessageReceived(
bytes32 signal,
uint256 srcChainId,
bytes calldata proof
) external view returns (bool);

/// Checks if a signal has been stored on the bridge contract by the
/// specified address.
function isSignalSent(
address sender,
bytes32 signal
) external view returns (bool);

/// Check if a signal has been received on the destination chain and sent
/// by the specified sender.
function isSignalReceived(
bytes32 signal,
bytes32 msgHash,
uint256 srcChainId,
address sender,
bytes calldata proof
) external view returns (bool);

Expand Down
14 changes: 8 additions & 6 deletions packages/protocol/contracts/bridge/TokenVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ contract TokenVault is EssentialContract {
string memory memo
) external payable nonReentrant {
require(
to != address(0) && to != resolve(destChainId, "token_vault"),
to != address(0) &&
to != resolve(destChainId, "token_vault", false),
"V:to"
);
require(msg.value > processingFee, "V:msgValue");
Expand All @@ -138,7 +139,7 @@ contract TokenVault is EssentialContract {

// Ether are held by the Bridge on L1 and by the EtherVault on L2, not
// the TokenVault
bytes32 signal = IBridge(resolve("bridge")).sendMessage{
bytes32 signal = IBridge(resolve("bridge", false)).sendMessage{
value: msg.value
}(message);

Expand Down Expand Up @@ -175,7 +176,8 @@ contract TokenVault is EssentialContract {
string memory memo
) external payable nonReentrant {
require(
to != address(0) && to != resolve(destChainId, "token_vault"),
to != address(0) &&
to != resolve(destChainId, "token_vault", false),
"V:to"
);
require(token != address(0), "V:token");
Expand Down Expand Up @@ -209,7 +211,7 @@ contract TokenVault is EssentialContract {
message.destChainId = destChainId;
message.owner = msg.sender;

message.to = resolve(destChainId, "token_vault");
message.to = resolve(destChainId, "token_vault", false);
message.data = abi.encodeWithSelector(
TokenVault.receiveERC20.selector,
canonicalToken,
Expand All @@ -224,7 +226,7 @@ contract TokenVault is EssentialContract {
message.refundAddress = refundAddress;
message.memo = memo;

bytes32 signal = IBridge(resolve("bridge")).sendMessage{
bytes32 signal = IBridge(resolve("bridge", false)).sendMessage{
value: msg.value
}(message);

Expand All @@ -249,7 +251,7 @@ contract TokenVault is EssentialContract {
) external nonReentrant onlyFromNamed("bridge") {
IBridge.Context memory ctx = IBridge(msg.sender).context();
require(
ctx.sender == resolve(ctx.srcChainId, "token_vault"),
ctx.sender == resolve(ctx.srcChainId, "token_vault", false),
"V:sender"
);

Expand Down
6 changes: 3 additions & 3 deletions packages/protocol/contracts/bridge/libs/LibBridgeData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ library LibBridgeData {
uint256[46] __gap;
}

bytes32 internal constant SIGNAL_PLACEHOLDER = bytes32(uint256(1));
bytes32 internal constant MESSAGE_HASH_PLACEHOLDER = bytes32(uint256(1));
uint256 internal constant CHAINID_PLACEHOLDER = type(uint256).max;
address internal constant SRC_CHAIN_SENDER_PLACEHOLDER =
0x0000000000000000000000000000000000000001;
address(uint160(uint256(1)));

// Note: These events must match the ones defined in Bridge.sol.
event MessageSent(bytes32 indexed signal, IBridge.Message message);
event MessageSent(bytes32 indexed msgHash, IBridge.Message message);

event DestChainEnabled(uint256 indexed chainId, bool enabled);

Expand Down
6 changes: 3 additions & 3 deletions packages/protocol/contracts/bridge/libs/LibBridgeInvoke.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ library LibBridgeInvoke {
function invokeMessageCall(
LibBridgeData.State storage state,
IBridge.Message memory message,
bytes32 signal,
bytes32 msgHash,
uint256 gasLimit
) internal returns (bool success) {
require(gasLimit > 0, "B:gasLimit");

state.ctx = IBridge.Context({
signal: signal,
msgHash: msgHash,
sender: message.sender,
srcChainId: message.srcChainId
});
Expand All @@ -40,7 +40,7 @@ library LibBridgeInvoke {
);

state.ctx = IBridge.Context({
signal: LibBridgeData.SIGNAL_PLACEHOLDER,
msgHash: LibBridgeData.MESSAGE_HASH_PLACEHOLDER,
sender: LibBridgeData.SRC_CHAIN_SENDER_PLACEHOLDER,
srcChainId: LibBridgeData.CHAINID_PLACEHOLDER
});
Expand Down
Loading

0 comments on commit 53adc98

Please sign in to comment.