diff --git a/packages/eventindexer/indexer/save_block_proven_event.go b/packages/eventindexer/indexer/save_block_proven_event.go index b2ded916ef6..ecc1167c4c1 100644 --- a/packages/eventindexer/indexer/save_block_proven_event.go +++ b/packages/eventindexer/indexer/save_block_proven_event.go @@ -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), ) diff --git a/packages/eventindexer/indexer/save_block_verified_event.go b/packages/eventindexer/indexer/save_block_verified_event.go index b1eba79cab3..e242b1ee54b 100644 --- a/packages/eventindexer/indexer/save_block_verified_event.go +++ b/packages/eventindexer/indexer/save_block_verified_event.go @@ -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), ) diff --git a/packages/eventindexer/migrations/1666650701_create_stats_table.sql b/packages/eventindexer/migrations/1666650701_create_stats_table.sql index 9359c113302..9a8ea8a6e93 100644 --- a/packages/eventindexer/migrations/1666650701_create_stats_table.sql +++ b/packages/eventindexer/migrations/1666650701_create_stats_table.sql @@ -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 , diff --git a/packages/eventindexer/mock/stat_repository.go b/packages/eventindexer/mock/stat_repository.go index c3bafb3257b..f49d29b01f2 100644 --- a/packages/eventindexer/mock/stat_repository.go +++ b/packages/eventindexer/mock/stat_repository.go @@ -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, } diff --git a/packages/eventindexer/repo/stat.go b/packages/eventindexer/repo/stat.go index 0d6492620cb..400d13768e5 100644 --- a/packages/eventindexer/repo/stat.go +++ b/packages/eventindexer/repo/stat.go @@ -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 { diff --git a/packages/eventindexer/repo/stat_test.go b/packages/eventindexer/repo/stat_test.go index f5c1590a30c..1c7d9128c3a 100644 --- a/packages/eventindexer/repo/stat_test.go +++ b/packages/eventindexer/repo/stat_test.go @@ -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, }, diff --git a/packages/eventindexer/stat.go b/packages/eventindexer/stat.go index bd5298d9541..58752298b66 100644 --- a/packages/eventindexer/stat.go +++ b/packages/eventindexer/stat.go @@ -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