-
Notifications
You must be signed in to change notification settings - Fork 6
Improves/request retry #131
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
base: main
Are you sure you want to change the base?
Changes from 9 commits
6c261dd
b88f342
49aa2b6
a3a8d67
20f3480
4267bf6
e229644
1f6256e
1ebbe47
3143bb6
84724cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,9 +16,11 @@ | |
|
|
||
| "github.com/MixinNetwork/mixin/logger" | ||
| "github.com/MixinNetwork/safe/apps/ethereum/abi" | ||
| "github.com/MixinNetwork/safe/util" | ||
| "github.com/ethereum/go-ethereum" | ||
| ga "github.com/ethereum/go-ethereum/accounts/abi" | ||
| "github.com/ethereum/go-ethereum/common" | ||
| "github.com/ethereum/go-ethereum/core/types" | ||
| "github.com/ethereum/go-ethereum/crypto" | ||
| "github.com/ethereum/go-ethereum/ethclient" | ||
| ) | ||
|
|
@@ -146,11 +148,7 @@ | |
| if err != nil { | ||
| return nil, err | ||
| } | ||
| blockHeight, err := ethereumNumberToUint64(b.Number) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| b.Height = blockHeight | ||
| b.Height = uint64(height) | ||
| for _, tx := range b.Tx { | ||
| tx.BlockHash = b.Hash | ||
| } | ||
|
|
@@ -284,19 +282,14 @@ | |
| } | ||
|
|
||
| func GetERC20TransferLogFromBlock(ctx context.Context, rpc string, chain, height int64) ([]*Transfer, error) { | ||
| client, err := ethclient.Dial(rpc) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| query := ethereum.FilterQuery{ | ||
| FromBlock: big.NewInt(height), | ||
| ToBlock: big.NewInt(height), | ||
| } | ||
| contractAbi, err := ga.JSON(strings.NewReader(abi.AssetABI)) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| logs, err := client.FilterLogs(ctx, query) | ||
| logs, err := filterLogsUntilSufficient(ctx, rpc, ethereum.FilterQuery{ | ||
| FromBlock: big.NewInt(height), | ||
| ToBlock: big.NewInt(height), | ||
| }) | ||
|
Comment on lines
+293
to
+296
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎. |
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
@@ -337,15 +330,31 @@ | |
| return res, nil | ||
| } | ||
| logger.Printf("callEthereumRPC(%s, %s, %v) => %v", rpc, method, params, err) | ||
| reason := strings.ToLower(err.Error()) | ||
| switch { | ||
| case strings.Contains(reason, "timeout"): | ||
| case strings.Contains(reason, "eof"): | ||
| case strings.Contains(reason, "handshake"): | ||
| default: | ||
| return res, err | ||
| if util.CheckRetryableError(err) { | ||
| time.Sleep(7 * time.Second) | ||
| continue | ||
| } | ||
| return res, err | ||
| } | ||
| } | ||
|
|
||
| func filterLogsUntilSufficient(ctx context.Context, rpc string, query ethereum.FilterQuery) ([]types.Log, error) { | ||
| client, err := ethclient.Dial(rpc) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
Comment on lines
+345
to
+349
|
||
| defer client.Close() | ||
|
|
||
| for { | ||
| logs, err := client.FilterLogs(ctx, query) | ||
| if ctx.Err() != nil { | ||
| return nil, ctx.Err() | ||
| } | ||
| if util.CheckRetryableError(err) { | ||
| time.Sleep(7 * time.Second) | ||
| continue | ||
| } | ||
| time.Sleep(7 * time.Second) | ||
| return logs, err | ||
|
Comment on lines
+352
to
+361
|
||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,11 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/base64" | ||
| "encoding/hex" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/MixinNetwork/safe/apps/bitcoin" | ||
| "github.com/MixinNetwork/safe/apps/ethereum" | ||
|
|
@@ -22,6 +20,7 @@ import ( | |
| ) | ||
|
|
||
| func GenerateTestTransactionProposal(c *cli.Context) error { | ||
| ctx := context.Background() | ||
| chain := c.Int("chain") | ||
| switch chain { | ||
| case common.SafeChainBitcoin: | ||
|
|
@@ -43,7 +42,7 @@ func GenerateTestTransactionProposal(c *cli.Context) error { | |
|
|
||
| addr := abi.GetFactoryAssetAddress("0x11EC02748116A983deeD59235302C3139D6e8cdD", common.SafeBitcoinChainId, "BTC", "Bitcoin", holder) | ||
| assetKey := strings.ToLower(addr.String()) | ||
| bondId := fetchAssetId(ethereum.GenerateAssetId(common.SafeChainPolygon, assetKey)) | ||
| bondId := fetchAssetId(ctx, ethereum.GenerateAssetId(common.SafeChainPolygon, assetKey)) | ||
|
|
||
| extra := []byte(receiver) | ||
| sid := uuid.Must(uuid.NewV4()).String() | ||
|
|
@@ -94,24 +93,13 @@ func GenerateTestTransactionApproval(c *cli.Context) error { | |
| return nil | ||
| } | ||
|
|
||
| func fetchAssetId(mixinId string) string { | ||
| client := &http.Client{Timeout: 10 * time.Second} | ||
| path := "https://api.mixin.one/network/assets/" + mixinId | ||
| resp, err := client.Get(path) | ||
| if err != nil { | ||
| func fetchAssetId(ctx context.Context, mixinId string) string { | ||
| asset, err := common.SafeReadAssetUntilSufficient(ctx, mixinId) | ||
| if err != nil || asset == nil { | ||
| panic(mixinId) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| var body struct { | ||
| Data struct { | ||
| AssetId string `json:"asset_id"` | ||
| MixinId string `json:"mixin_id"` | ||
| } `json:"data"` | ||
| } | ||
| json.NewDecoder(resp.Body).Decode(&body) | ||
| if body.Data.MixinId != mixinId { | ||
| if asset.KernelAssetID != mixinId { | ||
| panic(mixinId) | ||
| } | ||
| return body.Data.AssetId | ||
| return asset.AssetID | ||
|
Comment on lines
+97
to
+104
|
||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -36,6 +36,9 @@ func (mw *MixinWallet) drainOutputsFromNetwork(ctx context.Context) { | |||||||
| } | ||||||||
| members := []string{mw.client.ClientID} | ||||||||
| utxos, err := listUnspentUTXOsUntilSufficient(ctx, mw.client, members, 1, "", checkpoint) | ||||||||
| if CheckRetryableError(err) { | ||||||||
|
||||||||
| if CheckRetryableError(err) { | |
| if CheckRetryableError(err) { | |
| fmt.Printf("drainOutputsFromNetwork retryable error at checkpoint %d: %v\n", checkpoint, err) |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,11 +4,8 @@ import ( | |||||||||||
| "context" | ||||||||||||
| "encoding/binary" | ||||||||||||
| "encoding/hex" | ||||||||||||
| "encoding/json" | ||||||||||||
| "fmt" | ||||||||||||
| "math/big" | ||||||||||||
| "net/http" | ||||||||||||
| "strings" | ||||||||||||
| "time" | ||||||||||||
|
|
||||||||||||
| "github.com/MixinNetwork/mixin/crypto" | ||||||||||||
|
|
@@ -18,6 +15,7 @@ import ( | |||||||||||
| "github.com/MixinNetwork/safe/common" | ||||||||||||
| "github.com/MixinNetwork/safe/keeper/store" | ||||||||||||
| "github.com/MixinNetwork/safe/mtg" | ||||||||||||
| "github.com/MixinNetwork/safe/util" | ||||||||||||
| "github.com/gofrs/uuid/v5" | ||||||||||||
| "github.com/shopspring/decimal" | ||||||||||||
| ) | ||||||||||||
|
|
@@ -229,40 +227,19 @@ func (node *Node) fetchAssetMetaFromMessengerOrEthereum(ctx context.Context, id, | |||||||||||
| return asset, node.store.WriteAssetMeta(ctx, asset) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| func (node *Node) fetchMixinAsset(_ context.Context, id string) (*store.Asset, error) { | ||||||||||||
| client := &http.Client{Timeout: 10 * time.Second} | ||||||||||||
| path := node.conf.MixinMessengerAPI + "/network/assets/" + id | ||||||||||||
| resp, err := client.Get(path) | ||||||||||||
| if err != nil { | ||||||||||||
| return nil, err | ||||||||||||
| } | ||||||||||||
| defer common.CloseOrPanic(resp.Body) | ||||||||||||
|
|
||||||||||||
| var body struct { | ||||||||||||
| Data *struct { | ||||||||||||
| AssetId string `json:"asset_id"` | ||||||||||||
| MixinId crypto.Hash `json:"mixin_id"` | ||||||||||||
| AssetKey string `json:"asset_key"` | ||||||||||||
| Symbol string `json:"symbol"` | ||||||||||||
| Name string `json:"name"` | ||||||||||||
| Precision uint32 `json:"precision"` | ||||||||||||
| ChainId string `json:"chain_id"` | ||||||||||||
| } `json:"data"` | ||||||||||||
| } | ||||||||||||
| err = json.NewDecoder(resp.Body).Decode(&body) | ||||||||||||
| if err != nil { | ||||||||||||
| func (node *Node) fetchMixinAsset(ctx context.Context, id string) (*store.Asset, error) { | ||||||||||||
| asset, err := common.SafeReadAssetUntilSufficient(ctx, id) | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This change removes use of Useful? React with 👍 / 👎. |
||||||||||||
| if err != nil || asset == nil { | ||||||||||||
| return nil, err | ||||||||||||
| } | ||||||||||||
| asset := body.Data | ||||||||||||
|
|
||||||||||||
| return &store.Asset{ | ||||||||||||
| AssetId: asset.AssetId, | ||||||||||||
| MixinId: asset.MixinId.String(), | ||||||||||||
| AssetId: asset.AssetID, | ||||||||||||
| MixinId: asset.KernelAssetID, | ||||||||||||
| AssetKey: asset.AssetKey, | ||||||||||||
| Symbol: asset.Symbol, | ||||||||||||
| Name: asset.Name, | ||||||||||||
| Decimals: asset.Precision, | ||||||||||||
| Chain: common.SafeAssetIdChain(asset.ChainId), | ||||||||||||
| Decimals: uint32(asset.Precision), | ||||||||||||
| Chain: common.SafeAssetIdChainNoPanic(asset.ChainID), | ||||||||||||
| CreatedAt: time.Now().UTC(), | ||||||||||||
| }, nil | ||||||||||||
| } | ||||||||||||
|
|
@@ -276,16 +253,15 @@ func (node *Node) fetchAssetMeta(ctx context.Context, id string) (*store.Asset, | |||||||||||
| for { | ||||||||||||
| meta, err = node.fetchMixinAsset(ctx, id) | ||||||||||||
| if err == nil { | ||||||||||||
|
||||||||||||
| if err == nil { | |
| if err == nil { | |
| if meta == nil { | |
| return nil, nil | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RPCGetBlockWithTransactionsno longer validates the RPC response’snumberfield and now unconditionally setsb.Heightfrom the requestedheight. If the RPC returnsnull(common when querying beyond the tip) or returns an unexpected block,json.Unmarshalcan still succeed with a zero-value struct and this function will incorrectly return a “valid” block with empty hash/number. Consider unmarshalling into a pointer and checking fornil, and/or parsing/validatingb.Number(and matching it toheight) before returning success.