Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 1 addition & 1 deletion accounts/abi/bind/backends/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func (b *BlockchainContractBackend) FilterLogs(ctx context.Context, query kaia.F
if !ok {
return nil, errors.New("BlockChainForCaller is not blockchain.BlockChain")
}
filter := filters.NewRangeFilter(&filterBackend{state.Database().TrieDB().DiskDB(), bc}, from, to, query.Addresses, query.Topics)
filter := filters.NewRangeFilter(&filterBackend{state.Database().TrieDB().DiskDB(), bc, nil}, from, to, query.Addresses, query.Topics)

logs, err := filter.Logs(ctx)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion accounts/abi/bind/backends/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func initBackendForFiltererTests(t *testing.T, bc *blockchain.BlockChain) *Block
mockBackend.EXPECT().SubscribeRemovedLogsEvent(any).DoAndReturn(subscribeRemovedLogsEvent).AnyTimes()
mockBackend.EXPECT().SubscribeChainEvent(any).DoAndReturn(subscribeChainEvent).AnyTimes()

f := filters.NewEventSystem(&event.TypeMux{}, mockBackend, false)
f := filters.NewEventSystem(&event.TypeMux{}, mockBackend)
c := NewBlockchainContractBackend(bc, nil, f)

return c
Expand Down
29 changes: 19 additions & 10 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ type SimulatedBackend struct {
database database.DBManager // In memory database to store our testing data
blockchain *blockchain.BlockChain // Kaia blockchain to handle the consensus

mu sync.Mutex
pendingBlock *types.Block // Currently pending block that will be imported on request
pendingState *state.StateDB // Currently pending state that will be the active on request
mu sync.Mutex
pendingBlock *types.Block // Currently pending block that will be imported on request
pendingState *state.StateDB // Currently pending state that will be the active on request
pendingReceipts types.Receipts // Currently receipts for the pending block

events *filters.EventSystem // Event system for filtering log events live

Expand All @@ -85,8 +86,8 @@ func NewSimulatedBackendWithDatabase(database database.DBManager, alloc blockcha
database: database,
blockchain: blockchain,
config: genesis.Config,
events: filters.NewEventSystem(new(event.TypeMux), &filterBackend{database, blockchain}, false),
}
backend.events = filters.NewEventSystem(new(event.TypeMux), &filterBackend{database, blockchain, backend})
backend.rollback()
return backend
}
Expand Down Expand Up @@ -132,11 +133,12 @@ func (b *SimulatedBackend) Rollback() {
}

func (b *SimulatedBackend) rollback() {
blocks, _ := blockchain.GenerateChain(b.config, b.blockchain.CurrentBlock(), gxhash.NewFaker(), b.database, 1, func(int, *blockchain.BlockGen) {})
blocks, receipts := blockchain.GenerateChain(b.config, b.blockchain.CurrentBlock(), gxhash.NewFaker(), b.database, 1, func(int, *blockchain.BlockGen) {})
stateDB, _ := b.blockchain.State()

b.pendingBlock = blocks[0]
b.pendingState, _ = state.New(b.pendingBlock.Root(), stateDB.Database(), nil, nil)
b.pendingReceipts = receipts[0]
}

// stateByBlockNumber retrieves a state by a given blocknumber.
Expand Down Expand Up @@ -531,7 +533,7 @@ func (b *SimulatedBackend) SendTransaction(_ context.Context, tx *types.Transact
}

// Include tx in chain.
blocks, _ := blockchain.GenerateChain(b.config, block, gxhash.NewFaker(), b.database, 1, func(number int, block *blockchain.BlockGen) {
blocks, receipts := blockchain.GenerateChain(b.config, block, gxhash.NewFaker(), b.database, 1, func(number int, block *blockchain.BlockGen) {
for _, tx := range b.pendingBlock.Transactions() {
block.AddTxWithChain(b.blockchain, tx)
}
Expand All @@ -541,6 +543,7 @@ func (b *SimulatedBackend) SendTransaction(_ context.Context, tx *types.Transact

b.pendingBlock = blocks[0]
b.pendingState, _ = state.New(b.pendingBlock.Root(), stateDB.Database(), nil, nil)
b.pendingReceipts = receipts[0]
return nil
}

Expand All @@ -559,7 +562,7 @@ func (b *SimulatedBackend) FilterLogs(ctx context.Context, query kaia.FilterQuer
to = query.ToBlock.Int64()
}
// Construct and execute the filter
filter := filters.NewRangeFilter(&filterBackend{b.database, b.blockchain}, from, to, query.Addresses, query.Topics)
filter := filters.NewRangeFilter(&filterBackend{b.database, b.blockchain, b}, from, to, query.Addresses, query.Topics)

// Run the filter and return all the logs
logs, err := filter.Logs(ctx)
Expand Down Expand Up @@ -654,13 +657,14 @@ func (b *SimulatedBackend) AdjustTime(adjustment time.Duration) error {
return errors.New("Could not adjust time on non-empty block")
}

blocks, _ := blockchain.GenerateChain(b.config, b.blockchain.CurrentBlock(), gxhash.NewFaker(), b.database, 1, func(number int, block *blockchain.BlockGen) {
blocks, receipts := blockchain.GenerateChain(b.config, b.blockchain.CurrentBlock(), gxhash.NewFaker(), b.database, 1, func(number int, block *blockchain.BlockGen) {
block.OffsetTime(int64(adjustment.Seconds()))
})
stateDB, _ := b.blockchain.State()

b.pendingBlock = blocks[0]
b.pendingState, _ = state.New(b.pendingBlock.Root(), stateDB.Database(), nil, nil)
b.pendingReceipts = receipts[0]

return nil
}
Expand All @@ -677,8 +681,9 @@ func (b *SimulatedBackend) PendingBlock() *types.Block {
// filterBackend implements filters.Backend to support filtering for logs without
// taking bloom-bits acceleration structures into account.
type filterBackend struct {
db database.DBManager
bc *blockchain.BlockChain
db database.DBManager
bc *blockchain.BlockChain
backend *SimulatedBackend
}

func (fb *filterBackend) ChainDB() database.DBManager { return fb.db }
Expand All @@ -695,6 +700,10 @@ func (fb *filterBackend) HeaderByNumber(_ context.Context, block rpc.BlockNumber
return fb.bc.GetHeaderByNumber(uint64(block.Int64())), nil
}

func (fb *filterBackend) Pending() (*types.Block, types.Receipts, *state.StateDB) {
return fb.backend.pendingBlock, fb.backend.pendingReceipts, fb.backend.pendingState.Copy()
}

func (fb *filterBackend) GetBlockReceipts(_ context.Context, hash common.Hash) types.Receipts {
return fb.bc.GetReceiptsByBlockHash(hash)
}
Expand Down
14 changes: 6 additions & 8 deletions api/api_ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,14 +707,12 @@ func (api *EthereumAPI) EstimateGas(ctx context.Context, args EthTransactionArgs

// GetBlockTransactionCountByNumber returns the number of transactions in the block with the given block number.
func (api *EthereumAPI) GetBlockTransactionCountByNumber(ctx context.Context, blockNr rpc.BlockNumber) *hexutil.Uint {
transactionCount, _ := api.publicTransactionPoolAPI.GetBlockTransactionCountByNumber(ctx, blockNr)
return transactionCount
return api.publicTransactionPoolAPI.GetBlockTransactionCountByNumber(ctx, blockNr)
}

// GetBlockTransactionCountByHash returns the number of transactions in the block with the given hash.
func (api *EthereumAPI) GetBlockTransactionCountByHash(ctx context.Context, blockHash common.Hash) *hexutil.Uint {
transactionCount, _ := api.publicTransactionPoolAPI.GetBlockTransactionCountByHash(ctx, blockHash)
return transactionCount
return api.publicTransactionPoolAPI.GetBlockTransactionCountByHash(ctx, blockHash)
}

// EthRPCTransaction represents a transaction that will serialize to the RPC representation of a transaction
Expand Down Expand Up @@ -968,8 +966,8 @@ func (api *EthereumAPI) GetTransactionByBlockHashAndIndex(ctx context.Context, b

// GetRawTransactionByBlockNumberAndIndex returns the bytes of the transaction for the given block number and index.
func (api *EthereumAPI) GetRawTransactionByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, index hexutil.Uint) hexutil.Bytes {
rawTx, err := api.publicTransactionPoolAPI.GetRawTransactionByBlockNumberAndIndex(ctx, blockNr, index)
if rawTx == nil || err != nil {
rawTx := api.publicTransactionPoolAPI.GetRawTransactionByBlockNumberAndIndex(ctx, blockNr, index)
if rawTx == nil {
return nil
}
if rawTx[0] == byte(types.EthereumTxTypeEnvelope) {
Expand All @@ -980,8 +978,8 @@ func (api *EthereumAPI) GetRawTransactionByBlockNumberAndIndex(ctx context.Conte

// GetRawTransactionByBlockHashAndIndex returns the bytes of the transaction for the given block hash and index.
func (api *EthereumAPI) GetRawTransactionByBlockHashAndIndex(ctx context.Context, blockHash common.Hash, index hexutil.Uint) hexutil.Bytes {
rawTx, err := api.publicTransactionPoolAPI.GetRawTransactionByBlockHashAndIndex(ctx, blockHash, index)
if rawTx == nil || err != nil {
rawTx := api.publicTransactionPoolAPI.GetRawTransactionByBlockHashAndIndex(ctx, blockHash, index)
if rawTx == nil {
return nil
}
if rawTx[0] == byte(types.EthereumTxTypeEnvelope) {
Expand Down
2 changes: 1 addition & 1 deletion api/api_public_klay.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type FeeHistoryResult struct {

// FeeHistory returns data relevant for fee estimation based on the specified range of blocks.
func (s *PublicKaiaAPI) FeeHistory(ctx context.Context, blockCount DecimalOrHex, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*FeeHistoryResult, error) {
oldest, reward, baseFee, gasUsed, err := s.b.FeeHistory(ctx, int(blockCount), lastBlock, rewardPercentiles)
oldest, reward, baseFee, gasUsed, err := s.b.FeeHistory(ctx, uint64(blockCount), lastBlock, rewardPercentiles)
if err != nil {
return nil, err
}
Expand Down
69 changes: 30 additions & 39 deletions api/api_public_transaction_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,59 +51,56 @@ func NewPublicTransactionPoolAPI(b Backend, nonceLock *AddrLocker) *PublicTransa
}

// GetBlockTransactionCountByNumber returns the number of transactions in the block with the given block number.
func (s *PublicTransactionPoolAPI) GetBlockTransactionCountByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*hexutil.Uint, error) {
block, err := s.b.BlockByNumber(ctx, blockNr)
if block != nil && err == nil {
func (s *PublicTransactionPoolAPI) GetBlockTransactionCountByNumber(ctx context.Context, blockNr rpc.BlockNumber) *hexutil.Uint {
block, _ := s.b.BlockByNumber(ctx, blockNr)
if block != nil {
n := hexutil.Uint(len(block.Transactions()))
return &n, err
return &n
}
return nil, err
return nil
}

// GetBlockTransactionCountByHash returns the number of transactions in the block with the given hash.
func (s *PublicTransactionPoolAPI) GetBlockTransactionCountByHash(ctx context.Context, blockHash common.Hash) (*hexutil.Uint, error) {
block, err := s.b.BlockByHash(ctx, blockHash)
if block != nil && err == nil {
func (s *PublicTransactionPoolAPI) GetBlockTransactionCountByHash(ctx context.Context, blockHash common.Hash) *hexutil.Uint {
block, _ := s.b.BlockByHash(ctx, blockHash)
if block != nil {
n := hexutil.Uint(len(block.Transactions()))
return &n, err
return &n
}
return nil, err
return nil
}

// GetTransactionByBlockNumberAndIndex returns the transaction for the given block number and index.
func (s *PublicTransactionPoolAPI) GetTransactionByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, index hexutil.Uint) (map[string]interface{}, error) {
block, err := s.b.BlockByNumber(ctx, blockNr)
if block != nil && err == nil {
return newRPCTransactionFromBlockIndex(block, uint64(index), s.b.ChainConfig()), nil
func (s *PublicTransactionPoolAPI) GetTransactionByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, index hexutil.Uint) map[string]interface{} {
block, _ := s.b.BlockByNumber(ctx, blockNr)
if block != nil {
return newRPCTransactionFromBlockIndex(block, uint64(index), s.b.ChainConfig())
}
return nil, err
return nil
}

// GetTransactionByBlockHashAndIndex returns the transaction for the given block hash and index.
func (s *PublicTransactionPoolAPI) GetTransactionByBlockHashAndIndex(ctx context.Context, blockHash common.Hash, index hexutil.Uint) (map[string]interface{}, error) {
block, err := s.b.BlockByHash(ctx, blockHash)
if block != nil && err == nil {
return newRPCTransactionFromBlockIndex(block, uint64(index), s.b.ChainConfig()), nil
func (s *PublicTransactionPoolAPI) GetTransactionByBlockHashAndIndex(ctx context.Context, blockHash common.Hash, index hexutil.Uint) map[string]interface{} {
if block, _ := s.b.BlockByHash(ctx, blockHash); block != nil {
return newRPCTransactionFromBlockIndex(block, uint64(index), s.b.ChainConfig())
}
return nil, err
return nil
}

// GetRawTransactionByBlockNumberAndIndex returns the bytes of the transaction for the given block number and index.
func (s *PublicTransactionPoolAPI) GetRawTransactionByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, index hexutil.Uint) (hexutil.Bytes, error) {
block, err := s.b.BlockByNumber(ctx, blockNr)
if block != nil && err == nil {
return newRPCRawTransactionFromBlockIndex(block, uint64(index)), nil
func (s *PublicTransactionPoolAPI) GetRawTransactionByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, index hexutil.Uint) hexutil.Bytes {
if block, _ := s.b.BlockByNumber(ctx, blockNr); block != nil {
return newRPCRawTransactionFromBlockIndex(block, uint64(index))
}
return nil, err
return nil
}

// GetRawTransactionByBlockHashAndIndex returns the bytes of the transaction for the given block hash and index.
func (s *PublicTransactionPoolAPI) GetRawTransactionByBlockHashAndIndex(ctx context.Context, blockHash common.Hash, index hexutil.Uint) (hexutil.Bytes, error) {
block, err := s.b.BlockByHash(ctx, blockHash)
if block != nil && err == nil {
return newRPCRawTransactionFromBlockIndex(block, uint64(index)), nil
func (s *PublicTransactionPoolAPI) GetRawTransactionByBlockHashAndIndex(ctx context.Context, blockHash common.Hash, index hexutil.Uint) hexutil.Bytes {
if block, _ := s.b.BlockByHash(ctx, blockHash); block != nil {
return newRPCRawTransactionFromBlockIndex(block, uint64(index))
}
return nil, err
return nil
}

// GetTransactionCount returns the number of transactions the given address has sent for the given block number or hash
Expand Down Expand Up @@ -576,18 +573,12 @@ func (s *PublicTransactionPoolAPI) RecoverFromTransaction(ctx context.Context, e
return common.Address{}, err
}

var bn uint64
if blockNumber == rpc.LatestBlockNumber || blockNumber == rpc.PendingBlockNumber {
bn = s.b.CurrentBlock().NumberU64()
} else {
bn = blockNumber.Uint64()
}

signer := types.MakeSigner(s.b.ChainConfig(), new(big.Int).SetUint64(bn))
state, _, err := s.b.StateAndHeaderByNumber(ctx, blockNumber)
state, header, err := s.b.StateAndHeaderByNumber(ctx, blockNumber)
if err != nil {
return common.Address{}, err
}
bn := header.Number.Uint64()
signer := types.MakeSigner(s.b.ChainConfig(), new(big.Int).SetUint64(bn))
_, err = tx.ValidateSender(signer, state, bn)
if err != nil {
return common.Address{}, err
Expand Down
3 changes: 2 additions & 1 deletion api/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type Backend interface {
RPCGasCap() *big.Int // global gas cap for eth/kaia_call/estimateGas/estimateComputationCost
RPCTxFeeCap() float64 // global tx fee cap in eth_signTransaction
Engine() consensus.Engine
FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error)
FeeHistory(ctx context.Context, blockCount uint64, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error)

// BlockChain API
SetHead(number uint64) error
Expand All @@ -72,6 +72,7 @@ type Backend interface {
BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error)
StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error)
StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error)
Pending() (*types.Block, types.Receipts, *state.StateDB)
GetBlockReceipts(ctx context.Context, blockHash common.Hash) types.Receipts
GetTxLookupInfoAndReceipt(ctx context.Context, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, *types.Receipt)
GetTxAndLookupInfo(hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64)
Expand Down
18 changes: 17 additions & 1 deletion api/mocks/backend_mock.go

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

2 changes: 1 addition & 1 deletion node/cn/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func (api *PublicDebugAPI) DumpBlock(ctx context.Context, blockNrOrHash rpc.Bloc
// If we're dumping the pending state, we need to request
// both the pending block as well as the pending state from
// the miner and operate on those
_, stateDb := api.cn.miner.Pending()
_, _, stateDb := api.cn.miner.Pending()
if stateDb == nil {
return state.Dump{}, fmt.Errorf("pending block is not prepared yet")
}
Expand Down
8 changes: 6 additions & 2 deletions node/cn/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,14 @@ func (b *CNAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rp
return nil, fmt.Errorf("invalid arguments; neither block nor hash specified")
}

func (b *CNAPIBackend) Pending() (*types.Block, types.Receipts, *state.StateDB) {
return b.cn.miner.Pending()
}

func (b *CNAPIBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
// Pending state is only known by the miner
if blockNr == rpc.PendingBlockNumber {
block, state := b.cn.miner.Pending()
block, _, state := b.cn.miner.Pending()
if block == nil || state == nil {
return nil, nil, fmt.Errorf("pending block is not prepared yet")
}
Expand Down Expand Up @@ -393,6 +397,6 @@ func (b *CNAPIBackend) StateAtTransaction(ctx context.Context, block *types.Bloc
return b.cn.stateAtTransaction(block, txIndex, reexec, base, readOnly, preferDisk)
}

func (b *CNAPIBackend) FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error) {
func (b *CNAPIBackend) FeeHistory(ctx context.Context, blockCount uint64, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error) {
return b.gpo.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles)
}
3 changes: 2 additions & 1 deletion node/cn/api_backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ func TestCNAPIBackend_BlockByNumberOrHash(t *testing.T) {
func TestCNAPIBackend_StateAndHeaderByNumber(t *testing.T) {
blockNum := uint64(123)
block := newBlock(int(blockNum))
var reciept types.Receipts

stateDB, err := state.New(common.Hash{}, state.NewDatabase(database.NewMemoryDBManager()), nil, nil)
if err != nil {
Expand All @@ -436,7 +437,7 @@ func TestCNAPIBackend_StateAndHeaderByNumber(t *testing.T) {
expectedHeader := block.Header()
{
mockCtrl, _, mockMiner, api := newCNAPIBackend(t)
mockMiner.EXPECT().Pending().Return(block, stateDB).Times(1)
mockMiner.EXPECT().Pending().Return(block, reciept, stateDB).Times(1)

returnedStateDB, header, err := api.StateAndHeaderByNumber(context.Background(), rpc.PendingBlockNumber)

Expand Down
Loading