From 439b4c6e2205e1edcc71d83e8e1f806eff56f7c4 Mon Sep 17 00:00:00 2001 From: Geoff Stuart Date: Fri, 20 Jan 2023 16:45:06 -0500 Subject: [PATCH] Cleanups --- chain/types/ethtypes/eth_types.go | 30 ++++++++++++++++++++-------- cli/evm.go | 10 +++++----- cli/state.go | 4 +++- itests/fevm_events_test.go | 33 ++++++++++++++++--------------- itests/splitstore_test.go | 14 +++++-------- 5 files changed, 52 insertions(+), 39 deletions(-) diff --git a/chain/types/ethtypes/eth_types.go b/chain/types/ethtypes/eth_types.go index eb0e12891..235cc7c79 100644 --- a/chain/types/ethtypes/eth_types.go +++ b/chain/types/ethtypes/eth_types.go @@ -364,14 +364,7 @@ func (h *EthHash) UnmarshalJSON(b []byte) error { } func decodeHexString(s string, expectedLen int) ([]byte, error) { - // Strip the leading 0x or 0X prefix since hex.DecodeString does not support it. - if strings.HasPrefix(s, "0x") || strings.HasPrefix(s, "0X") { - s = s[2:] - } - // Sometimes clients will omit a leading zero in a byte; pad so we can decode correctly. - if len(s)%2 == 1 { - s = "0" + s - } + s = handleHexStringPrefix(s) if len(s) != expectedLen*2 { return nil, xerrors.Errorf("expected hex string length sans prefix %d, got %d", expectedLen*2, len(s)) } @@ -382,6 +375,27 @@ func decodeHexString(s string, expectedLen int) ([]byte, error) { return b, nil } +func DecodeHexString(s string) ([]byte, error) { + s = handleHexStringPrefix(s) + b, err := hex.DecodeString(s) + if err != nil { + return nil, xerrors.Errorf("cannot parse hex value: %w", err) + } + return b, nil +} + +func handleHexStringPrefix(s string) string { + // Strip the leading 0x or 0X prefix since hex.DecodeString does not support it. + if strings.HasPrefix(s, "0x") || strings.HasPrefix(s, "0X") { + s = s[2:] + } + // Sometimes clients will omit a leading zero in a byte; pad so we can decode correctly. + if len(s)%2 == 1 { + s = "0" + s + } + return s +} + func EthHashFromCid(c cid.Cid) (EthHash, error) { return ParseEthHash(c.Hash().HexString()[8:]) } diff --git a/cli/evm.go b/cli/evm.go index 738788448..5d6d05f49 100644 --- a/cli/evm.go +++ b/cli/evm.go @@ -121,7 +121,7 @@ var EvmCallSimulateCmd = &cli.Command{ return err } - params, err := hex.DecodeString(cctx.Args().Get(2)) + params, err := ethtypes.DecodeHexString(cctx.Args().Get(2)) if err != nil { return err } @@ -165,7 +165,7 @@ var EvmGetContractAddress = &cli.Command{ return err } - salt, err := hex.DecodeString(cctx.Args().Get(1)) + salt, err := ethtypes.DecodeHexString(cctx.Args().Get(1)) if err != nil { return xerrors.Errorf("Could not decode salt: %w", err) } @@ -184,7 +184,7 @@ var EvmGetContractAddress = &cli.Command{ return err } - contract, err := hex.DecodeString(string(contractHex)) + contract, err := ethtypes.DecodeHexString(string(contractHex)) if err != nil { return xerrors.Errorf("Could not decode contract file: %w", err) } @@ -233,7 +233,7 @@ var EvmDeployCmd = &cli.Command{ return xerrors.Errorf("failed to read contract: %w", err) } if cctx.Bool("hex") { - contract, err = hex.DecodeString(string(contract)) + contract, err = ethtypes.DecodeHexString(string(contract)) if err != nil { return xerrors.Errorf("failed to decode contract: %w", err) } @@ -355,7 +355,7 @@ var EvmInvokeCmd = &cli.Command{ } var calldata []byte - calldata, err = hex.DecodeString(cctx.Args().Get(1)) + calldata, err = ethtypes.DecodeHexString(cctx.Args().Get(1)) if err != nil { return xerrors.Errorf("decoding hex input data: %w", err) } diff --git a/cli/state.go b/cli/state.go index 9513098d3..c69299946 100644 --- a/cli/state.go +++ b/cli/state.go @@ -776,7 +776,9 @@ var StateGetActorCmd = &cli.Command{ fmt.Printf("Nonce:\t\t%d\n", a.Nonce) fmt.Printf("Code:\t\t%s (%s)\n", a.Code, strtype) fmt.Printf("Head:\t\t%s\n", a.Head) - fmt.Printf("Delegated address:\t\t%s\n", a.Address) + if a.Address != nil { + fmt.Printf("Delegated address:\t\t%s\n", a.Address) + } return nil }, diff --git a/itests/fevm_events_test.go b/itests/fevm_events_test.go index 30dd7015f..079a7a699 100644 --- a/itests/fevm_events_test.go +++ b/itests/fevm_events_test.go @@ -12,6 +12,7 @@ import ( "github.com/filecoin-project/go-address" + "github.com/filecoin-project/lotus/chain/types/ethtypes" "github.com/filecoin-project/lotus/itests/kit" ) @@ -45,22 +46,22 @@ func TestFEVMEvents(t *testing.T) { require.NoError(err) t.Logf("actor ID address is %s", idAddr) - // var ( - // earliest = "earliest" - // latest = "latest" - // ) - // - // // Install a filter. - // filter, err := client.EthNewFilter(ctx, &api.EthFilterSpec{ - // FromBlock: &earliest, - // ToBlock: &latest, - // }) - // require.NoError(err) - // - // // No logs yet. - // res, err := client.EthGetFilterLogs(ctx, filter) - // require.NoError(err) - // require.Empty(res.NewLogs) + var ( + earliest = "earliest" + latest = "latest" + ) + + // Install a filter. + filter, err := client.EthNewFilter(ctx, ðtypes.EthFilterSpec{ + FromBlock: &earliest, + ToBlock: &latest, + }) + require.NoError(err) + + // No logs yet. + res, err := client.EthGetFilterLogs(ctx, filter) + require.NoError(err) + require.Empty(res.Results) // log a zero topic event with data ret := client.EVM().InvokeSolidity(ctx, fromAddr, idAddr, []byte{0x00, 0x00, 0x00, 0x00}, nil) diff --git a/itests/splitstore_test.go b/itests/splitstore_test.go index b5339d24c..4bbe56536 100644 --- a/itests/splitstore_test.go +++ b/itests/splitstore_test.go @@ -351,13 +351,11 @@ func splitStorePruneIndex(ctx context.Context, t *testing.T, n *kit.TestFullNode } func ipldExists(ctx context.Context, t *testing.T, c cid.Cid, n *kit.TestFullNode) bool { - _, err := n.ChainReadObj(ctx, c) - if ipld.IsNotFound(err) { - return false - } else if err != nil { - t.Fatalf("ChainReadObj failure on existence check: %s", err) + found, err := n.ChainHasObj(ctx, c) + if err != nil { + t.Fatalf("ChainHasObj failure: %s", err) } - return true + return found } // Create on chain unreachable garbage for a network to exercise splitstore @@ -414,12 +412,10 @@ func (g *Garbager) Exists(ctx context.Context, c cid.Cid) bool { return false } else if err != nil { g.t.Fatalf("ChainReadObj failure on existence check: %s", err) + return false // unreachable } else { return true } - - g.t.Fatal("unreachable") - return false } func (g *Garbager) newPeerID(ctx context.Context) abi.ChainEpoch {