[BUG]: Two submits in one block list the same timestamp twice and lose the first price

Aperta Adatta ai principianti
#33 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
solidity
Ambito
blockchain

Direzione di ricerca

Inizia da submitValue in src/Oracle.sol intorno alla riga 140, dove priceTimestamps.push(nowTs) viene eseguito incondizionatamente mentre i prezzi sono memorizzati in mapping indicizzati per timestamp; leggi poi getPriceHistoryRange e getPriceHistoryLength per vedere come vengono consumati gli array. Metti la riproduzione in test/SameBlockHistory.t.sol ed esegui forge test --match-path test/SameBlockHistory.t.sol -vv per confermare l'output duplicato 62/62 sul main attuale. Scegli una correzione (saltare il push quando l'ultimo timestamp corrisponde, oppure spostare la cronologia in un array di struct) e verifica che ogni lettore degli array di cronologia compili ancora; fatto è che quel test passa senza timestamp duplicati e con il valore del primo submit preservato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug
Bug Description

submitValue always does priceTimestamps.push(nowTs) but stores the prices in mappings keyed by timestamp (src/Oracle.sol:140). When two submits land in the same block the timestamp gets pushed twice and the second submit overwrites the first one's prices. So getPriceHistoryRange returns the same timestamp twice with the second values both times and the first submitted value is gone.

Nothing breaks on chain but anyone reading the history off chain gets duplicate points and a missing price. A fix could skip the push when the last timestamp is the same or store the history as an array of structs.

Steps to Reproduce
  1. Two users stake and wait out the deposit lock.
  2. Both submit in the same block (3000e18 and then 3100e18).
  3. getPriceHistoryRange(0, 2) returns timestamp 62 twice and the 3000e18 submit is not there.
Logs and Screenshots
Test I used: test/SameBlockHistory.t.sol run with forge test --match-path test/SameBlockHistory.t.sol -vv
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {Test, console2} from "lib/forge-std/src/Test.sol";
import {ERC20} from "lib/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
import {Oracle} from "../src/Oracle.sol";

contract Tok is ERC20 {
    constructor() ERC20("W", "W") {}

    function mint(address to, uint256 a) external {
        _mint(to, a);
    }
}

contract SameBlockHistoryTest is Test {
    function test_twoSubmitsInOneBlock() public {
        Tok t = new Tok();
        Oracle o = new Oracle(address(this), "Feed", "d", address(t), 60, 50_000, 60, 60, 100, 0, 5);
        address a = makeAddr("a");
        address b = makeAddr("b");
        t.mint(a, 1e18);
        t.mint(b, 1e18);
        vm.startPrank(a);
        t.approve(address(o), 1e18);
        o.depositTokens(1e18);
        vm.stopPrank();
        vm.startPrank(b);
        t.approve(address(o), 1e18);
        o.depositTokens(1e18);
        vm.stopPrank();
        vm.warp(vm.getBlockTimestamp() + 61);

        vm.prank(a);
        o.submitValue(3000e18);
        vm.prank(b);
        o.submitValue(3100e18); // same block

        uint256 n = o.getPriceHistoryLength();
        (uint256[] memory ts, uint256[] memory agg, uint256[] memory latest) = o.getPriceHistoryRange(0, n);
        for (uint256 i = 0; i < n; i++) {
            console2.log(ts[i], agg[i], latest[i]);
        }
        assertFalse(n == 2 && ts[0] == ts[1], "same timestamp listed twice");
    }
}

Output on main 86df9c0:

Ran 1 test for test/SameBlockHistory.t.sol:SameBlockHistoryTest
[FAIL: same timestamp listed twice] test_twoSubmitsInOneBlock() (gas: 2147292)
Logs:
  62 3050000000000000000000 3100000000000000000000
  62 3050000000000000000000 3100000000000000000000
Suite result: FAILED. 0 passed; 1 failed; 0 skipped; finished in 1.08ms (821.33µs CPU time)
Environment Details

macOS with forge 1.8.7 (same as CI) and solc 0.8.33 with via_ir. Upstream main at 86df9c0. Local tests only.

Impact

Low - Minor inconvenience

Code of Conduct
  • I have joined the Discord server and will post updates there
  • I have searched existing issues to avoid duplicates
Lingua principale
Solidity
Stelle
3
Fork
6
Merge medio
14h 14m
PR unite (30g)
5

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di StabilityNexus/OrbOracle-Solidity

Tutte le issue di StabilityNexus/OrbOracle-Solidity

Issue simili

Altre issue su Blockchain

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.