Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 41 additions & 25 deletions pkg/lumera/modules/action_msg/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package action_msg
import (
"context"
"fmt"
"strconv"

actionapi "github.com/LumeraProtocol/lumera/api/lumera/action"
actiontypes "github.com/LumeraProtocol/lumera/x/action/types"
Expand All @@ -22,13 +23,13 @@ import (
"google.golang.org/protobuf/encoding/protojson"
)

// Default gas parameters
// Default parameters
const (
defaultGasLimit = uint64(200000)
defaultMinGasLimit = uint64(100000)
defaultMaxGasLimit = uint64(1000000)
defaultGasAdjustment = float64(3.0)
defaultGasAdjustment = float64(1.5)
defaultGasPadding = uint64(50000)
defaultFeeDenom = "ulume"
defaultGasPrice = "0.000001" // Price per unit of gas
)

// module implements the Module interface
Expand All @@ -39,10 +40,10 @@ type module struct {
keyName string
chainID string
gasLimit uint64
minGasLimit uint64
maxGasLimit uint64
gasAdjustment float64
gasPadding uint64
feeDenom string
gasPrice string
}

// newModule creates a new ActionMsg module client
Expand Down Expand Up @@ -70,13 +71,26 @@ func newModule(conn *grpc.ClientConn, kr keyring.Keyring, keyName string, chainI
keyName: keyName,
chainID: chainID,
gasLimit: defaultGasLimit,
minGasLimit: defaultMinGasLimit,
maxGasLimit: defaultMaxGasLimit,
gasAdjustment: defaultGasAdjustment,
gasPadding: defaultGasPadding,
feeDenom: defaultFeeDenom,
gasPrice: defaultGasPrice,
}, nil
}

// calculateFee calculates the transaction fee based on gas usage
func (m *module) calculateFee(gasAmount uint64) string {
gasPrice, _ := strconv.ParseFloat(m.gasPrice, 64)
feeAmount := gasPrice * float64(gasAmount)

// Ensure we have at least 1 token as fee to meet minimum requirements
if feeAmount < 1 {
feeAmount = 1
}

return fmt.Sprintf("%.0f%s", feeAmount, m.feeDenom)
}

// FinalizeCascadeAction finalizes a CASCADE action with the given parameters
func (m *module) FinalizeCascadeAction(
ctx context.Context,
Expand Down Expand Up @@ -142,6 +156,9 @@ func (m *module) FinalizeCascadeAction(
WithKeyring(m.kr).
WithBroadcastMode("sync")

// Use a minimal fee for simulation
minFee := fmt.Sprintf("1%s", m.feeDenom)

// Simulate transaction to get gas estimate
txBuilder, err := tx.Factory{}.
WithTxConfig(clientCtx.TxConfig).
Expand All @@ -152,36 +169,41 @@ func (m *module) FinalizeCascadeAction(
WithGas(m.gasLimit).
WithGasAdjustment(m.gasAdjustment).
WithSignMode(signingtypes.SignMode_SIGN_MODE_DIRECT).
WithFees("1000stake").
WithFees(minFee).
BuildUnsignedTx(msg)
if err != nil {
return nil, fmt.Errorf("failed to build unsigned tx for simulation: %w", err)
}

pubKey, err := key.GetPubKey()
if err != nil {
return nil, fmt.Errorf("failed to get public key: %w", err)
}

txBuilder.SetSignatures(signingtypes.SignatureV2{
PubKey: pubKey, // your signing pubkey
PubKey: pubKey,
Data: &signingtypes.SingleSignatureData{SignMode: signingtypes.SignMode_SIGN_MODE_DIRECT, Signature: nil},
Sequence: accInfo.Sequence,
})

simulatedGas, err := m.simulateTx(ctx, clientCtx, txBuilder)
if err != nil {
return nil, fmt.Errorf("simulation failed: %w", err)
}

// Calculate gas with adjustment and padding
adjustedGas := uint64(float64(simulatedGas) * m.gasAdjustment)
gasToUse := adjustedGas + m.gasPadding

// Apply gas bounds
if gasToUse > m.maxGasLimit {
gasToUse = m.maxGasLimit
}

logtrace.Info(ctx, "using simulated gas", logtrace.Fields{"simulatedGas": simulatedGas, "adjustedGas": gasToUse})
// Calculate fee based on adjusted gas
fee := m.calculateFee(gasToUse)
logtrace.Info(ctx, "using simulated gas and calculated fee", logtrace.Fields{
"simulatedGas": simulatedGas,
"adjustedGas": gasToUse,
"fee": fee,
})

// Create transaction factory with final gas
// Create transaction factory with final gas and calculated fee
factory := tx.Factory{}.
WithTxConfig(clientCtx.TxConfig).
WithKeybase(m.kr).
Expand All @@ -190,7 +212,8 @@ func (m *module) FinalizeCascadeAction(
WithChainID(m.chainID).
WithGas(gasToUse).
WithGasAdjustment(m.gasAdjustment).
WithSignMode(signingtypes.SignMode_SIGN_MODE_DIRECT)
WithSignMode(signingtypes.SignMode_SIGN_MODE_DIRECT).
WithFees(fee)

// Build and sign transaction
txBuilder, err = factory.BuildUnsignedTx(msg)
Expand Down Expand Up @@ -255,13 +278,6 @@ func (m *module) simulateTx(ctx context.Context, clientCtx client.Context, txBui
TxBytes: txBytes,
}

// Check if we have the tx in the request too
if simReq.Tx != nil {
logtrace.Info(ctx, "simulation request has tx field", logtrace.Fields{
"txFieldPresent": true,
})
}

logtrace.Info(ctx, "sending simulation request", logtrace.Fields{
"requestBytes": len(simReq.TxBytes),
"requestType": fmt.Sprintf("%T", simReq),
Expand Down
1 change: 1 addition & 0 deletions sdk/event/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
TaskProgressRegistrationFailure EventType = "task.progress.registration_failure"
TaskProgressRegistrationSuccessful EventType = "task.progress.registration_successful"
TaskCompleted EventType = "task.completed"
TxhasReceived EventType = "txhash.received"
TaskFailed EventType = "task.failed"
)

Expand Down
6 changes: 3 additions & 3 deletions sdk/go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module github.com/LumeraProtocol/supernode/sdk

go 1.24.0
go 1.24.1

replace github.com/LumeraProtocol/supernode => ../

require (
github.com/LumeraProtocol/lumera v0.4.5
github.com/LumeraProtocol/supernode v0.0.0-00010101000000-000000000000
github.com/cosmos/cosmos-sdk v0.53.0
github.com/dgraph-io/ristretto/v2 v2.2.0
github.com/google/uuid v1.6.0
golang.org/x/sync v0.13.0
google.golang.org/grpc v1.72.0
)

Expand All @@ -29,7 +29,6 @@ require (
github.com/99designs/keyring v1.2.2 // indirect
github.com/DataDog/datadog-go v3.2.0+incompatible // indirect
github.com/DataDog/zstd v1.5.7 // indirect
github.com/LumeraProtocol/lumera v0.4.4 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/speakeasy v0.2.0 // indirect
github.com/bytedance/sonic v1.13.2 // indirect
Expand Down Expand Up @@ -150,6 +149,7 @@ require (
golang.org/x/crypto v0.37.0 // indirect
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 // indirect
golang.org/x/net v0.39.0 // indirect
golang.org/x/sync v0.13.0 // indirect
golang.org/x/sys v0.32.0 // indirect
golang.org/x/term v0.31.0 // indirect
golang.org/x/text v0.24.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions sdk/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.48
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.48.1 h1:8nn+rsCvTq9axyEh382S0PFLBeaFwNsT43IrPWzctRU=
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.48.1/go.mod h1:viRWSEhtMZqz1rhwmOVKkWl6SwmVowfL9O2YR5gI2PE=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/LumeraProtocol/lumera v0.4.4 h1:jMMyKs5fT5mShdLwVDM2QA+hJ4zNk4Tn0teaKGQSvak=
github.com/LumeraProtocol/lumera v0.4.4/go.mod h1:MRqVY+f8edEBkDvpr4z2nJpglp3Qj1OUvjeWvrvIUSM=
github.com/LumeraProtocol/lumera v0.4.5 h1:eeDeUFMKYAbCKDZVZzPsFpdiypoWsFUD37fd49EyGSE=
github.com/LumeraProtocol/lumera v0.4.5/go.mod h1:c1M+sjewuCvxw+pznwlspUzenDJI8Y+suKB3RFKS2Wo=
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/VividCortex/gohistogram v1.0.0 h1:6+hBz+qvs0JOrrNhhmR7lFxo5sINxBCGXrdtl/UvroE=
Expand Down
18 changes: 18 additions & 0 deletions sdk/task/cascade.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"strings"
"time"

"github.com/LumeraProtocol/supernode/sdk/adapters/lumera"
Expand Down Expand Up @@ -189,6 +190,12 @@ func (t *CascadeTask) attemptRegistration(ctx context.Context, index int, sn lum
return fmt.Errorf("upload rejected by %s: %s", sn.CosmosAddress, resp.Message)
}

txhash := CleanTxHash(resp.Message)
t.logEvent(ctx, event.TxhasReceived, "txhash received", map[string]interface{}{
"txhash": txhash,
"supernode": sn.CosmosAddress,
})

t.logger.Info(ctx, "upload OK", "taskID", t.TaskID, "address", sn.CosmosAddress)
return nil
}
Expand Down Expand Up @@ -225,3 +232,14 @@ func (t *CascadeTask) fail(ctx context.Context, failureEvent event.EventType, er

return err
}

func CleanTxHash(input string) string {
// Split by colon and get the last part
parts := strings.Split(input, ":")
if len(parts) <= 1 {
return input
}

// Return the last part with spaces trimmed
return strings.TrimSpace(parts[len(parts)-1])
}
6 changes: 3 additions & 3 deletions supernode/services/cascade/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cascade

import (
"context"
"fmt"
"time"

"github.com/LumeraProtocol/supernode/pkg/logtrace"
Expand Down Expand Up @@ -99,14 +100,13 @@ func (task *CascadeRegistrationTask) Register(ctx context.Context, req *Register
logtrace.Info(ctx, "Finalize Action Error", logtrace.Fields{
"error": err.Error(),
})

return &RegisterResponse{Success: true, Message: "successfully uploaded input data"}, nil
return nil, err
}

logtrace.Info(ctx, "Finalize Action Response", logtrace.Fields{
"resp": resp.Code,
"log": resp.TxHash})

// Return success when the cascade action is finalized without errors
return &RegisterResponse{Success: true, Message: "successfully uploaded and finalized input data"}, nil
return &RegisterResponse{Success: true, Message: fmt.Sprintf("successfully uploaded and finalized input data with txID: %s", resp.TxHash)}, nil
}
3 changes: 1 addition & 2 deletions tests/system/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/std"
sdk "github.com/cosmos/cosmos-sdk/types"
)

type (
Expand All @@ -40,7 +39,7 @@ func NewLumeradCLI(t *testing.T, sut *SystemUnderTest, verbose bool) *LumeradCli
sut.AwaitNextBlock,
sut.nodesCount,
filepath.Join(WorkDir, sut.outputDir),
"1"+sdk.DefaultBondDenom,
"1"+"ulume",
verbose,
assert.NoError,
true,
Expand Down
Loading
Loading