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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge transactions with same hash when writing to event db #2338

Merged
merged 6 commits into from May 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -34,7 +34,7 @@ func (edb *EventDb) addTransactions(txns []Transaction) error {
}

func mergeAddTransactionsEvents() *eventsMergerImpl[Transaction] {
return newEventsMerger[Transaction](TagAddTransactions)
return newEventsMerger[Transaction](TagAddTransactions, withUniqueEventOverwrite())
}

// GetTransactionByHash finds the transaction record by hash
Expand Down
75 changes: 51 additions & 24 deletions code/go/0chain.net/smartcontract/dbs/event/transaction_test.go
Expand Up @@ -13,33 +13,60 @@ import (
"github.com/stretchr/testify/require"
)

func TestAddTransaction(t *testing.T) {
t.Skip("only for local debugging, requires local postgresql")
access := config.DbAccess{
Enabled: true,
Name: "events_db",
User: os.Getenv("POSTGRES_USER"),
Password: os.Getenv("POSTGRES_PASSWORD"),
Host: os.Getenv("POSTGRES_HOST"),
Port: os.Getenv("POSTGRES_PORT"),
MaxIdleConns: 100,
MaxOpenConns: 200,
ConnMaxLifetime: 20 * time.Second,
func TestTagAddTransactions(t *testing.T) {
edb, clean := GetTestEventDB(t)
defer clean()

round := int64(7)

transactionsEvents := []Event{
{
BlockNumber: round,
Type: TypeStats,
Tag: TagAddTransactions,
Index: "one",
Data: Transaction{Hash: "one", Fee: 3},
},
{
BlockNumber: round,
Type: TypeStats,
Tag: TagAddTransactions,
Index: "one",
Data: Transaction{Hash: "one", Fee: 3},
},
{
BlockNumber: round,
TxHash: "2",
Type: TypeStats,
Tag: TagAddTransactions,
Index: "two",
Data: Transaction{Hash: "two", Fee: 5},
},
{
BlockNumber: round,
Type: TypeStats,
Tag: TagAddTransactions,
Index: "two",
Data: Transaction{Hash: "two", Fee: 7},
},
{
BlockNumber: round,
Type: TypeStats,
Tag: TagAddTransactions,
Index: "three",
Data: Transaction{Hash: "three", Fee: 11},
},
}
eventDb, err := NewEventDb(access, config.DbSettings{})
require.NoError(t, err)
defer eventDb.Close()
err = eventDb.AutoMigrate()
events, err := mergeEvents(round, "", transactionsEvents)
require.NoError(t, err)
require.Len(t, events, 1)
require.Len(t, events[0].Data, 3, "the five events should have been merged into three")
dabasov marked this conversation as resolved.
Show resolved Hide resolved

tr := Transaction{}
err = eventDb.addTransactions([]Transaction{tr})
require.NoError(t, err, "Error while inserting Transaction to event Database")
var count int64
eventDb.Get().Table("transactions").Count(&count)
require.Equal(t, int64(1), count, "Transaction not getting inserted")
err = eventDb.Drop()
require.NoError(t, err)
require.NoError(t, edb.addStat(events[0]))

var txs []Transaction
edb.Get().Find(&txs)
require.Len(t, txs, 3)
}

func TestFindTransactionByHash(t *testing.T) {
Expand Down