Skip to content

Commit

Permalink
bigint => string
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberhorsey committed May 28, 2023
1 parent e8eb108 commit be7815f
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 15 deletions.
7 changes: 6 additions & 1 deletion packages/eventindexer/indexer/save_block_proven_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,13 @@ func (svc *Service) updateAverageProofTime(ctx context.Context, event *taikol1.T

proofTime := provenAt - proposedAt

avg, ok := new(big.Int).SetString(stat.AverageProofTime, 10)
if !ok {
return errors.New("unable to convert average proof time to string")
}

newAverageProofTime := calcNewAverage(
stat.AverageProofTime,
avg,
new(big.Int).SetUint64(stat.NumProofs),
new(big.Int).SetUint64(proofTime),
)
Expand Down
7 changes: 6 additions & 1 deletion packages/eventindexer/indexer/save_block_verified_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,13 @@ func (svc *Service) updateAverageBlockReward(ctx context.Context, event *taikol1
return errors.Wrap(err, "svc.statRepo.Find")
}

avg, ok := new(big.Int).SetString(stat.AverageProofReward, 10)
if !ok {
return errors.New("unable to convert average proof reward to string")
}

newAverageProofReward := calcNewAverage(
stat.AverageProofReward,
avg,
new(big.Int).SetUint64(stat.NumVerifiedBlocks),
new(big.Int).SetUint64(reward),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS stats (
id int NOT NULL PRIMARY KEY AUTO_INCREMENT,
average_proof_time bigint NOT NULL DEFAULT 0,
average_proof_reward bigint NOT NULL DEFAULT 0,
average_proof_time VARCHAR(255) NOT NULL DEFAULT "0",
average_proof_reward VARCHAR(255) NOT NULL DEFAULT "0",
num_proofs int NOT NULL default 0,
num_verified_blocks int NOT NULL default 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ,
Expand Down
14 changes: 12 additions & 2 deletions packages/eventindexer/mock/stat_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@ func NewStatRepository() *StatRepository {
return &StatRepository{}
}
func (r *StatRepository) Save(ctx context.Context, opts eventindexer.SaveStatOpts) (*eventindexer.Stat, error) {
proofReward := ""
if opts.ProofReward != nil {
proofReward = opts.ProofReward.String()
}

proofTime := ""
if opts.ProofTime != nil {
proofTime = opts.ProofTime.String()
}

r.stats = &eventindexer.Stat{
ID: 1,
AverageProofTime: opts.ProofTime,
AverageProofReward: opts.ProofReward,
AverageProofTime: proofTime,
AverageProofReward: proofReward,
NumProofs: 1,
NumVerifiedBlocks: 1,
}
Expand Down
4 changes: 2 additions & 2 deletions packages/eventindexer/repo/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ func (r *StatRepository) Save(ctx context.Context, opts eventindexer.SaveStatOpt

if opts.ProofReward != nil {
s.NumVerifiedBlocks++
s.AverageProofReward = opts.ProofReward
s.AverageProofReward = opts.ProofReward.String()
}

if opts.ProofTime != nil {
s.NumProofs++
s.AverageProofTime = opts.ProofTime
s.AverageProofTime = opts.ProofTime.String()
}

if err := r.db.GormDB().Save(s).Error; err != nil {
Expand Down
4 changes: 2 additions & 2 deletions packages/eventindexer/repo/stat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func TestIntegration_Stat_Find(t *testing.T) {
"success",
&eventindexer.Stat{
ID: 1,
AverageProofReward: proofReward,
AverageProofTime: big.NewInt(0),
AverageProofReward: proofReward.String(),
AverageProofTime: "0",
NumProofs: 0,
NumVerifiedBlocks: 1,
},
Expand Down
10 changes: 5 additions & 5 deletions packages/eventindexer/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
// into the Data field to be unmarshalled into a concrete struct
// dependant on the name of the event
type Stat struct {
ID int `json:"id"`
AverageProofTime *big.Int `json:"averageProofTime"`
AverageProofReward *big.Int `json:"averageProofReward"`
NumProofs uint64 `json:"numProofs"`
NumVerifiedBlocks uint64 `json:"numVerifiedBlocks"`
ID int `json:"id"`
AverageProofTime string `json:"averageProofTime"`
AverageProofReward string `json:"averageProofReward"`
NumProofs uint64 `json:"numProofs"`
NumVerifiedBlocks uint64 `json:"numVerifiedBlocks"`
}

// SaveStatOpts
Expand Down

0 comments on commit be7815f

Please sign in to comment.