Skip to content

Commit 4615c67

Browse files
committed
review code
1 parent 4303d10 commit 4615c67

File tree

7 files changed

+198
-61
lines changed

7 files changed

+198
-61
lines changed

init.sh

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,15 @@ echo "Home directory: $HETUD_HOME"
1818
# Build the binary
1919
# go build ./cmd/hetud
2020

21-
# Clear the home folder
22-
rm -rf "$HETUD_HOME"
21+
# Clear the home folder (guarded)
22+
if [[ -z "${HETUD_HOME:-}" || "$HETUD_HOME" = "/" ]]; then
23+
echo "Refusing to remove HETUD_HOME=$HETUD_HOME"
24+
exit 1
25+
fi
26+
echo "About to remove $HETUD_HOME (override with SKIP_RESET=1 to skip)"
27+
if [[ "${SKIP_RESET:-0}" != "1" ]]; then
28+
rm -rf "$HETUD_HOME"
29+
fi
2330

2431
# Initialize configuration
2532
# hetud config keyring-backend "$KEYRING"
@@ -64,7 +71,12 @@ jq ".consensus_params[\"block\"][\"time_iota_ms\"]=\"30000\"" "$GENESIS" > "$TMP
6471
jq ".consensus_params[\"block\"][\"max_gas\"]=\"10000000\"" "$GENESIS" > "$TMPGENESIS" && mv "$TMPGENESIS" "$GENESIS"
6572

6673
# Modify the configuration file
67-
sed -i '' 's/create_empty_blocks = true/create_empty_blocks = false/g' "$ETHCONFIG"
74+
# Cross-platform in-place sed
75+
if sed --version >/dev/null 2>&1; then
76+
sed -i 's/create_empty_blocks = true/create_empty_blocks = false/g' "$ETHCONFIG" # GNU sed
77+
else
78+
sed -i '' 's/create_empty_blocks = true/create_empty_blocks = false/g' "$ETHCONFIG" # BSD sed (macOS)
79+
fi
6880

6981
# Allocate genesis accounts
7082
hetud add-genesis-account "$KEY" 100000000000000000000000000ahetu --keyring-backend "$KEYRING" --home "$HETUD_HOME"
@@ -73,10 +85,10 @@ hetud add-genesis-account "$KEY" 100000000000000000000000000ahetu --keyring-back
7385
hetud gentx "$KEY" 2000000000000000000000ahetu --keyring-backend "$KEYRING" --chain-id "$CHAINID" --home "$HETUD_HOME" --fees 1000000ahetu --gas 200000
7486

7587
# Collect genesis transactions
76-
hetud collect-gentxs
88+
hetud collect-gentxs --home "$HETUD_HOME"
7789

7890
# Validate genesis file
79-
hetud validate-genesis
91+
hetud validate-genesis --home "$HETUD_HOME"
8092

8193
# Configure P2P for external connections
8294
# Get the node ID

x/blockinflation/keeper/coinbase.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,11 @@ func (k Keeper) RunCoinbase(ctx sdk.Context, blockEmission math.Int) error {
179179
k.Logger(ctx).Error("subnet not found", "netuid", netuid)
180180
continue
181181
}
182-
params := stakeworktypes.ParseEpochParams(subnet.Params)
182+
params, err := stakeworktypes.ParseEpochParams(subnet.Params)
183+
if err != nil {
184+
k.Logger(ctx).Error("failed to parse epoch parameters", "netuid", netuid, "err", err)
185+
continue
186+
}
183187
tempo := params.Tempo
184188
if k.stakeworkKeeper.ShouldRunEpoch(ctx, netuid, tempo) {
185189
k.Logger(ctx).Debug("Epoch triggered", "netuid", netuid, "block", ctx.BlockHeight())

x/event/keeper/keeper.go

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding/json"
99
"fmt"
1010
"math/big"
11+
"strconv"
1112

1213
"cosmossdk.io/log"
1314
"cosmossdk.io/math"
@@ -25,6 +26,7 @@ import (
2526
)
2627

2728
// ----------- Event topic constants -----------
29+
// Deprecated: Use Keeper instance fields (k.topicXXX) instead of these global constants
2830
var (
2931
// Legacy events remain unchanged (backward compatibility)
3032
SubnetRegisteredTopic = crypto.Keccak256Hash([]byte("SubnetRegistered(address,uint16,uint256,uint256,address,string)")).Hex()
@@ -61,6 +63,23 @@ type Keeper struct {
6163
subnetManagerABI abi.ABI
6264
neuronManagerABI abi.ABI
6365
globalStakingABI abi.ABI
66+
67+
// Event topic IDs
68+
topicSubnetRegistered string
69+
topicStakedSelf string
70+
topicUnstakedSelf string
71+
topicStakedDelegated string
72+
topicUnstakedDelegated string
73+
topicWeightsSet string
74+
topicNetworkRegistered string
75+
topicSubnetActivated string
76+
topicNetworkConfigUpdated string
77+
topicNeuronRegistered string
78+
topicNeuronDeregistered string
79+
topicNeuronStakeChanged string
80+
topicServiceUpdated string
81+
topicSubnetAllocationChanged string
82+
topicDeallocatedFromSubnet string
6483
}
6584

6685
// ----------- Keeper initialization -----------
@@ -75,7 +94,7 @@ func NewKeeper(
7594
neuronManagerABI abi.ABI,
7695
globalStakingABI abi.ABI,
7796
) *Keeper {
78-
return &Keeper{
97+
k := &Keeper{
7998
cdc: cdc,
8099
storeKey: storeKey,
81100
subnetRegistryABI: subnetRegistryABI,
@@ -86,6 +105,24 @@ func NewKeeper(
86105
neuronManagerABI: neuronManagerABI,
87106
globalStakingABI: globalStakingABI,
88107
}
108+
// Legacy events
109+
k.topicSubnetRegistered = k.subnetRegistryABI.Events["SubnetRegistered"].ID.Hex()
110+
k.topicStakedSelf = k.stakingSelfABI.Events["Staked"].ID.Hex()
111+
k.topicUnstakedSelf = k.stakingSelfABI.Events["Unstaked"].ID.Hex()
112+
k.topicStakedDelegated = k.stakingDelegatedABI.Events["Staked"].ID.Hex()
113+
k.topicUnstakedDelegated = k.stakingDelegatedABI.Events["Unstaked"].ID.Hex()
114+
k.topicWeightsSet = k.weightsABI.Events["WeightsSet"].ID.Hex()
115+
// New events
116+
k.topicNetworkRegistered = k.subnetManagerABI.Events["NetworkRegistered"].ID.Hex()
117+
k.topicSubnetActivated = k.subnetManagerABI.Events["SubnetActivated"].ID.Hex()
118+
k.topicNetworkConfigUpdated = k.subnetManagerABI.Events["NetworkConfigUpdated"].ID.Hex()
119+
k.topicNeuronRegistered = k.neuronManagerABI.Events["NeuronRegistered"].ID.Hex()
120+
k.topicNeuronDeregistered = k.neuronManagerABI.Events["NeuronDeregistered"].ID.Hex()
121+
k.topicNeuronStakeChanged = k.neuronManagerABI.Events["StakeAllocationChanged"].ID.Hex()
122+
k.topicServiceUpdated = k.neuronManagerABI.Events["ServiceUpdated"].ID.Hex()
123+
k.topicSubnetAllocationChanged = k.globalStakingABI.Events["SubnetAllocationChanged"].ID.Hex()
124+
k.topicDeallocatedFromSubnet = k.globalStakingABI.Events["DeallocatedFromSubnet"].ID.Hex()
125+
return k
89126
}
90127

91128
// ----------- Logger -----------
@@ -106,51 +143,51 @@ func (k *Keeper) HandleEvmLogs(ctx sdk.Context, logs []ethTypes.Log) {
106143

107144
switch topic {
108145
// Legacy events (maintain compatibility)
109-
case SubnetRegisteredTopic:
146+
case k.topicSubnetRegistered:
110147
k.Logger(ctx).Debug("Identified SubnetRegistered event")
111148
k.handleSubnetRegistered(ctx, log)
112-
case StakedSelfTopic:
149+
case k.topicStakedSelf:
113150
k.Logger(ctx).Debug("Identified StakedSelf event")
114151
k.handleStaked(ctx, log)
115-
case UnstakedSelfTopic:
152+
case k.topicUnstakedSelf:
116153
k.Logger(ctx).Debug("Identified UnstakedSelf event")
117154
k.handleUnstaked(ctx, log)
118-
case StakedDelegatedTopic:
155+
case k.topicStakedDelegated:
119156
k.Logger(ctx).Debug("Identified StakedDelegated event")
120157
k.handleDelegatedStaked(ctx, log)
121-
case UnstakedDelegatedTopic:
158+
case k.topicUnstakedDelegated:
122159
k.Logger(ctx).Debug("Identified UnstakedDelegated event")
123160
k.handleDelegatedUnstaked(ctx, log)
124-
case WeightsSetTopic:
161+
case k.topicWeightsSet:
125162
k.Logger(ctx).Debug("Identified WeightsSet event")
126163
k.handleWeightsSet(ctx, log)
127164

128165
// New events
129-
case NetworkRegisteredTopic:
166+
case k.topicNetworkRegistered:
130167
k.Logger(ctx).Debug("Identified NetworkRegistered event")
131168
k.handleNetworkRegistered(ctx, log)
132-
case SubnetActivatedTopic:
169+
case k.topicSubnetActivated:
133170
k.Logger(ctx).Debug("Identified SubnetActivated event")
134171
k.handleSubnetActivated(ctx, log)
135-
case NetworkConfigUpdatedTopic:
172+
case k.topicNetworkConfigUpdated:
136173
k.Logger(ctx).Debug("Identified NetworkConfigUpdated event")
137174
k.handleNetworkConfigUpdated(ctx, log)
138-
case NeuronRegisteredTopic:
175+
case k.topicNeuronRegistered:
139176
k.Logger(ctx).Debug("Identified NeuronRegistered event")
140177
k.handleNeuronRegistered(ctx, log)
141-
case NeuronDeregisteredTopic:
178+
case k.topicNeuronDeregistered:
142179
k.Logger(ctx).Debug("Identified NeuronDeregistered event")
143180
k.handleNeuronDeregistered(ctx, log)
144-
case NeuronStakeChangedTopic:
181+
case k.topicNeuronStakeChanged:
145182
k.Logger(ctx).Debug("Identified NeuronStakeChanged event")
146183
k.handleNeuronStakeChanged(ctx, log)
147-
case ServiceUpdatedTopic:
184+
case k.topicServiceUpdated:
148185
k.Logger(ctx).Debug("Identified ServiceUpdated event")
149186
k.handleServiceUpdated(ctx, log)
150-
case SubnetAllocationChangedTopic:
187+
case k.topicSubnetAllocationChanged:
151188
k.Logger(ctx).Debug("Identified SubnetAllocationChanged event")
152189
k.handleSubnetAllocationChanged(ctx, log)
153-
case DeallocatedFromSubnetTopic:
190+
case k.topicDeallocatedFromSubnet:
154191
k.Logger(ctx).Debug("Identified DeallocatedFromSubnet event")
155192
k.handleDeallocatedFromSubnet(ctx, log)
156193
default:
@@ -385,10 +422,10 @@ func (k Keeper) handleNetworkRegistered(ctx sdk.Context, log ethTypes.Log) {
385422

386423
// Override Core network parameters (if event has values)
387424
if event.Hyperparams.Rho != 0 {
388-
params["rho"] = fmt.Sprintf("%.1f", float64(event.Hyperparams.Rho)/10000.0) // uint16 to float64, assuming original value is scaled by 10000
425+
params["rho"] = strconv.FormatFloat(float64(event.Hyperparams.Rho)/10000.0, 'f', -1, 64)
389426
}
390427
if event.Hyperparams.Kappa != 0 {
391-
params["kappa"] = fmt.Sprintf("%.1f", float64(event.Hyperparams.Kappa)/10000.0) // uint16 to float64, assuming original value is scaled by 10000
428+
params["kappa"] = fmt.Sprintf("%d", event.Hyperparams.Kappa)
392429
}
393430
if event.Hyperparams.ImmunityPeriod != 0 {
394431
params["immunity_period"] = fmt.Sprintf("%d", event.Hyperparams.ImmunityPeriod)

x/stakework/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ type EpochParams struct {
125125
### 1. Run Epoch
126126

127127
```go
128-
result, err := k.RunEpoch(ctx, netuid, raoEmission)
128+
result, err := k.RunEpoch(ctx, netuid, ahetuEmission)
129129
if err != nil {
130130
return err
131131
}

x/stakework/keeper/epoch.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ func (k Keeper) RunEpoch(ctx sdk.Context, netuid uint16, raoEmission uint64) (*t
3333
logger.Debug("Retrieved subnet data", "netuid", netuid, "subnet", subnet)
3434

3535
// 2. Parse subnet parameters
36-
params := types.ParseEpochParams(subnet.Params)
36+
params, err := types.ParseEpochParams(subnet.Params)
37+
if err != nil {
38+
logger.Error("Failed to parse epoch parameters", "netuid", netuid, "err", err)
39+
return nil, fmt.Errorf("failed to parse epoch parameters: %w", err)
40+
}
3741
logger.Debug("Parsed subnet parameters",
3842
"kappa", params.Kappa,
3943
"alpha", params.Alpha,
@@ -540,7 +544,8 @@ func (k Keeper) distributeEmission(normIncentive, normDividends []float64, raoEm
540544
emission := make([]uint64, n)
541545

542546
for i := 0; i < n; i++ {
543-
emission[i] = uint64(normIncentive[i]*float64(raoEmission)) + uint64(normDividends[i]*float64(raoEmission))
547+
// emission[i] = uint64(normIncentive[i]*float64(raoEmission)) + uint64(normDividends[i]*float64(raoEmission))
548+
emission[i] = uint64((normIncentive[i] + normDividends[i]) * float64(raoEmission))
544549
}
545550

546551
return emission

x/stakework/module.go

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package stakework
22

33
import (
44
"encoding/json"
5+
"fmt"
56

67
"cosmossdk.io/core/appmodule"
78
"cosmossdk.io/depinject"
@@ -112,25 +113,8 @@ func (AppModule) ConsensusVersion() uint64 { return 1 }
112113

113114
// BeginBlock executes at the beginning of each block
114115
func (am AppModule) BeginBlock(ctx sdk.Context, _ interface{}) {
115-
// Check if epochs need to be run
116-
am.checkAndRunEpochs(ctx)
117-
}
118-
119-
// checkAndRunEpochs checks and runs epochs
120-
func (am AppModule) checkAndRunEpochs(ctx sdk.Context) {
121-
// Get all subnets
122-
subnets := am.keeper.GetEventKeeper().GetAllSubnets(ctx)
123-
124-
for _, subnet := range subnets {
125-
// Attempt to run epoch
126-
_, err := am.keeper.RunEpoch(ctx, subnet.Netuid, 1000000) // Default 1M rao emission
127-
if err != nil {
128-
// If not an error due to time not being due, log it
129-
if err != keeper.ErrEpochNotDue {
130-
ctx.Logger().Error("Failed to run epoch", "netuid", subnet.Netuid, "error", err)
131-
}
132-
}
133-
}
116+
// 移除 checkAndRunEpochs 调用
117+
// 依赖 blockinflation 模块的 coinbase.go 来运行 epoch
134118
}
135119

136120
// Dependency injection support
@@ -143,9 +127,8 @@ func (am AppModule) IsAppModule() {}
143127

144128
// ProvideModule provides module dependencies
145129
func ProvideModule(in depinject.Config) (keeper.Keeper, error) {
146-
// This needs to be implemented based on the actual dependency injection configuration
147-
// For now, return a simple implementation
148-
return keeper.Keeper{}, nil
130+
// TODO: Implement proper dependency injection
131+
return keeper.Keeper{}, fmt.Errorf("ProvideModule not yet implemented")
149132
}
150133

151134
// InitGenesis initializes the genesis state

0 commit comments

Comments
 (0)