Skip to content

Commit

Permalink
limit number of sectors per message (#3)
Browse files Browse the repository at this point in the history
* Added functionality to limit the maximum number of sectors per message in the extend function

* Set default value for max_sectors to 500 in the createRequest function
  • Loading branch information
strahe authored May 16, 2024
1 parent 80bf8de commit 2c4f50b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 30 deletions.
43 changes: 24 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,28 @@ Authorization: Bearer <token>
创建一个续期请求
#### 请求参数
| 参数名 || 是否必须 | 说明 |
|----------------|---------------------------|------|---------------------------------------------------------------|
| miner | f01234 || 节点名字 |
| from | 2024-05-12T07:34:47+00:00 || sector到期范围的开始时间,RFC3339格式,表明续期这个范围的sector |
| to | 2024-05-13T07:34:47+00:00 || sector到期的结束时间,RFC3339格式,表明续期这个范围的sector |
| extension | 20160 || 续期的Epoch数量,即在原有效期的基础上追加对应的高度 |
| new_expiration | 3212223 || 不管之前的有效期是多少,统一续期到指定的高度,如果设置了这个值,extension将被忽略 |
| tolerance | 20160,7天,默认值 || 续期公差,精度,将到期时间相近的sector聚合成相同的到期时间,减少消息大小,降低gas, 且最低续期时间不能低于这个值 |
| dry_run | false,默认 || 是否是测试,如果是测试,不会真正执行续期操作,只会做一些检查 |
| 参数名 || 是否必须 | 说明 |
|----------------|---------------------------|------|------------------------------------------------------------------------------------------|
| miner | f01234 || 节点名字 |
| from | 2024-05-12T07:34:47+00:00 || sector到期范围的开始时间,RFC3339格式,表明续期这个范围的sector |
| to | 2024-05-13T07:34:47+00:00 || sector到期的结束时间,RFC3339格式,表明续期这个范围的sector |
| extension | 20160 || 续期的Epoch数量,即在原有效期的基础上追加对应的高度 |
| new_expiration | 3212223 || 不管之前的有效期是多少,统一续期到指定的高度,如果设置了这个值,extension将被忽略 |
| tolerance | 20160,7天,默认值 || 续期公差,精度,将到期时间相近的sector聚合成相同的到期时间,减少消息大小,降低gas, 且最低续期时间不能低于这个值, 使用new_expiration时,这个值会被忽略 |
| max_sectors | 500, 默认值 || 单条消息允许包含的最大sector数量,最大25000, 并非严格限制,最小统计单位是deadline |
| dry_run | false,默认 || 是否是测试,如果是测试,不会真正执行续期操作,只会做一些检查 |
#### 请求示例
```json
{
"miner": "f01234",
"from": "2024-05-12T07:34:47+00:00",
"to": "2024-05-13T07:34:47+00:00",
"extension": 20160,
"new_expiration": 3212223,
"tolerance": 20160,
"dry_run": false
"miner": "f01234",
"from": "2024-05-12T07:34:47+00:00",
"to": "2024-05-13T07:34:47+00:00",
"extension": 20160,
"new_expiration": 3212223,
"tolerance": 20160,
"max_sectors": 500,
"dry_run": false
}
```
Expand All @@ -80,6 +82,7 @@ Authorization: Bearer <token>
| from | "2024-05-12T07:34:47Z" | 筛选过期sector的开始时间,创建时指定的参数 |
| to | "2024-05-13T07:34:47Z" | 筛选过期sector的结束时间,创建时指定的参数 |
| new_expiration | null | 新的过期时间,创建时指定的参数 |
| max_sectors | 500 | 单条消息允许包含的最大sector数量,创建时指定的参数 |
| messages | null | 续期上链的消息,array |
| tolerance | 20160 | 续期公差,创建时指定的参数 |
| miner | "t017387" | 矿工 |
Expand All @@ -95,14 +98,15 @@ Authorization: Bearer <token>
#### 返回示例
```json
{
"data": {
"data": {
"confirmed_at": null,
"created_at": "2024-05-11T13:39:40.74831759+08:00",
"dry_run": true,
"dry_run_result": "",
"error": "failed to get active sector set: RPCConnectionError",
"extension": 21000,
"tolerance": 20160,
"tolerance": 20160,
"max_sectors": 500,
"from": "2024-05-12T07:34:47Z",
"id": 11,
"messages": null,
Expand All @@ -112,7 +116,7 @@ Authorization: Bearer <token>
"to": "2024-05-13T07:34:47Z",
"took": 526.841994321,
"updated_at": "2024-05-11T13:40:16.237069667+08:00"
}
}
}
```
Expand All @@ -136,6 +140,7 @@ Authorization: Bearer <token>
],
"miner": "f017387",
"new_expiration": null,
"max_sectors": 500,
"status": "pending",
"to": "2024-05-14T15:18:47+08:00",
"took": 526.841994321,
Expand Down
27 changes: 20 additions & 7 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ func newImplAPI(srv *Service) *implAPI {
}

type createRequestArgs struct {
Miner address.Address `json:"miner"`
From time.Time `json:"from"`
To time.Time `json:"to"`
Extension *abi.ChainEpoch `json:"extension"`
NewExpiration *abi.ChainEpoch `json:"new_expiration"`
Tolerance *abi.ChainEpoch `json:"tolerance"`
Miner address.Address `json:"miner"` // miner address
From time.Time `json:"from"` // expiration from
To time.Time `json:"to"` // expiration to
Extension *abi.ChainEpoch `json:"extension"` // extension to set
NewExpiration *abi.ChainEpoch `json:"new_expiration"` // new expiration to set
Tolerance *abi.ChainEpoch `json:"tolerance"` // tolerance for expiration
MaxSectors *int `json:"max_sectors"` // max sectors to include in a single message
DryRun bool `json:"dry_run"`
}

Expand All @@ -65,8 +66,20 @@ func (a *implAPI) create(w http.ResponseWriter, r *http.Request) {
warpResponse(w, http.StatusBadRequest, nil, fmt.Errorf("to must be greater than from"))
return
}

var maxSectors int
if args.MaxSectors == nil {
maxSectors = 500 // default value
} else {
maxSectors = *args.MaxSectors
}
if maxSectors < 0 {
warpResponse(w, http.StatusBadRequest, nil, fmt.Errorf("max_sectors must be greater than 0"))
return
}

req, err := a.srv.createRequest(r.Context(), args.Miner, args.From, args.To,
args.Extension, args.NewExpiration, args.Tolerance, args.DryRun)
args.Extension, args.NewExpiration, args.Tolerance, maxSectors, args.DryRun)
if err != nil {
warpResponse(w, http.StatusBadRequest, nil, err)
return
Expand Down
1 change: 1 addition & 0 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Request struct {
Extension *abi.ChainEpoch `json:"extension"`
NewExpiration *abi.ChainEpoch `json:"new_expiration"`
Tolerance abi.ChainEpoch `json:"tolerance"`
MaxSectors int `json:"max_sectors"`
Status RequestStatus `gorm:"index" json:"status"`
Messages []*Message `json:"messages"`
Error string `json:"error"`
Expand Down
33 changes: 29 additions & 4 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ func (s *Service) processRequest(ctx context.Context) error {

start := time.Now()
messages, dryRuns, err := s.extend(ctx, request.Miner.Address,
fromEpoch, toEpoch, request.Extension, request.NewExpiration, request.Tolerance, request.DryRun)
fromEpoch, toEpoch, request.Extension, request.NewExpiration,
request.Tolerance, request.MaxSectors, request.DryRun)
if err != nil {
log.Errorf("processing request %d failed: %s, took: %s", request.ID, err, time.Since(start))
request.Status = RequestStatusFailed
Expand Down Expand Up @@ -191,7 +192,8 @@ func (s *Service) processRequest(ctx context.Context) error {
return nil
}

func (s *Service) createRequest(ctx context.Context, minerAddr address.Address, from, to time.Time, extension, newExpiration, tolerance *abi.ChainEpoch, dryRun bool) (*Request, error) {
func (s *Service) createRequest(ctx context.Context, minerAddr address.Address, from, to time.Time,
extension, newExpiration, tolerance *abi.ChainEpoch, maxSectors int, dryRun bool) (*Request, error) {
if extension == nil && newExpiration == nil {
return nil, fmt.Errorf("either extension or new_expiration must be set")
}
Expand Down Expand Up @@ -219,6 +221,18 @@ func (s *Service) createRequest(ctx context.Context, minerAddr address.Address,
}
}

nv, err := s.api.StateNetworkVersion(ctx, types.EmptyTSK)
if err != nil {
return nil, fmt.Errorf("failed to get network version: %w", err)
}
sectorsMax, err := policy.GetAddressedSectorsMax(nv)
if err != nil {
return nil, fmt.Errorf("failed to get addressed sectors max: %w", err)
}
if maxSectors > sectorsMax {
return nil, fmt.Errorf("max sectors must be less than %d", sectorsMax)
}

request := &Request{
Miner: Address{minerAddr},
From: from,
Expand All @@ -227,6 +241,7 @@ func (s *Service) createRequest(ctx context.Context, minerAddr address.Address,
NewExpiration: newExpiration,
Tolerance: tol,
Status: RequestStatusCreated,
MaxSectors: maxSectors,
DryRun: dryRun,
}

Expand All @@ -245,7 +260,8 @@ func (s *Service) getRequest(_ context.Context, id uint) (*Request, error) {
}

func (s *Service) extend(ctx context.Context, addr address.Address, from, to abi.ChainEpoch,
extension *abi.ChainEpoch, newExpiration *abi.ChainEpoch, tolerance abi.ChainEpoch, dryRun bool) ([]*Message, []*PseudoExtendSectorExpirationParams, error) {
extension *abi.ChainEpoch, newExpiration *abi.ChainEpoch, tolerance abi.ChainEpoch,
maxSectors int, dryRun bool) ([]*Message, []*PseudoExtendSectorExpirationParams, error) {

if extension == nil && newExpiration == nil {
return nil, nil, fmt.Errorf("either extension or new expiration must be set")
Expand All @@ -267,6 +283,15 @@ func (s *Service) extend(ctx context.Context, addr address.Address, from, to abi
if err != nil {
return nil, nil, fmt.Errorf("failed to get addressed sectors max: %w", err)
}

addrSectors := sectorsMax
if maxSectors != 0 {
addrSectors = maxSectors
if addrSectors > sectorsMax {
return nil, nil, fmt.Errorf("the specified max-sectors exceeds the maximum limit")
}
}

time1 := time.Now()
activeSet, err := warpActiveSectors(ctx, s.api, addr, false) // only for debug, do not cache in production
if err != nil {
Expand Down Expand Up @@ -449,7 +474,7 @@ func (s *Service) extend(ctx context.Context, addr address.Address, from, to abi
sectorsInDecl := int(sectorsWithoutClaimsCount) + len(sectorsWithClaims)
scount += sectorsInDecl

if scount > sectorsMax || len(p.Extensions) >= declMax {
if scount > addrSectors || len(p.Extensions) >= declMax {
params = append(params, p)
p = miner.ExtendSectorExpiration2Params{}
scount = sectorsInDecl
Expand Down

0 comments on commit 2c4f50b

Please sign in to comment.