-
Notifications
You must be signed in to change notification settings - Fork 23
/
blocktime.go
95 lines (86 loc) · 2.67 KB
/
blocktime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package base
import (
"context"
"fmt"
"gorm.io/gorm/clause"
)
// StoreBlockTime stores a block time for a chain.
func (s Store) StoreBlockTime(ctx context.Context, chainID uint32, blockNumber, timestamp uint64) error {
dbTx := s.DB().WithContext(ctx)
if s.db.Dialector.Name() == "sqlite" {
dbTx = dbTx.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: ChainIDFieldName}, {Name: BlockNumberFieldName}},
DoNothing: true,
})
} else {
dbTx = dbTx.Clauses(clause.Insert{
Modifier: "IGNORE",
})
}
dbTx = dbTx.Create(&BlockTime{
ChainID: chainID,
BlockNumber: blockNumber,
Timestamp: timestamp,
})
if dbTx.Error != nil {
return fmt.Errorf("could not store block time: %w", dbTx.Error)
}
return nil
}
// RetrieveBlockTime retrieves a block time for a chain and block number.
func (s Store) RetrieveBlockTime(ctx context.Context, chainID uint32, blockNumber uint64) (uint64, error) {
var blockTime BlockTime
dbTx := s.DB().WithContext(ctx).
Model(&BlockTime{}).
Where(&BlockTime{
ChainID: chainID,
BlockNumber: blockNumber,
}).
First(&blockTime)
if dbTx.Error != nil {
return 0, fmt.Errorf("could not retrieve block time: %w", dbTx.Error)
}
return blockTime.Timestamp, nil
}
// RetrieveLastBlockStored retrieves the last block number that has a stored block time.
func (s Store) RetrieveLastBlockStored(ctx context.Context, chainID uint32) (uint64, error) {
var blockTime uint64
dbTx := s.DB().WithContext(ctx).
Model(&BlockTime{}).
Where(&BlockTime{
ChainID: chainID,
}).
Select(fmt.Sprintf("MAX(%s)", BlockNumberFieldName)).Scan(&blockTime)
if dbTx.Error != nil {
return 0, fmt.Errorf("could not retrieve last block time: %w", dbTx.Error)
}
return blockTime, nil
}
// RetrieveFirstBlockStored retrieves the first block number that has a stored block time.
func (s Store) RetrieveFirstBlockStored(ctx context.Context, chainID uint32) (uint64, error) {
var blockTime uint64
dbTx := s.DB().WithContext(ctx).
Model(&BlockTime{}).
Where(&BlockTime{
ChainID: chainID,
}).
Select(fmt.Sprintf("COALESCE(MIN(%s), 0)", BlockNumberFieldName)).Scan(&blockTime)
if dbTx.Error != nil {
return 0, fmt.Errorf("could not retrieve first block time: %w", dbTx.Error)
}
return blockTime, nil
}
// RetrieveBlockTimesCountForChain retrieves the number of block times stored for a chain.
func (s Store) RetrieveBlockTimesCountForChain(ctx context.Context, chainID uint32) (int64, error) {
var count int64
dbTx := s.DB().WithContext(ctx).
Model(&BlockTime{}).
Where(&BlockTime{
ChainID: chainID,
}).
Count(&count)
if dbTx.Error != nil {
return 0, fmt.Errorf("could not count block times: %w", dbTx.Error)
}
return count, nil
}