Skip to content

Commit b3eb81b

Browse files
committed
Add wait after transaction broadcast when processing Tx after broadcast
1 parent 0aba363 commit b3eb81b

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

pkg/lumera/modules/tx/impl.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"math"
77
"strconv"
8+
"time"
89

910
"github.com/LumeraProtocol/supernode/v2/pkg/logtrace"
1011
lumeracodec "github.com/LumeraProtocol/supernode/v2/pkg/lumera/codec"
@@ -184,6 +185,20 @@ func (m *module) BroadcastTransaction(ctx context.Context, txBytes []byte) (*sdk
184185
return resp, nil
185186
}
186187

188+
// GetTransaction queries a transaction by its hash
189+
func (m *module) GetTransaction(ctx context.Context, txHash string) (*sdktx.GetTxResponse, error) {
190+
req := &sdktx.GetTxRequest{
191+
Hash: txHash,
192+
}
193+
194+
resp, err := m.client.GetTx(ctx, req)
195+
if err != nil {
196+
return nil, fmt.Errorf("failed to get transaction: %w", err)
197+
}
198+
199+
return resp, nil
200+
}
201+
187202
// CalculateFee calculates the transaction fee based on gas usage and config
188203
func (m *module) CalculateFee(gasAmount uint64, config *TxConfig) string {
189204
// Determine gas price (numeric) and denom. Accept both plain number (e.g., "0.025")
@@ -272,6 +287,32 @@ func (m *module) ProcessTransaction(ctx context.Context, msgs []types.Msg, accou
272287
return result, fmt.Errorf("failed to broadcast transaction: %w", err)
273288
}
274289

290+
if result != nil && result.TxResponse != nil && result.TxResponse.Code == 0 && len(result.TxResponse.Events) == 0 {
291+
logtrace.Info(ctx, "Transaction broadcast successful, waiting for inclusion to get events...", nil)
292+
293+
// Retry 5 times with 1 second intervals
294+
var txResp *sdktx.GetTxResponse
295+
for i := 0; i < 5; i++ {
296+
time.Sleep(1 * time.Second)
297+
298+
txResp, err = m.GetTransaction(ctx, result.TxResponse.TxHash)
299+
if err == nil && txResp != nil && txResp.TxResponse != nil {
300+
// Successfully got the transaction with events
301+
logtrace.Info(ctx, fmt.Sprintf("Retrieved transaction with %d events", len(txResp.TxResponse.Events)), nil)
302+
result.TxResponse = txResp.TxResponse
303+
break
304+
}
305+
306+
if err != nil {
307+
logtrace.Warn(ctx, fmt.Sprintf("Attempt %d: failed to query transaction: %v", i+1, err), nil)
308+
}
309+
}
310+
}
311+
312+
if len(result.TxResponse.Events) == 0 {
313+
logtrace.Error(ctx, "Failed to retrieve transaction events after 5 attempts", nil)
314+
}
315+
275316
return result, nil
276317
}
277318

pkg/lumera/modules/tx/interface.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ type Module interface {
3535
// BroadcastTransaction broadcasts a signed transaction and returns the result
3636
BroadcastTransaction(ctx context.Context, txBytes []byte) (*sdktx.BroadcastTxResponse, error)
3737

38+
// GetTransaction queries a transaction by its hash
39+
GetTransaction(ctx context.Context, txHash string) (*sdktx.GetTxResponse, error)
40+
3841
// CalculateFee calculates the transaction fee based on gas usage and config
3942
CalculateFee(gasAmount uint64, config *TxConfig) string
4043

pkg/lumera/modules/tx/tx_mock.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/testutil/lumera.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ func (m *MockTxModule) ProcessTransaction(ctx context.Context, msgs []sdktypes.M
210210
return &sdktx.BroadcastTxResponse{}, nil
211211
}
212212

213+
// GetTransaction queries a transaction by its hash
214+
func (m *MockTxModule) GetTransaction(ctx context.Context, txHash string) (*sdktx.GetTxResponse, error) {
215+
// Mock implementation returns empty transaction response
216+
return &sdktx.GetTxResponse{}, nil
217+
}
218+
213219
// MockNodeModule implements the node.Module interface for testing
214220
type MockNodeModule struct{}
215221

0 commit comments

Comments
 (0)