Skip to content

Commit

Permalink
feat: adding ObservationsCached event
Browse files Browse the repository at this point in the history
  • Loading branch information
wei3erHase committed Aug 14, 2024
1 parent f3a7d46 commit ea6b819
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
11 changes: 3 additions & 8 deletions solidity/contracts/DataReceiver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import {Governable} from '@defi-wonderland/solidity-utils/solidity/contracts/Gov
import {OracleSidechain} from './OracleSidechain.sol';
import {IDataReceiver, IOracleFactory, IOracleSidechain, IBridgeReceiverAdapter} from '../interfaces/IDataReceiver.sol';

import {console} from 'hardhat/console.sol';

/** TODO:
* - [x] cache observations
* - [x] remove ObservationsData from event
* - [ ] add ObservationsCached event
* - [ ] remove console logs
* - [x] add ObservationsCached event
* - [x] remove console logs
*/

/// @title The DataReceiver contract
Expand Down Expand Up @@ -56,12 +54,9 @@ contract DataReceiver is IDataReceiver, Governable {
deployedOracles[_poolSalt] = _oracle;
}
// Try to write observations data into oracle
console.log('writing', _poolNonce);
if (_oracle.write(_observationsData, _poolNonce)) {
console.log('writed', _poolNonce);
emit ObservationsAdded(_poolSalt, _poolNonce, msg.sender);
} else {
console.log('caching', _poolNonce);
// Query pool's current nonce
uint24 _currentNonce = _oracle.poolNonce();
// Discard old observations (already written in the oracle)
Expand All @@ -73,13 +68,13 @@ contract DataReceiver is IDataReceiver, Governable {
for (uint256 _i; _i < _observationsData.length; ++_i) {
cachedObservations[_poolSalt][_poolNonce].push(_observationsData[_i]);
}
emit ObservationsCached(_poolSalt, _poolNonce, msg.sender);
while (_currentNonce <= _poolNonce) {
// Try backfilling pending observations (from current to {sent|first empty} nonce)
IOracleSidechain.ObservationData[] memory _cachedObservations = cachedObservations[_poolSalt][_currentNonce];
// If the struct is not empty, write it into the oracle
if (_cachedObservations.length > 0) {
// Since observation nonce == oracle nonce, we can safely write the observations
console.log('writing', _currentNonce);
_oracle.write(_cachedObservations, _currentNonce);
emit ObservationsAdded(_poolSalt, _currentNonce, msg.sender);
// Clear out the written observations
Expand Down
6 changes: 6 additions & 0 deletions solidity/interfaces/IDataReceiver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ interface IDataReceiver is IGovernable {
/// @return _receiverAdapter Handler of the broadcast
event ObservationsAdded(bytes32 indexed _poolSalt, uint24 _poolNonce, address _receiverAdapter);

/// @notice Emitted when a broadcast observation is cached for later processing
/// @param _poolSalt Identifier of the pool to fetch
/// @return _poolNonce Nonce of the observation broadcast
/// @return _receiverAdapter Handler of the broadcast
event ObservationsCached(bytes32 indexed _poolSalt, uint24 _poolNonce, address _receiverAdapter);

/// @notice Emitted when a new adapter whitelisting rule is set
/// @param _adapter Address of the adapter
/// @param _isAllowed New whitelisting status
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/oracle-sidechain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe('@skip-on-coverage OracleSidechain.sol', () => {

// NOTE: chai is failing to compare the emitted struct[], so we are not checking the arguments
await expect(tx1).to.emit(allowedDataReceiver, 'ObservationsAdded').withArgs(salt, 1, deployer.address);
// TODO: await expect(tx2).to.emit(allowedDataReceiver, 'ObservationsCached');
await expect(tx2).to.emit(allowedDataReceiver, 'ObservationsCached').withArgs(salt, 3, deployer.address);
await expect(tx2).not.to.emit(allowedDataReceiver, 'ObservationsAdded');
await expect(tx3).to.emit(allowedDataReceiver, 'ObservationsAdded').withArgs(salt, 2, deployer.address);
await expect(tx4).to.emit(allowedDataReceiver, 'ObservationsAdded').withArgs(salt, 3, deployer.address);
Expand Down

0 comments on commit ea6b819

Please sign in to comment.