Skip to content

Commit

Permalink
api: /chain/dateToBlock fix overflow for big timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
jordipainan authored and p4u committed Jun 22, 2023
1 parent 4de6027 commit 3e6b4b6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion api/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func (a *API) chainEstimateHeightHandler(msg *apirest.APIdata, ctx *httprouter.H
return err
}
data, err := json.Marshal(struct {
Height uint32 `json:"height"`
Height uint64 `json:"height"`
}{Height: height},
)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions api/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func (a *API) walletElectionHandler(msg *apirest.APIdata, ctx *httprouter.HTTPCo
}

// Set startBlock and endBlock
var startBlock uint32
var startBlock uint64
// if start date is empty, do not attempt to parse it. Set startBlock to 0, starting the
// election immediately. Otherwise, ensure the startBlock is in the future
if !description.StartDate.IsZero() {
Expand All @@ -336,7 +336,7 @@ func (a *API) walletElectionHandler(msg *apirest.APIdata, ctx *httprouter.HTTPCo
if startBlock == 0 {
// If startBlock is set to 0 (process starts asap), set the blockcount to the desired
// end block, minus the expected start block of the process
blockCount = blockCount - uint32(a.vocinfo.Height()) + 3
blockCount = blockCount - uint64(a.vocinfo.Height()) + 3
}

// Set the envelope and process models
Expand Down Expand Up @@ -421,8 +421,8 @@ func (a *API) walletElectionHandler(msg *apirest.APIdata, ctx *httprouter.HTTPCo
// Build the process transaction
process := &models.Process{
EntityId: wallet.Address().Bytes(),
StartBlock: startBlock,
BlockCount: blockCount,
StartBlock: uint32(startBlock),
BlockCount: uint32(blockCount),
CensusRoot: root,
CensusURI: &description.Census.URL,
Status: models.ProcessStatus_READY,
Expand Down
19 changes: 12 additions & 7 deletions vochain/vochaininfo/vochaininfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vochaininfo
import (
"context"
"fmt"
"math"
"sync"
"time"

Expand Down Expand Up @@ -58,42 +59,46 @@ func (vi *VochainInfo) BlockTimes() *[5]int32 {
}

// EstimateBlockHeight provides an estimation time for a future blockchain height number.
func (vi *VochainInfo) EstimateBlockHeight(target time.Time) (uint32, error) {
func (vi *VochainInfo) EstimateBlockHeight(target time.Time) (uint64, error) {
currentTime := time.Now()
// diff time in seconds
diffTime := target.Unix() - currentTime.Unix()

// block time in ms
times := vi.BlockTimes()
getMaxTimeFrom := func(i int) uint32 {
getMaxTimeFrom := func(i int) uint64 {
for ; i >= 0; i-- {
if times[i] != 0 {
return uint32(times[i])
return uint64(times[i])
}
}
return 10000 // fallback
}
inPast := diffTime < 0
absDiff := diffTime
// check diff is not too big
if absDiff > math.MaxUint64/1000 {
return 0, fmt.Errorf("target time %v is too far in the future", target)
}
if inPast {
absDiff = -absDiff
}
t := uint32(0)
t := uint64(0)
switch {
// if less than around 15 minutes missing
case absDiff < 900:
t = getMaxTimeFrom(1)
// if less than around 6 hours missing
case absDiff < 21600:
t = getMaxTimeFrom(3)
// if less than around 6 hours missing
// if more than around 6 hours missing
default:
t = getMaxTimeFrom(4)
}
// Multiply by 1000 because t is represented in seconds, not ms.
// Dividing t first can floor the integer, leading to divide-by-zero
currentHeight := uint32(vi.Height())
blockDiff := (uint32(absDiff*1000) / t)
currentHeight := uint64(vi.Height())
blockDiff := (uint64(absDiff*1000) / t)
if inPast {
if blockDiff > currentHeight {
return 0, fmt.Errorf("target time %v is before origin", target)
Expand Down

0 comments on commit 3e6b4b6

Please sign in to comment.