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

Open Beginner friendly
#33 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
solidity
Domain
blockchain

Research direction

Start at submitValue in src/Oracle.sol around line 140, where priceTimestamps.push(nowTs) runs unconditionally while prices are stored in timestamp-keyed mappings; read getPriceHistoryRange and getPriceHistoryLength next to see how the arrays are consumed. Drop the reproduction into test/SameBlockHistory.t.sol and run forge test --match-path test/SameBlockHistory.t.sol -vv to confirm the duplicate 62/62 output on the current main. Pick one fix (skip the push when the last timestamp matches, or move history to an array of structs) and check every reader of the history arrays still compiles; done is that test passing with no duplicate timestamps and the first submit's value preserved.

Written by the indexing model from the issue text.

Description

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
Dominant language
Solidity
Stars
3
Forks
6
Avg merge
14h 14m
Merged PRs (30d)
5

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from StabilityNexus/OrbOracle-Solidity

All issues in StabilityNexus/OrbOracle-Solidity

Similar issues

More Blockchain issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.