Skip to content

Commit

Permalink
add more db read functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nautsimon committed Oct 26, 2023
1 parent 1a5fadf commit b0b3047
Show file tree
Hide file tree
Showing 17 changed files with 937 additions and 150 deletions.
14 changes: 12 additions & 2 deletions services/sinner/api/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ func (t *APISuite) TestGetOrigin() {
err := t.db.StoreOriginSent(t.GetTestContext(), originSent)
Nil(t.T(), err)

result, err := t.sinnerAPI.GetOriginInfo(t.GetTestContext(), txHash, int(chainID))
result, err := t.sinnerAPI.GetOriginInfo(t.GetTestContext(), messageHash)
Nil(t.T(), err)
NotNil(t.T(), result)
Equal(t.T(), txHash, *result.Response.OriginTxHash)

results, err := t.sinnerAPI.GetOriginInfos(t.GetTestContext(), txHash, int(chainID))
Nil(t.T(), err)
NotNil(t.T(), results)
Equal(t.T(), txHash, *results.Response[0].OriginTxHash)
}

func (t *APISuite) TestGetExecuted() {
Expand All @@ -51,10 +56,15 @@ func (t *APISuite) TestGetExecuted() {
err := t.db.StoreExecuted(t.GetTestContext(), executed)
Nil(t.T(), err)

result, err := t.sinnerAPI.GetDestinationInfo(t.GetTestContext(), txHash, int(chainID))
result, err := t.sinnerAPI.GetDestinationInfo(t.GetTestContext(), messageHash)
Nil(t.T(), err)
NotNil(t.T(), result)
Equal(t.T(), txHash, *result.Response.TxHash)

results, err := t.sinnerAPI.GetDestinationInfos(t.GetTestContext(), txHash, int(chainID))
Nil(t.T(), err)
NotNil(t.T(), results)
Equal(t.T(), txHash, *results.Response[0].TxHash)
}

func (t *APISuite) TestMessageStatus() {
Expand Down
2 changes: 1 addition & 1 deletion services/sinner/contracts/origin/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (p *ParserImpl) ParseSent(log ethTypes.Log) (*model.OriginSent, error) {
TxHash: iFace.Raw.TxHash.String(),
TxIndex: iFace.Raw.TxIndex,
DestinationChainID: iFace.Destination,
Message: common.Bytes2Hex(iFace.Message[:]),
Message: common.Bytes2Hex(iFace.Message),
Nonce: iFace.Nonce,
MessageHash: common.Bytes2Hex(iFace.MessageHash[:]),
}
Expand Down
12 changes: 8 additions & 4 deletions services/sinner/db/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ type EventDBWriter interface {
//
//nolint:interfacebloat
type EventDBReader interface {
// RetrieveOriginSent gets the OriginSent record.
RetrieveOriginSent(ctx context.Context, chainID uint32, txHash string) (model.OriginSent, error)
// RetrieveOriginSent gets the Origin Sent event with a message hash.
RetrieveOriginSent(ctx context.Context, messageHash string) (model.OriginSent, error)
// RetrieveOriginSents gets the Origin Sent events tied to a tx hash.
RetrieveOriginSents(ctx context.Context, chainID uint32, txHash string) ([]model.OriginSent, error)
// RetrieveExecuted gets the Executed event with a message hash.
RetrieveExecuted(ctx context.Context, messageHash string) (model.Executed, error)
// RetrieveExecuteds gets the Executed events tied to a tx hash.
RetrieveExecuteds(ctx context.Context, chainID uint32, txHash string) ([]model.Executed, error)
// RetrieveMessageStatus gets status of a message.
RetrieveMessageStatus(ctx context.Context, messageHash string) (graphqlModel.MessageStatus, error)
// RetrieveLastStoredBlock gets the last block stored in sinner.
RetrieveLastStoredBlock(ctx context.Context, chainID uint32, address common.Address) (uint64, error)
// RetrieveExecuted gets the Executed record.
RetrieveExecuted(ctx context.Context, chainID uint32, txHash string) (model.Executed, error)
}

// EventDB stores events.
Expand Down
66 changes: 56 additions & 10 deletions services/sinner/db/mocks/event_db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions services/sinner/db/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type OriginSent struct {
// BlockNumber is the block number in which the tx occurred.
BlockNumber uint64 `gorm:"column:block_number"`
// TxHash is the hash of the tx.
TxHash string `gorm:"column:tx_hash;primaryKey;index:idx_tx_hash_origin,priority:1,sort:desc"`
TxHash string `gorm:"column:tx_hash;index:idx_tx_hash_origin,priority:1,sort:desc"`
// TxIndex is the index of the tx in a block.
TxIndex uint `gorm:"column:tx_index"`
// Sender is the address of the sender of the tx.
Expand All @@ -76,7 +76,7 @@ type OriginSent struct {
// MessageID is the keccaked message.
MessageID string `gorm:"column:message_id"`
// MessageHash is the message hash.
MessageHash string `gorm:"column:message_hash"`
MessageHash string `gorm:"column:message_hash;primaryKey"`
// ChainID is the chain id.
ChainID uint32 `gorm:"column:chain_id;primaryKey;index:idx_tx_hash_origin,priority:2,sort:desc"`
// Destination is the destination chain id.
Expand Down Expand Up @@ -114,11 +114,11 @@ type Executed struct {
// BlockNumber is the block number in which the tx occurred.
BlockNumber uint64 `gorm:"column:block_number"`
// TxHash is the hash of the tx.
TxHash string `gorm:"column:tx_hash;primaryKey;index:idx_tx_hash_executed,priority:1,sort:desc"`
TxHash string `gorm:"column:tx_hash;index:idx_tx_hash_executed,priority:1,sort:desc"`
// TxIndex is the index of the tx in a block.
TxIndex uint `gorm:"column:tx_index"`
// MessageHash is the message hash.
MessageHash string `gorm:"column:message_hash"`
MessageHash string `gorm:"column:message_hash;primaryKey"`
// ChainID is the chain id.
ChainID uint32 `gorm:"column:chain_id;primaryKey;index:idx_tx_hash_executed,priority:2,sort:desc"`
// RemoteDomain is the destination.
Expand Down
16 changes: 14 additions & 2 deletions services/sinner/db/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,17 @@ func (t *DBSuite) TestRetrieveOriginSent() {
Nil(t.T(), err)

// Test: Retrieve and validate the OriginSent record
retrievedRecord, err := testDB.RetrieveOriginSent(t.GetTestContext(), chainID, txHash)
retrievedRecord, err := testDB.RetrieveOriginSent(t.GetTestContext(), messageHash)
Nil(t.T(), err)
Equal(t.T(), txHash, retrievedRecord.TxHash)
Equal(t.T(), chainID, retrievedRecord.ChainID)
Equal(t.T(), messageHash, retrievedRecord.MessageHash)

retrievedRecords, err := testDB.RetrieveOriginSents(t.GetTestContext(), chainID, txHash)
Nil(t.T(), err)
Equal(t.T(), txHash, retrievedRecords[0].TxHash)
Equal(t.T(), chainID, retrievedRecords[0].ChainID)
Equal(t.T(), messageHash, retrievedRecords[0].MessageHash)
})
}

Expand All @@ -118,10 +124,16 @@ func (t *DBSuite) TestRetrieveExecuted() {
Nil(t.T(), err)

// Test: Retrieve and validate the Executed record
retrievedRecord, err := testDB.RetrieveExecuted(t.GetTestContext(), chainID, txHash)
retrievedRecord, err := testDB.RetrieveExecuted(t.GetTestContext(), messageHash)
Nil(t.T(), err)
Equal(t.T(), txHash, retrievedRecord.TxHash)
Equal(t.T(), chainID, retrievedRecord.ChainID)
Equal(t.T(), messageHash, retrievedRecord.MessageHash)

retrievedRecords, err := testDB.RetrieveExecuteds(t.GetTestContext(), chainID, txHash)
Nil(t.T(), err)
Equal(t.T(), txHash, retrievedRecords[0].TxHash)
Equal(t.T(), chainID, retrievedRecords[0].ChainID)
Equal(t.T(), messageHash, retrievedRecords[0].MessageHash)
})
}
50 changes: 38 additions & 12 deletions services/sinner/db/sql/base/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func (s Store) RetrieveMessageStatus(ctx context.Context, messageHash string) (graphqlModel.MessageStatus, error) {
var record model.MessageStatus

err := s.DB().WithContext(ctx).
err := s.DB().WithContext(ctx).Model(&model.MessageStatus{}).
Where("message_hash = ?", messageHash).
First(&record).Error

Expand Down Expand Up @@ -41,7 +41,7 @@ func (s Store) RetrieveMessageStatus(ctx context.Context, messageHash string) (g
// RetrieveLastStoredBlock gets the last stored block.
func (s Store) RetrieveLastStoredBlock(ctx context.Context, chainID uint32, address common.Address) (uint64, error) {
var lastIndexed model.LastIndexed
err := s.DB().WithContext(ctx).
err := s.DB().WithContext(ctx).Model(&model.LastIndexed{}).
Where("contract_address = ? AND chain_id = ?", address.String(), chainID).
Order("block_number DESC").First(&lastIndexed).Error

Expand All @@ -56,28 +56,54 @@ func (s Store) RetrieveLastStoredBlock(ctx context.Context, chainID uint32, addr
return lastIndexed.BlockNumber, nil
}

// RetrieveOriginSent gets the OriginSent record.
func (s Store) RetrieveOriginSent(ctx context.Context, chainID uint32, txHash string) (model.OriginSent, error) {
// RetrieveOriginSent gets the Origin Sent event.
func (s Store) RetrieveOriginSent(ctx context.Context, messageHash string) (model.OriginSent, error) {
var originSentRecord model.OriginSent
err := s.DB().WithContext(ctx).
Where("chain_id = ? AND tx_hash = ?", chainID, txHash).
err := s.DB().WithContext(ctx).Model(&model.OriginSent{}).
Where("message_hash = ?", messageHash).
First(&originSentRecord).Error

if err != nil {
return model.OriginSent{}, fmt.Errorf("could not retrieve OriginSent record: %w", err)
return model.OriginSent{}, fmt.Errorf("could not retrieve Origin Sent event: %w", err)
}
return originSentRecord, nil
}

// RetrieveExecuted gets the Executed record.
func (s Store) RetrieveExecuted(ctx context.Context, chainID uint32, txHash string) (model.Executed, error) {
var executedRecord model.Executed
err := s.DB().WithContext(ctx).
// RetrieveOriginSents gets the Origin Sent events.
func (s Store) RetrieveOriginSents(ctx context.Context, chainID uint32, txHash string) ([]model.OriginSent, error) {
var originSentRecord []model.OriginSent
err := s.DB().WithContext(ctx).Model(&model.OriginSent{}).
Where("chain_id = ? AND tx_hash = ?", chainID, txHash).
Scan(&originSentRecord).Error

if err != nil {
return []model.OriginSent{}, fmt.Errorf("could not retrieve Origin Sent event: %w", err)
}
return originSentRecord, nil
}

// RetrieveExecuted gets a Executed event.
func (s Store) RetrieveExecuted(ctx context.Context, messageHash string) (model.Executed, error) {
var executedRecord model.Executed
err := s.DB().WithContext(ctx).Model(&model.Executed{}).
Where("message_hash = ?", messageHash).
First(&executedRecord).Error

if err != nil {
return model.Executed{}, fmt.Errorf("could not retrieve Executed record: %w", err)
return model.Executed{}, fmt.Errorf("could not retrieve Executed event: %w", err)
}
return executedRecord, nil
}

// RetrieveExecuteds gets Executed events.
func (s Store) RetrieveExecuteds(ctx context.Context, chainID uint32, txHash string) ([]model.Executed, error) {
var executedRecord []model.Executed
err := s.DB().WithContext(ctx).Model(&model.Executed{}).
Where("chain_id = ? AND tx_hash = ?", chainID, txHash).
Scan(&executedRecord).Error

if err != nil {
return []model.Executed{}, fmt.Errorf("could not retrieve Executed event: %w", err)
}
return executedRecord, nil
}
Loading

0 comments on commit b0b3047

Please sign in to comment.