tcc 事务,分支代码不执行
Only try will be executed, confirm and cancel will not be executed. `func (uc *OrderUsecase) CreateOrder(ctx context.Context, o *model.Order) (*model.Order, error) { // Check whether the chain is supported supported, err := uc.grpcClient.Block.IsSupportedChainNetwork(ctx, &blockV1.IsSupportedChainNetworkRequest{ Chain: o.Chain, Network: o.Network }) if err != nil { return nil, err } if !supported.Supported { return nil, errors.InternalServer("CHAIN_NOT_SUPPORTED", "unsupported chain network") } // Get the currency tokens, err := uc.grpcClient.Block.GetTokens(ctx, &blockV1.GetTokensRequest{ Chain: o.Chain, Network: o.Network }) if err != nil { return nil, err } var tokenAddress string for _, token := range tokens.Tokens { if strings.EqualFold(token.Symbol, o.Symbol) { tokenAddress = token.Address o.Symbol = token.Symbol break } } if len(tokenAddress) == 0 { return nil, errors.InternalServer("TOKEN_NOT_FOUND", "token not found") } // Set the order information o.TokenAddress = tokenAddress // Check whether the order exists order, err := uc.repo.FindByOrderID(ctx, o.OrderID, o.MerchantID) if err != nil { return nil, err } if order.ID > 0 { return nil, errors.InternalServer("ORDER_EXISTS", "order already exists") } // Try to get an active or new address from the database address, err := uc.grpcClient.Block.FindActiveOrNewAddress(ctx, &blockV1.FindActiveOrNewAddressRequest{ Chain: o.Chain, Network: o.Network }) if err != nil { return nil, err } // Set the order information o.Address = address.Address o.CreatedTimeStamp = time.Now().Unix() o.ExpiredTimeStamp = o.CreatedTimeStamp + 3600 // Create a TCC transaction using DTM dtmServer := "discovery:///dtmservice" busiServer := "discovery:///block-service" gid := dtmgrpc.MustGenGid(dtmServer) uc.log.Infof("gid: %s", gid) err = dtmgrpc.TccGlobalTransaction(dtmServer, gid, func(tcc *dtmgrpc.TccGrpc) error { // Update the address status var tryReply blockV1.TryUpdateAddressStatusReply if err := tcc.CallBranch(&blockV1.TryUpdateAddressStatusRequest{ Chain: o.Chain, Network: o.Network, Address: address.Address, Status: blockV1.AddressStatus_ADDRESS_STATUS_IN_USE }, busiServer+"/api.block.v1.Block/TryUpdateAddressStatus", busiServer+"/api.block.v1.Block/ConfirmUpdateAddressStatus", busiServer+"/api.block.v1.Block/CancelUpdateAddressStatus", &tryReply) err != nil { return err } // Save the order to the database _, err = uc.repo.Save(ctx, o) err = errors.InternalServer("ORDER_SAVE_FAILED", "order save failed") if err != nil { return err } return nil }) if err != nil { uc.log.Errorf("TCC transaction execution error: %v", err) return nil, err } uc.log.Info("TCC transaction successfully submitted") return o, nil }
内容来源: dtm-labs/dtm