summaryrefslogtreecommitdiff
path: root/ledger/blockqueue_test.go
blob: e74fbc0b3bcc323a47d558afbcb8f55b49b03a78 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// 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 ledger

import (
	"context"
	"database/sql"
	"errors"
	"fmt"
	"testing"
	"time"

	"github.com/stretchr/testify/require"

	"github.com/algorand/go-algorand/agreement"
	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/crypto"
	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/data/bookkeeping"
	"github.com/algorand/go-algorand/ledger/ledgercore"
	"github.com/algorand/go-algorand/ledger/store/blockdb"
	ledgertesting "github.com/algorand/go-algorand/ledger/testing"
	"github.com/algorand/go-algorand/logging"
	"github.com/algorand/go-algorand/protocol"
	"github.com/algorand/go-algorand/test/partitiontest"
	"github.com/algorand/go-algorand/util/db"
)

func randomBlock(r basics.Round) blockEntry {
	b := bookkeeping.Block{}
	c := agreement.Certificate{}

	b.BlockHeader.Round = r
	b.BlockHeader.TimeStamp = int64(crypto.RandUint64())
	b.RewardsPool = testPoolAddr
	b.FeeSink = testSinkAddr
	c.Round = r

	return blockEntry{
		block: b,
		cert:  c,
	}
}

func randomInitChain(proto protocol.ConsensusVersion, nblock int) []blockEntry {
	res := make([]blockEntry, 0)
	for i := 0; i < nblock; i++ {
		blkent := randomBlock(basics.Round(i))
		blkent.cert = agreement.Certificate{}
		blkent.block.CurrentProtocol = proto
		res = append(res, blkent)
	}
	return res
}

func TestPutBlockTooOld(t *testing.T) {
	partitiontest.PartitionTest(t)

	genesisInitState, _, _ := ledgertesting.Genesis(10)

	dbName := fmt.Sprintf("%s.%d", t.Name(), crypto.RandUint64())
	const inMem = true
	cfg := config.GetDefaultLocal()
	cfg.Archival = true
	l, err := OpenLedger(logging.Base(), dbName, inMem, genesisInitState, cfg)
	require.NoError(t, err)
	defer l.Close()

	blk := bookkeeping.Block{}
	var cert agreement.Certificate
	err = l.blockQ.putBlock(blk, cert) // try putBlock for a block in a previous round

	expectedErr := &ledgercore.BlockInLedgerError{}
	require.True(t, errors.As(err, expectedErr))

	blkent := randomBlock(1)
	blk = blkent.block
	cert = blkent.cert
	err = l.blockQ.putBlock(blk, cert) // add block for round 1 to blockQueue
	require.NoError(t, err)

	err = l.blockQ.putBlock(blk, cert) // try adding same block again (should fail)
	require.True(t, errors.As(err, expectedErr))
}

// TestGetEncodedBlockCert tests getEncodedBlockCert with valid and invalid round numbers.
func TestGetEncodedBlockCert(t *testing.T) {
	partitiontest.PartitionTest(t)

	genesisInitState, _, _ := ledgertesting.Genesis(10)

	const inMem = true
	cfg := config.GetDefaultLocal()
	l, err := OpenLedger(logging.Base(), t.Name(), inMem, genesisInitState, cfg)
	require.NoError(t, err)
	defer l.Close()

	blkent := randomBlock(1)
	blk := blkent.block
	cert := blkent.cert
	err = l.blockQ.putBlock(blk, cert)
	require.NoError(t, err)

	var blkBytes []byte
	var certBytes []byte

	blkBytes, certBytes, err = l.blockQ.getEncodedBlockCert(0)
	require.Equal(t, protocol.Encode(&genesisInitState.Block), blkBytes)
	require.Equal(t, protocol.Encode(&agreement.Certificate{}), certBytes)
	require.NoError(t, err)

	blkBytes, certBytes, err = l.blockQ.getEncodedBlockCert(1)
	require.Equal(t, protocol.Encode(&blk), blkBytes)
	require.Equal(t, protocol.Encode(&cert), certBytes)
	require.NoError(t, err)

	_, _, err = l.blockQ.getEncodedBlockCert(100) // should not be entry for this round

	expectedErr := &ledgercore.ErrNoEntry{}
	require.True(t, errors.As(err, expectedErr))
}

// it is not great to use trackers here but at the moment there is no abstraction for the ledger
type uptoTracker struct {
	emptyTracker
}

// committedUpTo in the emptyTracker just stores the committed round.
func (t *uptoTracker) committedUpTo(committedRnd basics.Round) (minRound, lookback basics.Round) {
	return 5_000, basics.Round(0)
}

// TestBlockQueueSyncerDeletion ensures that the block queue syncer deletes no more than maxDeletionBatchSize blocks at time
func TestBlockQueueSyncerDeletion(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	tests := []struct {
		name             string
		expectedEarliest basics.Round
		tracker          ledgerTracker
	}{
		{"max_batch", maxDeletionBatchSize, nil}, // no trackers, max deletion
		{"5k_tracker", 5_000, &uptoTracker{}},    // tracker sets minToSave to 5k
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {

			const dbMem = true
			blockDBs, err := db.OpenPair(t.Name()+".block.sqlite", dbMem)
			require.NoError(t, err)

			log := logging.TestingLog(t)
			err = blockDBs.Wdb.Atomic(func(ctx context.Context, tx *sql.Tx) error {
				return initBlocksDB(tx, log, []bookkeeping.Block{}, false)
			})
			require.NoError(t, err)

			// add 15k blocks
			const maxBlocks = maxDeletionBatchSize + maxDeletionBatchSize/2 // 15_000
			err = blockDBs.Wdb.Atomic(func(ctx context.Context, tx *sql.Tx) error {
				for i := 0; i < maxBlocks; i++ {
					err0 := blockdb.BlockPut(
						tx,
						bookkeeping.Block{BlockHeader: bookkeeping.BlockHeader{Round: basics.Round(i)}},
						agreement.Certificate{})
					if err0 != nil {
						return err0
					}
				}
				return nil
			})
			require.NoError(t, err)

			var earliest, latest basics.Round
			err = blockDBs.Rdb.Atomic(func(ctx context.Context, tx *sql.Tx) error {
				var err0 error
				earliest, err0 = blockdb.BlockEarliest(tx)
				if err0 != nil {
					return err0
				}
				latest, err0 = blockdb.BlockLatest(tx)
				return err0
			})
			require.NoError(t, err)
			require.Equal(t, basics.Round(0), earliest)
			require.Equal(t, basics.Round(maxBlocks-1), latest)

			// trigger deletion and ensure no more than 10k blocks gone
			//make a minimal ledger for blockqueue

			l := &Ledger{
				log:      log,
				blockDBs: blockDBs,
			}
			if test.tracker != nil {
				l.trackers.trackers = append(l.trackers.trackers, test.tracker)
			}
			blockq, _ := newBlockQueue(l)
			err = blockq.start()
			require.NoError(t, err)

			// add a block. Eventually the syncer will called on an empty ledger
			// forcing deleting all 15_000 rounds. The deletion scoping should limit it to 10_000 rounds instead
			err = blockq.putBlock(bookkeeping.Block{BlockHeader: bookkeeping.BlockHeader{Round: maxBlocks}}, agreement.Certificate{})
			require.NoError(t, err)

			require.Eventually(t, func() bool {
				var latest basics.Round
				err = blockDBs.Rdb.Atomic(func(ctx context.Context, tx *sql.Tx) error {
					var err0 error
					latest, err0 = blockdb.BlockLatest(tx)
					return err0
				})
				require.NoError(t, err)
				return latest == maxBlocks
			}, 1*time.Second, 10*time.Millisecond)

			blockq.stop()

			err = blockDBs.Rdb.Atomic(func(ctx context.Context, tx *sql.Tx) error {
				var err0 error
				earliest, err0 = blockdb.BlockEarliest(tx)
				return err0
			})
			require.NoError(t, err)
			require.Equal(t, test.expectedEarliest, earliest)
		})
	}
}