Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/ Challenge based on rounds #1226

Merged
merged 58 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 55 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
540586e
Fix
Jayashsatolia403 Aug 27, 2023
07f5e53
Merge branch 'sprint-1.10' into fix/challenge
Jayashsatolia403 Aug 27, 2023
1c1a2ca
Updated gosdk
Jayashsatolia403 Aug 27, 2023
00311aa
Fix
Jayashsatolia403 Aug 27, 2023
c42e87d
Fix
Jayashsatolia403 Aug 27, 2023
bdc93bb
Fix
Jayashsatolia403 Aug 27, 2023
56d7295
Logging
Jayashsatolia403 Aug 27, 2023
7f8edd6
Debug
Jayashsatolia403 Aug 28, 2023
b972453
Merge branch 'sprint-1.10' into fix/challenge
Jayashsatolia403 Aug 28, 2023
f51bfdf
Fix
Jayashsatolia403 Aug 28, 2023
ea4729d
Fix
Jayashsatolia403 Aug 28, 2023
003d9d6
Debug
Jayashsatolia403 Aug 29, 2023
213ab60
Debug
Jayashsatolia403 Aug 29, 2023
6708b08
Debug
Jayashsatolia403 Aug 29, 2023
55a99de
Fix unit tests
Jayashsatolia403 Aug 30, 2023
80b4c49
Logigng
Jayashsatolia403 Aug 30, 2023
d1d305a
Merge branch 'sprint-1.10' into fix/challenge
Jayashsatolia403 Aug 31, 2023
bd87f50
Updated gosdk
Jayashsatolia403 Aug 31, 2023
95e7904
Merge remote-tracking branch 'origin/fix/challenge' into fix/challenge
Jayashsatolia403 Aug 31, 2023
3f93a1f
Updated gosdk
Jayashsatolia403 Sep 1, 2023
8a65cea
Updated gosdk
Jayashsatolia403 Sep 1, 2023
bf82cb9
Updated gosdk
Jayashsatolia403 Sep 1, 2023
3274048
Updated gosdk
Jayashsatolia403 Sep 1, 2023
e39b16b
Updated gosdk
Jayashsatolia403 Sep 1, 2023
e3ee111
Updated gosdk
Jayashsatolia403 Sep 1, 2023
4b58e49
Updated gosdk
Jayashsatolia403 Sep 1, 2023
65b6b25
Fix
Jayashsatolia403 Sep 1, 2023
65dc753
Fix
Jayashsatolia403 Sep 1, 2023
25750e7
Merge remote-tracking branch 'origin/staging' into fix/challenge
Jayashsatolia403 Sep 2, 2023
31782ec
Updated gosdk
Jayashsatolia403 Sep 2, 2023
b8af7f8
Fix
Jayashsatolia403 Sep 2, 2023
448a09e
Fix
Jayashsatolia403 Sep 2, 2023
efab74c
log config too
Jayashsatolia403 Sep 2, 2023
46015ce
Fix phase locking (#1230)
Hitenjain14 Sep 3, 2023
2385332
Merge branch 'staging' into fix/challenge
Jayashsatolia403 Sep 3, 2023
509a87e
Fix
Jayashsatolia403 Sep 7, 2023
ab32bbb
Merge remote-tracking branch 'origin/fix/challenge' into fix/challenge
Jayashsatolia403 Sep 7, 2023
b954a4b
Added logging
Jayashsatolia403 Sep 7, 2023
9eceee9
Debug
Jayashsatolia403 Sep 8, 2023
7a993d9
Debug
Jayashsatolia403 Sep 8, 2023
8f228dd
Added reference to logs
Jayashsatolia403 Sep 8, 2023
683893e
Fix
Jayashsatolia403 Sep 10, 2023
6a30535
Merge remote-tracking branch 'origin/sprint-1.11' into fix/challenge
Jayashsatolia403 Sep 10, 2023
2da2044
Fix
Jayashsatolia403 Sep 10, 2023
7309d88
Updated gosdk
Jayashsatolia403 Sep 10, 2023
f886e85
Merge remote-tracking branch 'origin/sprint-1.11' into fix/challenge
Jayashsatolia403 Sep 10, 2023
32142f3
Use slice instead of ll
Jayashsatolia403 Sep 10, 2023
93ba71d
Validator last 5 transactions
Jayashsatolia403 Sep 10, 2023
e82b5c7
Merge branch 'sprint-1.11' into fix/challenge
Jayashsatolia403 Sep 11, 2023
9720565
Fix
Jayashsatolia403 Sep 11, 2023
3b14575
Merge remote-tracking branch 'origin/fix/challenge' into fix/challenge
Jayashsatolia403 Sep 11, 2023
5bd621c
Merge branch 'sprint-1.11' into fix/challenge
Jayashsatolia403 Sep 11, 2023
edbae06
Fix
Jayashsatolia403 Sep 12, 2023
a32bffd
Merge remote-tracking branch 'origin/fix/challenge' into fix/challenge
Jayashsatolia403 Sep 12, 2023
cad4512
Merge branch 'sprint-1.11' into fix/challenge
Jayashsatolia403 Sep 13, 2023
55a8fdc
Updated gosdk
Jayashsatolia403 Sep 14, 2023
a775f65
Resolved comments
Jayashsatolia403 Sep 14, 2023
98a8dcd
Updated gosdk
Jayashsatolia403 Sep 15, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions code/go/0chain.net/blobber/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"strconv"
"time"

"github.com/0chain/blobber/code/go/0chain.net/blobbercore/config"
Expand All @@ -13,7 +14,7 @@ import (

type cctCB struct {
done chan struct{}
cct time.Duration
cct int64
err error
}

Expand All @@ -35,14 +36,16 @@ func (c *cctCB) OnInfoAvailable(op int, status int, info string, errStr string)
}

m = m["fields"].(map[string]interface{})
cct := m["max_challenge_completion_time"].(string)

d, err := time.ParseDuration(cct)
cctString := m["max_challenge_completion_rounds"].(string)

cct, err := strconv.ParseInt(cctString, 10, 64)
if err != nil {
c.err = err
return
}
c.cct = d

c.cct = cct
}

func setCCTFromChain() error {
Expand Down
2 changes: 1 addition & 1 deletion code/go/0chain.net/blobbercore/allocation/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func findAllocations(ctx context.Context, offset int64) (allocs []*Allocation, c

func shouldFinalize(sa *transaction.StorageAllocation) bool {
var now = common.Now()
return sa.Until() < now && !sa.Finalized
return sa.Expiration < now && !sa.Finalized
}

func updateAllocation(ctx context.Context, a *Allocation) {
Expand Down
20 changes: 14 additions & 6 deletions code/go/0chain.net/blobbercore/challenge/challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func syncOpenChallenges(ctx context.Context) {
params := make(map[string]string)
params["blobber"] = node.Self.ID

params["limit"] = "50"
params["limit"] = "20"
if lastChallengeRound > 0 {
params["from"] = strconv.FormatInt(lastChallengeRound, 10)
}
Expand Down Expand Up @@ -154,7 +154,6 @@ func validateOnValidators(c *ChallengeEntity) {
zap.Any("challenge_id", c.ChallengeID),
zap.Time("created", createdTime),
zap.Error(err))
//TODO: Should we delete the challenge from map or send it back to the todo channel?
deleteChallenge(c.RoundCreatedAt)
tx.Rollback()
return
Expand Down Expand Up @@ -191,12 +190,21 @@ func (c *ChallengeEntity) getCommitTransaction() (*transaction.Transaction, erro
tx := datastore.GetStore().GetTransaction(ctx)

createdTime := common.ToTime(c.CreatedAt)
logging.Logger.Info("[challenge]commit",

logging.Logger.Info("[challenge]verify: ",
zap.Any("challenge_id", c.ChallengeID),
zap.Time("created", createdTime),
zap.Any("openchallenge", c))
zap.Time("created", createdTime))

currentRound := roundInfo.CurrentRound + int64(float64(roundInfo.LastRoundDiff)*(float64(time.Since(roundInfo.CurrentRoundCaptureTime).Milliseconds())/float64(GetRoundInterval.Milliseconds())))
logging.Logger.Info("[challenge]commit",
zap.Any("currentRound", currentRound),
zap.Any("roundInfo.LastRoundDiff", roundInfo.LastRoundDiff),
zap.Any("roundInfo.CurrentRound", roundInfo.CurrentRound),
zap.Any("roundInfo.CurrentRoundCaptureTime", roundInfo.CurrentRoundCaptureTime),
zap.Any("time.Since(roundInfo.CurrentRoundCaptureTime).Milliseconds()", time.Since(roundInfo.CurrentRoundCaptureTime).Milliseconds()),
)

if time.Since(common.ToTime(c.CreatedAt)) > config.StorageSCConfig.ChallengeCompletionTime {
if currentRound-c.RoundCreatedAt > config.StorageSCConfig.ChallengeCompletionTime {
c.CancelChallenge(ctx, ErrExpiredCCT)
if err := tx.Commit().Error; err != nil {
logging.Logger.Error("[challenge]verify(Commit): ",
Expand Down
33 changes: 32 additions & 1 deletion code/go/0chain.net/blobbercore/challenge/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package challenge

import (
"context"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/gosdk/zcncore"
"sync"
"time"

Expand All @@ -13,12 +15,20 @@ import (
"golang.org/x/sync/semaphore"
)

const GetRoundInterval = 3 * time.Minute

type TodoChallenge struct {
Id string
CreatedAt time.Time
Status ChallengeStatus
}

type RoundInfo struct {
CurrentRound int64
CurrentRoundCaptureTime time.Time
LastRoundDiff int64
}

func Int64Comparator(a, b interface{}) int {
aAsserted := a.(int64)
bAsserted := b.(int64)
Expand All @@ -36,6 +46,7 @@ var (
toProcessChallenge = make(chan *ChallengeEntity, 100)
challengeMap = treemap.NewWith(Int64Comparator)
challengeMapLock = sync.RWMutex{}
roundInfo = RoundInfo{}
)

const batchSize = 5
Expand All @@ -59,12 +70,32 @@ func startPullWorker(ctx context.Context) {
}

func startWorkers(ctx context.Context) {
go getRoundWorker(ctx)

// start challenge listeners
go challengeProcessor(ctx)

go commitOnChainWorker(ctx)
}

func getRoundWorker(ctx context.Context) {
ticker := time.NewTicker(GetRoundInterval)

for {
select {
case <-ctx.Done():
return
case <-ticker.C:
network := zcncore.GetNetwork()
currentRound, _ := sdk.GetRoundFromSharders(network.Sharders)

roundInfo.LastRoundDiff = currentRound - roundInfo.CurrentRound
roundInfo.CurrentRound = currentRound
roundInfo.CurrentRoundCaptureTime = time.Now()
}
}
}

func challengeProcessor(ctx context.Context) {
defer func() {
if r := recover(); r != nil {
Expand Down Expand Up @@ -151,7 +182,7 @@ func commitOnChainWorker(ctx context.Context) {
}()
err := challenge.VerifyChallengeTransaction(txn)
if err == nil || err != ErrEntityNotFound {
deleteChallenge(int64(challenge.RoundCreatedAt))
deleteChallenge(challenge.RoundCreatedAt)
}
}(&chall)
}
Expand Down
2 changes: 1 addition & 1 deletion code/go/0chain.net/blobbercore/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func ValidChain(chain string) error {
// If any field it required then it can simply be added in this struct and we are
// good to go
type StorageSCConfiguration struct {
ChallengeCompletionTime time.Duration
ChallengeCompletionTime int64
}

var StorageSCConfig StorageSCConfiguration
8 changes: 8 additions & 0 deletions code/go/0chain.net/blobbercore/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/0chain/blobber/code/go/0chain.net/core/transaction"
"net/http"
"os"
"runtime/pprof"
Expand Down Expand Up @@ -221,6 +222,9 @@ func setupHandlers(r *mux.Router) {
r.HandleFunc("/_config", RateLimitByCommmitRL(common.ToJSONResponse(GetConfig)))
// r.HandleFunc("/_stats", common.AuthenticateAdmin(StatsHandler))
r.HandleFunc("/_stats", RateLimitByCommmitRL(StatsHandler))

r.HandleFunc("/_logs", RateLimitByCommmitRL(common.ToJSONResponse(GetLogs)))

// r.HandleFunc("/_statsJSON", common.AuthenticateAdmin(common.ToJSONResponse(stats.StatsJSONHandler)))
r.HandleFunc("/_statsJSON", RateLimitByCommmitRL(common.ToJSONResponse(stats.StatsJSONHandler)))
// r.HandleFunc("/_cleanupdisk", common.AuthenticateAdmin(common.ToJSONResponse(WithReadOnlyConnection(CleanupDiskHandler))))
Expand Down Expand Up @@ -778,6 +782,10 @@ func GetConfig(ctx context.Context, r *http.Request) (interface{}, error) {
return config.Configuration, nil
}

func GetLogs(ctx context.Context, r *http.Request) (interface{}, error) {
return transaction.Last50Transactions, nil
}

func CleanupDiskHandler(ctx context.Context, r *http.Request) (interface{}, error) {

err := CleanupDiskFiles(ctx)
Expand Down
11 changes: 2 additions & 9 deletions code/go/0chain.net/blobbercore/stats/blobberstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,21 +306,14 @@ func (bs *BlobberStats) loadAllocationStats(ctx context.Context) {
bs.AllocationStats = append(bs.AllocationStats, as)
}

if err = rows.Err(); err != nil && err != sql.ErrNoRows {
if err = rows.Err(); err != nil && !errors.Is(err, sql.ErrNoRows) {
Logger.Error("Error in scanning record for blobber stats",
zap.Error(err))
return
}

var count int64
Jayashsatolia403 marked this conversation as resolved.
Show resolved Hide resolved
err = db.Table("reference_objects").Count(&count).Error
if err != nil {
Logger.Error("loadAllocationStats err", zap.Any("err", err))
return
}

if requestData != nil {
pagination := GeneratePagination(requestData.Page, requestData.Limit, requestData.Offset, int(count))
pagination := GeneratePagination(requestData.Page, requestData.Limit, requestData.Offset, len(bs.AllocationStats))
bs.AllocationListPagination = pagination
}
}
Expand Down
2 changes: 2 additions & 0 deletions code/go/0chain.net/blobbercore/stats/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ const tpl = `
</tr>
</table>

<h3><a href='_logs'>Last 50 Transactions</a></h3>

<h1>
Allocation Stats
</h1>
Expand Down
26 changes: 20 additions & 6 deletions code/go/0chain.net/core/transaction/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package transaction

import (
"encoding/json"
"github.com/0chain/gosdk/core/transaction"
"sync"
"time"

"github.com/0chain/blobber/code/go/0chain.net/blobbercore/config"
"github.com/0chain/blobber/code/go/0chain.net/core/logging"
"go.uber.org/zap"

Expand All @@ -16,6 +16,8 @@ import (
"github.com/0chain/blobber/code/go/0chain.net/core/node"
)

var Last50Transactions []string
Jayashsatolia403 marked this conversation as resolved.
Show resolved Hide resolved

// Transaction entity that encapsulates the transaction related data and meta data
type Transaction struct {
Hash string `json:"hash,omitempty"`
Expand Down Expand Up @@ -92,10 +94,6 @@ type StorageAllocation struct {
ParityShards int64 `json:"parity_shards"`
}

func (sa *StorageAllocation) Until() common.Timestamp {
return sa.Expiration + common.Timestamp(config.StorageSCConfig.ChallengeCompletionTime/time.Second)
}

type StorageAllocationBlobber struct {
BlobberID string `json:"blobber_id"`
Size int64 `json:"size"`
Expand Down Expand Up @@ -139,6 +137,22 @@ func (t *Transaction) GetTransaction() zcncore.TransactionScheme {

func (t *Transaction) ExecuteSmartContract(address, methodName string, input interface{}, val uint64) error {
t.wg.Add(1)

logging.Logger.Info("Jayash", zap.Any("address", address), zap.Any("methodName", methodName), zap.Any("input", input), zap.Any("val", val))
Jayashsatolia403 marked this conversation as resolved.
Show resolved Hide resolved

sn := transaction.SmartContractTxnData{Name: methodName, InputArgs: input}
snBytes, err := json.Marshal(sn)
if err != nil {
logging.Logger.Error("Jayash", zap.Error(err))
Jayashsatolia403 marked this conversation as resolved.
Show resolved Hide resolved
return err
}

if len(Last50Transactions) == 50 {
Last50Transactions = Last50Transactions[1:]
} else {
Last50Transactions = append(Last50Transactions, string(snBytes))
}

nonce := monitor.getNextUnusedNonce()
if err := t.zcntxn.SetTransactionNonce(nonce); err != nil {
logging.Logger.Error("Failed to set nonce.",
Expand All @@ -151,7 +165,7 @@ func (t *Transaction) ExecuteSmartContract(address, methodName string, input int
zap.Any("hash", t.zcntxn.GetTransactionHash()),
zap.Any("nonce", nonce))

_, err := t.zcntxn.ExecuteSmartContract(address, methodName, input, uint64(val))
_, err = t.zcntxn.ExecuteSmartContract(address, methodName, input, uint64(val))
if err != nil {
t.wg.Done()
logging.Logger.Error("Failed to execute SC.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,12 @@ import (
)

func ChallengeHandler(ctx context.Context, r *http.Request) (interface{}, error) {
return challengeHandler(ctx, r)
res, err := challengeHandler(ctx, r)

if len(Last5Transactions) >= 5 {
Jayashsatolia403 marked this conversation as resolved.
Show resolved Hide resolved
Last5Transactions = Last5Transactions[1:]
}
Last5Transactions = append(Last5Transactions, res)

return res, err
}
2 changes: 1 addition & 1 deletion code/go/0chain.net/validatorcore/storage/handler_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ func SetupHandlers(r *mux.Router) {

r.HandleFunc("/debug", common.ToJSONResponse(DumpGoRoutines))

r.HandleFunc("/stats", statsHandler)
r.HandleFunc("/_stats", statsHandler)
}
24 changes: 21 additions & 3 deletions code/go/0chain.net/validatorcore/storage/stats_handler.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package storage

import (
"encoding/json"
"fmt"
"net/http"
"sync"
)

var Last5Transactions []interface{}

type Stats struct {
TotalChallenges int
SuccessfulChallenges int
Expand Down Expand Up @@ -88,9 +91,24 @@ func statsHandler(w http.ResponseWriter, r *http.Request) {
<td>` + fmt.Sprintf("%d", result.FailedChallenges) + `</td>
</tr>
</table>
</body>
</html>
`
<div class="transactions">
<h2>Last 5 Transactions</h2>
<ul>
`
for _, transaction := range Last5Transactions {
jsonData, err := json.Marshal(transaction)
if err != nil {
statsHTML += "<li>Failed to marshal transaction</li>"
continue
}
statsHTML += "<li>" + string(jsonData) + "</li>"
}
statsHTML += `
</ul>
</div>
</body>
</html>
`

w.Header().Set("Content-Type", "text/html")
_, err := w.Write([]byte(statsHTML))
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/0chain/errors v1.0.3
github.com/0chain/gosdk v1.8.18-0.20230901213317-53d640a9b7f9
github.com/0chain/gosdk v1.10.1-0.20230910134005-e36c15157b5b
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/didip/tollbooth/v6 v6.1.2
github.com/go-openapi/runtime v0.26.0
Expand Down
Loading