Skip to content

Commit

Permalink
Add on_chain field on /file/stats/<alloc> endpoint (#669)
Browse files Browse the repository at this point in the history
  • Loading branch information
avanaur authored May 6, 2022
1 parent 894e579 commit 902adcf
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions code/go/0chain.net/blobbercore/handler/storage_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ func (fsh *StorageHandler) GetFileStats(ctx context.Context, r *http.Request) (i
wm, _ := writemarker.GetWriteMarkerEntity(ctx, fileref.WriteMarker)
if wm != nil && fileStats != nil {
fileStats.WriteMarkerRedeemTxn = wm.CloseTxnID
fileStats.OnChain = wm.OnChain()
}
var statsMap map[string]interface{}
statsBytes, _ := json.Marshal(fileStats)
Expand Down
1 change: 1 addition & 0 deletions code/go/0chain.net/blobbercore/stats/filestats.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type FileStats struct {
FailedChallenges int64 `gorm:"column:num_of_failed_challenges" json:"num_of_failed_challenges"`
LastChallengeResponseTxn string `gorm:"column:last_challenge_txn;size:64" json:"last_challenge_txn"`
WriteMarkerRedeemTxn string `gorm:"-" json:"write_marker_txn"`
OnChain bool `gorm:"-" json:"on_chain"`
datastore.ModelWithTS
}

Expand Down
4 changes: 4 additions & 0 deletions code/go/0chain.net/blobbercore/writemarker/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ func (wm *WriteMarkerEntity) UpdateStatus(ctx context.Context, status WriteMarke
return
}

func (wm *WriteMarkerEntity) OnChain() bool {
return wm.Status == Committed
}

// GetWriteMarkerEntity get WriteMarkerEntity from postgres
func GetWriteMarkerEntity(ctx context.Context, allocation_root string) (*WriteMarkerEntity, error) {
db := datastore.GetStore().GetTransaction(ctx)
Expand Down
13 changes: 13 additions & 0 deletions code/go/0chain.net/blobbercore/writemarker/entity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package writemarker

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestWriteMarkerEntity_OnChain(t *testing.T) {
assert.False(t, (&WriteMarkerEntity{Status: Accepted}).OnChain())
assert.False(t, (&WriteMarkerEntity{Status: Failed}).OnChain())
assert.True(t, (&WriteMarkerEntity{Status: Committed}).OnChain())
assert.False(t, (&WriteMarkerEntity{}).OnChain()) // unspecified
}

0 comments on commit 902adcf

Please sign in to comment.