eth_getFilterLogs, eth_getLogs implementation (#248)

This commit is contained in:
noot
2020-04-13 15:18:50 -04:00
committed by GitHub
parent 9b8dd598bb
commit 199484fc2e
10 changed files with 506 additions and 95 deletions
+5 -5
View File
@@ -43,8 +43,7 @@ func HandleMsgEthereumTx(ctx sdk.Context, k Keeper, msg types.MsgEthereumTx) sdk
return sdk.ResultFromError(err)
}
txHash := tmtypes.Tx(ctx.TxBytes()).Hash()
ethHash := common.BytesToHash(txHash)
txHash := msg.Hash()
st := types.StateTransition{
Sender: sender,
@@ -56,11 +55,12 @@ func HandleMsgEthereumTx(ctx sdk.Context, k Keeper, msg types.MsgEthereumTx) sdk
Payload: msg.Data.Payload,
Csdb: k.CommitStateDB.WithContext(ctx),
ChainID: intChainID,
THash: &ethHash,
THash: &txHash,
Simulate: ctx.IsCheckTx(),
}
// Prepare db for logs
k.CommitStateDB.Prepare(ethHash, common.Hash{}, k.TxCount)
// TODO: block hash
k.CommitStateDB.Prepare(txHash, txHash, k.TxCount)
k.TxCount++
// TODO: move to keeper
@@ -73,7 +73,7 @@ func HandleMsgEthereumTx(ctx sdk.Context, k Keeper, msg types.MsgEthereumTx) sdk
k.Bloom.Or(k.Bloom, returnData.Bloom)
// update transaction logs in KVStore
err = k.SetTransactionLogs(ctx, returnData.Logs, txHash)
err = k.SetTransactionLogs(ctx, returnData.Logs, txHash[:])
if err != nil {
return sdk.ResultFromError(err)
}
+7 -1
View File
@@ -40,7 +40,6 @@ type ReturnData struct {
// TODO: update godoc, it doesn't explain what it does in depth.
func (st StateTransition) TransitionCSDB(ctx sdk.Context) (*ReturnData, error) {
returnData := new(ReturnData)
contractCreation := st.Recipient == nil
cost, err := core.IntrinsicGas(st.Payload, contractCreation, true)
@@ -123,11 +122,13 @@ func (st StateTransition) TransitionCSDB(ctx sdk.Context) (*ReturnData, error) {
bloomInt := big.NewInt(0)
var bloomFilter ethtypes.Bloom
var logs []*ethtypes.Log
if st.THash != nil && !st.Simulate {
logs, err = csdb.GetLogs(*st.THash)
if err != nil {
return nil, err
}
bloomInt = ethtypes.LogsBloom(logs)
bloomFilter = ethtypes.BytesToBloom(bloomInt.Bytes())
}
@@ -171,6 +172,11 @@ func (st StateTransition) TransitionCSDB(ctx sdk.Context) (*ReturnData, error) {
// Out of gas check does not need to be done here since it is done within the EVM execution
ctx.WithGasMeter(currentGasMeter).GasMeter().ConsumeGas(gasConsumed, "EVM execution consumption")
err = st.Csdb.SetLogs(*st.THash, logs)
if err != nil {
return nil, err
}
returnData.Logs = logs
returnData.Bloom = bloomInt
returnData.Result = &sdk.Result{Data: resultData, GasUsed: gasConsumed}
+13 -1
View File
@@ -157,6 +157,18 @@ func (csdb *CommitStateDB) SetCode(addr ethcmn.Address, code []byte) {
}
}
// SetLogs sets the logs for a transaction in the KVStore.
func (csdb *CommitStateDB) SetLogs(hash ethcmn.Hash, logs []*ethtypes.Log) error {
store := csdb.ctx.KVStore(csdb.storeKey)
enc, err := EncodeLogs(logs)
if err != nil {
return err
}
store.Set(LogsKey(hash[:]), enc)
return nil
}
// AddLog adds a new log to the state and sets the log metadata from the state.
func (csdb *CommitStateDB) AddLog(log *ethtypes.Log) {
csdb.journal.append(addLogChange{txhash: csdb.thash})
@@ -288,7 +300,7 @@ func (csdb *CommitStateDB) GetCommittedState(addr ethcmn.Address, hash ethcmn.Ha
return ethcmn.Hash{}
}
// GetLogs returns the current logs for a given hash in the state.
// GetLogs returns the current logs for a given transaction hash from the KVStore.
func (csdb *CommitStateDB) GetLogs(hash ethcmn.Hash) ([]*ethtypes.Log, error) {
if csdb.logs[hash] != nil {
return csdb.logs[hash], nil