Merge pull request #10358 from filecoin-project/asr/fix-get-actor

fix: EthGetBalance: lookup balance at correct state
This commit is contained in:
Aayush Rajasekaran 2023-02-27 15:38:19 -05:00 committed by GitHub
commit 4accfacbdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package itests
import (
"context"
"strconv"
"testing"
"time"
@ -11,6 +12,7 @@ import (
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/types/ethtypes"
"github.com/filecoin-project/lotus/itests/kit"
@ -95,3 +97,39 @@ func TestEthGetBalanceBuiltinActor(t *testing.T) {
require.NoError(t, err)
require.Equal(t, ethtypes.EthBigInt{Int: big.NewInt(10).Int}, ebal)
}
func TestEthBalanceCorrectLookup(t *testing.T) {
blockTime := 100 * time.Millisecond
client, _, ens := kit.EnsembleMinimal(t, kit.MockProofs(), kit.ThroughRPC())
ens.InterconnectAll().BeginMining(blockTime)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
_, ethAddr, filAddr := client.EVM().NewAccount()
val := int64(100)
smsg, err := client.MpoolPushMessage(ctx, &types.Message{
To: filAddr,
From: client.DefaultKey.Address,
Value: abi.NewTokenAmount(val),
}, nil)
require.NoError(t, err)
ml, err := client.StateWaitMsg(ctx, smsg.Cid(), 3, api.LookbackNoLimit, false)
require.NoError(t, err)
require.True(t, ml.Receipt.ExitCode.IsSuccess())
bal, err := client.EVM().EthGetBalance(ctx, ethAddr, strconv.FormatInt(int64(ml.Height-2), 10))
require.NoError(t, err)
require.Equal(t, int64(0), bal.Int64())
bal, err = client.EVM().EthGetBalance(ctx, ethAddr, strconv.FormatInt(int64(ml.Height-1), 10))
require.NoError(t, err)
require.Equal(t, val, bal.Int64())
bal, err = client.EVM().EthGetBalance(ctx, ethAddr, strconv.FormatInt(int64(ml.Height), 10))
require.NoError(t, err)
require.Equal(t, val, bal.Int64())
}

View File

@ -636,7 +636,12 @@ func (a *EthModule) EthGetBalance(ctx context.Context, address ethtypes.EthAddre
return ethtypes.EthBigInt{}, xerrors.Errorf("cannot parse block param: %s", blkParam)
}
actor, err := a.StateGetActor(ctx, filAddr, ts.Key())
st, _, err := a.StateManager.TipSetState(ctx, ts)
if err != nil {
return ethtypes.EthBigInt{}, xerrors.Errorf("failed to compute tipset state: %w", err)
}
actor, err := a.StateManager.LoadActorRaw(ctx, filAddr, st)
if xerrors.Is(err, types.ErrActorNotFound) {
return ethtypes.EthBigIntZero, nil
} else if err != nil {