summaryrefslogtreecommitdiff
path: root/node/impls.go
blob: b7ee54fab75652c12792591f98a6c9137b4d2859 (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
// Copyright (C) 2019-2024 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 node

import (
	"context"
	"errors"

	"github.com/algorand/go-algorand/agreement"
	"github.com/algorand/go-algorand/catchup"
	"github.com/algorand/go-algorand/data"
	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/data/bookkeeping"
	"github.com/algorand/go-algorand/ledger"
	"github.com/algorand/go-algorand/logging"
	"github.com/algorand/go-algorand/network"
	"github.com/algorand/go-algorand/util/execpool"
)

// TODO these implementations should be pushed down into the corresponding structs or alternatively turned into new structs in the correct subpackages

type blockAuthenticatorImpl struct {
	*data.Ledger
	*agreement.AsyncVoteVerifier
}

func (i blockAuthenticatorImpl) Authenticate(block *bookkeeping.Block, cert *agreement.Certificate) error {
	return cert.Authenticate(*block, i.Ledger, i.AsyncVoteVerifier)
}

func (i blockAuthenticatorImpl) Quit() {
	i.AsyncVoteVerifier.Quit()
}

type blockValidatorImpl struct {
	l                *data.Ledger
	verificationPool execpool.BacklogPool
}

// Validate implements BlockValidator.Validate.
func (i blockValidatorImpl) Validate(ctx context.Context, e bookkeeping.Block) (agreement.ValidatedBlock, error) {
	b := &e
	lvb, err := i.l.Validate(ctx, *b, i.verificationPool)
	if err != nil {
		return nil, err
	}

	return validatedBlock{vb: lvb}, nil
}

// agreementLedger implements the agreement.Ledger interface.
type agreementLedger struct {
	*data.Ledger
	UnmatchedPendingCertificates chan catchup.PendingUnmatchedCertificate
	n                            network.GossipNode
}

func makeAgreementLedger(ledger *data.Ledger, net network.GossipNode) agreementLedger {
	return agreementLedger{
		Ledger:                       ledger,
		UnmatchedPendingCertificates: make(chan catchup.PendingUnmatchedCertificate, 1),
		n:                            net,
	}
}

// EnsureBlock implements agreement.LedgerWriter.EnsureBlock.
func (l agreementLedger) EnsureBlock(e bookkeeping.Block, c agreement.Certificate) {
	l.Ledger.EnsureBlock(&e, c)
	// let the network know that we've made some progress.
	l.n.OnNetworkAdvance()
}

// EnsureValidatedBlock implements agreement.LedgerWriter.EnsureValidatedBlock.
func (l agreementLedger) EnsureValidatedBlock(ve agreement.ValidatedBlock, c agreement.Certificate) {
	l.Ledger.EnsureValidatedBlock(ve.(validatedBlock).vb, c)
	// let the network know that we've made some progress.
	l.n.OnNetworkAdvance()
}

// EnsureDigest implements agreement.LedgerWriter.EnsureDigest.
func (l agreementLedger) EnsureDigest(cert agreement.Certificate, verifier *agreement.AsyncVoteVerifier) {
	// let the network know that we've made some progress.
	// this might be controverasl since we haven't received the entire block, but we did get the
	// certificate, which means that network connections are likely to be just fine.
	l.n.OnNetworkAdvance()

	// clear out the pending certificates ( if any )
	select {
	case pendingCert := <-l.UnmatchedPendingCertificates:
		logging.Base().Debugf("agreementLedger.EnsureDigest has flushed out pending request for certificate for round %d in favor of recent certificate for round %d", pendingCert.Cert.Round, cert.Round)
	default:
	}

	// The channel send to UnmatchedPendingCertificates is guaranteed to be non-blocking since due to the fact that -
	// 1. the channel capacity is 1
	// 2. we just cleared a single item off this channel ( if there was any )
	// 3. the EnsureDigest method is being called with the agreement service guarantee
	// 4. no other senders to this channel exists
	l.UnmatchedPendingCertificates <- catchup.PendingUnmatchedCertificate{Cert: cert, VoteVerifier: verifier}
}

// Wrapping error with a LedgerDroppedRoundError when an old round is requested but the ledger has already dropped the entry
func (l agreementLedger) LookupAgreement(rnd basics.Round, addr basics.Address) (basics.OnlineAccountData, error) {
	record, err := l.Ledger.LookupAgreement(rnd, addr)
	var e *ledger.RoundOffsetError
	if errors.As(err, &e) {
		err = &agreement.LedgerDroppedRoundError{
			Err: err,
		}
	}
	return record, err
}