Skip to content

Commit

Permalink
fix: runtime time.
Browse files Browse the repository at this point in the history
  • Loading branch information
romever committed Nov 19, 2024
1 parent ba3dd4d commit bf7a643
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 53 deletions.
4 changes: 4 additions & 0 deletions api/internal/logic/runtime/runtimelistlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
}
Expand Down
150 changes: 97 additions & 53 deletions job/task/runtime_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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,
Expand All @@ -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),
Expand All @@ -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
}
}
}

Expand Down

0 comments on commit bf7a643

Please sign in to comment.