Skip to content

Commit

Permalink
Merge pull request #910 from maticnetwork/shivam/POS-1599
Browse files Browse the repository at this point in the history
fix : testcases
  • Loading branch information
0xsharma committed Jul 24, 2023
2 parents 43e114b + 973f116 commit a801b75
Show file tree
Hide file tree
Showing 50 changed files with 391 additions and 340 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ protoc:
generate-mocks:
go generate mockgen -destination=./tests/bor/mocks/IHeimdallClient.go -package=mocks ./consensus/bor IHeimdallClient
go generate mockgen -destination=./eth/filters/IBackend.go -package=filters ./eth/filters Backend
go generate mockgen -destination=../eth/filters/IDatabase.go -package=filters . Database

geth:
$(GORUN) build/ci.go install ./cmd/geth
Expand Down
8 changes: 6 additions & 2 deletions common/leak/ignore_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ func IgnoreList() []goleak.Option {
goleak.IgnoreTopFunction("github.com/rjeczalik/notify._Cfunc_CFRunLoopRun"),

// todo: this leaks should be fixed
goleak.IgnoreTopFunction("github.com/ethereum/go-ethereum/metrics.(*meterArbiter).tick"),
goleak.IgnoreTopFunction("github.com/ethereum/go-ethereum/accounts/abi/bind/backends.nullSubscription.func1"),
goleak.IgnoreTopFunction("github.com/ethereum/go-ethereum/accounts/abi/bind/backends.(*filterBackend).SubscribeNewTxsEvent.func1"),
goleak.IgnoreTopFunction("github.com/ethereum/go-ethereum/accounts/abi/bind/backends.(*filterBackend).SubscribePendingLogsEvent.func1"),
goleak.IgnoreTopFunction("github.com/ethereum/go-ethereum/consensus/ethash.(*remoteSealer).loop"),
goleak.IgnoreTopFunction("github.com/ethereum/go-ethereum/core.(*BlockChain).updateFutureBlocks"),
goleak.IgnoreTopFunction("github.com/ethereum/go-ethereum/core/state/snapshot.(*diskLayer).generate"),
goleak.IgnoreTopFunction("github.com/ethereum/go-ethereum/accounts/abi/bind/backends.nullSubscription.func1"),
goleak.IgnoreTopFunction("github.com/ethereum/go-ethereum/eth/filters.(*EventSystem).eventLoop"),
goleak.IgnoreTopFunction("github.com/ethereum/go-ethereum/event.NewSubscription.func1"),
goleak.IgnoreTopFunction("github.com/ethereum/go-ethereum/event.NewSubscription"),
goleak.IgnoreTopFunction("github.com/ethereum/go-ethereum/metrics.(*meterArbiter).tick"),
}
}
2 changes: 1 addition & 1 deletion consensus/bor/bor.go
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ func (c *Bor) FinalizeAndAssemble(ctx context.Context, chain consensus.ChainHead
header.UncleHash = types.CalcUncleHash(nil)

// Assemble block
block := types.NewBlock(header, txs, nil, receipts, new(trie.Trie))
block := types.NewBlock(header, txs, nil, receipts, trie.NewStackTrie(nil))

// set state sync
bc := chain.(core.BorStateSyncer)
Expand Down
1 change: 1 addition & 0 deletions consensus/bor/bor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func TestGenesisContractChange(t *testing.T) {
Code: []byte{0x1, 0x1},
},
},
Config: &params.ChainConfig{},
}

db := rawdb.NewMemoryDatabase()
Expand Down
3 changes: 0 additions & 3 deletions consensus/bor/genesis_contract_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions consensus/bor/span_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2798,6 +2798,8 @@ func TestTransactionIndices(t *testing.T) {
block.AddTx(tx)
})

borReceipts := make([]types.Receipts, len(receipts))

check := func(tail *uint64, chain *BlockChain) {
stored := rawdb.ReadTxIndexTail(chain.db)
if tail == nil && stored != nil {
Expand Down Expand Up @@ -2841,7 +2843,7 @@ func TestTransactionIndices(t *testing.T) {
for _, l := range limit {
frdir := t.TempDir()
ancientDb, _ := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), frdir, "", false)
_, _ = rawdb.WriteAncientBlocks(ancientDb, append([]*types.Block{gspec.ToBlock()}, blocks...), append([]types.Receipts{{}}, receipts...), []types.Receipts{nil}, big.NewInt(0))
_, _ = rawdb.WriteAncientBlocks(ancientDb, append([]*types.Block{gspec.ToBlock()}, blocks...), append([]types.Receipts{{}}, receipts...), append([]types.Receipts{{}}, borReceipts...), big.NewInt(0))

l := l

Expand All @@ -2867,7 +2869,7 @@ func TestTransactionIndices(t *testing.T) {
ancientDb, _ := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false)
defer ancientDb.Close()

_, _ = rawdb.WriteAncientBlocks(ancientDb, append([]*types.Block{gspec.ToBlock()}, blocks...), append([]types.Receipts{{}}, receipts...), []types.Receipts{nil}, big.NewInt(0))
_, _ = rawdb.WriteAncientBlocks(ancientDb, append([]*types.Block{gspec.ToBlock()}, blocks...), append([]types.Receipts{{}}, receipts...), append([]types.Receipts{{}}, borReceipts...), big.NewInt(0))

limit = []uint64{0, 64 /* drop stale */, 32 /* shorten history */, 64 /* extend history */, 0 /* restore all */}

Expand Down Expand Up @@ -4387,10 +4389,12 @@ func TestTxIndexer(t *testing.T) {
},
}

borReceipts := make([]types.Receipts, len(receipts))

for _, c := range cases {
frdir := t.TempDir()
db, _ := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), frdir, "", false)
_, _ = rawdb.WriteAncientBlocks(db, append([]*types.Block{gspec.ToBlock()}, blocks...), append([]types.Receipts{{}}, receipts...), []types.Receipts{nil}, big.NewInt(0))
_, _ = rawdb.WriteAncientBlocks(db, append([]*types.Block{gspec.ToBlock()}, blocks...), append([]types.Receipts{{}}, receipts...), append([]types.Receipts{{}}, borReceipts...), big.NewInt(0))

// Index the initial blocks from ancient store
chain, _ := NewBlockChain(db, nil, gspec, nil, engine, vm.Config{}, nil, &c.limitA, nil)
Expand Down
4 changes: 4 additions & 0 deletions core/bor_fee_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ var transferFeeLogSig = common.HexToHash("0x4dfe1bbbcf077ddc3e01291eea2d5c70c2b4
var feeAddress = common.HexToAddress("0x0000000000000000000000000000000000001010")
var bigZero = big.NewInt(0)

func GetFeeAddress() common.Address {
return feeAddress
}

// AddTransferLog adds transfer log into state
func AddTransferLog(
state vm.StateDB,
Expand Down
9 changes: 0 additions & 9 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,6 @@ func (b *BlockGen) AddTx(tx *types.Transaction) {
// the block in chain will be returned.
func (b *BlockGen) AddTxWithChain(bc *BlockChain, tx *types.Transaction) {
b.addTx(bc, vm.Config{}, tx)
b.statedb.SetTxContext(tx.Hash(), len(b.txs))

receipt, err := ApplyTransaction(b.config, bc, &b.header.Coinbase, b.gasPool, b.statedb, b.header, tx, &b.header.GasUsed, vm.Config{}, nil)
if err != nil {
panic(err)
}

b.txs = append(b.txs, tx)
b.receipts = append(b.receipts, receipt)
}

// AddTxWithVMConfig adds a transaction to the generated block. If no coinbase has
Expand Down
9 changes: 9 additions & 0 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,15 @@ func (g *Genesis) MustCommit(db ethdb.Database) *types.Block {
return block
}

// GenesisBlockForTesting creates and writes a block in which addr has the given wei balance.
func GenesisBlockForTesting(db ethdb.Database, addr common.Address, balance *big.Int) *types.Block {
g := Genesis{
Alloc: GenesisAlloc{addr: {Balance: balance}},
BaseFee: big.NewInt(params.InitialBaseFee),
}
return g.MustCommit(db)
}

// DefaultGenesisBlock returns the Ethereum main net genesis block.
func DefaultGenesisBlock() *Genesis {
return &Genesis{
Expand Down
1 change: 1 addition & 0 deletions core/rawdb/ancient_scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var chainFreezerNoSnappy = map[string]bool{
ChainFreezerBodiesTable: false,
ChainFreezerReceiptTable: false,
ChainFreezerDifficultyTable: true,
freezerBorReceiptTable: false,
}

// The list of identifiers of ancient stores.
Expand Down
6 changes: 6 additions & 0 deletions core/rawdb/chain_freezer.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,12 @@ func (f *chainFreezer) freezeRange(nfdb *nofreezedb, number, limit uint64) (hash
return fmt.Errorf("can't write td to Freezer: %v", err)
}

// bor block receipt
borBlockReceipt := ReadBorReceiptRLP(nfdb, hash, number)
if err := op.AppendRaw(freezerBorReceiptTable, number, borBlockReceipt); err != nil {
return fmt.Errorf("can't write bor-receipt to freezer: %v", err)
}

hashes = append(hashes, hash)
}

Expand Down
1 change: 1 addition & 0 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func applyTransaction(msg *Message, config *params.ChainConfig, gp *GasPool, sta
statedb.AddBalance(result.BurntContractAddress, result.FeeBurnt)
}

// TODO(raneet10) Double check
statedb.AddBalance(evm.Context.Coinbase, result.FeeTipped)
output1 := new(big.Int).SetBytes(result.SenderInitBalance.Bytes())
output2 := new(big.Int).SetBytes(coinbaseBalance.Bytes())
Expand Down
13 changes: 10 additions & 3 deletions core/state_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ func u64(val uint64) *uint64 { return &val }
// contain invalid transactions
func TestStateProcessorErrors(t *testing.T) {
var (
cacheConfig = &CacheConfig{
TrieCleanLimit: 154,
TrieCleanJournal: "triecache",
Preimages: true,
}

config = &params.ChainConfig{
ChainID: big.NewInt(1),
HomesteadBlock: big.NewInt(0),
Expand Down Expand Up @@ -256,7 +262,7 @@ func TestStateProcessorErrors(t *testing.T) {
},
}
blockchain, _ = NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil, nil)
parallelBlockchain, _ = NewParallelBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{ParallelEnable: true, ParallelSpeculativeProcesses: 8}, nil, nil, nil)
parallelBlockchain, _ = NewParallelBlockChain(db, cacheConfig, gspec, nil, ethash.NewFaker(), vm.Config{ParallelEnable: true, ParallelSpeculativeProcesses: 8}, nil, nil, nil)
)

defer blockchain.Stop()
Expand Down Expand Up @@ -303,7 +309,7 @@ func TestStateProcessorErrors(t *testing.T) {
},
}
blockchain, _ = NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil, nil)
parallelBlockchain, _ = NewParallelBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{ParallelEnable: true, ParallelSpeculativeProcesses: 8}, nil, nil, nil)
parallelBlockchain, _ = NewParallelBlockChain(db, cacheConfig, gspec, nil, ethash.NewFaker(), vm.Config{ParallelEnable: true, ParallelSpeculativeProcesses: 8}, nil, nil, nil)
)

defer blockchain.Stop()
Expand Down Expand Up @@ -360,6 +366,7 @@ func TestStateProcessorErrors(t *testing.T) {
TerminalTotalDifficultyPassed: true,
// TODO marcello double check
ShanghaiTime: u64(0),
Bor: &params.BorConfig{BurntContract: map[string]string{"0": "0x000000000000000000000000000000000000dead"}},
},
Alloc: GenesisAlloc{
common.HexToAddress("0x71562b71999873DB5b286dF957af199Ec94617F7"): GenesisAccount{
Expand All @@ -370,7 +377,7 @@ func TestStateProcessorErrors(t *testing.T) {
}
genesis = gspec.MustCommit(db)
blockchain, _ = NewBlockChain(db, nil, gspec, nil, beacon.New(ethash.NewFaker()), vm.Config{}, nil, nil, nil)
parallelBlockchain, _ = NewParallelBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{ParallelEnable: true, ParallelSpeculativeProcesses: 8}, nil, nil, nil)
parallelBlockchain, _ = NewParallelBlockChain(db, cacheConfig, gspec, nil, beacon.New(ethash.NewFaker()), vm.Config{ParallelEnable: true, ParallelSpeculativeProcesses: 8}, nil, nil, nil)
tooBigInitCode = [params.MaxInitCodeSize + 1]byte{}
smallInitCode = [320]byte{}
)
Expand Down
23 changes: 12 additions & 11 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,15 +441,16 @@ func (st *StateTransition) TransitionDb(interruptCtx context.Context) (*Executio
effectiveTip = cmath.BigMin(msg.GasTipCap, new(big.Int).Sub(msg.GasFeeCap, st.evm.Context.BaseFee))
}

if st.evm.Config.NoBaseFee && msg.GasFeeCap.Sign() == 0 && msg.GasTipCap.Sign() == 0 {
// Skip fee payment when NoBaseFee is set and the fee fields
// are 0. This avoids a negative effectiveTip being applied to
// the coinbase when simulating calls.
} else {
fee := new(big.Int).SetUint64(st.gasUsed())
fee.Mul(fee, effectiveTip)
st.state.AddBalance(st.evm.Context.Coinbase, fee)
}
// TODO(raneet10): Double check. We might want to inculcate this fix in a separate condition
// if st.evm.Config.NoBaseFee && msg.GasFeeCap.Sign() == 0 && msg.GasTipCap.Sign() == 0 {
// // Skip fee payment when NoBaseFee is set and the fee fields
// // are 0. This avoids a negative effectiveTip being applied to
// // the coinbase when simulating calls.
// } else {
// fee := new(big.Int).SetUint64(st.gasUsed())
// fee.Mul(fee, effectiveTip)
// st.state.AddBalance(st.evm.Context.Coinbase, fee)
// }

amount := new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), effectiveTip)

Expand All @@ -458,8 +459,8 @@ func (st *StateTransition) TransitionDb(interruptCtx context.Context) (*Executio
var burntContractAddress common.Address

if rules.IsLondon {
burntContractAddress := common.HexToAddress(st.evm.ChainConfig().Bor.CalculateBurntContract(st.evm.Context.BlockNumber.Uint64()))
burnAmount := new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), st.evm.Context.BaseFee)
burntContractAddress = common.HexToAddress(st.evm.ChainConfig().Bor.CalculateBurntContract(st.evm.Context.BlockNumber.Uint64()))
burnAmount = new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), st.evm.Context.BaseFee)

if !st.noFeeBurnAndTip {
st.state.AddBalance(burntContractAddress, burnAmount)
Expand Down

0 comments on commit a801b75

Please sign in to comment.