Skip to content

Commit

Permalink
Merge pull request #10358 from filecoin-project/asr/fix-get-actor
Browse files Browse the repository at this point in the history
fix: EthGetBalance: lookup balance at correct state
  • Loading branch information
arajasek authored Feb 27, 2023
2 parents 38f4a81 + 17e680e commit 4accfac
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
38 changes: 38 additions & 0 deletions itests/eth_balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package itests

import (
"context"
"strconv"
"testing"
"time"

Expand All @@ -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"
Expand Down Expand Up @@ -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())
}
7 changes: 6 additions & 1 deletion node/impl/full/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4accfac

Please sign in to comment.