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

feat(status-page): updates for a3 #13821

Merged
merged 14 commits into from
May 27, 2023
2 changes: 1 addition & 1 deletion packages/eventindexer/http/get_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func Test_GetStats(t *testing.T) {
"success",
"0x123",
http.StatusOK,
[]string{`{"id":1,"averageProofTime":5,"averageProofReward":7,"numProofs":1}`},
[]string{`{"id":1,"averageProofTime":5,"averageProofReward":7,"numProofs":1,"numVerifiedBlocks":1}`},
},
}

Expand Down
16 changes: 10 additions & 6 deletions packages/eventindexer/indexer/save_block_proven_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"math/big"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
Expand Down Expand Up @@ -75,30 +74,35 @@ func (svc *Service) saveBlockProvenEvent(
eventindexer.BlockProvenEventsProcessed.Inc()

if event.Prover.Hex() != systemProver.Hex() && event.Prover.Hex() != oracleProver.Hex() {
if err := svc.updateAverageBlockTime(ctx, event); err != nil {
return errors.Wrap(err, "svc.updateAverageBlockTime")
if err := svc.updateAverageProofTime(ctx, event); err != nil {
return errors.Wrap(err, "svc.updateAverageProofTime")
}
}

return nil
}

func (svc *Service) updateAverageBlockTime(ctx context.Context, event *taikol1.TaikoL1BlockProven) error {
func (svc *Service) updateAverageProofTime(ctx context.Context, event *taikol1.TaikoL1BlockProven) error {
block, err := svc.taikol1.GetBlock(nil, event.Id)
if err != nil {
return errors.Wrap(err, "svc.taikoL1.GetBlock")
}

eventBlock, err := svc.ethClient.BlockByHash(ctx, event.Raw.BlockHash)
if err != nil {
return errors.Wrap(err, "svc.ethClient.BlockByHash")
}

stat, err := svc.statRepo.Find(ctx)
if err != nil {
return errors.Wrap(err, "svc.statRepo.Find")
}

proposedAt := block.ProposedAt

provenAt := time.Now().Unix()
provenAt := eventBlock.Time()
davidtaikocha marked this conversation as resolved.
Show resolved Hide resolved

proofTime := uint64(provenAt) - proposedAt
proofTime := provenAt - proposedAt

newAverageProofTime := calcNewAverage(stat.AverageProofTime, stat.NumProofs, proofTime)

Expand Down
4 changes: 3 additions & 1 deletion packages/eventindexer/indexer/save_block_verified_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func (svc *Service) saveBlockVerifiedEvents(
for {
event := events.Event

log.Infof("new blockVerified event, blockId: %v", event.Id)

if event.Raw.Removed {
continue
}
Expand Down Expand Up @@ -78,7 +80,7 @@ func (svc *Service) updateAverageBlockReward(ctx context.Context, event *taikol1
return errors.Wrap(err, "svc.statRepo.Find")
}

newAverageProofReward := calcNewAverage(stat.AverageProofReward, stat.NumProofs, reward)
newAverageProofReward := calcNewAverage(stat.AverageProofReward, stat.NumVerifiedBlocks, reward)

_, err = svc.statRepo.Save(ctx, eventindexer.SaveStatOpts{
ProofReward: &newAverageProofReward,
Expand Down
167 changes: 93 additions & 74 deletions packages/eventindexer/indexer/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,36 +58,40 @@ func (svc *Service) subscribeBlockProven(ctx context.Context, chainID *big.Int,
log.Info("context finished")
return
case err := <-sub.Err():
log.Errorf("sub.Err(): %v", err)
errChan <- errors.Wrap(err, "sub.Err()")
case event := <-sink:
log.Infof("blockProvenEvent from subscription for prover %v", event.Prover.Hex())
go func() {
log.Infof("blockProvenEvent from subscription for prover %v", event.Prover.Hex())

if err := svc.saveBlockProvenEvent(ctx, chainID, event); err != nil {
eventindexer.BlockProvenEventsProcessedError.Inc()
if err := svc.saveBlockProvenEvent(ctx, chainID, event); err != nil {
eventindexer.BlockProvenEventsProcessedError.Inc()

log.Errorf("svc.subscribe, svc.saveBlockProvenEvent: %v", err)
log.Errorf("svc.subscribe, svc.saveBlockProvenEvent: %v", err)

continue
}

block, err := svc.blockRepo.GetLatestBlockProcessed(chainID)
if err != nil {
log.Errorf("svc.subscribe, blockRepo.GetLatestBlockProcessed: %v", err)
continue
}
return
davidtaikocha marked this conversation as resolved.
Show resolved Hide resolved
}

if block.Height < event.Raw.BlockNumber {
err = svc.blockRepo.Save(eventindexer.SaveBlockOpts{
Height: event.Raw.BlockNumber,
Hash: event.Raw.BlockHash,
ChainID: chainID,
})
block, err := svc.blockRepo.GetLatestBlockProcessed(chainID)
if err != nil {
log.Errorf("svc.subscribe, svc.blockRepo.Save: %v", err)
log.Errorf("svc.subscribe, blockRepo.GetLatestBlockProcessed: %v", err)
return
}

eventindexer.BlocksProcessed.Inc()
}
if block.Height < event.Raw.BlockNumber {
err = svc.blockRepo.Save(eventindexer.SaveBlockOpts{
Height: event.Raw.BlockNumber,
Hash: event.Raw.BlockHash,
ChainID: chainID,
})
if err != nil {
log.Errorf("svc.subscribe, svc.blockRepo.Save: %v", err)
return
}

eventindexer.BlocksProcessed.Inc()
}
}()
}
}
}
Expand All @@ -114,48 +118,58 @@ func (svc *Service) subscribeBlockProposed(ctx context.Context, chainID *big.Int
log.Info("context finished")
return
case err := <-sub.Err():
log.Errorf("sub.Err(): %v", err)
errChan <- errors.Wrap(err, "sub.Err()")
case event := <-sink:
log.Infof("blockProposedEvent from subscription")
go func() {
log.Infof("blockProposedEvent from subscription")

tx, _, err := svc.ethClient.TransactionByHash(ctx, event.Raw.TxHash)
if err != nil {
log.Errorf("svc.ethClient.TransactionByHash: %v", err)

tx, _, err := svc.ethClient.TransactionByHash(ctx, event.Raw.TxHash)
if err != nil {
log.Errorf("svc.ethClient.TransactionByHash: %v", err)
}
return
}

sender, err := svc.ethClient.TransactionSender(ctx, tx, event.Raw.BlockHash, event.Raw.TxIndex)
if err != nil {
log.Errorf("svc.ethClient.TransactionSender: %v", err)
}
sender, err := svc.ethClient.TransactionSender(ctx, tx, event.Raw.BlockHash, event.Raw.TxIndex)
if err != nil {
log.Errorf("svc.ethClient.TransactionSender: %v", err)

log.Infof("blockProposed by: %v", sender.Hex())
return
}

if err := svc.saveBlockProposedEvent(ctx, chainID, event, sender); err != nil {
eventindexer.BlockProposedEventsProcessedError.Inc()
log.Infof("blockProposed by: %v", sender.Hex())

log.Errorf("svc.subscribe, svc.saveBlockProposedEvent: %v", err)
if err := svc.saveBlockProposedEvent(ctx, chainID, event, sender); err != nil {
eventindexer.BlockProposedEventsProcessedError.Inc()

continue
}
log.Errorf("svc.subscribe, svc.saveBlockProposedEvent: %v", err)

block, err := svc.blockRepo.GetLatestBlockProcessed(chainID)
if err != nil {
log.Errorf("svc.subscribe, blockRepo.GetLatestBlockProcessed: %v", err)
continue
}
return
}

if block.Height < event.Raw.BlockNumber {
err = svc.blockRepo.Save(eventindexer.SaveBlockOpts{
Height: event.Raw.BlockNumber,
Hash: event.Raw.BlockHash,
ChainID: chainID,
})
block, err := svc.blockRepo.GetLatestBlockProcessed(chainID)
if err != nil {
log.Errorf("svc.subscribe, svc.blockRepo.Save: %v", err)
log.Errorf("svc.subscribe, blockRepo.GetLatestBlockProcessed: %v", err)

return
}

eventindexer.BlocksProcessed.Inc()
}
if block.Height < event.Raw.BlockNumber {
err = svc.blockRepo.Save(eventindexer.SaveBlockOpts{
Height: event.Raw.BlockNumber,
Hash: event.Raw.BlockHash,
ChainID: chainID,
})
if err != nil {
log.Errorf("svc.subscribe, svc.blockRepo.Save: %v", err)

return
}

eventindexer.BlocksProcessed.Inc()
}
}()
}
}
}
Expand All @@ -167,6 +181,7 @@ func (svc *Service) subscribeBlockVerified(ctx context.Context, chainID *big.Int
if err != nil {
log.Errorf("svc.taikoL1.WatchBlockVerified: %v", err)
}

log.Info("resubscribing to BlockVerified events")

return svc.taikol1.WatchBlockVerified(&bind.WatchOpts{
Expand All @@ -182,35 +197,39 @@ func (svc *Service) subscribeBlockVerified(ctx context.Context, chainID *big.Int
log.Info("context finished")
return
case err := <-sub.Err():
log.Errorf("sub.Err(): %v", err)
errChan <- errors.Wrap(err, "sub.Err()")
case event := <-sink:
log.Infof("blockVerifiedEvent from subscription")

if err := svc.saveBlockVerifiedEvent(ctx, chainID, event); err != nil {
eventindexer.BlockVerifiedEventsProcessedError.Inc()
log.Errorf("svc.subscribe, svc.saveBlockProvenEvent: %v", err)

continue
}

block, err := svc.blockRepo.GetLatestBlockProcessed(chainID)
if err != nil {
log.Errorf("svc.subscribe, blockRepo.GetLatestBlockProcessed: %v", err)
continue
}

if block.Height < event.Raw.BlockNumber {
err = svc.blockRepo.Save(eventindexer.SaveBlockOpts{
Height: event.Raw.BlockNumber,
Hash: event.Raw.BlockHash,
ChainID: chainID,
})
go func() {
log.Infof("blockVerifiedEvent from subscription")

if err := svc.saveBlockVerifiedEvent(ctx, chainID, event); err != nil {
eventindexer.BlockVerifiedEventsProcessedError.Inc()
log.Errorf("svc.subscribe, svc.saveBlockVerifiedEvent: %v", err)

return
}

block, err := svc.blockRepo.GetLatestBlockProcessed(chainID)
if err != nil {
log.Errorf("svc.subscribe, svc.blockRepo.Save: %v", err)
log.Errorf("svc.subscribe, blockRepo.GetLatestBlockProcessed: %v", err)
return
}

eventindexer.BlocksProcessed.Inc()
}
if block.Height < event.Raw.BlockNumber {
err = svc.blockRepo.Save(eventindexer.SaveBlockOpts{
Height: event.Raw.BlockNumber,
Hash: event.Raw.BlockHash,
ChainID: chainID,
})
if err != nil {
log.Errorf("svc.subscribe, svc.blockRepo.Save: %v", err)
return
}

eventindexer.BlocksProcessed.Inc()
}
}()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CREATE TABLE IF NOT EXISTS stats (
average_proof_time int NOT NULL DEFAULT 0,
average_proof_reward int 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 ,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
Expand Down
1 change: 1 addition & 0 deletions packages/eventindexer/mock/stat_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func (r *StatRepository) Save(ctx context.Context, opts eventindexer.SaveStatOpt
AverageProofTime: *opts.ProofTime,
AverageProofReward: *opts.ProofReward,
NumProofs: 1,
NumVerifiedBlocks: 1,
}

return r.stats, nil
Expand Down
1 change: 1 addition & 0 deletions packages/eventindexer/repo/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func (r *StatRepository) Save(ctx context.Context, opts eventindexer.SaveStatOpt
}

if opts.ProofReward != nil {
s.NumVerifiedBlocks++
s.AverageProofReward = *opts.ProofReward
}

Expand Down
3 changes: 2 additions & 1 deletion packages/eventindexer/repo/stat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestIntegration_Stat_Save(t *testing.T) {
}
}

func TestIntegration_Event_Find(t *testing.T) {
func TestIntegration_Stat_Find(t *testing.T) {
db, close, err := testMysql(t)
assert.Equal(t, nil, err)

Expand Down Expand Up @@ -71,6 +71,7 @@ func TestIntegration_Event_Find(t *testing.T) {
AverageProofReward: 4,
AverageProofTime: 0,
NumProofs: 0,
NumVerifiedBlocks: 1,
},
nil,
},
Expand Down
1 change: 1 addition & 0 deletions packages/eventindexer/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Stat struct {
AverageProofTime uint64 `json:"averageProofTime"`
AverageProofReward uint64 `json:"averageProofReward"`
NumProofs uint64 `json:"numProofs"`
NumVerifiedBlocks uint64 `json:"numVerifiedBlocks"`
}

// SaveStatOpts
Expand Down
3 changes: 3 additions & 0 deletions packages/relayer/indexer/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ func (svc *Service) subscribeMessageSent(ctx context.Context, chainID *big.Int,

if err != nil {
log.Errorf("svc.subscribe, svc.handleEvent: %v", err)
return
}

block, err := svc.blockRepo.GetLatestBlockProcessedForEvent(relayer.EventNameMessageSent, chainID)
if err != nil {
log.Errorf("svc.subscribe, blockRepo.GetLatestBlockProcessedForEvent: %v", err)
return
}

if block.Height < event.Raw.BlockNumber {
Expand All @@ -84,6 +86,7 @@ func (svc *Service) subscribeMessageSent(ctx context.Context, chainID *big.Int,
})
if err != nil {
log.Errorf("svc.subscribe, svc.blockRepo.Save: %v", err)
return
}

relayer.BlocksProcessed.Inc()
Expand Down
1 change: 1 addition & 0 deletions packages/status-page/.default.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ VITE_NODE_ENV=production
VITE_L1_RPC_URL="https://l1rpc.internal.taiko.xyz"
VITE_L2_RPC_URL="https://l2rpc.internal.taiko.xyz"
VITE_L3_RPC_URL="https://l2rpc.internal.taiko.xyz"
VITE_TAIKO_TOKEN_ADDRESS="0xB7f8BC63BbcaD18155201308C8f3540b07f84F5e"
VITE_L2_TAIKO_L2_ADDRESS="0x1000777700000000000000000000000000000001"
VITE_L2_TAIKO_L1_ADDRESS="0x0B306BF915C4d645ff596e518fAf3F9669b97016"
VITE_L3_TAIKO_L2_ADDRESS="0x1000777700000000000000000000000000000001"
Expand Down
Loading