summaryrefslogtreecommitdiff
path: root/data/datatest/impls.go
blob: fefcb054dadfe8344e96a30f13c4dd9510aae3f1 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (C) 2019-2023 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 datatest

import (
	"context"
	"fmt"

	"github.com/algorand/go-algorand/agreement"
	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/crypto"
	"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/data/committee"
	"github.com/algorand/go-algorand/protocol"
)

// This file is a copy of node/impls.go.
type entryValidatorImpl struct {
	l *data.Ledger
}

type validatedBlock struct {
	blk *bookkeeping.Block
}

// Validate implements BlockValidator.Validate.
func (i entryValidatorImpl) Validate(ctx context.Context, e bookkeeping.Block) (agreement.ValidatedBlock, error) {
	ve := validatedBlock{
		blk: &e,
	}

	return ve, nil
}

type entryFactoryImpl struct {
	l *data.Ledger
}

// AssembleBlock implements Ledger.AssembleBlock.
func (i entryFactoryImpl) AssembleBlock(round basics.Round) (agreement.ValidatedBlock, error) {
	prev, err := i.l.BlockHdr(round - 1)
	if err != nil {
		return nil, fmt.Errorf("could not make proposals: could not read block from ledger at round %v: %v", round, err)
	}

	b := bookkeeping.MakeBlock(prev)
	b.RewardsState = prev.RewardsState
	return validatedBlock{blk: &b}, nil
}

// WithSeed implements the agreement.ValidatedBlock interface.
func (ve validatedBlock) WithSeed(s committee.Seed) agreement.ValidatedBlock {
	newblock := ve.blk.WithSeed(s)
	return validatedBlock{blk: &newblock}
}

// Block implements the agreement.ValidatedBlock interface.
func (ve validatedBlock) Block() bookkeeping.Block {
	return *ve.blk
}

type ledgerImpl struct {
	l *data.Ledger
}

// NextRound implements Ledger.NextRound.
func (i ledgerImpl) NextRound() basics.Round {
	return i.l.NextRound()
}

func (i ledgerImpl) Seed(r basics.Round) (committee.Seed, error) {
	block, err := i.l.BlockHdr(r)
	if err != nil {
		return committee.Seed{}, err
	}
	return block.Seed, nil
}

func (i ledgerImpl) LookupDigest(r basics.Round) (crypto.Digest, error) {
	blockhdr, err := i.l.BlockHdr(r)
	if err != nil {
		return crypto.Digest{}, err
	}
	return crypto.Digest(blockhdr.Hash()), nil
}

// Lookup implements Ledger.LookupAgreement.
func (i ledgerImpl) LookupAgreement(r basics.Round, addr basics.Address) (basics.OnlineAccountData, error) {
	a, err := i.l.LookupAgreement(r, addr)
	return a, err
}

// Circulation implements Ledger.Circulation.
func (i ledgerImpl) Circulation(r basics.Round, voteRnd basics.Round) (basics.MicroAlgos, error) {
	return i.l.Circulation(r, voteRnd)
}

// Wait implements Ledger.Wait.
func (i ledgerImpl) Wait(r basics.Round) chan struct{} {
	return i.l.Wait(r)
}

// EnsureValidatedBlock implements Ledger.EnsureValidatedBlock.
func (i ledgerImpl) EnsureValidatedBlock(e agreement.ValidatedBlock, c agreement.Certificate) {
	i.l.EnsureBlock(e.(validatedBlock).blk, c)
}

// EnsureBlock implements Ledger.EnsureBlock.
func (i ledgerImpl) EnsureBlock(e bookkeeping.Block, c agreement.Certificate) {
	i.l.EnsureBlock(&e, c)
}

// ConsensusParams implements Ledger.ConsensusParams.
func (i ledgerImpl) ConsensusParams(r basics.Round) (config.ConsensusParams, error) {
	return i.l.ConsensusParams(r)
}

// ConsensusParams implements Ledger.ConsensusVersion.
func (i ledgerImpl) ConsensusVersion(r basics.Round) (protocol.ConsensusVersion, error) {
	return i.l.ConsensusVersion(r)
}

// EnsureDigest implements Ledger.EnsureDigest.
func (i ledgerImpl) EnsureDigest(cert agreement.Certificate, verifier *agreement.AsyncVoteVerifier) {
	r := cert.Round
	consistencyCheck := func() bool {
		if r < i.NextRound() {
			b, err := i.l.Block(r)
			if err != nil {
				panic(err)
			}

			if b.Digest() != cert.Proposal.BlockDigest {
				err := fmt.Errorf("testLedger.EnsureDigest called with conflicting entries in round %v", r)
				panic(err)
			}
			return true
		}
		return false
	}

	if consistencyCheck() {
		return
	}
}