Skip to content

Commit

Permalink
idb(postgres): add protocol version check before writing to database. (
Browse files Browse the repository at this point in the history
  • Loading branch information
winder authored Mar 15, 2023
1 parent 922653f commit cf0074c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
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]
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")
}

0 comments on commit cf0074c

Please sign in to comment.