diff --git a/ledger/accountdb_test.go b/ledger/accountdb_test.go index 3b68a60d00..c290ed827c 100644 --- a/ledger/accountdb_test.go +++ b/ledger/accountdb_test.go @@ -148,8 +148,8 @@ func TestAccountDBInit(t *testing.T) { proto := config.Consensus[protocol.ConsensusCurrentVersion] - dbs, _ := dbOpenTest(t, true) - setDbLogging(t, dbs) + dbs, _ := storetesting.DbOpenTest(t, true) + storetesting.SetDbLogging(t, dbs) defer dbs.Close() tx, err := dbs.Wdb.Handle.Begin() @@ -209,8 +209,8 @@ func TestAccountDBRound(t *testing.T) { proto := config.Consensus[protocol.ConsensusCurrentVersion] - dbs, _ := dbOpenTest(t, true) - setDbLogging(t, dbs) + dbs, _ := storetesting.DbOpenTest(t, true) + storetesting.SetDbLogging(t, dbs) defer dbs.Close() tx, err := dbs.Wdb.Handle.Begin() @@ -365,8 +365,8 @@ func TestAccountDBInMemoryAcct(t *testing.T) { for i, test := range tests { - dbs, _ := dbOpenTest(t, true) - setDbLogging(t, dbs) + dbs, _ := storetesting.DbOpenTest(t, true) + storetesting.SetDbLogging(t, dbs) defer dbs.Close() tx, err := dbs.Wdb.Handle.Begin() @@ -437,8 +437,8 @@ func TestAccountDBInMemoryAcct(t *testing.T) { func TestAccountStorageWithStateProofID(t *testing.T) { partitiontest.PartitionTest(t) - dbs, _ := dbOpenTest(t, true) - setDbLogging(t, dbs) + dbs, _ := storetesting.DbOpenTest(t, true) + storetesting.SetDbLogging(t, dbs) defer dbs.Close() tx, err := dbs.Wdb.Handle.Begin() @@ -651,8 +651,8 @@ func cleanupTestDb(dbs db.Pair, dbName string, inMemory bool) { } func benchmarkReadingAllBalances(b *testing.B, inMemory bool) { - dbs, fn := dbOpenTest(b, inMemory) - setDbLogging(b, dbs) + dbs, fn := storetesting.DbOpenTest(b, inMemory) + storetesting.SetDbLogging(b, dbs) defer cleanupTestDb(dbs, fn, inMemory) benchmarkInitBalances(b, b.N, dbs, protocol.ConsensusCurrentVersion) @@ -682,8 +682,8 @@ func BenchmarkReadingAllBalancesDisk(b *testing.B) { } func benchmarkReadingRandomBalances(b *testing.B, inMemory bool) { - dbs, fn := dbOpenTest(b, inMemory) - setDbLogging(b, dbs) + dbs, fn := storetesting.DbOpenTest(b, inMemory) + storetesting.SetDbLogging(b, dbs) defer cleanupTestDb(dbs, fn, inMemory) accounts := benchmarkInitBalances(b, b.N, dbs, protocol.ConsensusCurrentVersion) @@ -721,8 +721,8 @@ func BenchmarkWritingRandomBalancesDisk(b *testing.B) { batchCount := 1000 startupAcct := 5 initDatabase := func() (*sql.Tx, func(), error) { - dbs, fn := dbOpenTest(b, false) - setDbLogging(b, dbs) + dbs, fn := storetesting.DbOpenTest(b, false) + storetesting.SetDbLogging(b, dbs) cleanup := func() { cleanupTestDb(dbs, fn, false) } @@ -963,8 +963,8 @@ func TestLookupKeysByPrefix(t *testing.T) { partitiontest.PartitionTest(t) t.Parallel() - dbs, fn := dbOpenTest(t, false) - setDbLogging(t, dbs) + dbs, fn := storetesting.DbOpenTest(t, false) + storetesting.SetDbLogging(t, dbs) defer cleanupTestDb(dbs, fn, false) // return account data, initialize DB tables from AccountsInitTest @@ -1144,8 +1144,8 @@ func TestLookupKeysByPrefix(t *testing.T) { func BenchmarkLookupKeyByPrefix(b *testing.B) { // learn something from BenchmarkWritingRandomBalancesDisk - dbs, fn := dbOpenTest(b, false) - setDbLogging(b, dbs) + dbs, fn := storetesting.DbOpenTest(b, false) + storetesting.SetDbLogging(b, dbs) defer cleanupTestDb(dbs, fn, false) // return account data, initialize DB tables from AccountsInitTest @@ -1422,8 +1422,8 @@ func TestCompactResourceDeltas(t *testing.T) { func TestLookupAccountAddressFromAddressID(t *testing.T) { partitiontest.PartitionTest(t) - dbs, _ := dbOpenTest(t, true) - setDbLogging(t, dbs) + dbs, _ := storetesting.DbOpenTest(t, true) + storetesting.SetDbLogging(t, dbs) defer dbs.Close() addrs := make([]basics.Address, 100) @@ -2080,8 +2080,8 @@ func initBoxDatabase(b *testing.B, totalBoxes, boxSize int) (db.Pair, func(), er } proto := config.Consensus[protocol.ConsensusCurrentVersion] - dbs, fn := dbOpenTest(b, false) - setDbLogging(b, dbs) + dbs, fn := storetesting.DbOpenTest(b, false) + storetesting.SetDbLogging(b, dbs) cleanup := func() { cleanupTestDb(dbs, fn, false) } @@ -2219,8 +2219,8 @@ func TestAccountOnlineQueries(t *testing.T) { proto := config.Consensus[protocol.ConsensusCurrentVersion] - dbs, _ := dbOpenTest(t, true) - setDbLogging(t, dbs) + dbs, _ := storetesting.DbOpenTest(t, true) + storetesting.SetDbLogging(t, dbs) defer dbs.Close() tx, err := dbs.Wdb.Handle.Begin() @@ -2723,8 +2723,8 @@ func TestAccountOnlineAccountsNewRoundFlip(t *testing.T) { func TestAccountOnlineRoundParams(t *testing.T) { partitiontest.PartitionTest(t) - dbs, _ := dbOpenTest(t, true) - setDbLogging(t, dbs) + dbs, _ := storetesting.DbOpenTest(t, true) + storetesting.SetDbLogging(t, dbs) defer dbs.Close() tx, err := dbs.Wdb.Handle.Begin() @@ -2776,8 +2776,8 @@ func TestAccountOnlineRoundParams(t *testing.T) { func TestOnlineAccountsDeletion(t *testing.T) { partitiontest.PartitionTest(t) - dbs, _ := dbOpenTest(t, true) - setDbLogging(t, dbs) + dbs, _ := storetesting.DbOpenTest(t, true) + storetesting.SetDbLogging(t, dbs) defer dbs.Close() tx, err := dbs.Wdb.Handle.Begin() diff --git a/ledger/acctupdates_test.go b/ledger/acctupdates_test.go index 4f90ec966d..7329c61e12 100644 --- a/ledger/acctupdates_test.go +++ b/ledger/acctupdates_test.go @@ -39,6 +39,7 @@ import ( "github.com/algorand/go-algorand/ledger/internal" "github.com/algorand/go-algorand/ledger/ledgercore" "github.com/algorand/go-algorand/ledger/store" + storetesting "github.com/algorand/go-algorand/ledger/store/testing" ledgertesting "github.com/algorand/go-algorand/ledger/testing" "github.com/algorand/go-algorand/logging" "github.com/algorand/go-algorand/protocol" @@ -93,7 +94,7 @@ func setupAccts(niter int) []map[basics.Address]basics.AccountData { } func makeMockLedgerForTrackerWithLogger(t testing.TB, inMemory bool, initialBlocksCount int, consensusVersion protocol.ConsensusVersion, accts []map[basics.Address]basics.AccountData, l logging.Logger) *mockLedgerForTracker { - dbs, fileName := dbOpenTest(t, inMemory) + dbs, fileName := storetesting.DbOpenTest(t, inMemory) dbs.Rdb.SetLogger(l) dbs.Wdb.SetLogger(l) @@ -1149,8 +1150,8 @@ func TestListCreatables(t *testing.T) { numElementsPerSegement := 25 // set up the database - dbs, _ := dbOpenTest(t, true) - setDbLogging(t, dbs) + dbs, _ := storetesting.DbOpenTest(t, true) + storetesting.SetDbLogging(t, dbs) defer dbs.Close() tx, err := dbs.Wdb.Handle.Begin() diff --git a/ledger/blockqueue_test.go b/ledger/blockqueue_test.go index 0f62798901..90aa7b4fee 100644 --- a/ledger/blockqueue_test.go +++ b/ledger/blockqueue_test.go @@ -26,6 +26,7 @@ import ( "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" ledgertesting "github.com/algorand/go-algorand/ledger/testing" @@ -34,6 +35,33 @@ import ( "github.com/algorand/go-algorand/test/partitiontest" ) +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) diff --git a/ledger/blockdb_test.go b/ledger/store/blockdb/blockdb_test.go similarity index 70% rename from ledger/blockdb_test.go rename to ledger/store/blockdb/blockdb_test.go index 25fe573c08..f659888c2b 100644 --- a/ledger/blockdb_test.go +++ b/ledger/store/blockdb/blockdb_test.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Affero General Public License // along with go-algorand. If not, see . -package ledger +package blockdb import ( "database/sql" @@ -26,24 +26,20 @@ import ( "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/store/blockdb" storetesting "github.com/algorand/go-algorand/ledger/store/testing" "github.com/algorand/go-algorand/protocol" "github.com/algorand/go-algorand/test/partitiontest" - "github.com/algorand/go-algorand/util/db" ) -// TODO[store-refactor]: temporary leftovers, refactor calls to store.* -func dbOpenTest(t testing.TB, inMemory bool) (db.Pair, string) { - return storetesting.DbOpenTest(t, inMemory) -} +var testPoolAddr = basics.Address{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff} +var testSinkAddr = basics.Address{0x2c, 0x2a, 0x6c, 0xe9, 0xa9, 0xa7, 0xc2, 0x8c, 0x22, 0x95, 0xfd, 0x32, 0x4f, 0x77, 0xa5, 0x4, 0x8b, 0x42, 0xc2, 0xb7, 0xa8, 0x54, 0x84, 0xb6, 0x80, 0xb1, 0xe1, 0x3d, 0x59, 0x9b, 0xeb, 0x36} -// TODO[store-refactor]: temporary leftovers, refactor calls to store.* -func setDbLogging(t testing.TB, dbs db.Pair) { - storetesting.SetDbLogging(t, dbs) +type testBlockEntry struct { + block bookkeeping.Block + cert agreement.Certificate } -func randomBlock(r basics.Round) blockEntry { +func randomBlock(r basics.Round) testBlockEntry { b := bookkeeping.Block{} c := agreement.Certificate{} @@ -53,14 +49,14 @@ func randomBlock(r basics.Round) blockEntry { b.FeeSink = testSinkAddr c.Round = r - return blockEntry{ + return testBlockEntry{ block: b, cert: c, } } -func randomInitChain(proto protocol.ConsensusVersion, nblock int) []blockEntry { - res := make([]blockEntry, 0) +func randomInitChain(proto protocol.ConsensusVersion, nblock int) []testBlockEntry { + res := make([]testBlockEntry, 0) for i := 0; i < nblock; i++ { blkent := randomBlock(basics.Round(i)) blkent.cert = agreement.Certificate{} @@ -70,20 +66,12 @@ func randomInitChain(proto protocol.ConsensusVersion, nblock int) []blockEntry { return res } -func blockChainBlocks(be []blockEntry) []bookkeeping.Block { - res := make([]bookkeeping.Block, 0) - for _, e := range be { - res = append(res, e.block) - } - return res -} - -func checkBlockDB(t *testing.T, tx *sql.Tx, blocks []blockEntry) { - next, err := blockdb.BlockNext(tx) +func checkBlockDB(t *testing.T, tx *sql.Tx, blocks []testBlockEntry) { + next, err := BlockNext(tx) require.NoError(t, err) require.Equal(t, next, basics.Round(len(blocks))) - latest, err := blockdb.BlockLatest(tx) + latest, err := BlockLatest(tx) if len(blocks) == 0 { require.Error(t, err) } else { @@ -91,7 +79,7 @@ func checkBlockDB(t *testing.T, tx *sql.Tx, blocks []blockEntry) { require.Equal(t, latest, basics.Round(len(blocks))-1) } - earliest, err := blockdb.BlockEarliest(tx) + earliest, err := BlockEarliest(tx) if len(blocks) == 0 { require.Error(t, err) } else { @@ -100,32 +88,40 @@ func checkBlockDB(t *testing.T, tx *sql.Tx, blocks []blockEntry) { } for rnd := basics.Round(0); rnd < basics.Round(len(blocks)); rnd++ { - blk, err := blockdb.BlockGet(tx, rnd) + blk, err := BlockGet(tx, rnd) require.NoError(t, err) require.Equal(t, blk, blocks[rnd].block) - blk, cert, err := blockdb.BlockGetCert(tx, rnd) + blk, cert, err := BlockGetCert(tx, rnd) require.NoError(t, err) require.Equal(t, blk, blocks[rnd].block) require.Equal(t, cert, blocks[rnd].cert) } - _, err = blockdb.BlockGet(tx, basics.Round(len(blocks))) + _, err = BlockGet(tx, basics.Round(len(blocks))) require.Error(t, err) } +func blockChainBlocks(be []testBlockEntry) []bookkeeping.Block { + res := make([]bookkeeping.Block, 0) + for _, e := range be { + res = append(res, e.block) + } + return res +} + func TestBlockDBEmpty(t *testing.T) { partitiontest.PartitionTest(t) - dbs, _ := dbOpenTest(t, true) - setDbLogging(t, dbs) + dbs, _ := storetesting.DbOpenTest(t, true) + storetesting.SetDbLogging(t, dbs) defer dbs.Close() tx, err := dbs.Wdb.Handle.Begin() require.NoError(t, err) defer tx.Rollback() - err = blockdb.BlockInit(tx, nil) + err = BlockInit(tx, nil) require.NoError(t, err) checkBlockDB(t, tx, nil) } @@ -133,8 +129,8 @@ func TestBlockDBEmpty(t *testing.T) { func TestBlockDBInit(t *testing.T) { partitiontest.PartitionTest(t) - dbs, _ := dbOpenTest(t, true) - setDbLogging(t, dbs) + dbs, _ := storetesting.DbOpenTest(t, true) + storetesting.SetDbLogging(t, dbs) defer dbs.Close() tx, err := dbs.Wdb.Handle.Begin() @@ -143,11 +139,11 @@ func TestBlockDBInit(t *testing.T) { blocks := randomInitChain(protocol.ConsensusCurrentVersion, 10) - err = blockdb.BlockInit(tx, blockChainBlocks(blocks)) + err = BlockInit(tx, blockChainBlocks(blocks)) require.NoError(t, err) checkBlockDB(t, tx, blocks) - err = blockdb.BlockInit(tx, blockChainBlocks(blocks)) + err = BlockInit(tx, blockChainBlocks(blocks)) require.NoError(t, err) checkBlockDB(t, tx, blocks) } @@ -155,8 +151,8 @@ func TestBlockDBInit(t *testing.T) { func TestBlockDBAppend(t *testing.T) { partitiontest.PartitionTest(t) - dbs, _ := dbOpenTest(t, true) - setDbLogging(t, dbs) + dbs, _ := storetesting.DbOpenTest(t, true) + storetesting.SetDbLogging(t, dbs) defer dbs.Close() tx, err := dbs.Wdb.Handle.Begin() @@ -165,13 +161,13 @@ func TestBlockDBAppend(t *testing.T) { blocks := randomInitChain(protocol.ConsensusCurrentVersion, 10) - err = blockdb.BlockInit(tx, blockChainBlocks(blocks)) + err = BlockInit(tx, blockChainBlocks(blocks)) require.NoError(t, err) checkBlockDB(t, tx, blocks) for i := 0; i < 10; i++ { blkent := randomBlock(basics.Round(len(blocks))) - err = blockdb.BlockPut(tx, blkent.block, blkent.cert) + err = BlockPut(tx, blkent.block, blkent.cert) require.NoError(t, err) blocks = append(blocks, blkent) diff --git a/ledger/txtail_test.go b/ledger/txtail_test.go index c6aee2c834..4e04abf01a 100644 --- a/ledger/txtail_test.go +++ b/ledger/txtail_test.go @@ -31,6 +31,7 @@ import ( "github.com/algorand/go-algorand/data/transactions" "github.com/algorand/go-algorand/ledger/ledgercore" "github.com/algorand/go-algorand/ledger/store" + storetesting "github.com/algorand/go-algorand/ledger/store/testing" ledgertesting "github.com/algorand/go-algorand/ledger/testing" "github.com/algorand/go-algorand/protocol" "github.com/algorand/go-algorand/test/partitiontest" @@ -148,8 +149,8 @@ func (t *txTailTestLedger) Block(r basics.Round) (bookkeeping.Block, error) { func (t *txTailTestLedger) initialize(ts *testing.T, protoVersion protocol.ConsensusVersion) error { // create a corresponding blockdb. inMemory := true - t.blockDBs, _ = dbOpenTest(ts, inMemory) - t.trackerDBs, _ = dbOpenTest(ts, inMemory) + t.blockDBs, _ = storetesting.DbOpenTest(ts, inMemory) + t.trackerDBs, _ = storetesting.DbOpenTest(ts, inMemory) t.protoVersion = protoVersion tx, err := t.trackerDBs.Wdb.Handle.Begin()