This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
model_sync_transactions.go
141 lines (116 loc) · 4.62 KB
/
model_sync_transactions.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package bux
import (
"context"
"github.com/mrz1836/go-datastore"
customTypes "github.com/mrz1836/go-datastore/custom_types"
)
// SyncTransaction is an object representing the chain-state sync configuration and results for a given transaction
//
// Gorm related models & indexes: https://gorm.io/docs/models.html - https://gorm.io/docs/indexes.html
type SyncTransaction struct {
// Base model
Model `bson:",inline"`
// Model specific fields
ID string `json:"id" toml:"id" yaml:"id" gorm:"<-:create;type:char(64);primaryKey;comment:This is the unique transaction id" bson:"_id"`
Configuration SyncConfig `json:"configuration" toml:"configuration" yaml:"configuration" gorm:"<-;type:text;comment:This is the configuration struct in JSON" bson:"configuration"`
LastAttempt customTypes.NullTime `json:"last_attempt" toml:"last_attempt" yaml:"last_attempt" gorm:"<-;comment:When the last broadcast occurred" bson:"last_attempt,omitempty"`
Results SyncResults `json:"results" toml:"results" yaml:"results" gorm:"<-;type:text;comment:This is the results struct in JSON" bson:"results"`
BroadcastStatus SyncStatus `json:"broadcast_status" toml:"broadcast_status" yaml:"broadcast_status" gorm:"<-;type:varchar(10);index;comment:This is the status of the broadcast" bson:"broadcast_status"`
P2PStatus SyncStatus `json:"p2p_status" toml:"p2p_status" yaml:"p2p_status" gorm:"<-;column:p2p_status;type:varchar(10);index;comment:This is the status of the p2p paymail requests" bson:"p2p_status"`
SyncStatus SyncStatus `json:"sync_status" toml:"sync_status" yaml:"sync_status" gorm:"<-;type:varchar(10);index;comment:This is the status of the on-chain sync" bson:"sync_status"`
// internal fields
transaction *Transaction
}
// newSyncTransaction will start a new model (config is required)
func newSyncTransaction(txID string, config *SyncConfig, opts ...ModelOps) *SyncTransaction {
// Do not allow making a model without the configuration
if config == nil {
return nil
}
// Broadcasting
bs := SyncStatusReady
if !config.Broadcast {
bs = SyncStatusSkipped
}
// Notify Paymail P2P
ps := SyncStatusPending
if !config.PaymailP2P {
ps = SyncStatusSkipped
}
// Sync
ss := SyncStatusReady
if !config.SyncOnChain {
ss = SyncStatusSkipped
}
return &SyncTransaction{
BroadcastStatus: bs,
Configuration: *config,
ID: txID,
Model: *NewBaseModel(ModelSyncTransaction, opts...),
P2PStatus: ps,
SyncStatus: ss,
}
}
// GetID will get the ID
func (m *SyncTransaction) GetID() string {
return m.ID
}
// GetModelName will get the name of the current model
func (m *SyncTransaction) GetModelName() string {
return ModelSyncTransaction.String()
}
// GetModelTableName will get the db table name of the current model
func (m *SyncTransaction) GetModelTableName() string {
return tableSyncTransactions
}
// Save will save the model into the Datastore
func (m *SyncTransaction) Save(ctx context.Context) error {
return Save(ctx, m)
}
// BeforeCreating will fire before the model is being inserted into the Datastore
func (m *SyncTransaction) BeforeCreating(_ context.Context) error {
m.Client().Logger().Debug().
Str("txID", m.ID).
Msgf("starting: %s BeforeCreate hook...", m.Name())
// Make sure ID is valid
if len(m.ID) == 0 {
return ErrMissingFieldID
}
m.Client().Logger().Debug().
Str("txID", m.ID).
Msgf("end: %s BeforeCreate hook", m.Name())
return nil
}
// AfterCreated will fire after the model is created in the Datastore
func (m *SyncTransaction) AfterCreated(_ context.Context) error {
m.Client().Logger().Debug().
Str("txID", m.ID).
Msgf("end: %s AfterCreate hook", m.Name())
m.Client().Logger().Debug().
Str("txID", m.ID).
Msgf("end: %s AfterCreate hook", m.Name())
return nil
}
// BeforeUpdating will fire before the model is being updated
func (m *SyncTransaction) BeforeUpdating(_ context.Context) error {
m.Client().Logger().Debug().
Str("txID", m.ID).
Msgf("starting: %s BeforeUpdate hook...", m.Name())
// Trim the results to the last 20
maxResultsLength := 20
ln := len(m.Results.Results)
if ln > maxResultsLength {
m.Client().Logger().Warn().
Str("txID", m.ID).
Msgf("trimming syncTx.Results")
m.Results.Results = m.Results.Results[ln-maxResultsLength:]
}
m.Client().Logger().Debug().
Str("txID", m.ID).
Msgf("end: %s BeforeUpdate hook", m.Name())
return nil
}
// Migrate model specific migration on startup
func (m *SyncTransaction) Migrate(client datastore.ClientInterface) error {
return client.IndexMetadata(client.GetTableName(tableSyncTransactions), metadataField)
}