Skip to content

Commit

Permalink
chore(eth): make EthHashFromCid even faster
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Sep 13, 2024
1 parent cadc89d commit 1de60ee
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
20 changes: 17 additions & 3 deletions chain/types/ethtypes/eth_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/ipfs/go-cid"
"github.com/multiformats/go-multihash"
mh "github.com/multiformats/go-multihash"
"github.com/multiformats/go-varint"
"golang.org/x/crypto/sha3"
"golang.org/x/xerrors"
Expand All @@ -26,6 +27,13 @@ import (
"github.com/filecoin-project/lotus/lib/must"
)

var expectedHashPrefix = cid.Prefix{
Version: 1,
Codec: cid.DagCBOR,
MhType: uint64(mh.BLAKE2B_MIN + 31),
MhLength: 32,
}.Bytes()

var ErrInvalidAddress = errors.New("invalid Filecoin Eth address")

// Research into Filecoin chain behaviour suggests that probabilistic finality
Expand Down Expand Up @@ -529,12 +537,18 @@ func handleHexStringPrefix(s string) string {
}

func EthHashFromCid(c cid.Cid) (EthHash, error) {
b := c.Hash()
if len(b) != EthHashLength+4 {
hash, found := bytes.CutPrefix(c.Bytes(), expectedHashPrefix)
if !found {
return EthHash{}, fmt.Errorf("CID does not have the expected prefix")
}

if len(hash) != EthHashLength {
// this shouldn't be possible since the prefix has the length, but just in case
return EthHash{}, fmt.Errorf("CID hash length is not 32 bytes")
}

var h EthHash
copy(h[:], b[4:])
copy(h[:], hash)
return h, nil
}

Expand Down
16 changes: 8 additions & 8 deletions chain/types/ethtypes/eth_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ func TestEthHash(t *testing.T) {
require.Equal(t, hash, string(jm))
}

_, err := EthHashFromCid(cid.Undef)
require.ErrorContains(t, err, "not 32 bytes")

_, err = EthHashFromCid(cid.MustParse("bafkqaaa"))
require.ErrorContains(t, err, "not 32 bytes")

_, err = EthHashFromCid(cid.MustParse("bafyrgqhai26anf3i7pips7q22coa4sz2fr4gk4q4sqdtymvvjyginfzaqewveaeqdh524nsktaq43j65v22xxrybrtertmcfxufdam3da3hbk"))
require.ErrorContains(t, err, "not 32 bytes")
for _, c := range []cid.Cid{
cid.Undef,
cid.MustParse("bafy2bzacaa"),
cid.MustParse("bafy2bzacidqenpags5upxuhzpynnbhaojm5cy6dfoiojibz4gk2u4degs4qiclksacibt65ogzfjqionu7o25nl3y4ayzsizwbc32crqgnrqntqv"),
} {
_, err := EthHashFromCid(c)
require.ErrorContains(t, err, "CID does not have the expected prefix")
}
}

func TestEthFilterID(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ require (
github.com/libp2p/go-msgio v0.3.0
github.com/manifoldco/promptui v0.9.0
github.com/mattn/go-isatty v0.0.20
github.com/mattn/go-sqlite3 v1.14.16
github.com/mattn/go-sqlite3 v1.14.22
github.com/mitchellh/go-homedir v1.1.0
github.com/multiformats/go-base32 v0.1.0
github.com/multiformats/go-multiaddr v0.12.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -908,8 +908,8 @@ github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/microcosm-cc/bluemonday v1.0.1/go.mod h1:hsXNsILzKxV+sX77C5b8FSuKF00vh2OMYv+xgHpAMF4=
github.com/miekg/dns v1.1.12/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
Expand Down

0 comments on commit 1de60ee

Please sign in to comment.