Skip to content

Commit 4303d10

Browse files
committed
review code
1 parent b9309fc commit 4303d10

File tree

12 files changed

+74
-71
lines changed

12 files changed

+74
-71
lines changed

init.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ CHAINID="hetu_560000-1"
66
MONIKER="localtestnet"
77
KEYRING="test"
88
KEYALGO="eth_secp256k1"
9-
LOGLEVEL="info"
10-
LOGLEVEL2="debug"
9+
LOGLEVEL="debug"
1110
TRACE=""
1211
HETUD_HOME="$HOME/.hetud"
1312
ETHCONFIG="$HETUD_HOME/config/config.toml"
@@ -88,4 +87,4 @@ echo "Node ID: $NODE_ID"
8887
# sed -i '' "s/external_address = \"\"/external_address = \"YOUR_IP:26656\"/g" "$ETHCONFIG"
8988

9089
# Start the node
91-
hetud start --pruning=nothing $TRACE --log_level "$LOGLEVEL2" --minimum-gas-prices=0.0001ahetu --home "$HETUD_HOME" --chain-id "$CHAINID"
90+
hetud start --pruning=nothing $TRACE --log_level "$LOGLEVEL" --minimum-gas-prices=0.0001ahetu --home "$HETUD_HOME" --chain-id "$CHAINID"

init_validators.sh

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ for i in $(seq 0 $((NUM_VALIDATORS - 1))); do
9999
--home "${HOME_PREFIX}"
100100

101101
echo "Adding genesis account for validator ${KEYS[$i]}..."
102-
hetud add-genesis-account "${KEYS[$i]}" "${GENESIS_BALANCE}${DENOM},${GENESIS_BALANCE}gas" \
102+
hetud add-genesis-account "${KEYS[$i]}" "${GENESIS_BALANCE}${DENOM}" \
103103
--keyring-backend="${KEYRING}" \
104104
--home "${HOME_PREFIX}"
105105
done
@@ -138,7 +138,7 @@ jq '.app_state["claims"]["params"]["duration_until_decay"]="100000s"' "$GENESIS"
138138

139139
# Claim module account:
140140
# 0xA61808Fe40fEb8B3433778BBC2ecECCAA47c8c47 || hetu15cvq3ljql6utxseh0zau9m8ve2j8erz89c94rj
141-
jq -r --arg amount_to_claim "$amount_to_claim" '.app_state["bank"]["balances"] += [{"address":"hetu15cvq3ljql6utxseh0zau9m8ve2j8erz89c94rj","coins":[{"denom":"ahetu", "amount":$amount_to_claim}, {"denom":"gas", "amount":$amount_to_claim}]}]' "$GENESIS" >"$TMP_GENESIS" && mv "$TMP_GENESIS" "$GENESIS"
141+
jq -r --arg amount_to_claim "$amount_to_claim" '.app_state["bank"]["balances"] += [{"address":"hetu15cvq3ljql6utxseh0zau9m8ve2j8erz89c94rj","coins":[{"denom":"ahetu", "amount":$amount_to_claim}]}]' "$GENESIS" >"$TMP_GENESIS" && mv "$TMP_GENESIS" "$GENESIS"
142142

143143
# Change proposal periods to pass within a reasonable time
144144
sed -i.bak 's/"max_deposit_period": "172800s"/"max_deposit_period": "30s"/g' "$GENESIS"
@@ -148,10 +148,16 @@ sed -i.bak 's/"expedited_voting_period": "86400s"/"expedited_voting_period": "15
148148
# Create gentx directory in primary node
149149
mkdir -p "${HOME_PREFIX}/config/gentx"
150150

151+
# Ensure bc is installed
152+
command -v bc >/dev/null 2>&1 || { echo >&2 "bc not installed."; exit 1; }
153+
151154
# Calculate total supply including claims amount
152155
total_supply=$(echo "$NUM_VALIDATORS * $GENESIS_BALANCE + $amount_to_claim" | bc)
153-
jq -r --arg total_supply "$total_supply" '.app_state["bank"]["supply"][0]["amount"]=$total_supply' "$GENESIS" >"$TMP_GENESIS" && mv "$TMP_GENESIS" "$GENESIS"
154-
jq -r --arg total_supply "$total_supply" '.app_state["bank"]["supply"][1]["amount"]=$total_supply' "$GENESIS" >"$TMP_GENESIS" && mv "$TMP_GENESIS" "$GENESIS"
156+
# Update only the ahetu supply entry
157+
jq -r --arg total_supply "$total_supply" '
158+
.app_state["bank"]["supply"] |=
159+
(map(if .denom == "ahetu" then .amount = $total_supply else . end))
160+
' "$GENESIS" >"$TMP_GENESIS" && mv "$GENESIS" "$GENESIS.bak" && mv "$TMP_GENESIS" "$GENESIS"
155161

156162
# Create clone directories, gentx, and get node IDs
157163
declare -a NODE_IDS
@@ -196,7 +202,7 @@ for i in $(seq 0 $((NUM_VALIDATORS - 1))); do
196202
"$CONFIG_TOML"
197203

198204
# Set minimum gas price
199-
sed -i.bak 's/^minimum-gas-prices *=.*/minimum-gas-prices = "0.0001gas"/g' "$APP_TOML"
205+
sed -i.bak 's/^minimum-gas-prices *=.*/minimum-gas-prices = "0.0001ahetu"/g' "$APP_TOML"
200206

201207
# Configure API and EVM settings in app.toml
202208
sed -i.bak \

validator.json

Lines changed: 0 additions & 13 deletions
This file was deleted.

x/blockinflation/keeper/abci.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ func (k Keeper) BeginBlocker(ctx sdk.Context) error {
1515

1616
// Mint and allocate block inflation
1717
if err := k.MintAndAllocateBlockInflation(ctx); err != nil {
18-
k.Logger(ctx).Error("failed to mint and allocate block inflation", "error", err.Error())
18+
k.Logger(ctx).Error("failed to mint and allocate block inflation",
19+
"err", err,
20+
"height", ctx.BlockHeight())
1921
return err
2022
}
2123

x/blockinflation/keeper/alpha_token.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package keeper
22

33
import (
4+
"bytes"
45
"fmt"
56
"math/big"
6-
"strings"
7+
"sync"
78

89
sdk "github.com/cosmos/cosmos-sdk/types"
910
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
@@ -14,6 +15,20 @@ import (
1415
eventabi "github.com/hetu-project/hetu/v1/x/event/abi"
1516
)
1617

18+
var (
19+
alphaABIOnce sync.Once
20+
alphaABIVal abi.ABI
21+
alphaABIValErr error
22+
)
23+
24+
// getAlphaTokenABI returns the cached AlphaToken ABI or parses it once if not cached
25+
func getAlphaTokenABI() (abi.ABI, error) {
26+
alphaABIOnce.Do(func() {
27+
alphaABIVal, alphaABIValErr = abi.JSON(bytes.NewReader(eventabi.AlphaTokenABI))
28+
})
29+
return alphaABIVal, alphaABIValErr
30+
}
31+
1732
// MintAlphaTokens mints alpha tokens to the specified address
1833
func (k Keeper) MintAlphaTokens(ctx sdk.Context, netuid uint16, recipient string, amount uint64) error {
1934
// 1. Get the subnet to find information about it
@@ -34,10 +49,10 @@ func (k Keeper) MintAlphaTokens(ctx sdk.Context, netuid uint16, recipient string
3449
return fmt.Errorf("invalid alpha token address: %s", alphaTokenAddress)
3550
}
3651

37-
// 4. Get the AlphaToken ABI
38-
alphaTokenABI, err := abi.JSON(strings.NewReader(string(eventabi.AlphaTokenABI)))
52+
// 4. Get the AlphaToken ABI (cached)
53+
alphaTokenABI, err := getAlphaTokenABI()
3954
if err != nil {
40-
return fmt.Errorf("failed to parse AlphaToken ABI: %w", err)
55+
return fmt.Errorf("failed to load AlphaToken ABI: %w", err)
4156
}
4257

4358
// 5. Convert recipient address to Ethereum address

x/blockinflation/keeper/coinbase.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,13 @@ func (k Keeper) RunCoinbase(ctx sdk.Context, blockEmission math.Int) error {
277277
}
278278
if ownerCut.IsPositive() {
279279
k.Logger(ctx).Info("Owner cut distributed", "netuid", netuid, "amount", ownerCut.String())
280-
// 铸造 Alpha 代币给子网所有者
281280
if err := k.MintAlphaTokens(ctx, netuid, subnet.Owner, ownerCut.Uint64()); err != nil {
282281
k.Logger(ctx).Error("Failed to mint alpha tokens for subnet owner",
283282
"netuid", netuid,
284283
"owner", subnet.Owner,
285284
"amount", ownerCut.Uint64(),
286285
"error", err,
287286
)
288-
// 继续处理,不中断流程
289287
}
290288
}
291289
} else {
@@ -327,8 +325,14 @@ func (k Keeper) getStakeMap(ctx sdk.Context, netuid uint16, accounts []string) m
327325
stakeMap[acc] = 0
328326
for _, s := range stakes {
329327
if s.Validator == acc {
330-
amount, _ := new(big.Int).SetString(s.Amount, 10)
331-
stakeMap[acc] += amount.Uint64()
328+
if amount, ok := new(big.Int).SetString(s.Amount, 10); ok {
329+
stakeMap[acc] += amount.Uint64()
330+
} else {
331+
k.Logger(ctx).Error("invalid stake amount string",
332+
"netuid", netuid,
333+
"validator", acc,
334+
"amount", s.Amount)
335+
}
332336
}
333337
}
334338
}

x/blockinflation/keeper/genesis.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ func (k Keeper) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data *blockinf
1111
// Set parameters
1212
k.SetParams(ctx, data.Params)
1313

14-
// Add debug information
15-
k.Logger(ctx).Info("DEBUG InitGenesis: Setting params", "params", data.Params)
14+
// Debug: Params being set
15+
k.Logger(ctx).Debug("InitGenesis: setting params", "params", data.Params)
1616

1717
// Verify parameters were set correctly
1818
params := k.GetParams(ctx)
19-
k.Logger(ctx).Info("DEBUG InitGenesis: Retrieved params", "params", params)
19+
k.Logger(ctx).Debug("InitGenesis: retrieved params", "params", params)
2020

2121
// Set total issuance
2222
if !data.TotalIssuance.Amount.IsNil() {
@@ -32,8 +32,8 @@ func (k Keeper) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data *blockinf
3232
if !data.PendingSubnetRewards.Amount.IsNil() {
3333
k.SetPendingSubnetRewards(ctx, data.PendingSubnetRewards)
3434
}
35-
k.Logger(ctx).Debug("DEBUG InitGenesis Params", "params", data.Params)
36-
k.Logger(ctx).Info("initialized blockinflation genesis state",
35+
36+
k.Logger(ctx).Info("blockinflation: initialized genesis state",
3737
"total_issuance", data.TotalIssuance.String(),
3838
"total_burned", data.TotalBurned.String(),
3939
"pending_subnet_rewards", data.PendingSubnetRewards.String(),
@@ -42,11 +42,10 @@ func (k Keeper) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data *blockinf
4242

4343
// ExportGenesis returns the blockinflation module's exported genesis.
4444
func (k Keeper) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) *blockinflationtypes.GenesisState {
45-
genesis := blockinflationtypes.DefaultGenesisState()
46-
genesis.Params = k.GetParams(ctx)
47-
genesis.TotalIssuance = k.GetTotalIssuance(ctx)
48-
genesis.TotalBurned = k.GetTotalBurned(ctx)
49-
genesis.PendingSubnetRewards = k.GetPendingSubnetRewards(ctx)
50-
51-
return genesis
45+
return &blockinflationtypes.GenesisState{
46+
Params: k.GetParams(ctx),
47+
TotalIssuance: k.GetTotalIssuance(ctx),
48+
TotalBurned: k.GetTotalBurned(ctx),
49+
PendingSubnetRewards: k.GetPendingSubnetRewards(ctx),
50+
}
5251
}

x/event/types/stake.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package types
22

3+
// ValidatorStake represents a validator's stake on a given subnet.
4+
// Amount is a string-encoded integer in Alpha's smallest unit (no decimals).
35
type ValidatorStake struct {
46
Netuid uint16 `json:"netuid"`
57
Validator string `json:"validator"`
68
Amount string `json:"amount"`
79
}
810

11+
// Delegation represents a staker's delegated stake to a validator on a subnet.
12+
// Amount is a string-encoded integer in Alpha's smallest unit (no decimals).
913
type Delegation struct {
1014
Netuid uint16 `json:"netuid"`
1115
Validator string `json:"validator"`

x/event/types/subnet.go

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package types
22

33
import (
4-
"encoding/json"
5-
64
"cosmossdk.io/math"
75
)
86

@@ -82,29 +80,7 @@ type NeuronInfo struct {
8280
PrometheusPort uint32 `json:"prometheus_port" yaml:"prometheus_port"`
8381
}
8482

85-
// MarshalJSON implements json.Marshaler
86-
func (s Subnet) MarshalJSON() ([]byte, error) {
87-
type Alias Subnet
88-
return json.Marshal(&struct {
89-
*Alias
90-
}{
91-
Alias: (*Alias)(&s),
92-
})
93-
}
94-
95-
// UnmarshalJSON implements json.Unmarshaler
96-
func (s *Subnet) UnmarshalJSON(data []byte) error {
97-
type Alias Subnet
98-
aux := &struct {
99-
*Alias
100-
}{
101-
Alias: (*Alias)(s),
102-
}
103-
if err := json.Unmarshal(data, &aux); err != nil {
104-
return err
105-
}
106-
return nil
107-
}
83+
// Default JSON marshalling is sufficient for Subnet; custom methods not required.
10884

10985
// SubnetPriceData represents subnet price-related data
11086
type SubnetPriceData struct {

x/event/types/utils.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ import (
44
"math/big"
55
)
66

7-
// AddBigIntString adds two big integer strings
7+
// AddBigIntString adds two base-10 big integer strings and returns the decimal string result.
8+
// Invalid inputs are treated as "0" (not recommended for financial logic).
89
func AddBigIntString(a, b string) string {
910
aInt, _ := new(big.Int).SetString(a, 10)
1011
bInt, _ := new(big.Int).SetString(b, 10)
1112
result := new(big.Int).Add(aInt, bInt)
1213
return result.String()
1314
}
1415

15-
// SubBigIntString subtracts two big integer strings
16+
// SubBigIntString subtracts b from a for base-10 big integer strings and clamps negatives to "0".
17+
// Invalid inputs are treated as "0" (not recommended for financial logic).
1618
func SubBigIntString(a, b string) string {
1719
aInt, _ := new(big.Int).SetString(a, 10)
1820
bInt, _ := new(big.Int).SetString(b, 10)

0 commit comments

Comments
 (0)