Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.

Commit 6b2891d

Browse files
authored
Merge pull request #64 from itzmeanjan/fix-block-insertion
Conflicting Block Entry Resolution
2 parents 71754fd + b4be574 commit 6b2891d

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

app/db/block.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,16 @@ func StoreBlock(dbWOTx *gorm.DB, block *PackedBlock, status *d.StatusHolder, que
4545

4646
log.Printf("[!] Block %d already present in DB, similar ❌\n", block.Block.Number)
4747

48-
// -- If block is going to be updated, it's better
49-
// we also remove associated entries for that block
50-
// i.e. transactions, events
51-
if err := RemoveEventsByBlockHash(dbWTx, persistedBlock.Hash); err != nil {
48+
// cascaded deletion !
49+
if err := DeleteBlock(dbWTx, block.Block.Number); err != nil {
5250
return err
5351
}
5452

55-
if err := RemoveTransactionsByBlockHash(dbWTx, persistedBlock.Hash); err != nil {
53+
if err := PutBlock(dbWTx, block.Block); err != nil {
5654
return err
5755
}
58-
// -- block data clean up ends here
5956

60-
if err := UpdateBlock(dbWTx, block.Block); err != nil {
61-
return err
62-
}
57+
blockInserted = true
6358

6459
} else {
6560

@@ -128,6 +123,12 @@ func PutBlock(dbWTx *gorm.DB, block *Blocks) error {
128123

129124
}
130125

126+
// DeleteBlock - Delete block entry, identified by block number, while
127+
// cascading all dependent entries ( i.e. in transactions/ events table )
128+
func DeleteBlock(dbWTx *gorm.DB, number uint64) error {
129+
return dbWTx.Where("number = ?", number).Delete(&Blocks{}).Error
130+
}
131+
131132
// UpdateBlock - Updating already existing block
132133
func UpdateBlock(dbWTx *gorm.DB, block *Blocks) error {
133134

app/db/model.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ type Blocks struct {
3131
TransactionRootHash string `gorm:"column:txroothash;type:char(66);not null"`
3232
ReceiptRootHash string `gorm:"column:receiptroothash;type:char(66);not null"`
3333
ExtraData []byte `gorm:"column:extradata;type:bytea"`
34-
Transactions Transactions `gorm:"foreignKey:blockhash"`
35-
Events Events `gorm:"foreignKey:blockhash"`
34+
Transactions Transactions `gorm:"foreignKey:blockhash;constraint:OnDelete:CASCADE;"`
35+
Events Events `gorm:"foreignKey:blockhash;constraint:OnDelete:CASCADE;"`
3636
}
3737

3838
// TableName - Overriding default table name
@@ -73,7 +73,7 @@ type Transactions struct {
7373
Nonce uint64 `gorm:"column:nonce;type:bigint;not null;index"`
7474
State uint64 `gorm:"column:state;type:smallint;not null"`
7575
BlockHash string `gorm:"column:blockhash;type:char(66);not null;index"`
76-
Events Events `gorm:"foreignKey:txhash"`
76+
Events Events `gorm:"foreignKey:txhash;constraint:OnDelete:CASCADE;"`
7777
}
7878

7979
// TableName - Overriding default table name

db/schema.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ create table transactions (
3636
nonce bigint not null,
3737
state smallint not null,
3838
blockhash char(66) not null,
39-
foreign key (blockhash) references blocks(hash)
39+
foreign key (blockhash) references blocks(hash) on delete cascade
4040
);
4141

4242
create index on transactions(from);
@@ -53,8 +53,8 @@ create table events (
5353
txhash char(66) not null,
5454
blockhash char(66) not null,
5555
primary key (blockhash, index),
56-
foreign key (txhash) references transactions(hash),
57-
foreign key (blockhash) references blocks(hash)
56+
foreign key (txhash) references transactions(hash) on delete cascade,
57+
foreign key (blockhash) references blocks(hash) on delete cascade
5858
);
5959

6060
create index on events(origin);
@@ -75,7 +75,7 @@ create table delivery_history (
7575
client char(42) not null,
7676
ts timestamp not null,
7777
endpoint varchar(100) not null,
78-
datalength bigint not null,
78+
datalength bigint not null
7979
);
8080

8181
create index on delivery_history(client);

0 commit comments

Comments
 (0)