From 5786c7245a62216726859c77c2634448e13d0609 Mon Sep 17 00:00:00 2001 From: Kartik Chopra Date: Tue, 3 Oct 2023 00:46:01 -0400 Subject: [PATCH 1/2] Adds test for commitment and bid, fixes builder sig verfication bug. --- go.mod | 4 ++ go.sum | 2 - pkg/structures/preconf/bid.go | 2 +- pkg/structures/preconf/bid_test.go | 61 ++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 pkg/structures/preconf/bid_test.go diff --git a/go.mod b/go.mod index 47a7eaa0..b71303b5 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/libp2p/go-msgio v0.3.0 github.com/multiformats/go-multiaddr v0.11.0 github.com/prometheus/client_golang v1.17.0 + github.com/stretchr/testify v1.8.4 github.com/urfave/cli/v2 v2.25.7 github.com/vmihailenco/msgpack/v5 v5.4.0 golang.org/x/crypto v0.13.0 @@ -28,6 +29,7 @@ require ( github.com/coreos/go-systemd/v22 v22.5.0 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/crate-crypto/go-kzg-4844 v0.3.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect github.com/docker/go-units v0.5.0 // indirect @@ -83,6 +85,7 @@ require ( github.com/opencontainers/runtime-spec v1.1.0 // indirect github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect github.com/prometheus/common v0.44.0 // indirect github.com/prometheus/procfs v0.11.1 // indirect @@ -107,6 +110,7 @@ require ( golang.org/x/text v0.13.0 // indirect golang.org/x/tools v0.12.1-0.20230815132531-74c255bcf846 // indirect google.golang.org/protobuf v1.31.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect lukechampine.com/blake3 v1.2.1 // indirect rsc.io/tmplfunc v0.0.3 // indirect ) diff --git a/go.sum b/go.sum index ccc1661f..67ebaaff 100644 --- a/go.sum +++ b/go.sum @@ -31,8 +31,6 @@ github.com/btcsuite/btcd v0.22.0-beta h1:LTDpDKUM5EeOFBPM8IXpinEcmZ6FWfNZbE3lfrf github.com/btcsuite/btcd v0.22.0-beta/go.mod h1:9n5ntfhhHQBIhUvlhDvD3Qg6fRUj4jkN0VB8L8svzOA= github.com/btcsuite/btcd/btcec/v2 v2.2.0 h1:fzn1qaOt32TuLjFlkzYSsBC35Q3KUjT1SwPxiMSCF5k= github.com/btcsuite/btcd/btcec/v2 v2.2.0/go.mod h1:U7MHm051Al6XmscBQ0BoNydpOTsFAn707034b5nY8zU= -github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U= -github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA= github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg= github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce/go.mod h1:0DVlHczLPewLcPGEIeUEzfOJhqGPQ0mJJRDBtD307+o= diff --git a/pkg/structures/preconf/bid.go b/pkg/structures/preconf/bid.go index 8cbc8975..9c71f0d2 100644 --- a/pkg/structures/preconf/bid.go +++ b/pkg/structures/preconf/bid.go @@ -56,7 +56,7 @@ func (p *PreconfCommitment) VerifyBuilderSignature() (*common.Address, error) { internalPayload := constructCommitmentPayload(p.TxnHash, p.Bid, p.Blocknumber, p.BidHash, p.Signature) - return eipVerify(internalPayload, p.DataHash, p.Signature) + return eipVerify(internalPayload, p.DataHash, p.CommitmentSignature) } func eipVerify(internalPayload apitypes.TypedData, expectedhash []byte, signature []byte) (*common.Address, error) { diff --git a/pkg/structures/preconf/bid_test.go b/pkg/structures/preconf/bid_test.go new file mode 100644 index 00000000..a299a140 --- /dev/null +++ b/pkg/structures/preconf/bid_test.go @@ -0,0 +1,61 @@ +package preconf_test + +import ( + "math/big" + "testing" + + "github.com/ethereum/go-ethereum/crypto" + "github.com/primevprotocol/mev-commit/pkg/structures/preconf" + "github.com/stretchr/testify/assert" +) + +func TestBids(t *testing.T) { + t.Parallel() + + t.Run("bid", func(t *testing.T) { + key, _ := crypto.GenerateKey() + + bid, err := preconf.ConstructSignedBid(big.NewInt(10), "0xkartik", big.NewInt(2), preconf.PrivateKeySigner{PrivKey: key}) + if err != nil { + t.Fatal(err) + } + address, err := bid.VerifySearcherSignature() + + if err != nil { + t.Fatal(err) + } + + expectedAddress := crypto.PubkeyToAddress(key.PublicKey) + + originatorAddress, pubkey, err := bid.BidOriginator() + if err != nil { + t.Fail() + } + assert.Equal(t, expectedAddress, *originatorAddress) + assert.Equal(t, expectedAddress, *address) + assert.Equal(t, key.PublicKey, *pubkey) + }) + t.Run("commitment", func(t *testing.T) { + userKey, _ := crypto.GenerateKey() + userSigner := preconf.PrivateKeySigner{PrivKey: userKey} + + builderKey, _ := crypto.GenerateKey() + builderSigner := preconf.PrivateKeySigner{PrivKey: builderKey} + + bid, err := preconf.ConstructSignedBid(big.NewInt(10), "0xkartik", big.NewInt(2), userSigner) + if err != nil { + t.Fatal(err) + } + commitment, err := preconf.ConstructCommitment(*bid, builderSigner) + if err != nil { + t.Fail() + } + address, err := commitment.VerifyBuilderSignature() + if err != nil { + t.Fail() + } + + assert.Equal(t, crypto.PubkeyToAddress(builderKey.PublicKey), *address) + + }) +} From f9ef896ac33b7d6daee421278f5eaed122e10e36 Mon Sep 17 00:00:00 2001 From: Kartik Chopra Date: Tue, 3 Oct 2023 00:55:00 -0400 Subject: [PATCH 2/2] Updates project structure, by moving up the preconf package. --- pkg/{structures => }/preconf/bid.go | 0 pkg/{structures => }/preconf/bid_test.go | 2 +- pkg/preconfirmation/preconfirmation.go | 2 +- pkg/preconfirmation/preconfirmation_test.go | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) rename pkg/{structures => }/preconf/bid.go (100%) rename pkg/{structures => }/preconf/bid_test.go (95%) diff --git a/pkg/structures/preconf/bid.go b/pkg/preconf/bid.go similarity index 100% rename from pkg/structures/preconf/bid.go rename to pkg/preconf/bid.go diff --git a/pkg/structures/preconf/bid_test.go b/pkg/preconf/bid_test.go similarity index 95% rename from pkg/structures/preconf/bid_test.go rename to pkg/preconf/bid_test.go index a299a140..965597be 100644 --- a/pkg/structures/preconf/bid_test.go +++ b/pkg/preconf/bid_test.go @@ -5,7 +5,7 @@ import ( "testing" "github.com/ethereum/go-ethereum/crypto" - "github.com/primevprotocol/mev-commit/pkg/structures/preconf" + "github.com/primevprotocol/mev-commit/pkg/preconf" "github.com/stretchr/testify/assert" ) diff --git a/pkg/preconfirmation/preconfirmation.go b/pkg/preconfirmation/preconfirmation.go index 03160c0f..c3c9d052 100644 --- a/pkg/preconfirmation/preconfirmation.go +++ b/pkg/preconfirmation/preconfirmation.go @@ -9,7 +9,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/primevprotocol/mev-commit/pkg/p2p" "github.com/primevprotocol/mev-commit/pkg/p2p/msgpack" - "github.com/primevprotocol/mev-commit/pkg/structures/preconf" + "github.com/primevprotocol/mev-commit/pkg/preconf" "github.com/primevprotocol/mev-commit/pkg/topology" ) diff --git a/pkg/preconfirmation/preconfirmation_test.go b/pkg/preconfirmation/preconfirmation_test.go index e9c57223..62e0bd12 100644 --- a/pkg/preconfirmation/preconfirmation_test.go +++ b/pkg/preconfirmation/preconfirmation_test.go @@ -10,8 +10,8 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/primevprotocol/mev-commit/pkg/p2p" p2ptest "github.com/primevprotocol/mev-commit/pkg/p2p/testing" + "github.com/primevprotocol/mev-commit/pkg/preconf" "github.com/primevprotocol/mev-commit/pkg/preconfirmation" - "github.com/primevprotocol/mev-commit/pkg/structures/preconf" "github.com/primevprotocol/mev-commit/pkg/topology" )