Skip to content

Commit

Permalink
get hardfork round by name (#1385)
Browse files Browse the repository at this point in the history
* get hardfork round by name

* fix naming

* fix default hardfork round
  • Loading branch information
Hitenjain14 committed Feb 2, 2024
1 parent 703cbd0 commit 312b5cf
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
80 changes: 77 additions & 3 deletions core/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/0chain/gosdk/core/encryption"
"github.com/0chain/gosdk/core/util"
"github.com/0chain/gosdk/zboxcore/logger"
"github.com/ethereum/go-ethereum/common/math"
)

const statSize = 20
Expand Down Expand Up @@ -117,9 +118,10 @@ func (h *NodeHolder) All() (res []string) {

const consensusThresh = 25
const (
GET_BALANCE = `/v1/client/get/balance?client_id=`
CURRENT_ROUND = "/v1/current-round"
GET_BLOCK_INFO = `/v1/block/get?`
GET_BALANCE = `/v1/client/get/balance?client_id=`
CURRENT_ROUND = "/v1/current-round"
GET_BLOCK_INFO = `/v1/block/get?`
GET_HARDFORK_ROUND = `/hardfork?name=`
)

func (h *NodeHolder) GetNonceFromSharders(clientID string) (int64, string, error) {
Expand Down Expand Up @@ -360,3 +362,75 @@ func (h *NodeHolder) GetRoundFromSharders() (int64, error) {

return round, nil
}

func (h *NodeHolder) GetHardForkRound(hardFork string) (int64, error) {
sharders := h.Healthy()
if len(sharders) == 0 {
return 0, stdErrors.New("get round failed. no sharders")
}

result := make(chan *util.GetResponse, len(sharders))

var numSharders = len(sharders)
// use 5 sharders to get round
if numSharders > 5 {
numSharders = 5
}

h.QueryFromSharders(numSharders, fmt.Sprintf("%s%s", GET_HARDFORK_ROUND, hardFork), result)

const consensusThresh = float32(25.0)

var rounds []int64

consensus := int64(0)
roundMap := make(map[int64]int64)
// If error then set it to max int64
round := int64(math.MaxInt64)

waitTimeC := time.After(10 * time.Second)
for i := 0; i < numSharders; i++ {
select {
case <-waitTimeC:
return 0, stdErrors.New("get round failed. consensus not reached")
case rsp := <-result:
if rsp == nil {
logger.Logger.Error("nil response")
continue
}
if rsp.StatusCode != http.StatusOK {
continue
}

var respRound int64
err := json.Unmarshal([]byte(rsp.Body), &respRound)

if err != nil {
continue
}

rounds = append(rounds, respRound)

sort.Slice(rounds, func(i, j int) bool {
return false
})

medianRound := rounds[len(rounds)/2]

roundMap[medianRound]++

if roundMap[medianRound] > consensus {

consensus = roundMap[medianRound]
round = medianRound
rate := consensus * 100 / int64(numSharders)

if rate >= int64(consensusThresh) {
return round, nil
}
}
}
}

return round, nil
}
4 changes: 4 additions & 0 deletions zcncore/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,10 @@ func GetRoundFromSharders() (int64, error) {
return Sharders.GetRoundFromSharders()
}

func GetHardForkRound(hardFork string) (int64, error) {
return Sharders.GetHardForkRound(hardFork)
}

func GetMagicBlockByNumber(ctx context.Context, numSharders int, number int64) (m *block.MagicBlock, err error) {

var result = make(chan *util.GetResponse, numSharders)
Expand Down

0 comments on commit 312b5cf

Please sign in to comment.