Skip to content

Commit

Permalink
Post review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz-sekara committed Sep 28, 2023
1 parent 1d71390 commit df4b28d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 75 deletions.
48 changes: 22 additions & 26 deletions core/chains/evm/client/simulated_backend_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rpc"

Expand All @@ -26,12 +25,6 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/utils"
)

var blockByTag = map[string]func(*core.BlockChain) *types.Header{
rpc.SafeBlockNumber.String(): func(chain *core.BlockChain) *types.Header { return chain.CurrentSafeBlock() },
rpc.LatestBlockNumber.String(): func(chain *core.BlockChain) *types.Header { return chain.CurrentHeader() },
rpc.FinalizedBlockNumber.String(): func(chain *core.BlockChain) *types.Header { return chain.CurrentFinalBlock() },
}

// SimulatedBackendClient is an Client implementation using a simulated
// blockchain backend. Note that not all RPC methods are implemented here.
type SimulatedBackendClient struct {
Expand Down Expand Up @@ -560,31 +553,17 @@ func (c *SimulatedBackendClient) BatchCallContext(ctx context.Context, b []rpc.B
if !is {
return fmt.Errorf("SimulatedBackendClient expected second arg to be a boolean for eth_getBlockByNumber, got: %T", elem.Args[1])
}

var header *types.Header
var err error
if blockFn, contains := blockByTag[blockNumOrTag]; contains {
header, err = blockFn(c.b.Blockchain()), nil
} else {
blockNum, ok := new(big.Int).SetString(blockNumOrTag, 0)
if !ok {
return fmt.Errorf("error while converting block number string: %s to big.Int ", blockNumOrTag)
}
header, err = c.b.HeaderByNumber(ctx, blockNum)
}
header, err := c.fetchHeader(ctx, blockNumOrTag)
if err != nil {
return err
}

if res, ok := elem.Result.(*evmtypes.Head); ok {
switch res := elem.Result.(type) {
case *evmtypes.Head:
case *evmtypes.Block:
res.Number = header.Number.Int64()
res.Hash = header.Hash()
res.Timestamp = time.Unix(int64(header.Time), 0).UTC()
} else if res, ok := elem.Result.(*evmtypes.Block); ok {
res.Number = header.Number.Int64()
res.Hash = header.Hash()
res.Timestamp = time.Unix(int64(header.Time), 0)
} else {
default:
return fmt.Errorf("SimulatedBackendClient Unexpected Type %T", elem.Result)
}
b[i].Error = err
Expand Down Expand Up @@ -727,3 +706,20 @@ func toCallMsg(params map[string]interface{}) ethereum.CallMsg {
func (c *SimulatedBackendClient) IsL2() bool {
return false
}

func (c *SimulatedBackendClient) fetchHeader(ctx context.Context, blockNumOrTag string) (*types.Header, error) {
switch blockNumOrTag {
case rpc.SafeBlockNumber.String():
return c.b.Blockchain().CurrentSafeBlock(), nil
case rpc.LatestBlockNumber.String():
return c.b.Blockchain().CurrentBlock(), nil
case rpc.FinalizedBlockNumber.String():
return c.b.Blockchain().CurrentFinalBlock(), nil
default:
blockNum, ok := new(big.Int).SetString(blockNumOrTag, 0)
if !ok {
return nil, fmt.Errorf("error while converting block number string: %s to big.Int ", blockNumOrTag)
}
return c.b.HeaderByNumber(ctx, blockNum)
}
}
24 changes: 12 additions & 12 deletions core/chains/evm/logpoller/log_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -982,12 +982,12 @@ func (lp *logPoller) LogsWithSigs(start, end int64, eventSigs []common.Hash, add
}

func (lp *logPoller) LogsCreatedAfter(eventSig common.Hash, address common.Address, after time.Time, confs Confirmations, qopts ...pg.QOpt) ([]Log, error) {
return lp.orm.SelectLogsCreatedAfter(address, eventSig, after, lp.confirmations(confs), qopts...)
return lp.orm.SelectLogsCreatedAfter(address, eventSig, after, lp.confirmationsToInt(confs), qopts...)
}

// IndexedLogs finds all the logs that have a topic value in topicValues at index topicIndex.
func (lp *logPoller) IndexedLogs(eventSig common.Hash, address common.Address, topicIndex int, topicValues []common.Hash, confs Confirmations, qopts ...pg.QOpt) ([]Log, error) {
return lp.orm.SelectIndexedLogs(address, eventSig, topicIndex, topicValues, lp.confirmations(confs), qopts...)
return lp.orm.SelectIndexedLogs(address, eventSig, topicIndex, topicValues, lp.confirmationsToInt(confs), qopts...)
}

// IndexedLogsByBlockRange finds all the logs that have a topic value in topicValues at index topicIndex within the block range
Expand All @@ -996,7 +996,7 @@ func (lp *logPoller) IndexedLogsByBlockRange(start, end int64, eventSig common.H
}

func (lp *logPoller) IndexedLogsCreatedAfter(eventSig common.Hash, address common.Address, topicIndex int, topicValues []common.Hash, after time.Time, confs Confirmations, qopts ...pg.QOpt) ([]Log, error) {
return lp.orm.SelectIndexedLogsCreatedAfter(address, eventSig, topicIndex, topicValues, after, lp.confirmations(confs), qopts...)
return lp.orm.SelectIndexedLogsCreatedAfter(address, eventSig, topicIndex, topicValues, after, lp.confirmationsToInt(confs), qopts...)
}

func (lp *logPoller) IndexedLogsByTxHash(eventSig common.Hash, txHash common.Hash, qopts ...pg.QOpt) ([]Log, error) {
Expand All @@ -1005,22 +1005,22 @@ func (lp *logPoller) IndexedLogsByTxHash(eventSig common.Hash, txHash common.Has

// LogsDataWordGreaterThan note index is 0 based.
func (lp *logPoller) LogsDataWordGreaterThan(eventSig common.Hash, address common.Address, wordIndex int, wordValueMin common.Hash, confs Confirmations, qopts ...pg.QOpt) ([]Log, error) {
return lp.orm.SelectDataWordGreaterThan(address, eventSig, wordIndex, wordValueMin, lp.confirmations(confs), qopts...)
return lp.orm.SelectDataWordGreaterThan(address, eventSig, wordIndex, wordValueMin, lp.confirmationsToInt(confs), qopts...)
}

// LogsDataWordRange note index is 0 based.
func (lp *logPoller) LogsDataWordRange(eventSig common.Hash, address common.Address, wordIndex int, wordValueMin, wordValueMax common.Hash, confs Confirmations, qopts ...pg.QOpt) ([]Log, error) {
return lp.orm.SelectDataWordRange(address, eventSig, wordIndex, wordValueMin, wordValueMax, lp.confirmations(confs), qopts...)
return lp.orm.SelectDataWordRange(address, eventSig, wordIndex, wordValueMin, wordValueMax, lp.confirmationsToInt(confs), qopts...)
}

// IndexedLogsTopicGreaterThan finds all the logs that have a topic value greater than topicValueMin at index topicIndex.
// Only works for integer topics.
func (lp *logPoller) IndexedLogsTopicGreaterThan(eventSig common.Hash, address common.Address, topicIndex int, topicValueMin common.Hash, confs Confirmations, qopts ...pg.QOpt) ([]Log, error) {
return lp.orm.SelectIndexLogsTopicGreaterThan(address, eventSig, topicIndex, topicValueMin, lp.confirmations(confs), qopts...)
return lp.orm.SelectIndexLogsTopicGreaterThan(address, eventSig, topicIndex, topicValueMin, lp.confirmationsToInt(confs), qopts...)
}

func (lp *logPoller) IndexedLogsTopicRange(eventSig common.Hash, address common.Address, topicIndex int, topicValueMin common.Hash, topicValueMax common.Hash, confs Confirmations, qopts ...pg.QOpt) ([]Log, error) {
return lp.orm.SelectIndexLogsTopicRange(address, eventSig, topicIndex, topicValueMin, topicValueMax, lp.confirmations(confs), qopts...)
return lp.orm.SelectIndexLogsTopicRange(address, eventSig, topicIndex, topicValueMin, topicValueMax, lp.confirmationsToInt(confs), qopts...)
}

// LatestBlock returns the latest block the log poller is on. It tracks blocks to be able
Expand All @@ -1040,15 +1040,15 @@ func (lp *logPoller) BlockByNumber(n int64, qopts ...pg.QOpt) (*LogPollerBlock,

// LatestLogByEventSigWithConfs finds the latest log that has confs number of blocks on top of the log.
func (lp *logPoller) LatestLogByEventSigWithConfs(eventSig common.Hash, address common.Address, confs Confirmations, qopts ...pg.QOpt) (*Log, error) {
return lp.orm.SelectLatestLogEventSigWithConfs(eventSig, address, lp.confirmations(confs), qopts...)
return lp.orm.SelectLatestLogEventSigWithConfs(eventSig, address, lp.confirmationsToInt(confs), qopts...)
}

func (lp *logPoller) LatestLogEventSigsAddrsWithConfs(fromBlock int64, eventSigs []common.Hash, addresses []common.Address, confs Confirmations, qopts ...pg.QOpt) ([]Log, error) {
return lp.orm.SelectLatestLogEventSigsAddrsWithConfs(fromBlock, addresses, eventSigs, lp.confirmations(confs), qopts...)
return lp.orm.SelectLatestLogEventSigsAddrsWithConfs(fromBlock, addresses, eventSigs, lp.confirmationsToInt(confs), qopts...)
}

func (lp *logPoller) LatestBlockByEventSigsAddrsWithConfs(fromBlock int64, eventSigs []common.Hash, addresses []common.Address, confs Confirmations, qopts ...pg.QOpt) (int64, error) {
return lp.orm.SelectLatestBlockNumberEventSigsAddrsWithConfs(fromBlock, eventSigs, addresses, lp.confirmations(confs), qopts...)
return lp.orm.SelectLatestBlockNumberEventSigsAddrsWithConfs(fromBlock, eventSigs, addresses, lp.confirmationsToInt(confs), qopts...)
}

// GetBlocksRange tries to get the specified block numbers from the log pollers
Expand Down Expand Up @@ -1204,7 +1204,7 @@ func (lp *logPoller) batchFetchBlocks(ctx context.Context, blocksRequested []str
// For example, query to retrieve unfulfilled requests by querying request log events without matching fulfillment log events.
// The order of events is not significant. Both logs must be inside the block range and have the minimum number of confirmations
func (lp *logPoller) IndexedLogsWithSigsExcluding(address common.Address, eventSigA, eventSigB common.Hash, topicIndex int, fromBlock, toBlock int64, confs Confirmations, qopts ...pg.QOpt) ([]Log, error) {
return lp.orm.SelectIndexedLogsWithSigsExcluding(eventSigA, eventSigB, topicIndex, address, fromBlock, toBlock, lp.confirmations(confs), qopts...)
return lp.orm.SelectIndexedLogsWithSigsExcluding(eventSigA, eventSigB, topicIndex, address, fromBlock, toBlock, lp.confirmationsToInt(confs), qopts...)
}

func EvmWord(i uint64) common.Hash {
Expand All @@ -1213,7 +1213,7 @@ func EvmWord(i uint64) common.Hash {
return common.BytesToHash(b)
}

func (lp *logPoller) confirmations(confs Confirmations) int {
func (lp *logPoller) confirmationsToInt(confs Confirmations) int {
if confs == Finalized {
return int(lp.getFinalityDepth())
}
Expand Down
2 changes: 1 addition & 1 deletion core/chains/evm/logpoller/log_poller_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ func Test_confirmations(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
lp := NewLogPoller(orm, ec, lggr, time.Hour, false, int64(finalityDepth), 3, 3, 20)
require.Equal(t, tt.wantConfs, lp.confirmations(tt.confs))
require.Equal(t, tt.wantConfs, lp.confirmationsToInt(tt.confs))
})
}
}
Expand Down
Loading

0 comments on commit df4b28d

Please sign in to comment.