diff --git a/helper/call.go b/helper/call.go index 56d6aaef..8578b5bc 100644 --- a/helper/call.go +++ b/helper/call.go @@ -1104,7 +1104,7 @@ func (c *ContractCaller) GetTronHeaderInfo(headerID uint64, contractAddress stri } // Call - data, err := c.TronChainRPC.TriggerConstantContract(contractAddress, btsPack) + data, err := c.TronChainRPC.TriggerConstantContractWithRetry(contractAddress, btsPack) if err != nil { return root, 0, 0, 0, types.HeimdallAddress{}, err } @@ -1134,7 +1134,7 @@ func (c *ContractCaller) GetSyncedCheckpointId(contractAddress string, rootChain } // Call - data, err := c.TronChainRPC.TriggerConstantContract(contractAddress, btsPack) + data, err := c.TronChainRPC.TriggerConstantContractWithRetry(contractAddress, btsPack) if err != nil { return 0, err } diff --git a/tron/tronclient.go b/tron/tronclient.go index 8848bcc2..ed5dc334 100644 --- a/tron/tronclient.go +++ b/tron/tronclient.go @@ -3,9 +3,11 @@ package tron import ( "context" "fmt" + "github.com/ethereum/go-ethereum/log" "math/big" "os" "strings" + "time" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" @@ -36,9 +38,7 @@ func NewClient(url string) *Client { } } -// // private abi methods -// func getABI(data string) (abi.ABI, error) { return abi.JSON(strings.NewReader(data)) } @@ -81,6 +81,28 @@ func (tc *Client) TriggerConstantContract(contractAddress string, data []byte) ( return response.ConstantResult[0], nil } +func (tc *Client) TriggerConstantContractWithRetry(contractAddress string, data []byte) ([]byte, error) { + const maxRetries = 5 + + var response []byte + var err error + + for attempt := 1; attempt <= maxRetries; attempt++ { + response, err = tc.TriggerConstantContract(contractAddress, data) + + if err == nil && response != nil { + log.Info("Successfully trigger tron constant contract", "attempt", attempt) + break + } + log.Error("Failed to trigger tron constant contract", + "err", err, "attempt", attempt, "maxRetries", maxRetries) + if attempt < maxRetries { + delay := attempt + time.Sleep(time.Duration(delay) * time.Second) + } + } + return response, err +} func (tc *Client) GetNowBlock(ctx context.Context) (int64, error) { block, err := tc.client.GetNowBlock2(ctx, &pb.EmptyMessage{}) if err != nil { @@ -100,7 +122,7 @@ func (tc *Client) CurrentHeaderBlock(contractAddress string, childBlockInterval } // Call - data, err := tc.TriggerConstantContract(contractAddress, btsPack) + data, err := tc.TriggerConstantContractWithRetry(contractAddress, btsPack) if err != nil { return 0, err } @@ -125,7 +147,7 @@ func (tc *Client) GetLastChildBlock(contractAddress string) (uint64, error) { if err != nil { return 0, err } - data, err := tc.TriggerConstantContract(contractAddress, btsPack) + data, err := tc.TriggerConstantContractWithRetry(contractAddress, btsPack) if err != nil { return 0, err }