@@ -16,7 +16,6 @@ import "./interfaces/ILLMOracle.sol";
1616 * - Slashing mechanism for false challenges
1717 */
1818contract TransactionManager {
19- // Events
2019 event ProposalSubmitted (bytes32 indexed proposalId , string transaction , address indexed submitter );
2120 event ProposalOptimisticallyApproved (bytes32 indexed proposalId );
2221 event ProposalChallenged (bytes32 indexed proposalId , address indexed challenger );
@@ -27,7 +26,28 @@ contract TransactionManager {
2726 event ChallengeResolved (bytes32 indexed proposalId , bool approved , uint256 yesVotes , uint256 noVotes );
2827 event ValidatorSlashed (bytes32 indexed proposalId , address indexed challenger , uint256 amount , bool wasHonest );
2928
30- // State variables
29+ error InvalidValidatorFactory ();
30+ error InvalidLLMOracle ();
31+ error EmptyTransaction ();
32+ error ProposalAlreadyExists ();
33+ error ChallengePeriodExpired ();
34+ error NotEnoughValidators ();
35+ error NotAValidator ();
36+ error AlreadyVoted ();
37+ error InvalidSignature ();
38+ error ChallengePeriodNotEnded ();
39+ error UseResolveChallengeForVotingProposals ();
40+ error InvalidProposalStateForFinalization ();
41+ error VotingPeriodNotEnded ();
42+ error InvalidProposalState ();
43+ error ProposalNotFound ();
44+ error NotASelectedValidator ();
45+ error AlreadySigned ();
46+ error VotingPeriodExpired ();
47+ error NotInVotingPeriod ();
48+ error InvalidSignatureLength ();
49+ error InvalidSignatureV ();
50+
3151 ValidatorFactory public validatorFactory;
3252 ILLMOracle public llmOracle;
3353
@@ -39,14 +59,12 @@ contract TransactionManager {
3959 mapping (bytes32 => address []) public proposalVoters;
4060 uint256 public proposalCount;
4161
42- // Constants
4362 uint256 public constant CHALLENGE_PERIOD = 10 ; // blocks - as per requirement
4463 uint256 public constant VOTING_PERIOD = 30 ; // blocks for voting after challenge
4564 uint256 public constant REQUIRED_SIGNATURES = 3 ; // 3 out of 5 validators
4665 uint256 public constant VALIDATOR_SET_SIZE = 5 ; // top 5 validators for consensus
4766 uint256 public constant SLASH_PERCENTAGE = 10 ; // 10% slash for false challenges
4867
49- // Proposal states
5068 enum ProposalState {
5169 Proposed, // Just submitted
5270 OptimisticApproved, // Enough signatures, optimistically approved
@@ -56,7 +74,6 @@ contract TransactionManager {
5674 Reverted // Proposal was invalid/rejected
5775 }
5876
59- // Structs
6077 struct Proposal {
6178 bytes32 proposalId;
6279 string transaction;
@@ -75,8 +92,8 @@ contract TransactionManager {
7592 }
7693
7794 constructor (address _validatorFactory , address _llmOracle ) {
78- require (_validatorFactory != address (0 ), " Invalid validator factory " );
79- require (_llmOracle != address (0 ), " Invalid LLM oracle " );
95+ if (_validatorFactory == address (0 )) revert InvalidValidatorFactory ( );
96+ if (_llmOracle == address (0 )) revert InvalidLLMOracle ( );
8097
8198 validatorFactory = ValidatorFactory (_validatorFactory);
8299 llmOracle = ILLMOracle (_llmOracle);
@@ -88,14 +105,14 @@ contract TransactionManager {
88105 * @return proposalId Unique identifier for the proposal
89106 */
90107 function submitProposal (string calldata transaction ) external returns (bytes32 proposalId ) {
91- require (bytes (transaction).length > 0 , " Empty transaction " );
108+ if (bytes (transaction).length == 0 ) revert EmptyTransaction ( );
92109
93110 proposalId = keccak256 (abi.encodePacked (transaction, block .timestamp , msg .sender ));
94- require (proposals[proposalId].proposalId == bytes32 (0 ), " Proposal already exists " );
111+ if (proposals[proposalId].proposalId != bytes32 (0 )) revert ProposalAlreadyExists ( );
95112
96113 // Get top validators for this proposal
97114 address [] memory topValidators = _getTopValidators ();
98- require (topValidators.length >= REQUIRED_SIGNATURES, " Not enough validators " );
115+ if (topValidators.length < REQUIRED_SIGNATURES) revert NotEnoughValidators ( );
99116
100117 // Perform LLM validation using external oracle
101118 bool llmResult = llmOracle.validateTransaction (transaction);
@@ -132,18 +149,18 @@ contract TransactionManager {
132149 */
133150 function signProposal (bytes32 proposalId , bytes calldata signature ) external {
134151 Proposal storage proposal = proposals[proposalId];
135- require (proposal.proposalId != bytes32 (0 ), " Proposal not found " );
136- require (proposal.state == ProposalState.Proposed, " Invalid proposal state " );
137- require (block .number <= proposal.challengeDeadline, " Challenge period expired " );
138- require ( ! hasValidatorSigned[proposalId][msg .sender ], " Already signed " );
152+ if (proposal.proposalId == bytes32 (0 )) revert ProposalNotFound ( );
153+ if (proposal.state != ProposalState.Proposed) revert InvalidProposalState ( );
154+ if (block .number > proposal.challengeDeadline) revert ChallengePeriodExpired ( );
155+ if ( hasValidatorSigned[proposalId][msg .sender ]) revert AlreadySigned ( );
139156
140157 // Verify that sender is one of the selected validators for this proposal
141- require ( _isSelectedValidator (proposalId, msg .sender ), " Not a selected validator " );
158+ if ( ! _isSelectedValidator (proposalId, msg .sender )) revert NotASelectedValidator ( );
142159
143160 // Verify the signature
144161 bytes32 messageHash = _getProposalHash (proposalId, proposal.transaction);
145162 address recoveredSigner = _recoverSigner (messageHash, signature);
146- require (recoveredSigner == msg .sender , " Invalid signature " );
163+ if (recoveredSigner != msg .sender ) revert InvalidSignature ( );
147164
148165 // Record the signature
149166 hasValidatorSigned[proposalId][msg .sender ] = true ;
@@ -166,10 +183,10 @@ contract TransactionManager {
166183 */
167184 function challengeProposal (bytes32 proposalId ) external {
168185 Proposal storage proposal = proposals[proposalId];
169- require (proposal.proposalId != bytes32 (0 ), " Proposal not found " );
170- require (proposal.state == ProposalState.OptimisticApproved, " Cannot challenge " );
171- require (block .number <= proposal.challengeDeadline, " Challenge period expired " );
172- require ( validatorFactory.isActiveValidator (msg .sender ), " Not a validator " );
186+ if (proposal.proposalId == bytes32 (0 )) revert ProposalNotFound ( );
187+ if (proposal.state != ProposalState.OptimisticApproved) revert InvalidProposalState ( );
188+ if (block .number > proposal.challengeDeadline) revert ChallengePeriodExpired ( );
189+ if ( ! validatorFactory.isActiveValidator (msg .sender )) revert NotAValidator ( );
173190
174191 proposal.state = ProposalState.Voting;
175192 proposal.challenger = msg .sender ;
@@ -187,16 +204,16 @@ contract TransactionManager {
187204 */
188205 function submitVote (bytes32 proposalId , bool support , bytes calldata signature ) external {
189206 Proposal storage proposal = proposals[proposalId];
190- require (proposal.proposalId != bytes32 (0 ), " Proposal not found " );
191- require (proposal.state == ProposalState.Voting, " Not in voting period " );
192- require (block .number <= proposal.votingDeadline, " Voting period expired " );
193- require ( validatorFactory.isActiveValidator (msg .sender ), " Not a validator " );
194- require ( ! hasValidatorVoted[proposalId][msg .sender ], " Already voted " );
207+ if (proposal.proposalId == bytes32 (0 )) revert ProposalNotFound ( );
208+ if (proposal.state != ProposalState.Voting) revert InvalidProposalState ( );
209+ if (block .number > proposal.votingDeadline) revert VotingPeriodExpired ( );
210+ if ( ! validatorFactory.isActiveValidator (msg .sender )) revert NotAValidator ( );
211+ if ( hasValidatorVoted[proposalId][msg .sender ]) revert AlreadyVoted ( );
195212
196213 // Verify the vote signature
197214 bytes32 voteHash = _getVoteHash (proposalId, support);
198215 address recoveredSigner = _recoverSigner (voteHash, signature);
199- require (recoveredSigner == msg .sender , " Invalid vote signature " );
216+ if (recoveredSigner != msg .sender ) revert InvalidSignature ( );
200217
201218 // Record the vote
202219 hasValidatorVoted[proposalId][msg .sender ] = true ;
@@ -218,9 +235,9 @@ contract TransactionManager {
218235 */
219236 function resolveChallenge (bytes32 proposalId ) external {
220237 Proposal storage proposal = proposals[proposalId];
221- require (proposal.proposalId != bytes32 (0 ), " Proposal not found " );
222- require (proposal.state == ProposalState.Voting, " Not in voting state " );
223- require (block .number > proposal.votingDeadline, " Voting period not ended " );
238+ if (proposal.proposalId == bytes32 (0 )) revert ProposalNotFound ( );
239+ if (proposal.state != ProposalState.Voting) revert InvalidProposalState ( );
240+ if (block .number <= proposal.votingDeadline) revert VotingPeriodNotEnded ( );
224241
225242 uint256 totalVotes = proposal.yesVotes + proposal.noVotes;
226243 bool approved = false ;
@@ -254,21 +271,21 @@ contract TransactionManager {
254271 */
255272 function finalizeProposal (bytes32 proposalId ) external {
256273 Proposal storage proposal = proposals[proposalId];
257- require (proposal.proposalId != bytes32 (0 ), " Proposal not found " );
274+ if (proposal.proposalId == bytes32 (0 )) revert ProposalNotFound ( );
258275
259276 bool approved = false ;
260277
261278 if (proposal.state == ProposalState.OptimisticApproved) {
262- require (block .number > proposal.challengeDeadline, " Challenge period not ended " );
279+ if (block .number <= proposal.challengeDeadline) revert ChallengePeriodNotEnded ( );
263280 // No challenge during period - approve
264281 approved = true ;
265282 proposal.executed = true ;
266283 proposal.state = ProposalState.Finalized;
267284 } else if (proposal.state == ProposalState.Voting) {
268285 // Should use resolveChallenge instead
269- revert ( " Use resolveChallenge for voting proposals " );
286+ revert UseResolveChallengeForVotingProposals ( );
270287 } else {
271- revert ( " Invalid proposal state for finalization " );
288+ revert InvalidProposalStateForFinalization ( );
272289 }
273290
274291 emit ProposalFinalized (proposalId, approved);
@@ -363,7 +380,7 @@ contract TransactionManager {
363380 * @return signer Recovered signer address
364381 */
365382 function _recoverSigner (bytes32 messageHash , bytes memory signature ) internal pure returns (address ) {
366- require (signature.length == 65 , " Invalid signature length " );
383+ if (signature.length != 65 ) revert InvalidSignatureLength ( );
367384
368385 bytes32 r;
369386 bytes32 s;
@@ -379,7 +396,7 @@ contract TransactionManager {
379396 v += 27 ;
380397 }
381398
382- require (v == 27 || v == 28 , " Invalid signature v value " );
399+ if (v != 27 && v != 28 ) revert InvalidSignatureV ( );
383400
384401 return ecrecover (messageHash, v, r, s);
385402 }
0 commit comments