fix: EthGetBalance: lookup balance at correct state

This commit is contained in:
Aayush 2023-02-27 13:53:36 -05:00
parent 9fba14b6b1
commit 17e680ea3c
2 changed files with 44 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package itests
import ( import (
"context" "context"
"strconv"
"testing" "testing"
"time" "time"
@ -11,6 +12,7 @@ import (
"github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big" "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"
"github.com/filecoin-project/lotus/chain/types/ethtypes" "github.com/filecoin-project/lotus/chain/types/ethtypes"
"github.com/filecoin-project/lotus/itests/kit" "github.com/filecoin-project/lotus/itests/kit"
@ -95,3 +97,39 @@ func TestEthGetBalanceBuiltinActor(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, ethtypes.EthBigInt{Int: big.NewInt(10).Int}, ebal) 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) 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) { if xerrors.Is(err, types.ErrActorNotFound) {
return ethtypes.EthBigIntZero, nil return ethtypes.EthBigIntZero, nil
} else if err != nil { } else if err != nil {