#How to query blockchain for current timestamp?

18 messages · Page 1 of 1 (latest)

glossy bronze
#

So I'm aware we can use

this.network.timestamp.get()

when getting and comparing the timestamp values INSIDE a smart contract.
But, I'm having difficulty trying to get this same timestamp value from an integration test (aka when I'm "outside" the smart contract, as a user).

My main question is, how do I query the local blockchain for the timestamp from an integration test?

velvet geyser
#

This should shed some light 😄

errant pebble
glossy bronze
glossy bronze
#

Ok so first question, for slot, do we use Mina.activeInstance.currentSlot() or Mina.activeInstance.getNetworkState().globalSlotSinceGenesis

#

second, regardless of which i use, the timestamp is not changing when called before and after a Mina transaction.

#

These are the functions I made based on MIna's globalSlotToTimestamp() function:

function globalSlotToTimestamp1() {
    let { genesisTimestamp, slotTime } = Mina.activeInstance.getNetworkConstants();
    let slot = Mina.activeInstance.getNetworkState().globalSlotSinceGenesis;
    return UInt64.from(slot).mul(slotTime).add(genesisTimestamp)
  }

function globalSlotToTimestamp2() {
    let { genesisTimestamp, slotTime } = Mina.activeInstance.getNetworkConstants();
    let slot = Mina.activeInstance.currentSlot();
    return UInt64.from(slot).mul(slotTime).add(genesisTimestamp);
  }

The following test:

    it('commits to a bounty solution', async () => {

        console.log(globalSlotToTimestamp1().toJSON())
        console.log(globalSlotToTimestamp2().toJSON())

        const txn = await Mina.transaction(hunter.publicKey, () => {
            zkApp.commitBountyKey(bountyKeyCommit);
        });
        await txn.prove();
        await txn.sign([hunter.privateKey]).send();

        console.log(globalSlotToTimestamp1().toJSON())
        console.log(globalSlotToTimestamp2().toJSON())


    });

Produces the following output:

  console.log
    1706011706239

      at Object.<anonymous> (src/Core.test.ts:67:17)

  console.log
    1706011886239

      at Object.<anonymous> (src/Core.test.ts:68:17)

  console.log
    1706011706239

      at Object.<anonymous> (src/Core.test.ts:85:17)

  console.log
    1706011886239

      at Object.<anonymous> (src/Core.test.ts:86:17)
worldly gale
#

Please note that you probably need to call fetchLastBlock() and

Mina.getNetworkConstants() returns:
default network constants if used outside of the transaction scope.
actual network constants if used within the transaction scope.

https://github.com/o1-labs/o1js/blob/main/CHANGELOG.md

paper spoke
#

@glossy bronze wrt the test:

  • the current slot doesn't change after every transaction (which your test seems to expect). It changes every 3 minutes, on the actual Mina network, and is updated every block (which could be a multiple of 3 min from the last block)
  • local blockchain has no notion of blocks, and the current slot never changes there. but you can "simulate" changing network state by manually calling Local.incrementGlobalSlot()
#

Also, re your original question:

If you have a zkapp instance around, you can query the local blockchain from outside your transaction in exactly the same way as from inside a contract:

let zkapp: SmartContract;

let timestamp = zkapp.network.timestamp.get();
glossy bronze
glossy bronze
reef laurel
#

Does the network just assumes that each block equals to 3 minutes of time, or are there some other factors involved in the Mina network recording of time?

Do block proposers set their own timestamp and it is valid as long as it's within certain expected time period?

I would like to understand more how time works in Mina.

paper spoke
glossy bronze
#

so for example, if I call currentSlot and get 100, the next three minutes, it should be 101. BUUUT, if the block doesn't update till six minutes, the next time I call currentSlot, I would get 102 and not 101? did I understand that correctly?

worldly gale