Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

idb(postgres): add protocol version check before writing to database. #1506

Merged
merged 2 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions idb/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ func (db *IndexerDb) init(opts idb.IndexerDbOptions) (chan struct{}, error) {

// AddBlock is part of idb.IndexerDb.
func (db *IndexerDb) AddBlock(vb *itypes.ValidatedBlock) error {
protoVersion := protocol.ConsensusVersion(vb.Block.CurrentProtocol)
_, ok := config.Consensus[protoVersion]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this reminds me of EnableAssetCloseAmount. do we still need to support AssetCloseAmount for the older txns? maybe it's one of the things we can deprecate in the next release?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to touch the API right now. This check is mainly to ensure you're running with up to date software. i.e. imagine a new txn field is added.

if !ok {
return fmt.Errorf("unknown protocol (%s) detected, this usually means you need to upgrade", protoVersion)
}

block := vb.Block
round := block.BlockHeader.Round
db.log.Printf("adding block %d", round)
Expand Down
20 changes: 20 additions & 0 deletions idb/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

sdk "github.com/algorand/go-algorand-sdk/v2/types"

"github.com/algorand/indexer/idb"
"github.com/algorand/indexer/types"
)

func Test_txnFilterOptimization(t *testing.T) {
Expand Down Expand Up @@ -54,3 +58,19 @@ func Test_txnFilterOptimization(t *testing.T) {
})
}
}

func Test_UnknownProtocol(t *testing.T) {
db := IndexerDb{}
protocol := "zzzzzzz"
err := db.AddBlock(&types.ValidatedBlock{
Block: sdk.Block{
BlockHeader: sdk.BlockHeader{
UpgradeState: sdk.UpgradeState{
CurrentProtocol: protocol,
},
},
},
})
require.ErrorContains(t, err, protocol)
require.ErrorContains(t, err, "you need to upgrade")
}