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

Update txs.go #9

Draft
wants to merge 33 commits into
base: x1
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
e754b94
Update txs.go
jacklevin74 Jan 22, 2024
71cecd8
Update config.go
jacklevin74 Jan 22, 2024
630e96b
Update txs.go
jacklevin74 Jan 22, 2024
7587eb8
Update txs.go
jacklevin74 Jan 22, 2024
68c54f2
Update txs.go
jacklevin74 Jan 22, 2024
98585f6
Update txs.go
jacklevin74 Jan 22, 2024
c4edba1
Update validators.go
jacklevin74 Jan 23, 2024
75d1024
Update control.go
jacklevin74 Jan 23, 2024
562d868
Update control.go
jacklevin74 Jan 23, 2024
7c4ff65
Update go.mod
jacklevin74 Jan 26, 2024
a454691
Update hooks.go
jacklevin74 Jan 26, 2024
c00c52e
Update hooks.go
jacklevin74 Jan 26, 2024
3094fd6
Update hooks.go
jacklevin74 Jan 26, 2024
d9a6337
Update control.go
jacklevin74 Jan 27, 2024
d6bf2d7
Update control.go
jacklevin74 Jan 27, 2024
cb8579b
Update control.go
jacklevin74 Jan 27, 2024
1ef679c
Update control.go
jacklevin74 Jan 27, 2024
099597a
Update gas_power_check.go
jacklevin74 Jan 28, 2024
5206a0d
Update control.go
jacklevin74 Jan 28, 2024
2ca3bfa
Update txs.go
jacklevin74 Jan 28, 2024
8d43b7f
Update gas_power_check.go
jacklevin74 Jan 28, 2024
417d246
Merge branch 'x1' into jacklevin74-patch-1
nibty Jan 28, 2024
4f24613
update the go.mod
nibty Jan 28, 2024
6142f33
Update hooks.go
jacklevin74 Jan 29, 2024
1d7acf4
Update control.go
jacklevin74 Jan 29, 2024
21db285
Update hooks.go
jacklevin74 Jan 29, 2024
febaead
Update control.go
jacklevin74 Jan 29, 2024
de96c65
Update txs.go
jacklevin74 Jan 29, 2024
33fa953
Update epoch_check.go
jacklevin74 Jan 29, 2024
057e248
Update hooks.go
jacklevin74 Jan 29, 2024
ecae842
Update txs.go
jacklevin74 Jan 29, 2024
525cdc1
Update txs.go
jacklevin74 Jan 29, 2024
dc41314
Update emitter.go
jacklevin74 Jan 30, 2024
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
2 changes: 2 additions & 0 deletions eventcheck/epochcheck/epoch_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ func CalcGasPowerUsed(e inter.EventPayloadI, rules opera.Rules) uint64 {
}

func (v *Checker) checkGas(e inter.EventPayloadI, rules opera.Rules) error {
// do not check gas
return nil
if e.GasPowerUsed() > rules.Economy.Gas.MaxEventGas {
return ErrTooBigGasUsed
}
Expand Down
70 changes: 41 additions & 29 deletions eventcheck/gaspowercheck/gas_power_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"math/big"
"time"
//"fmt"

"github.com/Fantom-foundation/lachesis-base/eventcheck/epochcheck"
"github.com/Fantom-foundation/lachesis-base/hash"
Expand Down Expand Up @@ -127,46 +128,57 @@ func CalcValidatorGasPower(e inter.EventI, eTime, prevTime inter.Timestamp, prev
if gasPower > maxGasPower {
gasPower = maxGasPower
}
//fmt.Printf("e.Creator: %v, gasPower %v gasPowerAllocatedBn: %v\n", e.Creator(), gasPower, gasPowerAllocatedBn)

return gasPower
}

func CalcValidatorGasPowerPerSec(
validator idx.ValidatorID,
validators *pos.Validators,
config Config,
validator idx.ValidatorID,
validators *pos.Validators,
config Config,
) (
perSec uint64,
maxGasPower uint64,
startup uint64,
perSec uint64,
maxGasPower uint64,
startup uint64,
) {
stake := validators.Get(validator)
if stake == 0 {
return 0, 0, 0
}

gas := config

validatorGasPowerPerSecBn := new(big.Int).SetUint64(gas.AllocPerSec)
mul(validatorGasPowerPerSecBn, uint64(stake))
div(validatorGasPowerPerSecBn, uint64(validators.TotalWeight()))
perSec = validatorGasPowerPerSecBn.Uint64()

maxGasPower = perSec * (uint64(gas.MaxAllocPeriod) / uint64(time.Second))
if maxGasPower < gas.MinEnsuredAlloc {
maxGasPower = gas.MinEnsuredAlloc
}

startup = perSec * (uint64(gas.StartupAllocPeriod) / uint64(time.Second))
if startup < gas.MinStartupGas {
startup = gas.MinStartupGas
}

return
stake := validators.Get(validator)
if stake == 0 {
return 0, 0, 0
}

// don't calculate, keep the same for all
//maxGasPower = 1000 * 28000 * 60
//startup = 100 * 28000
//return

gas := config

validatorGasPowerPerSecBn := new(big.Int).SetUint64(gas.AllocPerSec)
mul(validatorGasPowerPerSecBn, uint64(stake))
div(validatorGasPowerPerSecBn, uint64(validators.TotalWeight()))
perSec = validatorGasPowerPerSecBn.Uint64()
//if validator == 7 {
perSec = 1000 * 28000
//}

maxGasPower = perSec * (uint64(gas.MaxAllocPeriod) / uint64(time.Second))
if maxGasPower < gas.MinEnsuredAlloc {
maxGasPower = gas.MinEnsuredAlloc
}

startup = perSec * (uint64(gas.StartupAllocPeriod) / uint64(time.Second))
if startup < gas.MinStartupGas {
startup = gas.MinStartupGas
}
//fmt.Printf("validator:%v , GasPowerPerSec: Persec: %v, maxGasPower: %v, startup: %v gas.AllocPerSec %v stake %v validators.TotalWeight() %v\n",validator, perSec, maxGasPower, startup, gas.AllocPerSec, stake, validators.TotalWeight())

return
}

// Validate event
func (v *Checker) Validate(e inter.EventI, selfParent inter.EventI) error {
return nil
gasPowers, err := v.CalcGasPower(e, selfParent)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion gossip/emitter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func DefaultConfig() Config {
Min: 150 * time.Millisecond,
Max: 10 * time.Minute,
Confirming: 170 * time.Millisecond,
DoublesignProtection: 11 * time.Minute, // should be greater than MaxEmitInterval
DoublesignProtection: 30 * time.Second, // should be greater than MaxEmitInterval
ParallelInstanceProtection: 1 * time.Minute,
},

Expand Down
55 changes: 47 additions & 8 deletions gossip/emitter/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package emitter

import (
"time"

"github.com/Fantom-foundation/lachesis-base/emitter/ancestor"
"github.com/Fantom-foundation/lachesis-base/inter/idx"
"github.com/Fantom-foundation/lachesis-base/inter/pos"
Expand Down Expand Up @@ -41,7 +41,28 @@ func eventMetric(orig ancestor.Metric, seq idx.Event) ancestor.Metric {
return kickStartMetric(ancestor.Metric(eventMetricF(uint64(orig))), seq)
}

// Function to get the top 50 elements from a slice
func top50(slice []idx.ValidatorID) []idx.ValidatorID {
if len(slice) > 50 {
return slice[:50] // Return the first 100 elements
}
return slice // Return the slice as is if it has less than or equal to 100 elements
}

// Function to check if a number is in the top 50 elements of a slice
func isInTop50(number idx.ValidatorID, slice []idx.ValidatorID) bool {
var v idx.ValidatorID
top50Slice := top50(slice)
for _, v = range top50Slice {
if v == number {
return true
}
}
return false
}

func (em *Emitter) isAllowedToEmit(e inter.EventI, eTxs bool, metric ancestor.Metric, selfParent *inter.Event) bool {
// for now allow only vals up to ID 30 to emit:
passedTime := e.CreationTime().Time().Sub(em.prevEmittedAtTime)
if passedTime < 0 {
passedTime = 0
Expand All @@ -50,6 +71,21 @@ func (em *Emitter) isAllowedToEmit(e inter.EventI, eTxs bool, metric ancestor.Me
if passedTimeIdle < 0 {
passedTimeIdle = 0
}
// metric is a decimal (0.0, 1.0], being an estimation of how much the event will advance the consensus
adjustedPassedTime := time.Duration(ancestor.Metric(passedTime/piecefunc.DecimalUnit) * metric)
adjustedPassedIdleTime := time.Duration(ancestor.Metric(passedTimeIdle/piecefunc.DecimalUnit) * metric)
passedBlocks := em.world.GetLatestBlockIndex() - em.prevEmittedAtBlock

supermajority := true
// Filter this node's events if not in top50 supermajority of stakers
if e.Creator() == em.config.Validator.ID && !isInTop50(e.Creator(), em.validators.SortedIDs()) {
//fmt.Println("This node is not in supermajority")
//supermajority = false
// disable check
supermajority = true
}

if (supermajority) {
if em.stakeRatio[e.Creator()] < 0.35*piecefunc.DecimalUnit {
// top validators emit event right after transaction is originated
passedTimeIdle = passedTime
Expand All @@ -60,10 +96,6 @@ func (em *Emitter) isAllowedToEmit(e inter.EventI, eTxs bool, metric ancestor.Me
if passedTimeIdle > passedTime {
passedTimeIdle = passedTime
}
// metric is a decimal (0.0, 1.0], being an estimation of how much the event will advance the consensus
adjustedPassedTime := time.Duration(ancestor.Metric(passedTime/piecefunc.DecimalUnit) * metric)
adjustedPassedIdleTime := time.Duration(ancestor.Metric(passedTimeIdle/piecefunc.DecimalUnit) * metric)
passedBlocks := em.world.GetLatestBlockIndex() - em.prevEmittedAtBlock
// Forbid emitting if not enough power and power is decreasing
{
threshold := em.config.EmergencyThreshold
Expand Down Expand Up @@ -100,7 +132,7 @@ func (em *Emitter) isAllowedToEmit(e inter.EventI, eTxs bool, metric ancestor.Me
factor := float64(e.GasPowerLeft().Min()) / float64(threshold)
adjustedEmitInterval := time.Duration(maxT - (maxT-minT)*factor)
if passedTime < adjustedEmitInterval {
return false
return true
}
}
}
Expand All @@ -124,11 +156,18 @@ func (em *Emitter) isAllowedToEmit(e inter.EventI, eTxs bool, metric ancestor.Me
if adjustedPassedIdleTime < em.intervals.Confirming &&
!em.idle() &&
!eTxs {
return false
return true
}
}

// only allow top validators
return true
} else {
// Enforce emitting if passed Max time (10 mins)
if passedTime >= em.intervals.Max {
return true
}
}
return false
}

func (em *Emitter) recheckIdleTime() {
Expand Down
11 changes: 6 additions & 5 deletions gossip/emitter/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,13 @@ func (em *Emitter) getSortedTxs() *types.TransactionsByPriceAndNonce {
em.Log.Error("Tx pool transactions fetching error", "err", err)
return nil
}
for from, txs := range pendingTxs {
//Do not block nodes from taking events from mempool
//for from, txs := range pendingTxs {
// Filter the excessive transactions from each sender
if len(txs) > em.config.MaxTxsPerAddress {
pendingTxs[from] = txs[:em.config.MaxTxsPerAddress]
}
}
// if len(txs) > em.config.MaxTxsPerAddress {
// pendingTxs[from] = txs[:em.config.MaxTxsPerAddress]
// }
//}
sortedTxs := types.NewTransactionsByPriceAndNonce(em.world.TxSigner, pendingTxs, em.world.GetRules().Economy.MinGasPrice)
em.cache.sortedTxs = sortedTxs
em.cache.poolCount = poolCount
Expand Down
35 changes: 35 additions & 0 deletions gossip/emitter/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package emitter

import (
"time"
"fmt"

"github.com/Fantom-foundation/lachesis-base/emitter/ancestor"
"github.com/Fantom-foundation/lachesis-base/inter/idx"
Expand All @@ -15,6 +16,32 @@ import (
"github.com/Fantom-foundation/go-opera/utils/adapters/vecmt2dagidx"
)

func isOdd(number idx.Epoch) bool {
return number%2 != 0
}

func mutateValidators(validators *pos.Validators) *pos.Validators {
// Define a set of IDs you want to process
targetIDs := map[idx.ValidatorID]struct{}{
1: {},
2: {},
3: {},
7: {},
21: {},
}

builder := pos.NewBuilder() // Assuming a builder is available in the "pos" package

for _, vid := range validators.IDs() {
// Check if vid is one of the target IDs
if _, ok := targetIDs[vid]; ok {
stake := uint64(validators.Get(vid))
builder.Set(vid, pos.Weight(stake))
}
}
return builder.Build()
}

// OnNewEpoch should be called after each epoch change, and on startup
func (em *Emitter) OnNewEpoch(newValidators *pos.Validators, newEpoch idx.Epoch) {
em.maxParents = em.config.MaxParents
Expand All @@ -29,6 +56,14 @@ func (em *Emitter) OnNewEpoch(newValidators *pos.Validators, newEpoch idx.Epoch)
em.syncStatus.becameValidator = time.Now()
}

//small_set:=isOdd(newEpoch)

//if small_set {
// newValidators = mutateValidators(newValidators)
//}
small_set:=false
fmt.Printf("Epoch: %v, small_set: %v vals: %v\n", newEpoch, small_set, newValidators.SortedIDs())

em.validators, em.epoch = newValidators, newEpoch

if !em.isValidator() {
Expand Down
31 changes: 10 additions & 21 deletions gossip/emitter/txs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package emitter

import (
"time"

"fmt"
"github.com/Fantom-foundation/lachesis-base/common/bigendian"
"github.com/Fantom-foundation/lachesis-base/hash"
"github.com/Fantom-foundation/lachesis-base/inter/idx"
Expand Down Expand Up @@ -81,26 +81,9 @@ func (em *Emitter) maxGasPowerToUse(e *inter.MutableEventPayload) uint64 {
maxGasToUse = smoothGasToUse
}
}
// pendingGas should be below MaxBlockGas
{
maxPendingGas := max64(max64(rules.Blocks.MaxBlockGas/3, rules.Economy.Gas.MaxEventGas), 15000000)
if maxPendingGas <= em.pendingGas {
return 0
}
if maxPendingGas < em.pendingGas+maxGasToUse {
maxGasToUse = maxPendingGas - em.pendingGas
}
}
// No txs if power is low
{
threshold := em.config.NoTxsThreshold
if e.GasPowerLeft().Min() <= threshold {
return 0
} else if e.GasPowerLeft().Min() < threshold+maxGasToUse {
maxGasToUse = e.GasPowerLeft().Min() - threshold
}
}
return maxGasToUse

//return maxGasToUse
return 15000000
}

func getTxRoundIndex(now, txTime time.Time, validatorsNum idx.Validator) int {
Expand Down Expand Up @@ -128,6 +111,8 @@ func (em *Emitter) isMyTxTurn(txHash common.Hash, sender common.Address, account

func (em *Emitter) addTxs(e *inter.MutableEventPayload, sorted *types.TransactionsByPriceAndNonce) {
maxGasUsed := em.maxGasPowerToUse(e)
//fmt.Println("CheckTxs maxGasUsed <= e.GasPowerUsed()", maxGasUsed ,e.GasPowerUsed(), e)

if maxGasUsed <= e.GasPowerUsed() {
return
}
Expand All @@ -137,8 +122,10 @@ func (em *Emitter) addTxs(e *inter.MutableEventPayload, sorted *types.Transactio
for tx := sorted.Peek(); tx != nil; tx = sorted.Peek() {
sender, _ := types.Sender(em.world.TxSigner, tx)
// check transaction epoch rules

if epochcheck.CheckTxs(types.Transactions{tx}, rules) != nil {
sorted.Pop()
fmt.Println("CheckTxs skipping tx ")
continue
}
// check there's enough gas power to originate the transaction
Expand All @@ -162,10 +149,12 @@ func (em *Emitter) addTxs(e *inter.MutableEventPayload, sorted *types.Transactio
}
// check transaction is not outdated
if !em.world.TxPool.Has(tx.Hash()) {
fmt.Println("World TX check: skipping tx ")
sorted.Pop()
continue
}
// add
fmt.Println("My Turn to Execute e.GasPowerUsed()+tx.Gas() >= maxGasUsed, e.GasPowerLeft() ", e.GasPowerUsed()+tx.Gas(), maxGasUsed, e.GasPowerLeft().Min())
e.SetGasPowerUsed(e.GasPowerUsed() + tx.Gas())
e.SetGasPowerLeft(e.GasPowerLeft().Sub(tx.Gas()))
e.SetTxs(append(e.Txs(), tx))
Expand Down
2 changes: 1 addition & 1 deletion gossip/emitter/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

const (
validatorChallenge = 4 * time.Second
validatorChallenge = 200 * time.Second
)

func (em *Emitter) recountConfirmingIntervals(validators *pos.Validators) {
Expand Down
Loading