From 577b32eead60e5fa040f580bca2fd925f3a00673 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mi=C5=82kowski?= <lukanus@uaznia.net> Date: Mon, 20 Mar 2023 13:48:53 +0100 Subject: [PATCH] next round --- datastore/datastore_test.go | 10 +- relay/old_test/relay_test.go | 449 ----------------------------- relay/old_test/submit_test.go | 518 ---------------------------------- relay/submit_test.go | 6 + 4 files changed, 14 insertions(+), 969 deletions(-) delete mode 100644 relay/old_test/submit_test.go diff --git a/datastore/datastore_test.go b/datastore/datastore_test.go index f0969951..375cbfe5 100644 --- a/datastore/datastore_test.go +++ b/datastore/datastore_test.go @@ -8,8 +8,11 @@ import ( datastore "github.com/blocknative/dreamboat/datastore" "github.com/blocknative/dreamboat/test/common" + "github.com/google/uuid" lru "github.com/hashicorp/golang-lru/v2" + tBadger "github.com/blocknative/dreamboat/datastore/transport/badger" + "github.com/blocknative/dreamboat/structs" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/flashbots/go-boost-utils/bls" @@ -24,9 +27,12 @@ func TestPutGetPayload(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - store := newMockDatastore() + tB, err := tBadger.Open("/tmp/" + t.Name() + uuid.New().String()) + require.NoError(t, err) + defer tB.Close() + cache, _ := lru.New[structs.PayloadKey, structs.BlockBidAndTrace](10) - ds := datastore.Datastore{TTLStorage: store, PayloadCache: cache} + ds := datastore.Datastore{TTLStorage: tB, PayloadCache: cache} payload := randomBlockBidAndTrace() diff --git a/relay/old_test/relay_test.go b/relay/old_test/relay_test.go index b3d72f94..3c5c2d8e 100644 --- a/relay/old_test/relay_test.go +++ b/relay/old_test/relay_test.go @@ -6,7 +6,6 @@ import ( "io" "math/rand" "strconv" - "sync" "testing" "time" @@ -29,8 +28,6 @@ import ( "github.com/lthibault/log" "github.com/stretchr/testify/require" - ds "github.com/ipfs/go-datastore" - ds_sync "github.com/ipfs/go-datastore/sync" badger "github.com/ipfs/go-ds-badger2" ) @@ -249,425 +246,6 @@ func TestGetPayload(t *testing.T) { require.EqualValues(t, submitRequest.ExecutionPayload, response.Data) } -func BenchmarkGetHeader(b *testing.B) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - ctrl := gomock.NewController(b) - - pk, _, _ := bls.GenerateNewKeypair() - - var datadir = "/tmp/" + b.Name() + uuid.New().String() - store, _ := badger.NewDatastore(datadir, &badger.DefaultOptions) - hc := datastore.NewHeaderController(100, time.Hour) - ds, err := datastore.NewDatastore(&datastore.TTLDatastoreBatcher{TTLDatastore: store}, store.DB, hc, 100) - require.NoError(b, err) - - bs := mocks.NewMockState(ctrl) - - proposerSigningDomain, _ := common.ComputeDomain( - types.DomainTypeBeaconProposer, - GenesisValidatorsRootRopsten) - - config := relay.RelayConfig{ - TTL: 5 * time.Minute, - SecretKey: pk, // pragma: allowlist secret - PubKey: types.PublicKey(random48Bytes()), - ProposerSigningDomain: map[string]types.Domain{"bellatrix": proposerSigningDomain}, - } - - ver := verify.NewVerificationManager(l, 20000) - ver.RunVerify(300) - - r := relay.NewRelay(log.New(), config, nil, nil, nil, ver, bs, ds, auction.NewAuctioneer(), nil) - - genesisTime := uint64(time.Now().Unix()) - bs.EXPECT().Genesis().AnyTimes().Return(structs.GenesisInfo{GenesisTime: genesisTime}) - submitRequest := validSubmitBlockRequest(b, proposerSigningDomain, genesisTime) - registration, _ := validValidatorRegistration(b, proposerSigningDomain) - - request := structs.HeaderRequest{} - request["slot"] = strconv.Itoa(int(submitRequest.Message.Slot)) - request["parent_hash"] = submitRequest.ExecutionPayload.ParentHash.String() - request["pubkey"] = registration.Message.Pubkey.String() - - signedBuilderBid, _ := relay.SubmitBlockRequestToSignedBuilderBid( - submitRequest, - pk, - &config.PubKey, - proposerSigningDomain, - ) - payload := relay.SubmitBlockRequestToBlockBidAndTrace("bellatrix", signedBuilderBid, submitRequest) - - // fill the datastore - key := relay.SubmissionToKey(submitRequest) - _ = ds.PutPayload(ctx, key, &payload, time.Minute) - header, _ := types.PayloadToPayloadHeader(submitRequest.ExecutionPayload) - pHeader := structs.HeaderAndTrace{ - Header: header, - Trace: &structs.BidTraceWithTimestamp{ - BidTraceExtended: structs.BidTraceExtended{ - BidTrace: *submitRequest.Message, - }, - Timestamp: uint64(time.Now().UnixMicro()), - }, - } - - jsHeader, _ := json.Marshal(pHeader) - _ = ds.PutHeader(ctx, structs.HeaderData{ - Slot: structs.Slot(submitRequest.Message.Slot), - HeaderAndTrace: pHeader, - Marshaled: jsHeader, - }, time.Minute) - - // _ = ds.PutRegistration(ctx, structs.PubKey{PublicKey: registration.Message.Pubkey}, *registration, time.Minute) - - b.ResetTimer() - b.ReportAllocs() - - for i := 0; i < b.N; i++ { - _, err := r.GetHeader(ctx, structs.NewMetricGroup(4), request) - if err != nil { - panic(err) - } - } -} - -func BenchmarkGetHeaderParallel(b *testing.B) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - ctrl := gomock.NewController(b) - - pk, _, _ := bls.GenerateNewKeypair() - - var datadir = "/tmp/" + b.Name() + uuid.New().String() - store, _ := badger.NewDatastore(datadir, &badger.DefaultOptions) - - hc := datastore.NewHeaderController(100, time.Hour) - ds, err := datastore.NewDatastore(&datastore.TTLDatastoreBatcher{TTLDatastore: store}, store.DB, hc, 100) - require.NoError(b, err) - bs := mocks.NewMockState(ctrl) - - proposerSigningDomain, _ := common.ComputeDomain( - types.DomainTypeBeaconProposer, - GenesisValidatorsRootRopsten) - - config := relay.RelayConfig{ - TTL: 5 * time.Minute, - SecretKey: pk, // pragma: allowlist secret - PubKey: types.PublicKey(random48Bytes()), - ProposerSigningDomain: map[string]types.Domain{"bellatrix": proposerSigningDomain}, - } - - ver := verify.NewVerificationManager(l, 20000) - ver.RunVerify(300) - - r := relay.NewRelay(log.New(), config, nil, nil, nil, ver, bs, ds, auction.NewAuctioneer(), nil) - - genesisTime := uint64(time.Now().Unix()) - bs.EXPECT().Genesis().AnyTimes().Return(structs.GenesisInfo{GenesisTime: genesisTime}) - submitRequest := validSubmitBlockRequest(b, proposerSigningDomain, genesisTime) - registration, _ := validValidatorRegistration(b, proposerSigningDomain) - - request := structs.HeaderRequest{} - request["slot"] = strconv.Itoa(int(submitRequest.Message.Slot)) - request["parent_hash"] = submitRequest.ExecutionPayload.ParentHash.String() - request["pubkey"] = registration.Message.Pubkey.String() - - signedBuilderBid, _ := relay.SubmitBlockRequestToSignedBuilderBid( - submitRequest, - pk, - &config.PubKey, - proposerSigningDomain, - ) - payload := relay.SubmitBlockRequestToBlockBidAndTrace("bellatrix", signedBuilderBid, submitRequest) - - // fill the datastore - key := relay.SubmissionToKey(submitRequest) - _ = ds.PutPayload(ctx, key, &payload, time.Minute) - header, _ := types.PayloadToPayloadHeader(submitRequest.ExecutionPayload) - - pHeader := structs.HeaderAndTrace{ - Header: header, - Trace: &structs.BidTraceWithTimestamp{ - BidTraceExtended: structs.BidTraceExtended{ - BidTrace: *submitRequest.Message, - }, - Timestamp: uint64(time.Now().UnixMicro()), - }, - } - - jsHeader, _ := json.Marshal(pHeader) - _ = ds.PutHeader(ctx, structs.HeaderData{ - Slot: structs.Slot(submitRequest.Message.Slot), - HeaderAndTrace: pHeader, - Marshaled: jsHeader, - }, time.Minute) - - // _ = ds.PutRegistration(ctx, structs.PubKey{PublicKey: registration.Message.Pubkey}, *registration, time.Minute) - - var wg sync.WaitGroup - defer wg.Wait() - - wg.Add(b.N) - - b.ResetTimer() - b.ReportAllocs() - - for i := 0; i < b.N; i++ { - go func() { - _, err := r.GetHeader(ctx, structs.NewMetricGroup(4), request) - if err != nil { - panic(err) - } - wg.Done() - }() - } -} - -func BenchmarkGetPayload(b *testing.B) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - ctrl := gomock.NewController(b) - - pk, _, _ := bls.GenerateNewKeypair() - - var datadir = "/tmp/" + b.Name() + uuid.New().String() - store, _ := badger.NewDatastore(datadir, &badger.DefaultOptions) - hc := datastore.NewHeaderController(100, time.Hour) - ds, err := datastore.NewDatastore(&datastore.TTLDatastoreBatcher{TTLDatastore: store}, store.DB, hc, 100) - require.NoError(b, err) - bs := mocks.NewMockState(ctrl) - - proposerSigningDomain, _ := common.ComputeDomain( - types.DomainTypeBeaconProposer, - GenesisValidatorsRootRopsten) - - config := relay.RelayConfig{ - TTL: 5 * time.Minute, - SecretKey: pk, // pragma: allowlist secret - PubKey: types.PublicKey(random48Bytes()), - ProposerSigningDomain: map[string]types.Domain{"bellatrix": proposerSigningDomain}, - } - - l := log.New() - - ver := verify.NewVerificationManager(l, 20000) - ver.RunVerify(300) - - r := relay.NewRelay(l, config, nil, nil, nil, ver, bs, ds, auction.NewAuctioneer(), nil) - - genesisTime := uint64(time.Now().Unix()) - submitRequest := validSubmitBlockRequest(b, proposerSigningDomain, genesisTime) - header, _ := types.PayloadToPayloadHeader(submitRequest.ExecutionPayload) - registration, sk := validValidatorRegistration(b, proposerSigningDomain) - - msg := &types.BlindedBeaconBlock{ - Slot: submitRequest.Message.Slot, - ProposerIndex: 2, - ParentRoot: types.Root{0x03}, - StateRoot: types.Root{0x04}, - Body: &types.BlindedBeaconBlockBody{ - Eth1Data: &types.Eth1Data{ - DepositRoot: types.Root{0x05}, - DepositCount: 5, - BlockHash: types.Hash{0x06}, - }, - ProposerSlashings: []*types.ProposerSlashing{}, - AttesterSlashings: []*types.AttesterSlashing{}, - Attestations: []*types.Attestation{}, - Deposits: []*types.Deposit{}, - VoluntaryExits: []*types.SignedVoluntaryExit{}, - SyncAggregate: &types.SyncAggregate{CommitteeBits: types.CommitteeBits{0x07}, CommitteeSignature: types.Signature{0x08}}, - ExecutionPayloadHeader: header, - }, - } - signature, _ := types.SignMessage(msg, proposerSigningDomain, sk) - request := &types.SignedBlindedBeaconBlock{ - Message: msg, - Signature: signature, - } - - signedBuilderBid, _ := relay.SubmitBlockRequestToSignedBuilderBid( - submitRequest, - pk, - &config.PubKey, - proposerSigningDomain, - ) - payload := relay.SubmitBlockRequestToBlockBidAndTrace("bellatrix", signedBuilderBid, submitRequest) - - // fill the datastore - key := structs.PayloadKey{ - BlockHash: request.Message.Body.ExecutionPayloadHeader.BlockHash, - Proposer: registration.Message.Pubkey, - Slot: structs.Slot(request.Message.Slot), - } - _ = ds.PutPayload(ctx, key, &payload, time.Minute) - pHeader := structs.HeaderAndTrace{ - Header: header, - Trace: &structs.BidTraceWithTimestamp{ - BidTraceExtended: structs.BidTraceExtended{ - BidTrace: *submitRequest.Message, - }, - Timestamp: uint64(time.Now().UnixMicro()), - }, - } - jsHeader, _ := json.Marshal(pHeader) - _ = ds.PutHeader(ctx, structs.HeaderData{ - Slot: structs.Slot(submitRequest.Message.Slot), - HeaderAndTrace: pHeader, - Marshaled: jsHeader, - }, time.Minute) - - // _ = ds.PutRegistration(ctx, structs.PubKey{PublicKey: registration.Message.Pubkey}, *registration, time.Minute) - - validatorsState := structs.ValidatorsState{ - KnownValidatorsByIndex: map[uint64]types.PubkeyHex{ - request.Message.ProposerIndex: registration.Message.Pubkey.PubkeyHex(), - }, - } - genesisState := structs.GenesisInfo{GenesisTime: genesisTime} - - bs.EXPECT().KnownValidators().Return(validatorsState).AnyTimes() - bs.EXPECT().Genesis().Return(genesisState).AnyTimes() - - b.ResetTimer() - b.ReportAllocs() - - for i := 0; i < b.N; i++ { - _, err := r.GetPayload(ctx, structs.NewMetricGroup(4), request) - if err != nil { - panic(err) - } - } -} - -func BenchmarkGetPayloadParallel(b *testing.B) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - ctrl := gomock.NewController(b) - - pk, _, _ := bls.GenerateNewKeypair() - - var datadir = "/tmp/" + b.Name() + uuid.New().String() - store, _ := badger.NewDatastore(datadir, &badger.DefaultOptions) - hc := datastore.NewHeaderController(100, time.Hour) - ds, err := datastore.NewDatastore(&datastore.TTLDatastoreBatcher{TTLDatastore: store}, store.DB, hc, 100) - require.NoError(b, err) - bs := mocks.NewMockState(ctrl) - - proposerSigningDomain, _ := common.ComputeDomain( - types.DomainTypeBeaconProposer, - GenesisValidatorsRootRopsten) - - l := log.New() - ver := verify.NewVerificationManager(l, 20000) - ver.RunVerify(300) - - config := relay.RelayConfig{ - TTL: 5 * time.Minute, - SecretKey: pk, // pragma: allowlist secret - PubKey: types.PublicKey(random48Bytes()), - ProposerSigningDomain: map[string]types.Domain{"bellatrix": proposerSigningDomain}, - } - r := relay.NewRelay(l, config, nil, nil, nil, ver, bs, ds, auction.NewAuctioneer(), nil) - - genesisTime := uint64(time.Now().Unix()) - submitRequest := validSubmitBlockRequest(b, proposerSigningDomain, genesisTime) - header, _ := types.PayloadToPayloadHeader(submitRequest.ExecutionPayload) - registration, sk := validValidatorRegistration(b, proposerSigningDomain) - - msg := &types.BlindedBeaconBlock{ - Slot: submitRequest.Message.Slot, - ProposerIndex: 2, - ParentRoot: types.Root{0x03}, - StateRoot: types.Root{0x04}, - Body: &types.BlindedBeaconBlockBody{ - Eth1Data: &types.Eth1Data{ - DepositRoot: types.Root{0x05}, - DepositCount: 5, - BlockHash: types.Hash{0x06}, - }, - ProposerSlashings: []*types.ProposerSlashing{}, - AttesterSlashings: []*types.AttesterSlashing{}, - Attestations: []*types.Attestation{}, - Deposits: []*types.Deposit{}, - VoluntaryExits: []*types.SignedVoluntaryExit{}, - SyncAggregate: &types.SyncAggregate{CommitteeBits: types.CommitteeBits{0x07}, CommitteeSignature: types.Signature{0x08}}, - ExecutionPayloadHeader: header, - }, - } - signature, _ := types.SignMessage(msg, proposerSigningDomain, sk) - request := &types.SignedBlindedBeaconBlock{ - Message: msg, - Signature: signature, - } - - signedBuilderBid, _ := relay.SubmitBlockRequestToSignedBuilderBid( - submitRequest, - pk, - &config.PubKey, - proposerSigningDomain, - ) - payload := relay.SubmitBlockRequestToBlockBidAndTrace("bellatrix", signedBuilderBid, submitRequest) - - // fill the datastore - key := structs.PayloadKey{ - BlockHash: request.Message.Body.ExecutionPayloadHeader.BlockHash, - Proposer: registration.Message.Pubkey, - Slot: structs.Slot(request.Message.Slot), - } - _ = ds.PutPayload(ctx, key, &payload, time.Minute) - - pHeader := structs.HeaderAndTrace{ - Header: header, - Trace: &structs.BidTraceWithTimestamp{ - BidTraceExtended: structs.BidTraceExtended{ - BidTrace: *submitRequest.Message, - }, - Timestamp: uint64(time.Now().UnixMicro()), - }, - } - - jsHeader, _ := json.Marshal(pHeader) - _ = ds.PutHeader(ctx, structs.HeaderData{ - Slot: structs.Slot(submitRequest.Message.Slot), - HeaderAndTrace: pHeader, - Marshaled: jsHeader, - }, time.Minute) - - // _ = ds.PutRegistration(ctx, structs.PubKey{PublicKey: registration.Message.Pubkey}, *registration, time.Minute) - - validatorsState := structs.ValidatorsState{ - KnownValidatorsByIndex: map[uint64]types.PubkeyHex{ - request.Message.ProposerIndex: registration.Message.Pubkey.PubkeyHex(), - }, - } - genesisState := structs.GenesisInfo{GenesisTime: genesisTime} - - bs.EXPECT().KnownValidators().Return(validatorsState).AnyTimes() - bs.EXPECT().Genesis().Return(genesisState).AnyTimes() - var wg sync.WaitGroup - defer wg.Wait() - - wg.Add(b.N) - - b.ResetTimer() - b.ReportAllocs() - - for i := 0; i < b.N; i++ { - go func() { - _, err := r.GetPayload(ctx, structs.NewMetricGroup(4), request) - if err != nil { - panic(err) - } - wg.Done() - }() - } -} func validValidatorRegistration(t require.TestingT, domain types.Domain) (*types.SignedValidatorRegistration, *bls.SecretKey) { sk, pubKey, err := blstools.GenerateNewKeypair() @@ -765,30 +343,3 @@ func randomTransactions(size int) []hexutil.Bytes { } return txs } - -type mockDatastore struct{ ds.Datastore } - -func newMockDatastore() mockDatastore { - return mockDatastore{ds_sync.MutexWrap(ds.NewMapDatastore())} -} - -func (d mockDatastore) PutWithTTL(ctx context.Context, key ds.Key, value []byte, ttl time.Duration) error { - go func() { - time.Sleep(ttl) - d.Delete(ctx, key) - }() - - return d.Datastore.Put(ctx, key, value) -} - -func (d mockDatastore) GetBatch(ctx context.Context, keys []ds.Key) (batch [][]byte, err error) { - for _, key := range keys { - data, err := d.Datastore.Get(ctx, key) - if err != nil { - continue - } - batch = append(batch, data) - } - - return -} diff --git a/relay/old_test/submit_test.go b/relay/old_test/submit_test.go deleted file mode 100644 index 45769503..00000000 --- a/relay/old_test/submit_test.go +++ /dev/null @@ -1,518 +0,0 @@ -package relay_test - -import ( - "context" - "testing" - "time" - - "github.com/blocknative/dreamboat/auction" - "github.com/blocknative/dreamboat/datastore" - "github.com/blocknative/dreamboat/test/common" - "github.com/blocknative/dreamboat/verify" - - "github.com/blocknative/dreamboat/relay/mocks" - "github.com/google/uuid" - - relay "github.com/blocknative/dreamboat/relay" - "github.com/blocknative/dreamboat/structs" - "github.com/flashbots/go-boost-utils/bls" - "github.com/flashbots/go-boost-utils/types" - "github.com/golang/mock/gomock" - "github.com/stretchr/testify/require" - - badger "github.com/ipfs/go-ds-badger2" -) - -func TestSubmitBlock(t *testing.T) { - t.Parallel() - - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - ctrl := gomock.NewController(t) - - sk, _, _ := bls.GenerateNewKeypair() - - var datadir = "/tmp/" + t.Name() + uuid.New().String() - store, _ := badger.NewDatastore(datadir, &badger.DefaultOptions) - - hc := datastore.NewHeaderController(100, time.Hour) - ds, err := datastore.NewDatastore(&datastore.TTLDatastoreBatcher{TTLDatastore: store}, store.DB, hc, 100) - require.NoError(t, err) - bs := mocks.NewMockState(ctrl) - - ver := verify.NewVerificationManager(l, 20000) - ver.RunVerify(300) - - relaySigningDomain, err := common.ComputeDomain( - types.DomainTypeAppBuilder, - types.Root{}.String()) - require.NoError(t, err) - - config := relay.RelayConfig{ - TTL: time.Minute, - SecretKey: sk, - RegistrationCacheTTL: time.Minute, - BuilderSigningDomain: relaySigningDomain, - } - - genesisTime := uint64(time.Now().Unix()) - submitRequest := validSubmitBlockRequest(t, relaySigningDomain, genesisTime) - signedBuilderBid, err := submitRequest.ToSignedBuilderBid( - config.SecretKey, - &config.PubKey, - relaySigningDomain) - require.NoError(t, err) - payload := submitRequest.ToBlockBidAndTrace("bellatrix", signedBuilderBid) - - bs.EXPECT().Genesis().AnyTimes().Return(structs.GenesisInfo{GenesisTime: genesisTime}) - - bVCli := mocks.NewMockBlockValidationClient(ctrl) - bVCli.EXPECT().ValidateBlock(gomock.Any(), gomock.Any()).Times(1) - vCache := mocks.NewMockValidatorCache(ctrl) - vStore := mocks.NewMockValidatorStore(ctrl) - vCache.EXPECT().Get(submitRequest.Message.ProposerPubkey).Return(structs.ValidatorCacheEntry{ - Time: time.Now(), - Entry: types.SignedValidatorRegistration{ - Message: &types.RegisterValidatorRequestMessage{ - FeeRecipient: submitRequest.Message.ProposerFeeRecipient, - }}, - }, true) - - r := relay.NewRelay(l, config, nil, vCache, vStore, ver, bs, ds, auction.NewAuctioneer(), bVCli) - - bs.EXPECT().HeadSlot().AnyTimes().Return(structs.Slot(submitRequest.Message.Slot)) - - err = r.SubmitBlock(ctx, structs.NewMetricGroup(4), submitRequest) - require.NoError(t, err) - - key := relay.SubmissionToKey(submitRequest) - gotPayload, _, err := ds.GetPayload(ctx, key) - require.NoError(t, err) - require.EqualValues(t, payload, *gotPayload) - - header, err := types.PayloadToPayloadHeader(submitRequest.ExecutionPayload) - require.NoError(t, err) - gotHeaders, err := ds.GetHeadersBySlot(ctx, submitRequest.Message.Slot) - require.NoError(t, err) - require.Len(t, gotHeaders, 1) - require.EqualValues(t, header, gotHeaders[0].Header) -} - -/* -func TestSubmitBlockInvalidTimestamp(t *testing.T) { - t.Parallel() - - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - ctrl := gomock.NewController(t) - - ds := &datastore.Datastore{TTLStorage: newMockDatastore()} - bs := mocks.NewMockState(ctrl) - sk, _, _ := bls.GenerateNewKeypair() - - l := log.New() - - bVCli := mocks.NewMockBlockValidationClient(ctrl) - //bVCli.EXPECT().ValidateBlock(gomock.Any(), gomock.Any()).Times(1) - ver := verify.NewVerificationManager(l, 20000) - ver.RunVerify(300) - - relaySigningDomain, err := common.ComputeDomain( - types.DomainTypeAppBuilder, - types.Root{}.String()) - require.NoError(t, err) - - config := relay.RelayConfig{ - TTL: 5 * time.Minute, - RegistrationCacheTTL: time.Minute, - SecretKey: sk, // pragma: allowlist secret - PubKey: types.PublicKey(random48Bytes()), - BuilderSigningDomain: relaySigningDomain, - } - - r := relay.NewRelay(l, config, nil, nil, nil, ver, bs, ds, auction.NewAuctioneer(), bVCli) - - genesisTime := uint64(time.Now().Unix()) - bs.EXPECT().Genesis().AnyTimes().Return(structs.GenesisInfo{GenesisTime: genesisTime}) - submitRequest := validSubmitBlockRequest(t, relaySigningDomain, genesisTime+1) // +1 in order to make timestamp invalid - - err = r.SubmitBlock(ctx, structs.NewMetricGroup(4), submitRequest) - require.Error(t, err) -} - -func TestSubmitBlocksTwoBuilders(t *testing.T) { - t.Parallel() - - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - ctrl := gomock.NewController(t) - - sk, _, _ := bls.GenerateNewKeypair() - - var datadir = "/tmp/" + uuid.New().String() - store, _ := badger.NewDatastore(datadir, &badger.DefaultOptions) - hc := datastore.NewHeaderController(100, time.Hour) - ds, err := datastore.NewDatastore(&datastore.TTLDatastoreBatcher{TTLDatastore: store}, store.DB, hc, 100) - require.NoError(t, err) - bs := mocks.NewMockState(ctrl) - - genesisTime := uint64(time.Now().Unix()) - bs.EXPECT().Genesis().AnyTimes().Return(structs.GenesisInfo{GenesisTime: genesisTime}) - - ver := verify.NewVerificationManager(l, 20000) - ver.RunVerify(300) - - relaySigningDomain, _ := common.ComputeDomain( - types.DomainTypeAppBuilder, - types.Root{}.String()) - - config := relay.RelayConfig{ - TTL: 5 * time.Minute, - RegistrationCacheTTL: time.Minute, - SecretKey: sk, // pragma: allowlist secret - PubKey: types.PublicKey(random48Bytes()), - BuilderSigningDomain: relaySigningDomain, - } - - proposerPubkey := types.PublicKey(random48Bytes()) - proposerFeeRecipient := types.Address(random20Bytes()) - - bVCli := mocks.NewMockBlockValidationClient(ctrl) - bVCli.EXPECT().ValidateBlock(gomock.Any(), gomock.Any()).Times(2) - vCache := mocks.NewMockValidatorCache(ctrl) - vStore := mocks.NewMockValidatorStore(ctrl) - vCache.EXPECT().Get(proposerPubkey).Return(structs.ValidatorCacheEntry{ - Time: time.Now(), - Entry: types.SignedValidatorRegistration{ - Message: &types.RegisterValidatorRequestMessage{ - FeeRecipient: proposerFeeRecipient, - }, - }, - }, true).AnyTimes() - - r := relay.NewRelay(l, config, nil, vCache, vStore, ver, bs, ds, auction.NewAuctioneer(), bVCli) - - // generate and send 1st block - skB1, pubKeyB1, err := blstools.GenerateNewKeypair() - require.NoError(t, err) - - slot := structs.Slot(rand.Uint64()) - payloadB1 := randomPayload() - payloadB1.Timestamp = genesisTime + (uint64(slot) * 12) - - msgB1 := &types.BidTrace{ - Slot: uint64(slot), - ParentHash: payloadB1.ParentHash, - BlockHash: payloadB1.BlockHash, - BuilderPubkey: pubKeyB1, - ProposerPubkey: proposerPubkey, - ProposerFeeRecipient: proposerFeeRecipient, - Value: types.IntToU256(10), - } - - bs.EXPECT().HeadSlot().AnyTimes().Return(structs.Slot(msgB1.Slot)) - - signatureB1, err := types.SignMessage(msgB1, relaySigningDomain, skB1) - require.NoError(t, err) - - submitRequestOne := &types.BuilderSubmitBlockRequest{ - Signature: signatureB1, - Message: msgB1, - ExecutionPayload: payloadB1, - } - - err = r.SubmitBlock(ctx, structs.NewMetricGroup(4), submitRequestOne) - require.NoError(t, err) - - skB2, pubKeyB2, err := blstools.GenerateNewKeypair() - require.NoError(t, err) - - payloadB2 := randomPayload() - payloadB2.Timestamp = genesisTime + (uint64(slot) * 12) - - msgB2 := &types.BidTrace{ - Slot: uint64(slot), - ParentHash: payloadB2.ParentHash, - BlockHash: payloadB2.BlockHash, - BuilderPubkey: pubKeyB2, - ProposerPubkey: proposerPubkey, - ProposerFeeRecipient: proposerFeeRecipient, - Value: types.IntToU256(1000), - } - - signatureB2, err := types.SignMessage(msgB2, relaySigningDomain, skB2) - require.NoError(t, err) - - submitRequestTwo := &types.BuilderSubmitBlockRequest{ - Signature: signatureB2, - Message: msgB2, - ExecutionPayload: payloadB2, - } - - err = r.SubmitBlock(ctx, structs.NewMetricGroup(4), submitRequestTwo) - require.NoError(t, err) - - // check that payload served from relay is 2nd builders - signedBuilderBid, err := relay.SubmitBlockRequestToSignedBuilderBid( - submitRequestOne, - config.SecretKey, - &config.PubKey, - relaySigningDomain) - require.NoError(t, err) - payload := relay.SubmitBlockRequestToBlockBidAndTrace("bellatrix", signedBuilderBid, submitRequestOne) - - key := relay.SubmissionToKey(submitRequestOne) - gotPayload, _, err := ds.GetPayload(ctx, key) - require.NoError(t, err) - require.EqualValues(t, payload, *gotPayload) - - header, err := types.PayloadToPayloadHeader(submitRequestTwo.ExecutionPayload) - require.NoError(t, err) - gotHeaders, err := ds.GetMaxProfitHeader(ctx, uint64(slot)) - - require.NoError(t, err) - require.EqualValues(t, header, gotHeaders.Header) -} - -func TestSubmitBlocksCancel(t *testing.T) { - t.Parallel() - - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - ctrl := gomock.NewController(t) - - sk, _, _ := bls.GenerateNewKeypair() - - var datadir = "/tmp/" + uuid.New().String() - store, _ := badger.NewDatastore(datadir, &badger.DefaultOptions) - hc := datastore.NewHeaderController(100, time.Hour) - ds, err := datastore.NewDatastore(&datastore.TTLDatastoreBatcher{TTLDatastore: store}, store.DB, hc, 100) - require.NoError(t, err) - - bs := mocks.NewMockState(ctrl) - - genesisTime := uint64(time.Now().Unix()) - bs.EXPECT().Genesis().AnyTimes().Return(structs.GenesisInfo{GenesisTime: genesisTime}) - - l := log.New() - ver := verify.NewVerificationManager(l, 20000) - ver.RunVerify(300) - - relaySigningDomain, _ := common.ComputeDomain( - types.DomainTypeAppBuilder, - types.Root{}.String()) - - config := relay.RelayConfig{ - TTL: 5 * time.Minute, - RegistrationCacheTTL: time.Minute, - SecretKey: sk, // pragma: allowlist secret - PubKey: types.PublicKey(random48Bytes()), - BuilderSigningDomain: relaySigningDomain, - } - - proposerPubkey := types.PublicKey(random48Bytes()) - proposerFeeRecipient := types.Address(random20Bytes()) - - bVCli := mocks.NewMockBlockValidationClient(ctrl) - bVCli.EXPECT().ValidateBlock(gomock.Any(), gomock.Any()).Times(2) - vCache := mocks.NewMockValidatorCache(ctrl) - vStore := mocks.NewMockValidatorStore(ctrl) - vCache.EXPECT().Get(proposerPubkey).Return(structs.ValidatorCacheEntry{ - Time: time.Now(), - Entry: types.SignedValidatorRegistration{ - Message: &types.RegisterValidatorRequestMessage{ - FeeRecipient: proposerFeeRecipient, - }, - }, - }, true).AnyTimes() - - r := relay.NewRelay(l, config, nil, vCache, vStore, ver, bs, ds, auction.NewAuctioneer(), bVCli) - - skB1, pubKeyB1, err := blstools.GenerateNewKeypair() - require.NoError(t, err) - - slot := structs.Slot(rand.Uint64()) - payloadB1 := randomPayload() - payloadB1.Timestamp = genesisTime + (uint64(slot) * 12) - - msgB1 := &types.BidTrace{ - Slot: uint64(slot), - ParentHash: payloadB1.ParentHash, - BlockHash: payloadB1.BlockHash, - BuilderPubkey: pubKeyB1, - ProposerPubkey: proposerPubkey, - ProposerFeeRecipient: proposerFeeRecipient, - Value: types.IntToU256(1000), - } - - bs.EXPECT().HeadSlot().AnyTimes().Return(slot) - - signatureB1, err := types.SignMessage(msgB1, relaySigningDomain, skB1) - require.NoError(t, err) - - submitRequestOne := &types.BuilderSubmitBlockRequest{ - Signature: signatureB1, - Message: msgB1, - ExecutionPayload: payloadB1, - } - - err = r.SubmitBlock(ctx, structs.NewMetricGroup(4), submitRequestOne) - require.NoError(t, err) - - // generate and send 2nd block from same builder - payloadB2 := randomPayload() - payloadB2.Timestamp = genesisTime + (uint64(slot) * 12) - - msgB2 := &types.BidTrace{ - Slot: uint64(slot), - ParentHash: payloadB2.ParentHash, - BlockHash: payloadB2.BlockHash, - BuilderPubkey: pubKeyB1, - ProposerPubkey: proposerPubkey, - ProposerFeeRecipient: proposerFeeRecipient, - Value: types.IntToU256(1), - } - - signatureB2, err := types.SignMessage(msgB2, relaySigningDomain, skB1) - require.NoError(t, err) - - submitRequestTwo := &types.BuilderSubmitBlockRequest{ - Signature: signatureB2, - Message: msgB2, - ExecutionPayload: payloadB2, - } - - err = r.SubmitBlock(ctx, structs.NewMetricGroup(4), submitRequestTwo) - require.NoError(t, err) - - // check that payload served from relay is 2nd block with lower value - header, err := types.PayloadToPayloadHeader(submitRequestTwo.ExecutionPayload) - require.NoError(t, err) - - gotHeaders, err := ds.GetMaxProfitHeader(ctx, uint64(slot)) - require.NoError(t, err) - - require.EqualValues(t, header, gotHeaders.Header) -} - -type vFeeProposer struct{ t structs.ValidatorCacheEntry } - -func VFeeProposer(t structs.ValidatorCacheEntry) gomock.Matcher { - return &vFeeProposer{t} -} -func (o *vFeeProposer) Matches(x interface{}) bool { - vce, ok := x.(structs.ValidatorCacheEntry) - if !ok { - return false - } - - return vce.Entry.Message.FeeRecipient == o.t.Entry.Message.FeeRecipient -} - -func (o *vFeeProposer) String() string { - return "is " + o.t.Entry.Message.FeeRecipient.String() -} - -func TestRegistartionCache(t *testing.T) { - t.Parallel() - - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - ctrl := gomock.NewController(t) - - sk, _, _ := bls.GenerateNewKeypair() - - var datadir = "/tmp/" + uuid.New().String() - store, _ := badger.NewDatastore(datadir, &badger.DefaultOptions) - hc := datastore.NewHeaderController(100, time.Hour) - ds, err := datastore.NewDatastore(&datastore.TTLDatastoreBatcher{TTLDatastore: store}, store.DB, hc, 100) - require.NoError(t, err) - - bs := mocks.NewMockState(ctrl) - - genesisTime := uint64(time.Now().Unix()) - bs.EXPECT().Genesis().AnyTimes().Return(structs.GenesisInfo{GenesisTime: genesisTime}) - - l := log.New() - ver := verify.NewVerificationManager(l, 20000) - ver.RunVerify(300) - - relaySigningDomain, _ := common.ComputeDomain( - types.DomainTypeAppBuilder, - types.Root{}.String()) - - config := relay.RelayConfig{ - TTL: 5 * time.Minute, - RegistrationCacheTTL: time.Minute, - SecretKey: sk, // pragma: allowlist secret - PubKey: types.PublicKey(random48Bytes()), - BuilderSigningDomain: relaySigningDomain, - } - - proposerPubkey := types.PublicKey(random48Bytes()) - proposerFeeRecipient := types.Address(random20Bytes()) - vCE := structs.ValidatorCacheEntry{ - Entry: types.SignedValidatorRegistration{ - Message: &types.RegisterValidatorRequestMessage{ - FeeRecipient: proposerFeeRecipient, - Pubkey: proposerPubkey, - }, - }, - } - bVCli := mocks.NewMockBlockValidationClient(ctrl) - bVCli.EXPECT().ValidateBlock(gomock.Any(), gomock.Any()).Times(1) - - vCache := mocks.NewMockValidatorCache(ctrl) - vCache.EXPECT().Get(proposerPubkey).Return(structs.ValidatorCacheEntry{}, false) - vCache.EXPECT().Add(proposerPubkey, VFeeProposer(vCE)).Return(false) - - vStore := mocks.NewMockValidatorStore(ctrl) - vStore.EXPECT().GetRegistration(gomock.Any(), proposerPubkey).Return(types.SignedValidatorRegistration{ - Message: &types.RegisterValidatorRequestMessage{ - FeeRecipient: proposerFeeRecipient, - Pubkey: proposerPubkey, - }, - }, nil) - - r := relay.NewRelay(l, config, nil, vCache, vStore, ver, bs, ds, auction.NewAuctioneer(), bVCli) - - skB1, pubKeyB1, err := blstools.GenerateNewKeypair() - require.NoError(t, err) - - slot := structs.Slot(rand.Uint64()) - payloadB1 := randomPayload() - payloadB1.Timestamp = genesisTime + (uint64(slot) * 12) - - msgB1 := &types.BidTrace{ - Slot: uint64(slot), - ParentHash: payloadB1.ParentHash, - BlockHash: payloadB1.BlockHash, - BuilderPubkey: pubKeyB1, - ProposerPubkey: proposerPubkey, - ProposerFeeRecipient: proposerFeeRecipient, - Value: types.IntToU256(1000), - } - - bs.EXPECT().HeadSlot().AnyTimes().Return(structs.Slot(slot)) - - signatureB1, err := types.SignMessage(msgB1, relaySigningDomain, skB1) - require.NoError(t, err) - - submitRequestOne := &types.BuilderSubmitBlockRequest{ - Signature: signatureB1, - Message: msgB1, - ExecutionPayload: payloadB1, - } - - err = r.SubmitBlock(ctx, structs.NewMetricGroup(4), submitRequestOne) - require.NoError(t, err) - - _, err = ds.GetMaxProfitHeader(ctx, uint64(slot)) - require.NoError(t, err) -} -*/ diff --git a/relay/submit_test.go b/relay/submit_test.go index 51443ade..d154f445 100644 --- a/relay/submit_test.go +++ b/relay/submit_test.go @@ -23,6 +23,11 @@ import ( "github.com/stretchr/testify/require" ) +// To migrate(?): +// TestSubmitBlockInvalidTimestamp - and other params +// TestSubmitBlocksTwoBuilders - probably doesn't matter in this test anymore +// TestSubmitBlocksCancel - this should probably go to auctioneer + type fields struct { d Datastore das DataAPIStore @@ -257,6 +262,7 @@ func TestRelay_SubmitBlock(t *testing.T) { proposerSigningDomain, err := ccommon.ComputeDomain( types.DomainTypeBeaconProposer, types.Root{}.String()) + require.NoError(t, err) var sbbb structs.SignedBlindedBeaconBlock switch s := gh.(type) {