From af9c9c71cb2ebf2a37ce5c711ff6c092c4e2e10c Mon Sep 17 00:00:00 2001 From: fredo Date: Fri, 21 Jun 2024 10:17:49 +0200 Subject: [PATCH] add tx index per eon --- gnoshcontracts/sequencer/sequencer.go | 72 +++++++++++++++++++++++---- src/ISequencer.sol | 3 +- src/Sequencer.sol | 12 +++++ test/Sequencer.t.sol | 39 ++++++++++++--- 4 files changed, 106 insertions(+), 20 deletions(-) diff --git a/gnoshcontracts/sequencer/sequencer.go b/gnoshcontracts/sequencer/sequencer.go index f426e3a..7b770dc 100644 --- a/gnoshcontracts/sequencer/sequencer.go +++ b/gnoshcontracts/sequencer/sequencer.go @@ -31,7 +31,7 @@ var ( // SequencerMetaData contains all meta data concerning the Sequencer contract. var SequencerMetaData = &bind.MetaData{ - ABI: "[{\"type\":\"function\",\"name\":\"submitDecryptionProgress\",\"inputs\":[{\"name\":\"message\",\"type\":\"bytes\",\"internalType\":\"bytes\"}],\"outputs\":[],\"stateMutability\":\"nonpayable\"},{\"type\":\"function\",\"name\":\"submitEncryptedTransaction\",\"inputs\":[{\"name\":\"eon\",\"type\":\"uint64\",\"internalType\":\"uint64\"},{\"name\":\"identityPrefix\",\"type\":\"bytes32\",\"internalType\":\"bytes32\"},{\"name\":\"encryptedTransaction\",\"type\":\"bytes\",\"internalType\":\"bytes\"},{\"name\":\"gasLimit\",\"type\":\"uint256\",\"internalType\":\"uint256\"}],\"outputs\":[],\"stateMutability\":\"payable\"},{\"type\":\"event\",\"name\":\"DecryptionProgressSubmitted\",\"inputs\":[{\"name\":\"message\",\"type\":\"bytes\",\"indexed\":false,\"internalType\":\"bytes\"}],\"anonymous\":false},{\"type\":\"event\",\"name\":\"TransactionSubmitted\",\"inputs\":[{\"name\":\"eon\",\"type\":\"uint64\",\"indexed\":false,\"internalType\":\"uint64\"},{\"name\":\"identityPrefix\",\"type\":\"bytes32\",\"indexed\":false,\"internalType\":\"bytes32\"},{\"name\":\"sender\",\"type\":\"address\",\"indexed\":false,\"internalType\":\"address\"},{\"name\":\"encryptedTransaction\",\"type\":\"bytes\",\"indexed\":false,\"internalType\":\"bytes\"},{\"name\":\"gasLimit\",\"type\":\"uint256\",\"indexed\":false,\"internalType\":\"uint256\"}],\"anonymous\":false},{\"type\":\"error\",\"name\":\"InsufficientFee\",\"inputs\":[]}]", + ABI: "[{\"type\":\"function\",\"name\":\"getTxCountForEon\",\"inputs\":[{\"name\":\"eon\",\"type\":\"uint64\",\"internalType\":\"uint64\"}],\"outputs\":[{\"name\":\"\",\"type\":\"uint64\",\"internalType\":\"uint64\"}],\"stateMutability\":\"view\"},{\"type\":\"function\",\"name\":\"submitDecryptionProgress\",\"inputs\":[{\"name\":\"message\",\"type\":\"bytes\",\"internalType\":\"bytes\"}],\"outputs\":[],\"stateMutability\":\"nonpayable\"},{\"type\":\"function\",\"name\":\"submitEncryptedTransaction\",\"inputs\":[{\"name\":\"eon\",\"type\":\"uint64\",\"internalType\":\"uint64\"},{\"name\":\"identityPrefix\",\"type\":\"bytes32\",\"internalType\":\"bytes32\"},{\"name\":\"encryptedTransaction\",\"type\":\"bytes\",\"internalType\":\"bytes\"},{\"name\":\"gasLimit\",\"type\":\"uint256\",\"internalType\":\"uint256\"}],\"outputs\":[],\"stateMutability\":\"payable\"},{\"type\":\"event\",\"name\":\"DecryptionProgressSubmitted\",\"inputs\":[{\"name\":\"message\",\"type\":\"bytes\",\"indexed\":false,\"internalType\":\"bytes\"}],\"anonymous\":false},{\"type\":\"event\",\"name\":\"TransactionSubmitted\",\"inputs\":[{\"name\":\"eon\",\"type\":\"uint64\",\"indexed\":true,\"internalType\":\"uint64\"},{\"name\":\"txIndex\",\"type\":\"uint64\",\"indexed\":true,\"internalType\":\"uint64\"},{\"name\":\"identityPrefix\",\"type\":\"bytes32\",\"indexed\":false,\"internalType\":\"bytes32\"},{\"name\":\"sender\",\"type\":\"address\",\"indexed\":false,\"internalType\":\"address\"},{\"name\":\"encryptedTransaction\",\"type\":\"bytes\",\"indexed\":false,\"internalType\":\"bytes\"},{\"name\":\"gasLimit\",\"type\":\"uint256\",\"indexed\":false,\"internalType\":\"uint256\"}],\"anonymous\":false},{\"type\":\"error\",\"name\":\"InsufficientFee\",\"inputs\":[]}]", } // SequencerABI is the input ABI used to generate the binding from. @@ -180,6 +180,37 @@ func (_Sequencer *SequencerTransactorRaw) Transact(opts *bind.TransactOpts, meth return _Sequencer.Contract.contract.Transact(opts, method, params...) } +// GetTxCountForEon is a free data retrieval call binding the contract method 0x7bbd164b. +// +// Solidity: function getTxCountForEon(uint64 eon) view returns(uint64) +func (_Sequencer *SequencerCaller) GetTxCountForEon(opts *bind.CallOpts, eon uint64) (uint64, error) { + var out []interface{} + err := _Sequencer.contract.Call(opts, &out, "getTxCountForEon", eon) + + if err != nil { + return *new(uint64), err + } + + out0 := *abi.ConvertType(out[0], new(uint64)).(*uint64) + + return out0, err + +} + +// GetTxCountForEon is a free data retrieval call binding the contract method 0x7bbd164b. +// +// Solidity: function getTxCountForEon(uint64 eon) view returns(uint64) +func (_Sequencer *SequencerSession) GetTxCountForEon(eon uint64) (uint64, error) { + return _Sequencer.Contract.GetTxCountForEon(&_Sequencer.CallOpts, eon) +} + +// GetTxCountForEon is a free data retrieval call binding the contract method 0x7bbd164b. +// +// Solidity: function getTxCountForEon(uint64 eon) view returns(uint64) +func (_Sequencer *SequencerCallerSession) GetTxCountForEon(eon uint64) (uint64, error) { + return _Sequencer.Contract.GetTxCountForEon(&_Sequencer.CallOpts, eon) +} + // SubmitDecryptionProgress is a paid mutator transaction binding the contract method 0x2d32522e. // // Solidity: function submitDecryptionProgress(bytes message) returns() @@ -426,6 +457,7 @@ func (it *SequencerTransactionSubmittedIterator) Close() error { // SequencerTransactionSubmitted represents a TransactionSubmitted event raised by the Sequencer contract. type SequencerTransactionSubmitted struct { Eon uint64 + TxIndex uint64 IdentityPrefix [32]byte Sender common.Address EncryptedTransaction []byte @@ -433,24 +465,42 @@ type SequencerTransactionSubmitted struct { Raw types.Log // Blockchain specific contextual infos } -// FilterTransactionSubmitted is a free log retrieval operation binding the contract event 0x6515f8e10d22a184f86cfbaeb024db7afde82add43a1b1c065e8d202e43ef1a0. +// FilterTransactionSubmitted is a free log retrieval operation binding the contract event 0xa7f1b5467be46c45249fb93063cceef96c63ddad03819246bc7770e32d4f5b7d. // -// Solidity: event TransactionSubmitted(uint64 eon, bytes32 identityPrefix, address sender, bytes encryptedTransaction, uint256 gasLimit) -func (_Sequencer *SequencerFilterer) FilterTransactionSubmitted(opts *bind.FilterOpts) (*SequencerTransactionSubmittedIterator, error) { +// Solidity: event TransactionSubmitted(uint64 indexed eon, uint64 indexed txIndex, bytes32 identityPrefix, address sender, bytes encryptedTransaction, uint256 gasLimit) +func (_Sequencer *SequencerFilterer) FilterTransactionSubmitted(opts *bind.FilterOpts, eon []uint64, txIndex []uint64) (*SequencerTransactionSubmittedIterator, error) { + + var eonRule []interface{} + for _, eonItem := range eon { + eonRule = append(eonRule, eonItem) + } + var txIndexRule []interface{} + for _, txIndexItem := range txIndex { + txIndexRule = append(txIndexRule, txIndexItem) + } - logs, sub, err := _Sequencer.contract.FilterLogs(opts, "TransactionSubmitted") + logs, sub, err := _Sequencer.contract.FilterLogs(opts, "TransactionSubmitted", eonRule, txIndexRule) if err != nil { return nil, err } return &SequencerTransactionSubmittedIterator{contract: _Sequencer.contract, event: "TransactionSubmitted", logs: logs, sub: sub}, nil } -// WatchTransactionSubmitted is a free log subscription operation binding the contract event 0x6515f8e10d22a184f86cfbaeb024db7afde82add43a1b1c065e8d202e43ef1a0. +// WatchTransactionSubmitted is a free log subscription operation binding the contract event 0xa7f1b5467be46c45249fb93063cceef96c63ddad03819246bc7770e32d4f5b7d. // -// Solidity: event TransactionSubmitted(uint64 eon, bytes32 identityPrefix, address sender, bytes encryptedTransaction, uint256 gasLimit) -func (_Sequencer *SequencerFilterer) WatchTransactionSubmitted(opts *bind.WatchOpts, sink chan<- *SequencerTransactionSubmitted) (event.Subscription, error) { +// Solidity: event TransactionSubmitted(uint64 indexed eon, uint64 indexed txIndex, bytes32 identityPrefix, address sender, bytes encryptedTransaction, uint256 gasLimit) +func (_Sequencer *SequencerFilterer) WatchTransactionSubmitted(opts *bind.WatchOpts, sink chan<- *SequencerTransactionSubmitted, eon []uint64, txIndex []uint64) (event.Subscription, error) { + + var eonRule []interface{} + for _, eonItem := range eon { + eonRule = append(eonRule, eonItem) + } + var txIndexRule []interface{} + for _, txIndexItem := range txIndex { + txIndexRule = append(txIndexRule, txIndexItem) + } - logs, sub, err := _Sequencer.contract.WatchLogs(opts, "TransactionSubmitted") + logs, sub, err := _Sequencer.contract.WatchLogs(opts, "TransactionSubmitted", eonRule, txIndexRule) if err != nil { return nil, err } @@ -482,9 +532,9 @@ func (_Sequencer *SequencerFilterer) WatchTransactionSubmitted(opts *bind.WatchO }), nil } -// ParseTransactionSubmitted is a log parse operation binding the contract event 0x6515f8e10d22a184f86cfbaeb024db7afde82add43a1b1c065e8d202e43ef1a0. +// ParseTransactionSubmitted is a log parse operation binding the contract event 0xa7f1b5467be46c45249fb93063cceef96c63ddad03819246bc7770e32d4f5b7d. // -// Solidity: event TransactionSubmitted(uint64 eon, bytes32 identityPrefix, address sender, bytes encryptedTransaction, uint256 gasLimit) +// Solidity: event TransactionSubmitted(uint64 indexed eon, uint64 indexed txIndex, bytes32 identityPrefix, address sender, bytes encryptedTransaction, uint256 gasLimit) func (_Sequencer *SequencerFilterer) ParseTransactionSubmitted(log types.Log) (*SequencerTransactionSubmitted, error) { event := new(SequencerTransactionSubmitted) if err := _Sequencer.contract.UnpackLog(event, "TransactionSubmitted", log); err != nil { diff --git a/src/ISequencer.sol b/src/ISequencer.sol index a0d1285..33ed27a 100644 --- a/src/ISequencer.sol +++ b/src/ISequencer.sol @@ -14,7 +14,8 @@ interface ISequencer { function submitDecryptionProgress(bytes memory message) external; event TransactionSubmitted( - uint64 eon, + uint64 indexed eon, + uint64 indexed txIndex, bytes32 identityPrefix, address sender, bytes encryptedTransaction, diff --git a/src/Sequencer.sol b/src/Sequencer.sol index d52f7b6..7e422d4 100644 --- a/src/Sequencer.sol +++ b/src/Sequencer.sol @@ -4,6 +4,9 @@ pragma solidity ^0.8.20; import "src/ISequencer.sol"; contract Sequencer is ISequencer { + + mapping(uint64 eon => uint64 txCount) private txCounters; + function submitEncryptedTransaction( uint64 eon, bytes32 identityPrefix, @@ -14,16 +17,25 @@ contract Sequencer is ISequencer { revert InsufficientFee(); } + uint64 index = txCounters[eon]; + txCounters[eon] = index + 1; + emit TransactionSubmitted( eon, + index, identityPrefix, msg.sender, encryptedTransaction, gasLimit ); + } function submitDecryptionProgress(bytes memory message) external { emit DecryptionProgressSubmitted(message); } + + function getTxCountForEon(uint64 eon) external view returns (uint64) { + return txCounters[eon]; + } } diff --git a/test/Sequencer.t.sol b/test/Sequencer.t.sol index 55ce832..ee38dd1 100644 --- a/test/Sequencer.t.sol +++ b/test/Sequencer.t.sol @@ -8,13 +8,6 @@ import "../src/Sequencer.sol"; contract SequencerTest is Test { Sequencer public sequencer; - event TransactionSubmitted( - uint64 eon, - bytes32 identityPrefix, - address sender, - bytes encryptedTransaction, - uint256 gasLimit - ); event DecryptionProgressSubmitted(bytes message); function setUp() public { @@ -23,14 +16,16 @@ contract SequencerTest is Test { function testSubmitTransaction() public { uint64 eon = 5; + uint64 txIndex = 0; bytes32 identityPrefix = hex"001122"; address sender = makeAddr("sender"); bytes memory encryptedTransaction = "aabbcc"; uint256 gasLimit = 8; vm.expectEmit(address(sequencer)); - emit TransactionSubmitted( + emit ISequencer.TransactionSubmitted( eon, + txIndex, identityPrefix, sender, encryptedTransaction, @@ -72,4 +67,32 @@ contract SequencerTest is Test { emit DecryptionProgressSubmitted(message); sequencer.submitDecryptionProgress(message); } + + function testTxCount() public { + uint64 eon = 5; + bytes32 identityPrefix = hex"001122"; + address sender = makeAddr("sender"); + bytes memory encryptedTransaction = "aabbcc"; + uint256 gasLimit = 8; + + assertEqUint(sequencer.getTxCountForEon(eon), 0); + vm.fee(20); + hoax(sender); + sequencer.submitEncryptedTransaction{value: 160}( + eon, + identityPrefix, + encryptedTransaction, + gasLimit + ); + assertEqUint(sequencer.getTxCountForEon(eon), 1); + assertEqUint(sequencer.getTxCountForEon(eon+1), 0); + sequencer.submitEncryptedTransaction{value: 160}( + eon+1, + identityPrefix, + encryptedTransaction, + gasLimit + ); + assertEqUint(sequencer.getTxCountForEon(eon), 1); + assertEqUint(sequencer.getTxCountForEon(eon+1), 1); + } }