From bf7a643862294dce6518e02f9290a001a6ae7d1d Mon Sep 17 00:00:00 2001 From: romever <360876221@qq.com> Date: Tue, 19 Nov 2024 18:26:46 +0800 Subject: [PATCH] fix: runtime time. --- .../logic/runtime/runtimelistlogic.go | 4 + job/task/runtime_task.go | 150 +++++++++++------- 2 files changed, 101 insertions(+), 53 deletions(-) diff --git a/api/internal/logic/runtime/runtimelistlogic.go b/api/internal/logic/runtime/runtimelistlogic.go index f5cbd4a..d1e3468 100644 --- a/api/internal/logic/runtime/runtimelistlogic.go +++ b/api/internal/logic/runtime/runtimelistlogic.go @@ -6,6 +6,7 @@ import ( "github.com/zeromicro/go-zero/core/logc" "github.com/zeromicro/go-zero/core/stores/sqlx" "oasisscan-backend/api/internal/errort" + "sort" "oasisscan-backend/api/internal/svc" "oasisscan-backend/api/internal/types" @@ -40,6 +41,9 @@ func (l *RuntimeListLogic) RuntimeList(req *types.RuntimeListRequest) (resp *typ Name: runtime.Name, }) } + sort.Slice(runtimeList, func(i, j int) bool { + return runtimeList[i].Name > runtimeList[j].Name + }) resp = &types.RuntimeListResponse{ List: runtimeList, } diff --git a/job/task/runtime_task.go b/job/task/runtime_task.go index 69792b6..7a8a9b3 100644 --- a/job/task/runtime_task.go +++ b/job/task/runtime_task.go @@ -152,7 +152,7 @@ func RuntimeRoundSync(ctx context.Context, svcCtx *svc.ServiceContext) { RuntimeId: runtimeId, Round: int64(header.Round), Version: int64(header.Version), - Timestamp: time.Unix(int64(header.Timestamp), 0), + Timestamp: time.Unix(int64(header.Timestamp), 0).UTC(), HeaderType: int64(header.HeaderType), PreviousHash: header.PreviousHash.String(), IoRoot: header.IORoot.String(), @@ -164,7 +164,26 @@ func RuntimeRoundSync(ctx context.Context, svcCtx *svc.ServiceContext) { } _, err = svcCtx.RuntimeRoundModel.Insert(ctx, runtimeRoundModel) if err != nil { - logc.Errorf(ctx, "node insert error, %v", err) + logc.Errorf(ctx, "runtime round insert error, %v", err) + return + } + } else { + runtimeRoundModel = &model.RuntimeRound{ + RuntimeId: runtimeId, + Round: int64(header.Round), + Version: int64(header.Version), + Timestamp: time.Unix(int64(header.Timestamp), 0).UTC(), + HeaderType: int64(header.HeaderType), + PreviousHash: header.PreviousHash.String(), + IoRoot: header.IORoot.String(), + StateRoot: header.StateRoot.String(), + MessagesHash: header.MessagesHash.String(), + InMsgsHash: header.InMessagesHash.String(), + UpdatedAt: time.Now(), + } + err = svcCtx.RuntimeRoundModel.Update(ctx, runtimeRoundModel) + if err != nil { + logc.Errorf(ctx, "runtime round update error, %v", err) return } } @@ -273,69 +292,69 @@ func RuntimeTransactionSync(ctx context.Context, svcCtx *svc.ServiceContext) { return } - if runtimeTransactionModel == nil { - tx, err := common.OpenUtxNoVerify(&txr.Tx) - if err != nil { - logc.Errorf(ctx, "tx decode error, %v", err) - return - } - - consensusFrom, consensusTo := "", "" - ethHash, evmFrom, evmTo := "", "", "" - if len(txr.Tx.AuthProofs) == 1 && txr.Tx.AuthProofs[0].Module == "evm.ethereum.v0" { - ethHash = hex.EncodeToString(common.Keccak256(txr.Tx.Body)) + tx, err := common.OpenUtxNoVerify(&txr.Tx) + if err != nil { + logc.Errorf(ctx, "tx decode error, %v", err) + return + } - txData := new(eth_types.Transaction) - if err := txData.UnmarshalBinary(txr.Tx.Body); err != nil { - logc.Errorf(ctx, "txData unmarshal error, %v", err) - return - } + consensusFrom, consensusTo := "", "" + ethHash, evmFrom, evmTo := "", "", "" + if len(txr.Tx.AuthProofs) == 1 && txr.Tx.AuthProofs[0].Module == "evm.ethereum.v0" { + ethHash = hex.EncodeToString(common.Keccak256(txr.Tx.Body)) - from, err := eth_types.Sender(eth_types.NewCancunSigner(txData.ChainId()), txData) - if err != nil { - logc.Errorf(ctx, "from address error, %s, %d, %v", runtimeId, scanRound, err) - return - } - evmFrom = strings.ToLower(from.Hex()) - if txData.To() != nil { - evmTo = strings.ToLower(txData.To().Hex()) - } - } else { - var body common.RuntimeTransactionBody - err = common.CborUnmarshalOmit(tx.Call.Body, &body) - if err != nil { - logc.Errorf(ctx, "runtime id:%s ,current round:%d, consensus body unmarshal error, %v", runtimeId, scanRound, err) - return - } - - for _, si := range tx.AuthInfo.SignerInfo { - address, err := si.AddressSpec.Address() - if err != nil { - logc.Errorf(ctx, "consensusFrom error, %v", err) - return - } - consensusFrom = address.ConsensusAddress().String() - } - consensusTo = body.To.ConsensusAddress().String() + txData := new(eth_types.Transaction) + if err := txData.UnmarshalBinary(txr.Tx.Body); err != nil { + logc.Errorf(ctx, "txData unmarshal error, %v", err) + return } - txrBodyJson, err := json.Marshal(txr) + from, err := eth_types.Sender(eth_types.NewCancunSigner(txData.ChainId()), txData) if err != nil { - logc.Errorf(ctx, "txrBodyJson error, %v", err) + logc.Errorf(ctx, "from address error, %s, %d, %v", runtimeId, scanRound, err) return } - - eventsJson, err := json.Marshal(txr.Events) + evmFrom = strings.ToLower(from.Hex()) + if txData.To() != nil { + evmTo = strings.ToLower(txData.To().Hex()) + } + } else { + var body common.RuntimeTransactionBody + err = common.CborUnmarshalOmit(tx.Call.Body, &body) if err != nil { - logc.Errorf(ctx, "eventsJson error, %v", err) + logc.Errorf(ctx, "runtime id:%s ,current round:%d, consensus body unmarshal error, %v", runtimeId, scanRound, err) return } - message := "" - if !txr.Result.IsSuccess() { - message = txr.Result.Failed.Message + for _, si := range tx.AuthInfo.SignerInfo { + address, err := si.AddressSpec.Address() + if err != nil { + logc.Errorf(ctx, "consensusFrom error, %v", err) + return + } + consensusFrom = address.ConsensusAddress().String() } + consensusTo = body.To.ConsensusAddress().String() + } + txrBodyJson, err := json.Marshal(txr) + if err != nil { + logc.Errorf(ctx, "txrBodyJson error, %v", err) + return + } + + eventsJson, err := json.Marshal(txr.Events) + if err != nil { + logc.Errorf(ctx, "eventsJson error, %v", err) + return + } + + message := "" + if !txr.Result.IsSuccess() { + message = txr.Result.Failed.Message + } + + if runtimeTransactionModel == nil { runtimeTransactionModel = &model.RuntimeTransaction{ RuntimeId: runtimeId, Round: scanRound, @@ -349,7 +368,7 @@ func RuntimeTransactionSync(ctx context.Context, svcCtx *svc.ServiceContext) { Method: string(tx.Call.Method), Result: txr.Result.IsSuccess(), Messages: message, - Timestamp: time.Unix(int64(scanRuntimeRound.Header.Timestamp), 0), + Timestamp: time.Unix(int64(scanRuntimeRound.Header.Timestamp), 0).UTC(), Type: txr.Tx.AuthProofs[0].Module, Raw: string(txrBodyJson), Events: string(eventsJson), @@ -361,6 +380,31 @@ func RuntimeTransactionSync(ctx context.Context, svcCtx *svc.ServiceContext) { logc.Errorf(ctx, "runtime transaction insert error, %v", err) return } + } else { + runtimeTransactionModel = &model.RuntimeTransaction{ + RuntimeId: runtimeId, + Round: scanRound, + TxHash: txr.Tx.Hash().Hex(), + Position: int64(i), + EvmHash: ethHash, + ConsensusFrom: consensusFrom, + ConsensusTo: consensusTo, + EvmFrom: evmFrom, + EvmTo: evmTo, + Method: string(tx.Call.Method), + Result: txr.Result.IsSuccess(), + Messages: message, + Timestamp: time.Unix(int64(scanRuntimeRound.Header.Timestamp), 0).UTC(), + Type: txr.Tx.AuthProofs[0].Module, + Raw: string(txrBodyJson), + Events: string(eventsJson), + UpdatedAt: time.Now(), + } + _, err = svcCtx.RuntimeTransactionModel.Insert(ctx, runtimeTransactionModel) + if err != nil { + logc.Errorf(ctx, "runtime transaction update error, %v", err) + return + } } }