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

Commit

Permalink
Merge pull request #64 from itzmeanjan/fix-block-insertion
Browse files Browse the repository at this point in the history
Conflicting Block Entry Resolution
  • Loading branch information
itzmeanjan authored Dec 6, 2021
2 parents 71754fd + b4be574 commit 6b2891d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
19 changes: 10 additions & 9 deletions app/db/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,16 @@ func StoreBlock(dbWOTx *gorm.DB, block *PackedBlock, status *d.StatusHolder, que

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

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

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

if err := UpdateBlock(dbWTx, block.Block); err != nil {
return err
}
blockInserted = true

} else {

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

}

// DeleteBlock - Delete block entry, identified by block number, while
// cascading all dependent entries ( i.e. in transactions/ events table )
func DeleteBlock(dbWTx *gorm.DB, number uint64) error {
return dbWTx.Where("number = ?", number).Delete(&Blocks{}).Error
}

// UpdateBlock - Updating already existing block
func UpdateBlock(dbWTx *gorm.DB, block *Blocks) error {

Expand Down
6 changes: 3 additions & 3 deletions app/db/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ type Blocks struct {
TransactionRootHash string `gorm:"column:txroothash;type:char(66);not null"`
ReceiptRootHash string `gorm:"column:receiptroothash;type:char(66);not null"`
ExtraData []byte `gorm:"column:extradata;type:bytea"`
Transactions Transactions `gorm:"foreignKey:blockhash"`
Events Events `gorm:"foreignKey:blockhash"`
Transactions Transactions `gorm:"foreignKey:blockhash;constraint:OnDelete:CASCADE;"`
Events Events `gorm:"foreignKey:blockhash;constraint:OnDelete:CASCADE;"`
}

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

// TableName - Overriding default table name
Expand Down
8 changes: 4 additions & 4 deletions db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ create table transactions (
nonce bigint not null,
state smallint not null,
blockhash char(66) not null,
foreign key (blockhash) references blocks(hash)
foreign key (blockhash) references blocks(hash) on delete cascade
);

create index on transactions(from);
Expand All @@ -53,8 +53,8 @@ create table events (
txhash char(66) not null,
blockhash char(66) not null,
primary key (blockhash, index),
foreign key (txhash) references transactions(hash),
foreign key (blockhash) references blocks(hash)
foreign key (txhash) references transactions(hash) on delete cascade,
foreign key (blockhash) references blocks(hash) on delete cascade
);

create index on events(origin);
Expand All @@ -75,7 +75,7 @@ create table delivery_history (
client char(42) not null,
ts timestamp not null,
endpoint varchar(100) not null,
datalength bigint not null,
datalength bigint not null
);

create index on delivery_history(client);
Expand Down

0 comments on commit 6b2891d

Please sign in to comment.