Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
shiqizng committed Jun 2, 2022
1 parent 55bcf52 commit adaabb2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,3 @@ coverage.txt
# mac OS
.DS_store

# ledger migration testing files
/idb/postgres/internal/migrations/local_ledger/*.sqlite
/idb/postgres/internal/migrations/local_ledger/*.sqlite-shm
/idb/postgres/internal/migrations/local_ledger/*.sqlite-wal
8 changes: 5 additions & 3 deletions idb/postgres/internal/migrations/local_ledger/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func RunMigration(round uint64, opts *idb.IndexerDbOptions) error {
go func() {
<-cancelCh
logger.Errorf("Ledger migration interrupted")
// exit process if migration is interrupted so that
// migration state doesn't get updated in db
os.Exit(1)
}()
}
Expand All @@ -45,6 +47,7 @@ func RunMigration(round uint64, opts *idb.IndexerDbOptions) error {
if opts.IndexerDatadir == "" {
return fmt.Errorf("RunMigration() err: indexer data directory missing")
}
// create algod client
if opts.AlgodDataDir != "" {
bot, err = fetcher.ForDataDir(opts.AlgodDataDir, logger)
if err != nil {
Expand All @@ -58,6 +61,8 @@ func RunMigration(round uint64, opts *idb.IndexerDbOptions) error {
} else {
return fmt.Errorf("RunMigration() err: unable to create algod client")
}

logger.Info("initializing ledger")
genesis, err := getGenesis(bot.Algod())
if err != nil {
return fmt.Errorf("RunMigration() err: %w", err)
Expand All @@ -76,9 +81,6 @@ func RunMigration(round uint64, opts *idb.IndexerDbOptions) error {
return fmt.Errorf("RunMigration() err: %w", err)
}
defer localLedger.Close()
if uint64(localLedger.Latest()) == round {
return nil
}
bot.SetNextRound(uint64(localLedger.Latest()) + 1)
proc, err := blockprocessor.MakeProcessor(localLedger, nil)
if err != nil {
Expand Down
48 changes: 45 additions & 3 deletions idb/postgres/internal/migrations/local_ledger/migration_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package localledger

import (
"os"
"path"
"path/filepath"
"testing"

"github.com/algorand/go-algorand-sdk/client/v2/algod"
Expand Down Expand Up @@ -62,7 +65,25 @@ func TestRunMigration(t *testing.T) {
httpmock.NewStringResponder(200, string(msgpack.Encode(blockCert3))))

// /v2/blocks/4
block4, err := test.MakeBlockForTxns(block3.BlockHeader, &txn)
assert.Nil(t, err)
blockCert4 := rpcs.EncodedBlockCert{
Block: block4,
}
httpmock.RegisterResponder("GET", "http://localhost/v2/blocks/4?format=msgpack",
httpmock.NewStringResponder(200, string(msgpack.Encode(blockCert4))))

// /v2/blocks/5
block5, err := test.MakeBlockForTxns(block4.BlockHeader, &txn)
assert.Nil(t, err)
blockCert5 := rpcs.EncodedBlockCert{
Block: block5,
}
httpmock.RegisterResponder("GET", "http://localhost/v2/blocks/5?format=msgpack",
httpmock.NewStringResponder(200, string(msgpack.Encode(blockCert5))))

// /v2/blocks/6
httpmock.RegisterResponder("GET", "http://localhost/v2/blocks/6?format=msgpack",
httpmock.NewStringResponder(404, string(json.Encode(algod.Status{}))))

// /v2/status/wait-for-block-after/{round}
Expand All @@ -74,15 +95,36 @@ func TestRunMigration(t *testing.T) {
AlgodAddr: "localhost",
AlgodToken: "AAAAA",
}
// migrate 3 rounds
err = RunMigration(3, &opts)
assert.NoError(t, err)

genesis := test.MakeGenesis()
genesisBlock := test.MakeGenesisBlock()
initState, err := util.CreateInitState(&genesis, &genesisBlock)
assert.NoError(t, err)

l, err := ledger.OpenLedger(logging.NewLogger(), "ledger", false, initState, algodConfig.GetDefaultLocal())
l, err := ledger.OpenLedger(logging.NewLogger(), filepath.Join(path.Dir(opts.IndexerDatadir), "ledger"), false, initState, algodConfig.GetDefaultLocal())
assert.NoError(t, err)
// check 3 rounds written to ledger
assert.Equal(t, uint64(3), uint64(l.Latest()))
l.Close()

// migration continues from last round
err = RunMigration(5, &opts)
assert.NoError(t, err)
l, err = ledger.OpenLedger(logging.NewLogger(), filepath.Join(path.Dir(opts.IndexerDatadir), "ledger"), false, initState, algodConfig.GetDefaultLocal())
assert.NoError(t, err)
assert.Equal(t, uint64(5), uint64(l.Latest()))
l.Close()

// remove ledger files
ledgerFiles := []string{"ledger.block.sqlite", "ledger.block.sqlite-shm", "ledger.block.sqlite-wal",
"ledger.tracker.sqlite", "ledger.tracker.sqlite-shm", "ledger.tracker.sqlite-wal"}

for _, fn := range ledgerFiles {
if _, err = os.Stat(fn); err == nil {
err = os.Remove(fn)
assert.NoError(t, err)
}
}

}

0 comments on commit adaabb2

Please sign in to comment.