-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix FK constraint error when running host on sqlite (#2141)
* Check for blocks before adding to avoid FK/ UK constraint
- Loading branch information
1 parent
940fbd1
commit 0a5cf6e
Showing
8 changed files
with
180 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package hostdb | ||
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
"math/big" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
gethcommon "github.com/ethereum/go-ethereum/common" | ||
|
||
"github.com/ethereum/go-ethereum/core/types" | ||
) | ||
|
||
func TestCanStoreAndRetrieveBlock(t *testing.T) { | ||
db, _ := createSQLiteDB(t) | ||
block1 := createBlock(batchNumber) | ||
block2 := createBlock(batchNumber + 1) | ||
|
||
// verify we get ErrNoRows for a non-existent block | ||
randomHash := gethcommon.Hash{} | ||
randomHash.SetBytes(make([]byte, 32)) // 32 bytes for appropriate length | ||
dbtx, _ := db.NewDBTransaction() | ||
statements := db.GetSQLStatement() | ||
_, err := GetBlockId(dbtx.Tx, statements, randomHash) | ||
if !errors.Is(err, sql.ErrNoRows) { | ||
t.Errorf("expected sql.ErrNoRows for non-existent block, got: %v", err) | ||
} | ||
dbtx.Rollback() | ||
|
||
dbtx, _ = db.NewDBTransaction() | ||
err = AddBlock(dbtx.Tx, statements, &block1) | ||
if err != nil { | ||
t.Errorf("could not store block1: %s", err) | ||
} | ||
err = dbtx.Write() | ||
if err != nil { | ||
t.Errorf("could not commit block1: %s", err) | ||
} | ||
|
||
dbtx, _ = db.NewDBTransaction() | ||
err = AddBlock(dbtx.Tx, statements, &block2) | ||
if err != nil { | ||
t.Errorf("could not store block2: %s", err) | ||
} | ||
err = dbtx.Write() | ||
if err != nil { | ||
t.Errorf("could not commit block2: %s", err) | ||
} | ||
|
||
dbtx, _ = db.NewDBTransaction() | ||
blockId, err := GetBlockId(dbtx.Tx, statements, block2.Hash()) | ||
if err != nil { | ||
t.Errorf("stored block but could not retrieve block ID: %s", err) | ||
} | ||
if *blockId != 2 { | ||
t.Errorf("expected block ID 2, got %d", *blockId) | ||
} | ||
dbtx.Rollback() | ||
} | ||
|
||
func TestAddBlockWithForeignKeyConstraint(t *testing.T) { | ||
db, _ := createSQLiteDB(t) | ||
dbtx, _ := db.NewDBTransaction() | ||
statements := db.GetSQLStatement() | ||
metadata := createRollupMetadata(batchNumber - 10) | ||
rollup := createRollup(batchNumber) | ||
block := types.NewBlock(&types.Header{}, nil, nil, nil) | ||
|
||
// add block | ||
err := AddBlock(dbtx.Tx, db.GetSQLStatement(), block.Header()) | ||
if err != nil { | ||
t.Errorf("could not store block. Cause: %s", err) | ||
} | ||
dbtx.Write() | ||
dbtx, _ = db.NewDBTransaction() | ||
|
||
// add rollup referencing block | ||
err = AddRollup(dbtx, db.GetSQLStatement(), &rollup, &metadata, block) | ||
if err != nil { | ||
t.Errorf("could not store rollup. Cause: %s", err) | ||
} | ||
dbtx.Write() | ||
|
||
// this should fail due to the UNIQUE constraint on block_host.hash | ||
dbtx, _ = db.NewDBTransaction() | ||
err = AddBlock(dbtx.Tx, statements, block.Header()) | ||
if !strings.Contains(err.Error(), "UNIQUE constraint failed: block_host.hash") { | ||
t.Fatalf("expected UNIQUE constraint error, got: %v", err) | ||
} | ||
|
||
// verify the block still exists | ||
_, err = GetBlockId(dbtx.Tx, statements, block.Hash()) | ||
if err != nil { | ||
t.Fatalf("failed to get block id after duplicate insert: %v", err) | ||
} | ||
|
||
dbtx.Rollback() | ||
} | ||
|
||
func createBlock(blockNum int64) types.Header { | ||
return types.Header{ | ||
Number: big.NewInt(blockNum), | ||
Time: uint64(time.Now().Unix()), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters