-
Notifications
You must be signed in to change notification settings - Fork 23
/
service.go
41 lines (37 loc) · 1.6 KB
/
service.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
package db
import (
"context"
"gorm.io/gorm"
"time"
)
// ChainListenerDB is the interface for the chain listener database.
type ChainListenerDB interface {
// PutLatestBlock upsers the latest block on a given chain id to be new height.
PutLatestBlock(ctx context.Context, chainID, height uint64) error
// LatestBlockForChain gets the latest block for a given chain id.
// will return ErrNoLatestBlockForChainID if no block exists for the chain.
LatestBlockForChain(ctx context.Context, chainID uint64) (uint64, error)
}
// LastIndexed is used to make sure we haven't missed any events while offline.
// since we event source - rather than use a state machine this is needed to make sure we haven't missed any events
// by allowing us to go back and source any events we may have missed.
//
// this does not inherit from gorm.model to allow us to use ChainID as a primary key.
type LastIndexed struct {
// CreatedAt is the creation time
CreatedAt time.Time
// UpdatedAt is the update time
UpdatedAt time.Time
// DeletedAt time
DeletedAt gorm.DeletedAt `gorm:"index"`
// ChainID is the chain id of the chain we're watching blocks on. This is our primary index.
ChainID uint64 `gorm:"column:chain_id;primaryKey;autoIncrement:false"`
// BlockHeight is the highest height we've seen on the chain
BlockNumber int `gorm:"block_number"`
}
// GetAllModels gets all models to migrate
// see: https://medium.com/@SaifAbid/slice-interfaces-8c78f8b6345d for an explanation of why we can't do this at initialization time
func GetAllModels() (allModels []interface{}) {
allModels = []interface{}{&LastIndexed{}}
return allModels
}