Skip to content

Commit

Permalink
chore(lib/common): Use Uint128 from scale instead of lib/common (Chai…
Browse files Browse the repository at this point in the history
…nSafe#2074)

* chore(lib/common): Use Uint128 from scale instead of lib/common
  • Loading branch information
kishansagathiya committed Nov 29, 2021
1 parent 55669c5 commit 0d5e989
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 215 deletions.
6 changes: 3 additions & 3 deletions lib/babe/babe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/lib/babe/mocks"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
"github.com/ChainSafe/gossamer/lib/genesis"
"github.com/ChainSafe/gossamer/lib/keystore"
Expand All @@ -25,6 +24,7 @@ import (
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/lib/utils"
"github.com/ChainSafe/gossamer/pkg/scale"

mock "github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand All @@ -37,8 +37,8 @@ var (

keyring, _ = keystore.NewSr25519Keyring()

maxThreshold = common.MaxUint128
minThreshold = &common.Uint128{}
maxThreshold = scale.MaxUint128
minThreshold = &scale.Uint128{}

genesisHeader *types.Header
emptyHeader = &types.Header{
Expand Down
27 changes: 17 additions & 10 deletions lib/babe/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ package babe

import (
"errors"
"fmt"
"math"
"math/big"

"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto"
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/gtank/merlin"
)

Expand All @@ -30,7 +31,7 @@ func makeTranscript(randomness Randomness, slot, epoch uint64) *merlin.Transcrip
// https://github.com/paritytech/substrate/blob/master/client/consensus/babe/src/authorship.rs#L239
func claimPrimarySlot(randomness Randomness,
slot, epoch uint64,
threshold *common.Uint128,
threshold *scale.Uint128,
keypair *sr25519.Keypair,
) (*VrfOutputAndProof, error) {
transcript := makeTranscript(randomness, slot, epoch)
Expand All @@ -43,7 +44,10 @@ func claimPrimarySlot(randomness Randomness,
logger.Tracef("claimPrimarySlot pub=%s slot=%d epoch=%d output=0x%x proof=0x%x",
keypair.Public().Hex(), slot, epoch, out, proof)

ok := checkPrimaryThreshold(randomness, slot, epoch, out, threshold, keypair.Public().(*sr25519.PublicKey))
ok, err := checkPrimaryThreshold(randomness, slot, epoch, out, threshold, keypair.Public().(*sr25519.PublicKey))
if err != nil {
return nil, fmt.Errorf("failed to compare with threshold, %w", err)
}
if !ok {
return nil, nil
}
Expand All @@ -58,25 +62,28 @@ func claimPrimarySlot(randomness Randomness,
func checkPrimaryThreshold(randomness Randomness,
slot, epoch uint64,
output [sr25519.VRFOutputLength]byte,
threshold *common.Uint128,
threshold *scale.Uint128,
pub *sr25519.PublicKey,
) bool {
) (bool, error) {
t := makeTranscript(randomness, slot, epoch)
inout := sr25519.AttachInput(output, pub, t)
res := sr25519.MakeBytes(inout, 16, babeVRFPrefix)

inoutUint := common.Uint128FromLEBytes(res)
inoutUint, err := scale.NewUint128(res)
if err != nil {
return false, fmt.Errorf("failed to convert bytes to Uint128: %w", err)
}

logger.Tracef("checkPrimaryThreshold pub=%s randomness=0x%x slot=%d epoch=%d threshold=0x%x output=0x%x inout=0x%x",
pub.Hex(), randomness, slot, epoch, threshold, output, res)

return inoutUint.Cmp(threshold) < 0
return inoutUint.Compare(threshold) < 0, nil
}

// CalculateThreshold calculates the slot lottery threshold
// equation: threshold = 2^128 * (1 - (1-c)^(1/len(authorities))
// see https://github.com/paritytech/substrate/blob/master/client/consensus/babe/src/authorship.rs#L44
func CalculateThreshold(C1, C2 uint64, numAuths int) (*common.Uint128, error) {
func CalculateThreshold(C1, C2 uint64, numAuths int) (*scale.Uint128, error) {
c := float64(C1) / float64(C2)
if c > 1 {
return nil, errors.New("invalid C1/C2: greater than 1")
Expand All @@ -103,12 +110,12 @@ func CalculateThreshold(C1, C2 uint64, numAuths int) (*common.Uint128, error) {

// special case where threshold is maximum
if thresholdBig.Cmp(shift) == 0 {
return common.MaxUint128, nil
return scale.MaxUint128, nil
}

if len(thresholdBig.Bytes()) > 16 {
return nil, errors.New("threshold must be under or equal to 16 bytes")
}

return common.Uint128FromBigInt(thresholdBig), nil
return scale.NewUint128(thresholdBig)
}
5 changes: 2 additions & 3 deletions lib/babe/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
"fmt"
"time"

"github.com/ChainSafe/gossamer/lib/common"

"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
"github.com/ChainSafe/gossamer/pkg/scale"
)

// Randomness is an alias for a byte array with length types.RandomnessLength
Expand Down Expand Up @@ -60,7 +59,7 @@ type epochData struct {
randomness Randomness
authorityIndex uint32
authorities []types.Authority
threshold *common.Uint128
threshold *scale.Uint128
}

func (ed *epochData) String() string {
Expand Down
10 changes: 6 additions & 4 deletions lib/babe/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
type verifierInfo struct {
authorities []types.Authority
randomness Randomness
threshold *common.Uint128
threshold *scale.Uint128
secondarySlots bool
}

Expand Down Expand Up @@ -242,7 +242,7 @@ type verifier struct {
epoch uint64
authorities []types.Authority
randomness Randomness
threshold *common.Uint128
threshold *scale.Uint128
secondarySlots bool
}

Expand Down Expand Up @@ -453,14 +453,16 @@ func (b *verifier) verifyPrimarySlotWinner(authorityIndex uint32,
}

// check that VRF output was under threshold
ok := checkPrimaryThreshold(b.randomness,
ok, err := checkPrimaryThreshold(b.randomness,
slot,
b.epoch,
vrfOutput,
b.threshold,
pk,
)

if err != nil {
return false, fmt.Errorf("failed to compare with threshold, %w", err)
}
if !ok {
return false, ErrVRFOutputOverThreshold
}
Expand Down
141 changes: 0 additions & 141 deletions lib/common/uint128.go

This file was deleted.

54 changes: 0 additions & 54 deletions lib/common/uint128_test.go

This file was deleted.

0 comments on commit 0d5e989

Please sign in to comment.