Skip to content

Commit bed782d

Browse files
Ravenyjhclaude
andauthored
feat(fine-tuning): improve service config reading and add configurable stake (#355)
- Optimize GetService to use direct contract call instead of GetAllServices - Add unified error handling using IsServiceNotExist helper - Add ProviderStake config field for customizable stake amount - Align with inference service implementation pattern Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 5c4d356 commit bed782d

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

api/fine-tuning/config/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type Service struct {
2727
GpuCount int64 `yaml:"gpuCount"`
2828
} `yaml:"quota"`
2929
PricePerToken int64 `yaml:"pricePerToken"`
30+
ProviderStake string `yaml:"providerStake"` // Stake amount for first-time service registration (default: 100000000000000000000 = 100 0G)
3031
CustomizedModels []CustomizedModel `yaml:"customizedModels"`
3132
// ModelLocalPaths maps model hash to local file path for any model (including predefined models)
3233
// When set, the broker will use the local model instead of downloading from 0G Storage
@@ -202,7 +203,7 @@ func GetConfig() *Config {
202203
Database: struct {
203204
FineTune string `yaml:"fineTune"`
204205
}{
205-
FineTune: "root:123456@tcp(0g-fine-tune-broker-db:3306)/fineTune?parseTime=true",
206+
FineTune: "root:123456@tcp(mysql:3306)/fineTune?parseTime=true",
206207
},
207208
GasPrice: "",
208209
Images: Images{

api/fine-tuning/internal/contract/service.go

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

33
import (
44
"context"
5-
"fmt"
65
"math/big"
76

87
"github.com/ethereum/go-ethereum/accounts/abi/bind"
@@ -46,7 +45,7 @@ func (c *ProviderContract) OccupyService(ctx context.Context, service config.Ser
4645
func (c *ProviderContract) AddOrUpdateService(ctx context.Context, service config.Service, occupied bool) error {
4746
// Get existing service if any
4847
old, err := c.GetService(ctx)
49-
if err != nil && err.Error() != "service not found" {
48+
if err != nil && !IsServiceNotExist(err) {
5049
return errors.Wrap(err, "get service from contract")
5150
}
5251

@@ -88,7 +87,14 @@ func (c *ProviderContract) addOrUpdateServiceWithOld(ctx context.Context, servic
8887
if old == nil {
8988
// First-time registration: need to stake
9089
stakeValue = DefaultProviderStake
91-
c.logger.Infof("First-time service registration, staking %s", stakeValue.String())
90+
if service.ProviderStake != "" {
91+
stakeValue, err = util.ConvertToBigInt(service.ProviderStake)
92+
if err != nil {
93+
c.logger.Errorf("[addOrUpdateServiceWithOld] Failed to convert provider stake - stake=%s, error=%v", service.ProviderStake, err)
94+
return errors.Wrap(err, "convert provider stake")
95+
}
96+
}
97+
c.logger.Infof("[addOrUpdateServiceWithOld] First-time service registration, stake amount: %s", stakeValue.String())
9298
}
9399
// If old exists, it's an update, stakeValue remains nil (no additional stake)
94100

@@ -154,26 +160,29 @@ func (c *ProviderContract) DeleteService(ctx context.Context) error {
154160
}
155161

156162
func (c *ProviderContract) GetService(ctx context.Context) (*contract.Service, error) {
163+
c.logger.Infof("[GetService] Starting to get service - provider=%s", c.ProviderAddress)
164+
157165
callOpts := &bind.CallOpts{
158166
Context: ctx,
159167
}
160168

161-
list, err := c.Contract.GetAllServices(callOpts)
169+
service, err := c.Contract.GetService(callOpts, common.HexToAddress(c.ProviderAddress))
162170
if err != nil {
163-
return nil, err
164-
}
165-
for i := range list {
166-
if list[i].Provider.String() == c.ProviderAddress {
167-
return &list[i], nil
168-
}
171+
// Wrap error to extract details from rpc.jsonError Data field
172+
wrappedErr := WrapContractError(err)
173+
c.logger.Errorf("[GetService] Contract error - provider=%s: %v", c.ProviderAddress, wrappedErr)
174+
return nil, wrappedErr
169175
}
170176

171-
return nil, fmt.Errorf("service not found")
177+
c.logger.Infof("[GetService] Retrieved service from contract - url=%s, models=%v, pricePerToken=%s, occupied=%v",
178+
service.Url, service.Models, service.PricePerToken.String(), service.Occupied)
179+
180+
return &service, nil
172181
}
173182

174183
func (c *ProviderContract) SyncServices(ctx context.Context, new config.Service) error {
175184
old, err := c.GetService(ctx)
176-
if err != nil && err.Error() != "service not found" {
185+
if err != nil && !IsServiceNotExist(err) {
177186
return err
178187
}
179188

0 commit comments

Comments
 (0)