Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eventindexer): int => string #13828

Merged
merged 5 commits into from
May 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions packages/eventindexer/http/get_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package http

import (
"context"
"math/big"
"net/http"
"net/http/httptest"
"testing"
Expand All @@ -15,13 +16,13 @@ import (
func Test_GetStats(t *testing.T) {
srv := newTestServer("")

var proofTime uint64 = 5
var proofTime = big.NewInt(5)

var proofReward uint64 = 7
var proofReward = big.NewInt(7)

_, err := srv.statRepo.Save(context.Background(), eventindexer.SaveStatOpts{
ProofTime: &proofTime,
ProofReward: &proofReward,
ProofTime: proofTime,
ProofReward: proofReward,
})

assert.Equal(t, nil, err)
Expand All @@ -36,7 +37,7 @@ func Test_GetStats(t *testing.T) {
"success",
"0x123",
http.StatusOK,
[]string{`{"id":1,"averageProofTime":5,"averageProofReward":7,"numProofs":1,"numVerifiedBlocks":1}`},
[]string{`{"id":1,"averageProofTime":"5","averageProofReward":"7","numProofs":1,"numVerifiedBlocks":1}`},
},
}

Expand Down
20 changes: 16 additions & 4 deletions packages/eventindexer/indexer/save_block_proven_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,19 @@ func (svc *Service) updateAverageProofTime(ctx context.Context, event *taikol1.T

proofTime := provenAt - proposedAt

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

newAverageProofTime := calcNewAverage(
avg,
new(big.Int).SetUint64(stat.NumProofs),
new(big.Int).SetUint64(proofTime),
)

_, err = svc.statRepo.Save(ctx, eventindexer.SaveStatOpts{
ProofTime: &newAverageProofTime,
ProofTime: newAverageProofTime,
})
if err != nil {
return errors.Wrap(err, "svc.statRepo.Save")
Expand All @@ -116,6 +125,9 @@ func (svc *Service) updateAverageProofTime(ctx context.Context, event *taikol1.T
return nil
}

func calcNewAverage(a, t, new uint64) uint64 {
return ((a * t) + new) / (t + 1)
func calcNewAverage(a, t, n *big.Int) *big.Int {
m := new(big.Int).Mul(a, t)
added := new(big.Int).Add(m, n)

return new(big.Int).Div(added, t.Add(t, big.NewInt(1)))
}
13 changes: 11 additions & 2 deletions packages/eventindexer/indexer/save_block_verified_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,19 @@ func (svc *Service) updateAverageBlockReward(ctx context.Context, event *taikol1
return errors.Wrap(err, "svc.statRepo.Find")
}

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

newAverageProofReward := calcNewAverage(
avg,
new(big.Int).SetUint64(stat.NumVerifiedBlocks),
new(big.Int).SetUint64(reward),
)

_, err = svc.statRepo.Save(ctx, eventindexer.SaveStatOpts{
ProofReward: &newAverageProofReward,
ProofReward: newAverageProofReward,
})
if err != nil {
return errors.Wrap(err, "svc.statRepo.Save")
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 int NOT NULL DEFAULT 0,
average_proof_reward int 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
12 changes: 10 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 All @@ -58,5 +58,13 @@ func (r *StatRepository) Find(ctx context.Context) (*eventindexer.Stat, error) {
return nil, errors.Wrap(err, "r.db.gormDB.FirstOrCreate")
}

if s.AverageProofReward == "" {
s.AverageProofReward = "0"
}

if s.AverageProofTime == "" {
s.AverageProofTime = "0"
}

return s, nil
}
13 changes: 7 additions & 6 deletions packages/eventindexer/repo/stat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package repo

import (
"context"
"math/big"
"testing"

"github.com/davecgh/go-spew/spew"
Expand All @@ -18,7 +19,7 @@ func TestIntegration_Stat_Save(t *testing.T) {
statRepo, err := NewStatRepository(db)
assert.Equal(t, nil, err)

var proofReward uint64 = 4
var proofReward = big.NewInt(4)

tests := []struct {
name string
Expand All @@ -28,7 +29,7 @@ func TestIntegration_Stat_Save(t *testing.T) {
{
"successProofReward",
eventindexer.SaveStatOpts{
ProofReward: &proofReward,
ProofReward: proofReward,
},
nil,
},
Expand All @@ -51,10 +52,10 @@ func TestIntegration_Stat_Find(t *testing.T) {
statRepo, err := NewStatRepository(db)
assert.Equal(t, nil, err)

var proofReward uint64 = 4
var proofReward = big.NewInt(4)

_, err = statRepo.Save(context.Background(), eventindexer.SaveStatOpts{
ProofReward: &proofReward,
ProofReward: proofReward,
})

assert.Equal(t, nil, err)
Expand All @@ -68,8 +69,8 @@ func TestIntegration_Stat_Find(t *testing.T) {
"success",
&eventindexer.Stat{
ID: 1,
AverageProofReward: 4,
AverageProofTime: 0,
AverageProofReward: proofReward.String(),
AverageProofTime: "0",
NumProofs: 0,
NumVerifiedBlocks: 1,
},
Expand Down
9 changes: 5 additions & 4 deletions packages/eventindexer/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@ package eventindexer

import (
"context"
"math/big"
)

// Event represents a stored EVM event. The fields will be serialized
// 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 uint64 `json:"averageProofTime"`
AverageProofReward uint64 `json:"averageProofReward"`
AverageProofTime string `json:"averageProofTime"`
AverageProofReward string `json:"averageProofReward"`
NumProofs uint64 `json:"numProofs"`
NumVerifiedBlocks uint64 `json:"numVerifiedBlocks"`
}

// SaveStatOpts
type SaveStatOpts struct {
ProofTime *uint64
ProofReward *uint64
ProofTime *big.Int
ProofReward *big.Int
}

// StatRepository is used to interact with stats in the store
Expand Down