Cleanups
This commit is contained in:
parent
fdb330eb58
commit
439b4c6e22
@ -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:])
|
||||
}
|
||||
|
10
cli/evm.go
10
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)
|
||||
}
|
||||
|
@ -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
|
||||
},
|
||||
|
@ -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)
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user