summaryrefslogtreecommitdiff
path: root/ledger/bulletin.go
blob: 1e95ee2ab7cc7829f30d1a6166df0fd49e31d59c (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// 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 (
	"context"
	"database/sql"
	"sync/atomic"

	"github.com/algorand/go-deadlock"

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

// notifier is a struct that encapsulates a single-shot channel; it will only be signaled once.
type notifier struct {
	signal   chan struct{}
	notified uint32
}

// makeNotifier constructs a notifier that has not been signaled.
func makeNotifier() notifier {
	return notifier{signal: make(chan struct{}), notified: 0}
}

// notify signals the channel if it hasn't already done so
func (notifier *notifier) notify() {
	if atomic.CompareAndSwapUint32(&notifier.notified, 0, 1) {
		close(notifier.signal)
	}
}

// bulletin provides an easy way to wait on a round to be written to the ledger.
// To use it, call <-Wait(round)
type bulletin struct {
	mu                          deadlock.Mutex
	pendingNotificationRequests map[basics.Round]notifier
	latestRound                 basics.Round
}

func makeBulletin() *bulletin {
	b := new(bulletin)
	b.pendingNotificationRequests = make(map[basics.Round]notifier)
	return b
}

// Wait returns a channel which gets closed when the ledger reaches a given round.
func (b *bulletin) Wait(round basics.Round) chan struct{} {
	b.mu.Lock()
	defer b.mu.Unlock()

	// Return an already-closed channel if we already have the block.
	if round <= b.latestRound {
		closed := make(chan struct{})
		close(closed)
		return closed
	}

	signal, exists := b.pendingNotificationRequests[round]
	if !exists {
		signal = makeNotifier()
		b.pendingNotificationRequests[round] = signal
	}
	return signal.signal
}

func (b *bulletin) loadFromDisk(l ledgerForTracker, _ basics.Round) error {
	b.pendingNotificationRequests = make(map[basics.Round]notifier)
	b.latestRound = l.Latest()
	return nil
}

func (b *bulletin) close() {
}

func (b *bulletin) newBlock(blk bookkeeping.Block, delta ledgercore.StateDelta) {
}

func (b *bulletin) committedUpTo(rnd basics.Round) (retRound, lookback basics.Round) {
	b.mu.Lock()
	defer b.mu.Unlock()

	for pending, signal := range b.pendingNotificationRequests {
		if pending > rnd {
			continue
		}

		delete(b.pendingNotificationRequests, pending)
		signal.notify()
	}

	b.latestRound = rnd
	return rnd, basics.Round(0)
}

func (b *bulletin) prepareCommit(dcc *deferredCommitContext) error {
	return nil
}

func (b *bulletin) commitRound(context.Context, *sql.Tx, *deferredCommitContext) error {
	return nil
}

func (b *bulletin) postCommit(ctx context.Context, dcc *deferredCommitContext) {
}

func (b *bulletin) handleUnorderedCommit(uint64, basics.Round, basics.Round) {
}
func (b *bulletin) produceCommittingTask(committedRound basics.Round, dbRound basics.Round, dcr *deferredCommitRange) *deferredCommitRange {
	return dcr
}