Skip to content

Commit

Permalink
fix: use random jitter for the transaction ID generation
Browse files Browse the repository at this point in the history
  • Loading branch information
mehcode committed Jun 30, 2020
1 parent 7c60fce commit c2a1a29
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions transaction_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package hedera
import (
"fmt"
"time"
"math/rand"

"github.com/hashgraph/hedera-sdk-go/proto"
)

var lastInstant time.Time

// TransactionID is the id used to identify a Transaction on the Hedera network. It consists of an AccountID and a
// a valid start time.
type TransactionID struct {
Expand All @@ -19,15 +18,10 @@ type TransactionID struct {
// NewTransactionID constructs a new Transaction id struct with the provided AccountID and the valid start time set
// to the current time - 10 seconds.
func NewTransactionID(accountID AccountID) TransactionID {
now := time.Now().Add(-10 * time.Second)
allowance := -(time.Duration(rand.Intn(5 * int(time.Second))) + (8 * time.Second))
validStart := time.Now().UTC().Add(allowance)

if !now.After(lastInstant) {
now = lastInstant.Add(time.Duration(1))
}

lastInstant = now

return TransactionID{accountID, now}
return TransactionID{accountID, validStart}
}

// NewTransactionIDWithValidStart constructs a new Transaction id struct with the provided AccountID and the valid start
Expand Down

0 comments on commit c2a1a29

Please sign in to comment.