Skip to content

Commit

Permalink
All numeric columns need to be text to avoid potential truncation
Browse files Browse the repository at this point in the history
In some cases, sqlite is turning numbers into scientific notation
which results in lost precision.
  • Loading branch information
seanmcgary committed Sep 12, 2024
1 parent 9e6f7ad commit 3912b40
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 192 deletions.
13 changes: 10 additions & 3 deletions internal/eigenState/operatorShares/operatorShares.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
type OperatorShares struct {
Operator string
Strategy string
Shares string `gorm:"type:numeric"`
Shares string
BlockNumber uint64
CreatedAt time.Time
}
Expand Down Expand Up @@ -167,6 +167,7 @@ func (osm *OperatorSharesModel) GetStateTransitions() (types.StateTransitions[Ac
} else {
record.Shares = record.Shares.Add(record.Shares, shares)
}
fmt.Printf("OperatorShares %+v\n", record)

return record, nil
}
Expand Down Expand Up @@ -268,7 +269,7 @@ func (osm *OperatorSharesModel) prepareState(blockNumber uint64) ([]OperatorShar
select
operator,
strategy,
shares
cast(shares as text) as shares
from operator_shares
where
block_number = @previousBlock
Expand All @@ -290,6 +291,7 @@ func (osm *OperatorSharesModel) prepareState(blockNumber uint64) ([]OperatorShar
// Map the existing records to a map for easier lookup
mappedRecords := make(map[SlotId]OperatorShares)
for _, record := range existingRecords {
fmt.Printf("Existing OperatorShares %+v\n", record)
slotId := NewSlotId(record.Operator, record.Strategy)
mappedRecords[slotId] = record
}
Expand All @@ -308,7 +310,12 @@ func (osm *OperatorSharesModel) prepareState(blockNumber uint64) ([]OperatorShar
if existingRecord, ok := mappedRecords[slotId]; ok {
existingShares, success := numbers.NewBig257().SetString(existingRecord.Shares, 10)
if !success {
osm.logger.Sugar().Errorw("Failed to convert existing shares to big.Int")
osm.logger.Sugar().Errorw("Failed to convert existing shares to big.Int",
zap.String("shares", existingRecord.Shares),
zap.String("operator", existingRecord.Operator),
zap.String("strategy", existingRecord.Strategy),
zap.Uint64("blockNumber", blockNumber),
)
continue
}
prepared.Shares = existingShares.Add(existingShares, newState.Shares)
Expand Down
2 changes: 1 addition & 1 deletion internal/eigenState/rewardSubmissions/rewardSubmissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type RewardSubmission struct {
Amount string
Strategy string
StrategyIndex uint64
Multiplier string `gorm:"type:numeric"`
Multiplier string
StartTimestamp *time.Time `gorm:"type:DATETIME"`
EndTimestamp *time.Time `gorm:"type:DATETIME"`
Duration uint64
Expand Down
9 changes: 7 additions & 2 deletions internal/eigenState/stakerShares/stakerShares.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
type StakerShares struct {
Staker string
Strategy string
Shares string `gorm:"type:numeric"`
Shares string
BlockNumber uint64
CreatedAt time.Time
}
Expand Down Expand Up @@ -579,7 +579,12 @@ func (ss *StakerSharesModel) prepareState(blockNumber uint64) ([]StakerSharesDif
if existingRecord, ok := mappedRecords[slotId]; ok {
existingShares, success := numbers.NewBig257().SetString(existingRecord.Shares, 10)
if !success {
ss.logger.Sugar().Errorw("Failed to convert existing shares to big.Int")
ss.logger.Sugar().Errorw("Failed to convert existing shares to big.Int",
zap.String("shares", existingRecord.Shares),
zap.String("staker", existingRecord.Staker),
zap.String("strategy", existingRecord.Strategy),
zap.Uint64("blockNumber", blockNumber),
)
continue
}
prepared.Shares = existingShares.Add(existingShares, newState.Shares)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func (m *SqliteMigration) Up(grm *gorm.DB) error {
`create table if not exists operator_shares (
operator TEXT NOT NULL,
strategy TEXT NOT NULL,
shares NUMERIC NOT NULL,
shares TEXT NOT NULL,
block_number INTEGER NOT NULL,
created_at DATETIME default current_timestamp,
unique (operator, strategy, block_number)
Expand Down
2 changes: 1 addition & 1 deletion internal/sqlite/migrations/202409082234_stakerShare/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func (m *SqliteMigration) Up(grm *gorm.DB) error {
`create table if not exists staker_shares (
staker TEXT NOT NULL,
strategy TEXT NOT NULL,
shares NUMERIC NOT NULL,
shares TEXT NOT NULL,
block_number INTEGER NOT NULL,
created_at DATETIME default current_timestamp,
unique (staker, strategy, block_number)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ func (m *SqliteMigration) Up(grm *gorm.DB) error {
avs TEXT NOT NULL,
reward_hash TEST NOT NULL,
token TEXT NOT NULL,
amount NUMERIC NOT NULL,
amount TEXT NOT NULL,
strategy TEXT NOT NULL,
strategy_index INTEGER NOT NULL,
multiplier NUMERIC NOT NULL,
multiplier TEXT NOT NULL,
start_timestamp DATETIME NOT NULL,
end_timestamp DATETIME NOT NULL,
duration INTEGER NOT NULL,
Expand Down
182 changes: 0 additions & 182 deletions internal/storage/tables.go

This file was deleted.

0 comments on commit 3912b40

Please sign in to comment.