Skip to content

Commit

Permalink
Revert GetBlockReceipts interface and some minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
trinhdn2 committed Jul 6, 2023
1 parent 938ab4b commit a8e89d9
Show file tree
Hide file tree
Showing 20 changed files with 102 additions and 85 deletions.
4 changes: 2 additions & 2 deletions accounts/abi/bind/backends/simulated.go
Expand Up @@ -569,8 +569,8 @@ func (fb *filterBackend) HeaderByNumber(ctx context.Context, block rpc.BlockNumb
return fb.bc.GetHeaderByNumber(uint64(block.Int64())), nil
}

func (fb *filterBackend) GetReceipts(ctx context.Context, hash common.Hash, config *params.ChainConfig) (types.Receipts, error) {
return core.GetBlockReceipts(fb.db, hash, core.GetBlockNumber(fb.db, hash), config), nil
func (fb *filterBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
return core.GetBlockReceipts(fb.db, hash, core.GetBlockNumber(fb.db, hash), fb.ChainConfig()), nil
}

func (fb *filterBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
Expand Down
3 changes: 1 addition & 2 deletions cmd/tomo/bugcmd.go
Expand Up @@ -105,5 +105,4 @@ const header = `Please answer these questions before submitting your issue. Than
#### What did you see instead?
#### System details
`
#### System details`
25 changes: 9 additions & 16 deletions console/console_test.go
Expand Up @@ -19,20 +19,19 @@ package console
import (
"bytes"
"errors"
"github.com/tomochain/tomochain/tomox"
"github.com/tomochain/tomochain/tomoxlending"
"io/ioutil"
"os"
"strings"
"testing"
"time"

"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/consensus/ethash"
"github.com/tomochain/tomochain/console/prompt"
"github.com/tomochain/tomochain/core"
"github.com/tomochain/tomochain/eth"
"github.com/tomochain/tomochain/internal/jsre"
"github.com/tomochain/tomochain/node"
"github.com/tomochain/tomochain/tomox"
"github.com/tomochain/tomochain/tomoxlending"
)

const (
Expand Down Expand Up @@ -67,10 +66,10 @@ func (p *hookedPrompter) PromptPassword(prompt string) (string, error) {
func (p *hookedPrompter) PromptConfirm(prompt string) (bool, error) {
return false, errors.New("not implemented")
}
func (p *hookedPrompter) SetHistory(history []string) {}
func (p *hookedPrompter) AppendHistory(command string) {}
func (p *hookedPrompter) ClearHistory() {}
func (p *hookedPrompter) SetWordCompleter(completer WordCompleter) {}
func (p *hookedPrompter) SetHistory(history []string) {}
func (p *hookedPrompter) AppendHistory(command string) {}
func (p *hookedPrompter) ClearHistory() {}
func (p *hookedPrompter) SetWordCompleter(completer prompt.WordCompleter) {}

// tester is a console test environment for the console tests to operate on.
type tester struct {
Expand All @@ -86,10 +85,7 @@ type tester struct {
// Please ensure you call Close() on the returned tester to avoid leaks.
func newTester(t *testing.T, confOverride func(*eth.Config)) *tester {
// Create a temporary storage for the node keys and initialize it
workspace, err := ioutil.TempDir("", "console-tester-")
if err != nil {
t.Fatalf("failed to create temporary keystore: %v", err)
}
workspace := t.TempDir()

// Create a networkless protocol stack and start an Ethereum service within
stack, err := node.New(&node.Config{DataDir: workspace, UseLightweightKDF: true, Name: testInstance})
Expand All @@ -99,9 +95,6 @@ func newTester(t *testing.T, confOverride func(*eth.Config)) *tester {
ethConf := &eth.Config{
Genesis: core.DeveloperGenesisBlock(15, common.Address{}),
Etherbase: common.HexToAddress(testAddress),
Ethash: ethash.Config{
PowMode: ethash.ModeTest,
},
}
if confOverride != nil {
confOverride(ethConf)
Expand Down Expand Up @@ -262,7 +255,7 @@ func TestPrettyError(t *testing.T) {
defer tester.Close(t)
tester.console.Evaluate("throw 'hello'")

want := jsre.ErrorColor("hello") + "\n"
want := jsre.ErrorColor("hello") + "\n\tat <eval>:1:1(1)\n\n"
if output := tester.output.String(); output != want {
t.Fatalf("pretty error mismatch: have %s, want %s", output, want)
}
Expand Down
2 changes: 1 addition & 1 deletion core/bench_test.go
Expand Up @@ -294,7 +294,7 @@ func benchReadChain(b *testing.B, full bool, count uint64) {
if full {
hash := header.Hash()
GetBody(db, hash, n)
GetBlockReceipts(db, hash, n)
GetBlockReceipts(db, hash, n, params.TestChainConfig)
}
}

Expand Down
10 changes: 5 additions & 5 deletions core/blockchain_test.go
Expand Up @@ -18,7 +18,6 @@ package core

import (
"fmt"
"github.com/tomochain/tomochain/core/rawdb"
"math/big"
"math/rand"
"sync"
Expand All @@ -27,6 +26,7 @@ import (

"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/consensus/ethash"
"github.com/tomochain/tomochain/core/rawdb"
"github.com/tomochain/tomochain/core/state"
"github.com/tomochain/tomochain/core/types"
"github.com/tomochain/tomochain/core/vm"
Expand Down Expand Up @@ -622,7 +622,7 @@ func TestFastVsFullChains(t *testing.T) {
} else if types.CalcUncleHash(fblock.Uncles()) != types.CalcUncleHash(ablock.Uncles()) {
t.Errorf("block #%d [%x]: uncles mismatch: have %v, want %v", num, hash, fblock.Uncles(), ablock.Uncles())
}
if freceipts, areceipts := GetBlockReceipts(fastDb, hash, GetBlockNumber(fastDb, hash)), GetBlockReceipts(archiveDb, hash, GetBlockNumber(archiveDb, hash)); types.DeriveSha(freceipts) != types.DeriveSha(areceipts) {
if freceipts, areceipts := GetBlockReceipts(fastDb, hash, GetBlockNumber(fastDb, hash), fast.Config()), GetBlockReceipts(archiveDb, hash, GetBlockNumber(archiveDb, hash), fast.Config()); types.DeriveSha(freceipts) != types.DeriveSha(areceipts) {
t.Errorf("block #%d [%x]: receipts mismatch: have %v, want %v", num, hash, freceipts, areceipts)
}
}
Expand Down Expand Up @@ -807,7 +807,7 @@ func TestChainTxReorgs(t *testing.T) {
if txn, _, _, _ := GetTransaction(db, tx.Hash()); txn != nil {
t.Errorf("drop %d: tx %v found while shouldn't have been", i, txn)
}
if rcpt, _, _, _ := GetReceipt(db, tx.Hash()); rcpt != nil {
if rcpt, _, _, _ := GetReceipt(db, tx.Hash(), blockchain.Config()); rcpt != nil {
t.Errorf("drop %d: receipt %v found while shouldn't have been", i, rcpt)
}
}
Expand All @@ -816,7 +816,7 @@ func TestChainTxReorgs(t *testing.T) {
if txn, _, _, _ := GetTransaction(db, tx.Hash()); txn == nil {
t.Errorf("add %d: expected tx to be found", i)
}
if rcpt, _, _, _ := GetReceipt(db, tx.Hash()); rcpt == nil {
if rcpt, _, _, _ := GetReceipt(db, tx.Hash(), blockchain.Config()); rcpt == nil {
t.Errorf("add %d: expected receipt to be found", i)
}
}
Expand All @@ -825,7 +825,7 @@ func TestChainTxReorgs(t *testing.T) {
if txn, _, _, _ := GetTransaction(db, tx.Hash()); txn == nil {
t.Errorf("share %d: expected tx to be found", i)
}
if rcpt, _, _, _ := GetReceipt(db, tx.Hash()); rcpt == nil {
if rcpt, _, _, _ := GetReceipt(db, tx.Hash(), blockchain.Config()); rcpt == nil {
t.Errorf("share %d: expected receipt to be found", i)
}
}
Expand Down
9 changes: 5 additions & 4 deletions core/database_util_test.go
Expand Up @@ -18,13 +18,14 @@ package core

import (
"bytes"
"github.com/tomochain/tomochain/core/rawdb"
"math/big"
"testing"

"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/core/rawdb"
"github.com/tomochain/tomochain/core/types"
"github.com/tomochain/tomochain/crypto/sha3"
"github.com/tomochain/tomochain/params"
"github.com/tomochain/tomochain/rlp"
)

Expand Down Expand Up @@ -361,14 +362,14 @@ func TestBlockReceiptStorage(t *testing.T) {

// Check that no receipt entries are in a pristine database
hash := common.BytesToHash([]byte{0x03, 0x14})
if rs := GetBlockReceipts(db, hash, 0); len(rs) != 0 {
if rs := GetBlockReceipts(db, hash, 0, params.TestChainConfig); len(rs) != 0 {
t.Fatalf("non existent receipts returned: %v", rs)
}
// Insert the receipt slice into the database and check presence
if err := WriteBlockReceipts(db, hash, 0, receipts); err != nil {
t.Fatalf("failed to write block receipts: %v", err)
}
if rs := GetBlockReceipts(db, hash, 0); len(rs) == 0 {
if rs := GetBlockReceipts(db, hash, 0, params.TestChainConfig); len(rs) == 0 {
t.Fatalf("no receipts returned")
} else {
for i := 0; i < len(receipts); i++ {
Expand All @@ -382,7 +383,7 @@ func TestBlockReceiptStorage(t *testing.T) {
}
// Delete the receipt slice and check purge
DeleteBlockReceipts(db, hash, 0)
if rs := GetBlockReceipts(db, hash, 0); len(rs) != 0 {
if rs := GetBlockReceipts(db, hash, 0, params.TestChainConfig); len(rs) != 0 {
t.Fatalf("deleted receipts returned: %v", rs)
}
}
19 changes: 14 additions & 5 deletions core/genesis.go
Expand Up @@ -22,13 +22,13 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/tomochain/tomochain/core/rawdb"
"math/big"
"strings"

"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/common/hexutil"
"github.com/tomochain/tomochain/common/math"
"github.com/tomochain/tomochain/core/rawdb"
"github.com/tomochain/tomochain/core/state"
"github.com/tomochain/tomochain/core/types"
"github.com/tomochain/tomochain/ethdb"
Expand Down Expand Up @@ -60,6 +60,7 @@ type Genesis struct {
Number uint64 `json:"number"`
GasUsed uint64 `json:"gasUsed"`
ParentHash common.Hash `json:"parentHash"`
BaseFee *big.Int `json:"baseFeePerGas"`
}

// GenesisAlloc specifies the initial state that is part of the genesis block.
Expand Down Expand Up @@ -140,10 +141,10 @@ func (e *GenesisMismatchError) Error() string {
// SetupGenesisBlock writes or updates the genesis block in db.
// The block that will be used is:
//
// genesis == nil genesis != nil
// +------------------------------------------
// db has no genesis | main-net default | genesis
// db has genesis | from DB | genesis (if compatible)
// genesis == nil genesis != nil
// +------------------------------------------
// db has no genesis | main-net default | genesis
// db has genesis | from DB | genesis (if compatible)
//
// The stored chain configuration will be updated if it is compatible (i.e. does not
// specify a fork block below the local head block). In case of a conflict, the
Expand Down Expand Up @@ -255,6 +256,13 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
if g.Difficulty == nil {
head.Difficulty = params.GenesisDifficulty
}
if g.Config != nil && g.Config.IsLondon(common.Big0) {
if g.BaseFee != nil {
head.BaseFee = g.BaseFee
} else {
head.BaseFee = new(big.Int).SetUint64(params.InitialBaseFee)
}
}
statedb.Commit(false)
statedb.Database().TrieDB().Commit(root, true)

Expand Down Expand Up @@ -358,6 +366,7 @@ func DeveloperGenesisBlock(period uint64, faucet common.Address) *Genesis {
Config: &config,
ExtraData: append(append(make([]byte, 32), faucet[:]...), make([]byte, 65)...),
GasLimit: 6283185,
BaseFee: big.NewInt(params.InitialBaseFee),
Difficulty: big.NewInt(1),
Alloc: map[common.Address]GenesisAccount{
common.BytesToAddress([]byte{1}): {Balance: big.NewInt(1)}, // ECRecover
Expand Down
3 changes: 2 additions & 1 deletion core/types/transaction.go
Expand Up @@ -828,7 +828,8 @@ func NewTransactionsByPriceAndNonce(signer Signer, txs map[common.Address]Transa
}
specialTxs := Transactions{}
for _, accTxs := range txs {
if len(accTxs) == 0 {
// check because sometimes the map value can be null
if accTxs == nil || len(accTxs) == 0 {
continue
}
from, _ := Sender(signer, accTxs[0])
Expand Down
4 changes: 2 additions & 2 deletions eth/api_backend.go
Expand Up @@ -160,8 +160,8 @@ func (b *EthApiBackend) GetBlock(ctx context.Context, blockHash common.Hash) (*t
return b.eth.blockchain.GetBlockByHash(blockHash), nil
}

func (b *EthApiBackend) GetReceipts(ctx context.Context, blockHash common.Hash, config *params.ChainConfig) (types.Receipts, error) {
return core.GetBlockReceipts(b.eth.chainDb, blockHash, core.GetBlockNumber(b.eth.chainDb, blockHash), config), nil
func (b *EthApiBackend) GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error) {
return core.GetBlockReceipts(b.eth.chainDb, blockHash, core.GetBlockNumber(b.eth.chainDb, blockHash), b.ChainConfig()), nil
}

func (b *EthApiBackend) GetLogs(ctx context.Context, blockHash common.Hash) ([][]*types.Log, error) {
Expand Down
8 changes: 6 additions & 2 deletions eth/config.go
Expand Up @@ -52,8 +52,12 @@ var DefaultConfig = Config{

TxPool: core.DefaultTxPoolConfig,
GPO: gasprice.Config{
Blocks: 20,
Percentile: 60,
Blocks: 20,
Percentile: 60,
MaxHeaderHistory: 1024,
MaxBlockHistory: 1024,
MaxPrice: gasprice.DefaultMaxPrice,
IgnorePrice: gasprice.DefaultIgnorePrice,
},
RPCGasCap: 50000000,
RPCEVMTimeout: 5 * time.Second,
Expand Down
4 changes: 2 additions & 2 deletions eth/filters/filter.go
Expand Up @@ -34,7 +34,7 @@ type Backend interface {
ChainDb() ethdb.Database
EventMux() *event.TypeMux
HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error)
GetReceipts(ctx context.Context, blockHash common.Hash, config *params.ChainConfig) (types.Receipts, error)
GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error)
GetLogs(ctx context.Context, blockHash common.Hash) ([][]*types.Log, error)
ChainConfig() *params.ChainConfig

Expand Down Expand Up @@ -216,7 +216,7 @@ func (f *Filter) checkMatches(ctx context.Context, header *types.Header) (logs [
if len(logs) > 0 {
// We have matching logs, check if we need to resolve full logs via the light client
if logs[0].TxHash == (common.Hash{}) {
receipts, err := f.backend.GetReceipts(ctx, header.Hash(), f.backend.ChainConfig())
receipts, err := f.backend.GetReceipts(ctx, header.Hash())
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion eth/filters/filter_system.go
Expand Up @@ -390,7 +390,7 @@ func (es *EventSystem) lightFilterLogs(header *types.Header, addresses []common.
logs := filterLogs(unfiltered, nil, nil, addresses, topics)
if len(logs) > 0 && logs[0].TxHash == (common.Hash{}) {
// We have matching but non-derived logs
receipts, err := es.backend.GetReceipts(ctx, header.Hash(), es.backend.ChainConfig())
receipts, err := es.backend.GetReceipts(ctx, header.Hash())
if err != nil {
return nil
}
Expand Down
10 changes: 7 additions & 3 deletions eth/filters/filter_system_test.go
Expand Up @@ -19,7 +19,6 @@ package filters
import (
"context"
"fmt"
"github.com/tomochain/tomochain/core/rawdb"
"math/big"
"math/rand"
"reflect"
Expand All @@ -31,6 +30,7 @@ import (
"github.com/tomochain/tomochain/consensus/ethash"
"github.com/tomochain/tomochain/core"
"github.com/tomochain/tomochain/core/bloombits"
"github.com/tomochain/tomochain/core/rawdb"
"github.com/tomochain/tomochain/core/types"
"github.com/tomochain/tomochain/ethdb"
"github.com/tomochain/tomochain/event"
Expand All @@ -48,6 +48,10 @@ type testBackend struct {
chainFeed *event.Feed
}

func (b *testBackend) ChainConfig() *params.ChainConfig {
return params.TestChainConfig
}

func (b *testBackend) ChainDb() ethdb.Database {
return b.db
}
Expand All @@ -71,12 +75,12 @@ func (b *testBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumbe

func (b *testBackend) GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error) {
number := core.GetBlockNumber(b.db, blockHash)
return core.GetBlockReceipts(b.db, blockHash, number), nil
return core.GetBlockReceipts(b.db, blockHash, number, b.ChainConfig()), nil
}

func (b *testBackend) GetLogs(ctx context.Context, blockHash common.Hash) ([][]*types.Log, error) {
number := core.GetBlockNumber(b.db, blockHash)
receipts := core.GetBlockReceipts(b.db, blockHash, number)
receipts := core.GetBlockReceipts(b.db, blockHash, number, b.ChainConfig())

logs := make([][]*types.Log, len(receipts))
for i, receipt := range receipts {
Expand Down
2 changes: 1 addition & 1 deletion internal/ethapi/api.go
Expand Up @@ -1849,7 +1849,7 @@ func (s *PublicTransactionPoolAPI) GetTransactionReceipt(ctx context.Context, ha
if err != nil {
return nil, err
}
receipts, err := s.b.GetReceipts(ctx, blockHash, s.b.ChainConfig())
receipts, err := s.b.GetReceipts(ctx, blockHash)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/ethapi/backend.go
Expand Up @@ -66,7 +66,7 @@ type Backend interface {
StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error)
StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error)
GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error)
GetReceipts(ctx context.Context, blockHash common.Hash, config *params.ChainConfig) (types.Receipts, error)
GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error)
GetTd(blockHash common.Hash) *big.Int
GetEVM(ctx context.Context, msg *core.Message, state *state.StateDB, tomoxState *tradingstate.TradingStateDB, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error)
SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
Expand Down
4 changes: 2 additions & 2 deletions les/api_backend.go
Expand Up @@ -134,8 +134,8 @@ func (b *LesApiBackend) GetBlock(ctx context.Context, blockHash common.Hash) (*t
return b.eth.blockchain.GetBlockByHash(ctx, blockHash)
}

func (b *LesApiBackend) GetReceipts(ctx context.Context, blockHash common.Hash, config *params.ChainConfig) (types.Receipts, error) {
return light.GetBlockReceipts(ctx, b.eth.odr, blockHash, core.GetBlockNumber(b.eth.chainDb, blockHash), config)
func (b *LesApiBackend) GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error) {
return light.GetBlockReceipts(ctx, b.eth.odr, blockHash, core.GetBlockNumber(b.eth.chainDb, blockHash), b.ChainConfig())
}

func (b *LesApiBackend) GetLogs(ctx context.Context, blockHash common.Hash) ([][]*types.Log, error) {
Expand Down

0 comments on commit a8e89d9

Please sign in to comment.