Skip to content

Commit

Permalink
feat: store complete log in database
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 16, 2024
1 parent d084b92 commit 5be2e09
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions internal/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ func (l *Log) ComputeHash(previous *Log) {
}
}

payload := l.Data
if hv, ok := payload.(hashValuer); ok {
payload = hv.hashValue()
}

if err := enc.Encode(struct {
// notes(gfyrag): Keep keys ordered! the order matter when hashing the log.
Type LogType `json:"type"`
Expand All @@ -151,7 +156,7 @@ func (l *Log) ComputeHash(previous *Log) {
Hash []byte `json:"hash"`
}{
Type: l.Type,
Data: l.Data,
Data: payload,
Date: l.Date,
IdempotencyKey: l.IdempotencyKey,
ID: l.ID,
Expand All @@ -171,37 +176,31 @@ func NewLog(t LogType, payload any) Log {
}
}

type hashValuer interface {
hashValue() any
}

type AccountMetadata map[string]metadata.Metadata

type NewTransactionLogPayload struct {
Transaction Transaction `json:"transaction"`
AccountMetadata AccountMetadata `json:"accountMetadata"`
}

// MarshalJSON override default json marshalling to remove postCommitVolumes and postCommitEffectiveVolumes fields
// We don't want to store pc(v)e on the logs
// Because :
// 1. It can change (effective only)
// 2. They are not part of the decision-making process
func (p NewTransactionLogPayload) MarshalJSON() ([]byte, error) {
type aux Transaction
type tx struct {
aux
PostCommitVolumes PostCommitVolumes `json:"postCommitVolumes,omitempty"`
PostCommitEffectiveVolumes PostCommitVolumes `json:"postCommitEffectiveVolumes,omitempty"`
}

return json.Marshal(struct {
Transaction tx `json:"transaction"`
AccountMetadata AccountMetadata `json:"accountMetadata"`
func (p NewTransactionLogPayload) hashValue() any {
// Exclude postCommitVolumes and postCommitEffectiveVolumes fields from transactions.
// We don't want those fields to be part of the hash as they are not part of the decision-making process.
return struct {
TransactionData
ID int `json:"id"`
}{
Transaction: tx{
aux: aux(p.Transaction),
},
AccountMetadata: p.AccountMetadata,
})
TransactionData: p.Transaction.TransactionData,
ID: p.Transaction.ID,
}
}

var _ hashValuer = (*NewTransactionLogPayload)(nil)

func NewTransactionLog(tx Transaction, accountMetadata AccountMetadata) Log {
if accountMetadata == nil {
accountMetadata = AccountMetadata{}
Expand Down

0 comments on commit 5be2e09

Please sign in to comment.