forked from algorand/go-algorand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
txtail_test.go
95 lines (84 loc) · 3.86 KB
/
txtail_test.go
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
// 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 (
"errors"
"testing"
"github.com/stretchr/testify/require"
"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/data/transactions"
"github.com/algorand/go-algorand/ledger/ledgercore"
"github.com/algorand/go-algorand/protocol"
)
func TestTxTailCheckdup(t *testing.T) {
ledger := makeMockLedgerForTracker(t, true, 1, protocol.ConsensusCurrentVersion)
proto := config.Consensus[protocol.ConsensusCurrentVersion]
tail := txTail{}
require.NoError(t, tail.loadFromDisk(ledger))
lastRound := basics.Round(proto.MaxTxnLife)
lookback := basics.Round(100)
txvalidity := basics.Round(10)
leasevalidity := basics.Round(32)
// push 1000 rounds into the txtail
for rnd := basics.Round(1); rnd < lastRound; rnd++ {
blk := bookkeeping.Block{
BlockHeader: bookkeeping.BlockHeader{
Round: rnd,
UpgradeState: bookkeeping.UpgradeState{
CurrentProtocol: protocol.ConsensusCurrentVersion,
},
},
}
txids := make(map[transactions.Txid]basics.Round, 1)
txids[transactions.Txid(crypto.Hash([]byte{byte(rnd % 256), byte(rnd / 256), byte(1)}))] = rnd + txvalidity
txleases := make(map[ledgercore.Txlease]basics.Round, 1)
txleases[ledgercore.Txlease{Sender: basics.Address(crypto.Hash([]byte{byte(rnd % 256), byte(rnd / 256), byte(2)})), Lease: crypto.Hash([]byte{byte(rnd % 256), byte(rnd / 256), byte(3)})}] = rnd + leasevalidity
delta := ledgercore.MakeStateDelta(&blk.BlockHeader, 0, 1)
delta.Txids = txids
delta.Txleases = txleases
tail.newBlock(blk, delta)
tail.committedUpTo(rnd.SubSaturate(lookback))
}
// test txid duplication testing.
for rnd := basics.Round(1); rnd < lastRound; rnd++ {
txid := transactions.Txid(crypto.Hash([]byte{byte(rnd % 256), byte(rnd / 256), byte(1)}))
err := tail.checkDup(proto, basics.Round(0), basics.Round(0), rnd+txvalidity, txid, ledgercore.Txlease{})
require.Errorf(t, err, "round %d", rnd)
if rnd < lastRound-lookback-txvalidity-1 {
var missingRoundErr *txtailMissingRound
require.Truef(t, errors.As(err, &missingRoundErr), "error a txtailMissingRound(%d) : %v ", rnd, err)
} else {
var txInLedgerErr *ledgercore.TransactionInLedgerError
require.Truef(t, errors.As(err, &txInLedgerErr), "error a TransactionInLedgerError(%d) : %v ", rnd, err)
}
}
// test lease detection
for rnd := basics.Round(1); rnd < lastRound; rnd++ {
lease := ledgercore.Txlease{Sender: basics.Address(crypto.Hash([]byte{byte(rnd % 256), byte(rnd / 256), byte(2)})), Lease: crypto.Hash([]byte{byte(rnd % 256), byte(rnd / 256), byte(3)})}
err := tail.checkDup(proto, rnd, basics.Round(0), rnd, transactions.Txid{}, lease)
require.Errorf(t, err, "round %d", rnd)
if rnd < lastRound-lookback-1 {
var missingRoundErr *txtailMissingRound
require.Truef(t, errors.As(err, &missingRoundErr), "error a txtailMissingRound(%d) : %v ", rnd, err)
} else {
var leaseInLedgerErr *ledgercore.LeaseInLedgerError
require.Truef(t, errors.As(err, &leaseInLedgerErr), "error a LeaseInLedgerError(%d) : %v ", rnd, err)
}
}
}