Skip to content

Commit

Permalink
Merge pull request #107 from rus-alex/fix/wrong-api-gas-used
Browse files Browse the repository at this point in the history
Fix of wrong gasUsed
  • Loading branch information
uprendis authored May 5, 2021
2 parents 5aa92b7 + 418c6d1 commit 1a4dfdd
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
4 changes: 4 additions & 0 deletions gossip/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ func NewStore(dbs kvdb.FlushableDBProducer, cfg StoreConfig) *Store {
s.evm = evmstore.NewStore(s.mainDB, cfg.EVM)
s.sfcapi = sfcapi.NewStore(s.table.SfcAPI)

if err := s.migrateData(); err != nil {
s.Log.Crit("Failed to migrate Gossip DB", "err", err)
}

return s
}

Expand Down
63 changes: 60 additions & 3 deletions gossip/store_migration.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gossip

import (
"fmt"

"github.com/Fantom-foundation/lachesis-base/kvdb"

"github.com/Fantom-foundation/go-opera/utils/migration"
Expand All @@ -12,17 +14,72 @@ func isEmptyDB(db kvdb.Iteratee) bool {
return !it.Next()
}

func (s *Store) Migrate() error {
func (s *Store) migrateData() error {
versions := migration.NewKvdbIDStore(s.table.Version)
if isEmptyDB(s.mainDB) && isEmptyDB(s.async.mainDB) {
// short circuit if empty DB
versions.SetID(s.migrations().ID())
return nil
}
return s.migrations().Exec(versions)
err := s.migrations().Exec(versions)
if err == nil {
err = s.Commit()
}

return err
}

func (s *Store) migrations() *migration.Migration {
return migration.
Begin("opera-gossip-store")
Begin("opera-gossip-store").
Next("used gas recovery", s.dataRecovery_UsedGas)
}

func (s *Store) dataRecovery_UsedGas() error {
start := s.GetGenesisBlockIndex()
if start == nil {
return fmt.Errorf("genesis block index is not set")
}

for n := *start; true; n++ {
b := s.GetBlock(n)
if b == nil {
break
}

var (
rr = s.EvmStore().GetReceipts(n)
cumulativeGasWrong uint64
cumulativeGasRight uint64
fixed bool
)
for i, r := range rr {
// simulate the bug
switch {
case n == *start: // genesis block
if i == len(b.InternalTxs)-2 || i == len(b.InternalTxs)-1 {
cumulativeGasWrong = 0
}
default: // other blocks
if i == len(b.InternalTxs)-1 || i == len(b.InternalTxs) {
cumulativeGasWrong = 0
}
}
// recalc
gasUsed := r.CumulativeGasUsed - cumulativeGasWrong
cumulativeGasWrong += gasUsed
cumulativeGasRight += gasUsed
// fix
if r.CumulativeGasUsed != cumulativeGasRight {
r.CumulativeGasUsed = cumulativeGasRight
r.GasUsed = gasUsed
fixed = true
}
}
if fixed {
s.EvmStore().SetReceipts(n, rr)
}
}

return nil
}
4 changes: 0 additions & 4 deletions integration/assembly.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ func rawApplyGenesis(gdb *gossip.Store, cdb *abft.Store, g opera.Genesis, cfg Co
func rawMakeEngine(gdb *gossip.Store, cdb *abft.Store, g opera.Genesis, cfg Configs, applyGenesis bool) (*abft.Lachesis, *vecmt.Index, gossip.BlockProc, error) {
blockProc := gossip.DefaultBlockProc(g)

err := gdb.Migrate()
if err != nil {
return nil, nil, blockProc, fmt.Errorf("failed to migrate Gossip DB: %v", err)
}
if applyGenesis {
_, err := gdb.ApplyGenesis(blockProc, g)
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions inter/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@ func (b *Block) EstimateSize() int {
return (len(b.Events)+len(b.InternalTxs)+len(b.Txs)+1+1)*32 + len(b.SkippedTxs)*4 + 8 + 8
}

func (b *Block) NotSkippedTxs() []common.Hash {
txs := append(b.InternalTxs, b.Txs...)

if len(b.SkippedTxs) == 0 {
// short circuit if nothing to skip
return txs
}
skipCount := 0
res := make([]common.Hash, 0, len(txs))
for i, tx := range txs {
if skipCount < len(b.SkippedTxs) && b.SkippedTxs[skipCount] == uint32(i) {
skipCount++
} else {
res = append(res, tx)
}
}
return res
}

func FilterSkippedTxs(txs types.Transactions, skippedTxs []uint32) types.Transactions {
if len(skippedTxs) == 0 {
// short circuit if nothing to skip
Expand Down

0 comments on commit 1a4dfdd

Please sign in to comment.