@@ -4,47 +4,85 @@ pragma solidity ^0.8.20;
44
55import "forge-std/src/Test.sol " ;
66import "../src/core/Initiator.sol " ;
7- import "../src/core/TxAuthManager.sol " ;
8- import "../src/core/TxManager.sol " ;
9- import "../src/core/TxRunner.sol " ;
7+ import {TxManagerBase} from "../src/core/TxManagerBase.sol " ;
8+ import {TxAuthManagerBase} from "../src/core/TxAuthManagerBase.sol " ;
109import {Strings} from "@openzeppelin/contracts/utils/Strings.sol " ;
1110
1211import {
1312 MsgInitiateTx,
1413 MsgInitiateTxResponse,
1514 ContractTransaction
1615} from "../src/proto/cross/core/initiator/Initiator.sol " ;
17- import {Account as AuthAccount, AuthType} from "../src/proto/cross/core/auth/Auth.sol " ;
16+ import {Account as AuthAccount, AuthType, TxAuthState } from "../src/proto/cross/core/auth/Auth.sol " ;
1817import {GoogleProtobufAny} from "@hyperledger-labs/yui-ibc-solidity/contracts/proto/GoogleProtobufAny.sol " ;
1918import {Tx} from "../src/proto/cross/core/tx/Tx.sol " ;
2019import {IbcCoreClientV1Height} from "../src/proto/ibc/core/client/v1/client.sol " ;
2120
22- contract MockTxRunner is TxRunner {
23- uint256 public runCount;
21+ contract MockTxManager is TxManagerBase {
22+ mapping (bytes32 => bool ) public txExists;
23+ bytes32 public lastCreatedTxId;
2424 bytes32 public lastRunTxId;
25+ uint256 public createTxCount;
26+ uint256 public runTxCount;
2527
26- function _runTx (bytes32 txId , MsgInitiateTx.Data storage ) internal virtual override {
27- runCount++ ;
28+ function createTx (bytes32 txId , MsgInitiateTx.Data calldata ) internal virtual override {
29+ txExists[txId] = true ;
30+ lastCreatedTxId = txId;
31+ createTxCount++ ;
32+ }
33+
34+ function runTxIfCompleted (bytes32 txId ) internal virtual override {
2835 lastRunTxId = txId;
36+ runTxCount++ ;
37+ }
38+
39+ function isTxRecorded (bytes32 txId ) internal view virtual override returns (bool ) {
40+ return txExists[txId];
41+ }
42+
43+ function setTxExists (bytes32 txId , bool exists ) public {
44+ txExists[txId] = exists;
2945 }
3046}
3147
32- contract InitiatorHarness is Initiator , TxAuthManager , TxManager , MockTxRunner {
48+ contract MockTxAuthManager is TxAuthManagerBase {
49+ mapping (bytes32 => bool ) public completed;
50+ bytes32 public lastInitTxId;
51+ bool private _signReturns = false ;
52+
53+ function initAuthState (bytes32 txId , Account.Data[] memory ) internal virtual override {
54+ lastInitTxId = txId;
55+ completed[txId] = false ;
56+ }
57+
58+ function isCompletedAuth (bytes32 txId ) internal view virtual override returns (bool ) {
59+ return completed[txId];
60+ }
61+
62+ function sign (bytes32 txId , Account.Data[] memory ) internal virtual override returns (bool ) {
63+ if (_signReturns) {
64+ completed[txId] = true ;
65+ }
66+ return _signReturns;
67+ }
68+
69+ function getAuthState (bytes32 ) internal view virtual override returns (TxAuthState.Data memory ) {
70+ revert ("MockTxAuthManager.getAuthState not implemented " );
71+ }
72+
73+ function setSignReturns (bool returnsValue ) public {
74+ _signReturns = returnsValue;
75+ }
76+ }
77+
78+ contract InitiatorHarness is Initiator , MockTxAuthManager , MockTxManager {
3379 function exposed_getRequiredAccounts (MsgInitiateTx.Data calldata msg_ )
3480 public
3581 pure
3682 returns (AuthAccount.Data[] memory out )
3783 {
3884 return _getRequiredAccounts (msg_);
3985 }
40-
41- function exposed_isCompletedAuth (bytes32 txId ) public view returns (bool ) {
42- return isCompletedAuth (txId);
43- }
44-
45- function exposed_isTxRecorded (bytes32 txId ) public view returns (bool ) {
46- return isTxRecorded (txId);
47- }
4886}
4987
5088contract InitiatorTest is Test {
@@ -98,6 +136,8 @@ contract InitiatorTest is Test {
98136 function test_initiateTx_SucceedsAsPendingWhenSignersNotMet () public {
99137 bytes32 txIdHash = sha256 (MsgInitiateTx.encode (baseMsg));
100138
139+ harness.setSignReturns (false );
140+
101141 vm.expectEmit (true , false , false , true , address (harness));
102142 emit TxInitiated (abi.encodePacked (txIdHash), address (this ));
103143
@@ -108,23 +148,18 @@ contract InitiatorTest is Test {
108148 uint256 (MsgInitiateTxResponse.InitiateTxStatus.INITIATE_TX_STATUS_PENDING),
109149 "Status should be PENDING "
110150 );
111- assertTrue (harness.exposed_isTxRecorded (txIdHash), "Tx should be recorded " );
112- assertFalse (harness.exposed_isCompletedAuth (txIdHash), "Auth should not be completed " );
113- assertEq (harness.runCount (), 0 , "MockTxRunner should not be called " );
151+ assertTrue (harness.txExists (txIdHash), "Mock: tx should be recorded " );
152+ assertFalse (harness.completed (txIdHash), "Mock: auth should not be completed " );
153+ assertEq (harness.createTxCount (), 1 , "Mock: createTx should be called once " );
154+ assertEq (harness.lastInitTxId (), txIdHash, "Mock: initAuthState should be called with txId " );
155+ assertEq (harness.runTxCount (), 0 , "Mock: runTxIfCompleted should not be called " );
114156 }
115157
116158 function test_initiateTx_SucceedsAsVerifiedWhenSignersMet () public {
117- baseMsg.contract_transactions = new ContractTransaction.Data [](1 );
118- baseMsg.contract_transactions[0 ].signers = new AuthAccount.Data [](2 );
119- baseMsg.contract_transactions[0 ].signers[0 ] = signerA;
120- baseMsg.contract_transactions[0 ].signers[1 ] = signerB;
121-
122- baseMsg.signers = new AuthAccount.Data [](2 );
123- baseMsg.signers[0 ] = signerA;
124- baseMsg.signers[1 ] = signerB;
125-
126159 bytes32 txIdHash = sha256 (MsgInitiateTx.encode (baseMsg));
127160
161+ harness.setSignReturns (true );
162+
128163 vm.expectEmit (true , false , false , true , address (harness));
129164 emit TxInitiated (abi.encodePacked (txIdHash), address (this ));
130165
@@ -135,10 +170,12 @@ contract InitiatorTest is Test {
135170 uint256 (MsgInitiateTxResponse.InitiateTxStatus.INITIATE_TX_STATUS_VERIFIED),
136171 "Status should be VERIFIED "
137172 );
138- assertTrue (harness.exposed_isTxRecorded (txIdHash), "Tx should be recorded " );
139- assertTrue (harness.exposed_isCompletedAuth (txIdHash), "Auth should be completed " );
140- assertEq (harness.runCount (), 1 , "MockTxRunner should be called " );
141- assertEq (harness.lastRunTxId (), txIdHash, "MockTxRunner called with correct txId " );
173+ assertTrue (harness.txExists (txIdHash), "Mock: tx should be recorded " );
174+ assertTrue (harness.completed (txIdHash), "Mock: auth should be completed " );
175+ assertEq (harness.createTxCount (), 1 , "Mock: createTx should be called once " );
176+ assertEq (harness.lastInitTxId (), txIdHash, "Mock: initAuthState should be called with txId " );
177+ assertEq (harness.runTxCount (), 1 , "Mock: runTxIfCompleted should be called once " );
178+ assertEq (harness.lastRunTxId (), txIdHash, "Mock: runTxIfCompleted should be called with txId " );
142179 }
143180
144181 function test_initiateTx_RevertWhen_ChainIdMismatch () public {
@@ -174,9 +211,10 @@ contract InitiatorTest is Test {
174211 }
175212
176213 function test_initiateTx_RevertWhen_TxIdAlreadyExists () public {
177- harness.initiateTx (baseMsg);
178214 bytes32 txIdHash = sha256 (MsgInitiateTx.encode (baseMsg));
179215
216+ harness.setTxExists (txIdHash, true );
217+
180218 vm.expectRevert (abi.encodeWithSelector (IInitiator.TxIDAlreadyExists.selector , txIdHash));
181219 harness.initiateTx (baseMsg);
182220 }
0 commit comments