summaryrefslogtreecommitdiff
path: root/ledger/time.go
blob: 979234eb640e46386eb241ca95de8019a91d32c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (C) 2019-2021 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package ledger

import (
	"fmt"

	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/data/bookkeeping"
	"github.com/algorand/go-algorand/ledger/ledgercore"
)

type timeTracker struct {
	timestamps map[basics.Round]int64
}

func (tt *timeTracker) loadFromDisk(l ledgerForTracker) error {
	latest := l.Latest()
	blkhdr, err := l.BlockHdr(latest)
	if err != nil {
		return err
	}

	tt.timestamps = make(map[basics.Round]int64)
	tt.timestamps[latest] = blkhdr.TimeStamp
	return nil
}

func (tt *timeTracker) close() {
}

func (tt *timeTracker) newBlock(blk bookkeeping.Block, delta ledgercore.StateDelta) {
	rnd := blk.Round()
	tt.timestamps[rnd] = delta.Hdr.TimeStamp
}

func (tt *timeTracker) committedUpTo(committedRnd basics.Round) basics.Round {
	for rnd := range tt.timestamps {
		if rnd < committedRnd {
			delete(tt.timestamps, rnd)
		}
	}
	return committedRnd
}

func (tt *timeTracker) timestamp(r basics.Round) (int64, error) {
	ts, ok := tt.timestamps[r]
	if ok {
		return ts, nil
	}

	return 0, fmt.Errorf("no record of timestamp for round %d", r)
}