|
17 | 17 | package ethtest
|
18 | 18 |
|
19 | 19 | import (
|
| 20 | + "context" |
20 | 21 | "crypto/rand"
|
| 22 | + "fmt" |
21 | 23 | "reflect"
|
| 24 | + "sync" |
| 25 | + "time" |
22 | 26 |
|
23 | 27 | "github.com/ethereum/go-ethereum/common"
|
24 | 28 | "github.com/ethereum/go-ethereum/consensus/misc/eip4844"
|
@@ -79,6 +83,8 @@ func (s *Suite) EthTests() []utesting.Test {
|
79 | 83 | {Name: "InvalidTxs", Fn: s.TestInvalidTxs},
|
80 | 84 | {Name: "NewPooledTxs", Fn: s.TestNewPooledTxs},
|
81 | 85 | {Name: "BlobViolations", Fn: s.TestBlobViolations},
|
| 86 | + {Name: "TestBlobTxWithoutSidecar", Fn: s.TestBlobTxWithoutSidecar}, |
| 87 | + {Name: "TestBlobTxWithMismatchedSidecar", Fn: s.TestBlobTxWithMismatchedSidecar}, |
82 | 88 | }
|
83 | 89 | }
|
84 | 90 |
|
@@ -825,3 +831,194 @@ func (s *Suite) TestBlobViolations(t *utesting.T) {
|
825 | 831 | conn.Close()
|
826 | 832 | }
|
827 | 833 | }
|
| 834 | + |
| 835 | +// mangleSidecar returns a copy of the given blob transaction where the sidecar |
| 836 | +// data has been modified to produce a different commitment hash. |
| 837 | +func mangleSidecar(tx *types.Transaction) *types.Transaction { |
| 838 | + sidecar := tx.BlobTxSidecar() |
| 839 | + copy := types.BlobTxSidecar{ |
| 840 | + Blobs: append([]kzg4844.Blob{}, sidecar.Blobs...), |
| 841 | + Commitments: append([]kzg4844.Commitment{}, sidecar.Commitments...), |
| 842 | + Proofs: append([]kzg4844.Proof{}, sidecar.Proofs...), |
| 843 | + } |
| 844 | + // zero the first commitment to alter the sidecar hash |
| 845 | + copy.Commitments[0] = kzg4844.Commitment{} |
| 846 | + return tx.WithBlobTxSidecar(©) |
| 847 | +} |
| 848 | + |
| 849 | +func (s *Suite) TestBlobTxWithoutSidecar(t *utesting.T) { |
| 850 | + t.Log(`This test checks that a blob transaction first advertised/transmitted without blobs will result in the sending peer being disconnected, and the full transaction should be successfully retrieved from another peer.`) |
| 851 | + tx := s.makeBlobTxs(1, 2, 42)[0] |
| 852 | + badTx := tx.WithoutBlobTxSidecar() |
| 853 | + s.testBadBlobTx(t, tx, badTx) |
| 854 | +} |
| 855 | + |
| 856 | +func (s *Suite) TestBlobTxWithMismatchedSidecar(t *utesting.T) { |
| 857 | + t.Log(`This test checks that a blob transaction first advertised/transmitted without blobs, whose commitment don't correspond to the blob_versioned_hashes in the transaction, will result in the sending peer being disconnected, and the full transaction should be successfully retrieved from another peer.`) |
| 858 | + tx := s.makeBlobTxs(1, 2, 43)[0] |
| 859 | + badTx := mangleSidecar(tx) |
| 860 | + s.testBadBlobTx(t, tx, badTx) |
| 861 | +} |
| 862 | + |
| 863 | +// readUntil reads eth protocol messages until a message of the target type is |
| 864 | +// received. It returns an error if there is a disconnect, or if the context |
| 865 | +// is cancelled before a message of the desired type can be read. |
| 866 | +func readUntil[T any](ctx context.Context, conn *Conn) (*T, error) { |
| 867 | + for { |
| 868 | + select { |
| 869 | + case <-ctx.Done(): |
| 870 | + return nil, context.Canceled |
| 871 | + default: |
| 872 | + } |
| 873 | + received, err := conn.ReadEth() |
| 874 | + if err != nil { |
| 875 | + if err == errDisc { |
| 876 | + return nil, errDisc |
| 877 | + } |
| 878 | + continue |
| 879 | + } |
| 880 | + |
| 881 | + switch res := received.(type) { |
| 882 | + case *T: |
| 883 | + return res, nil |
| 884 | + } |
| 885 | + } |
| 886 | +} |
| 887 | + |
| 888 | +// readUntilDisconnect reads eth protocol messages until the peer disconnects. |
| 889 | +// It returns whether the peer disconnects in the next 100ms. |
| 890 | +func readUntilDisconnect(conn *Conn) (disconnected bool) { |
| 891 | + ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 892 | + defer cancel() |
| 893 | + _, err := readUntil[struct{}](ctx, conn) |
| 894 | + return err == errDisc |
| 895 | +} |
| 896 | + |
| 897 | +func (s *Suite) testBadBlobTx(t *utesting.T, tx *types.Transaction, badTx *types.Transaction) { |
| 898 | + stage1, stage2, stage3 := new(sync.WaitGroup), new(sync.WaitGroup), new(sync.WaitGroup) |
| 899 | + stage1.Add(1) |
| 900 | + stage2.Add(1) |
| 901 | + stage3.Add(1) |
| 902 | + |
| 903 | + errc := make(chan error) |
| 904 | + |
| 905 | + badPeer := func() { |
| 906 | + // announce the correct hash from the bad peer. |
| 907 | + // when the transaction is first requested before transmitting it from the bad peer, |
| 908 | + // trigger step 2: connection and announcement by good peers |
| 909 | + |
| 910 | + conn, err := s.dial() |
| 911 | + if err != nil { |
| 912 | + errc <- fmt.Errorf("dial fail: %v", err) |
| 913 | + return |
| 914 | + } |
| 915 | + defer conn.Close() |
| 916 | + |
| 917 | + if err := conn.peer(s.chain, nil); err != nil { |
| 918 | + errc <- fmt.Errorf("bad peer: peering failed: %v", err) |
| 919 | + return |
| 920 | + } |
| 921 | + |
| 922 | + ann := eth.NewPooledTransactionHashesPacket{ |
| 923 | + Types: []byte{types.BlobTxType}, |
| 924 | + Sizes: []uint32{uint32(badTx.Size())}, |
| 925 | + Hashes: []common.Hash{badTx.Hash()}, |
| 926 | + } |
| 927 | + |
| 928 | + if err := conn.Write(ethProto, eth.NewPooledTransactionHashesMsg, ann); err != nil { |
| 929 | + errc <- fmt.Errorf("sending announcement failed: %v", err) |
| 930 | + return |
| 931 | + } |
| 932 | + |
| 933 | + req, err := readUntil[eth.GetPooledTransactionsPacket](context.Background(), conn) |
| 934 | + if err != nil { |
| 935 | + errc <- fmt.Errorf("failed to read GetPooledTransactions message: %v", err) |
| 936 | + return |
| 937 | + } |
| 938 | + |
| 939 | + stage1.Done() |
| 940 | + stage2.Wait() |
| 941 | + |
| 942 | + // the good peer is connected, and has announced the tx. |
| 943 | + // proceed to send the incorrect one from the bad peer. |
| 944 | + |
| 945 | + resp := eth.PooledTransactionsPacket{RequestId: req.RequestId, PooledTransactionsResponse: eth.PooledTransactionsResponse(types.Transactions{badTx})} |
| 946 | + if err := conn.Write(ethProto, eth.PooledTransactionsMsg, resp); err != nil { |
| 947 | + errc <- fmt.Errorf("writing pooled tx response failed: %v", err) |
| 948 | + return |
| 949 | + } |
| 950 | + if !readUntilDisconnect(conn) { |
| 951 | + errc <- fmt.Errorf("expected bad peer to be disconnected") |
| 952 | + return |
| 953 | + } |
| 954 | + stage3.Done() |
| 955 | + } |
| 956 | + |
| 957 | + goodPeer := func() { |
| 958 | + stage1.Wait() |
| 959 | + |
| 960 | + conn, err := s.dial() |
| 961 | + if err != nil { |
| 962 | + errc <- fmt.Errorf("dial fail: %v", err) |
| 963 | + return |
| 964 | + } |
| 965 | + defer conn.Close() |
| 966 | + |
| 967 | + if err := conn.peer(s.chain, nil); err != nil { |
| 968 | + errc <- fmt.Errorf("peering failed: %v", err) |
| 969 | + return |
| 970 | + } |
| 971 | + |
| 972 | + ann := eth.NewPooledTransactionHashesPacket{ |
| 973 | + Types: []byte{types.BlobTxType}, |
| 974 | + Sizes: []uint32{uint32(tx.Size())}, |
| 975 | + Hashes: []common.Hash{tx.Hash()}, |
| 976 | + } |
| 977 | + |
| 978 | + if err := conn.Write(ethProto, eth.NewPooledTransactionHashesMsg, ann); err != nil { |
| 979 | + errc <- fmt.Errorf("sending announcement failed: %v", err) |
| 980 | + return |
| 981 | + } |
| 982 | + |
| 983 | + // wait until the bad peer has transmitted the incorrect transaction |
| 984 | + stage2.Done() |
| 985 | + stage3.Wait() |
| 986 | + |
| 987 | + // the bad peer has transmitted the bad tx, and been disconnected. |
| 988 | + // transmit the same tx but with correct sidecar from the good peer. |
| 989 | + |
| 990 | + var req *eth.GetPooledTransactionsPacket |
| 991 | + req, err = readUntil[eth.GetPooledTransactionsPacket](context.Background(), conn) |
| 992 | + if err != nil { |
| 993 | + errc <- fmt.Errorf("reading pooled tx request failed: %v", err) |
| 994 | + return |
| 995 | + } |
| 996 | + |
| 997 | + if req.GetPooledTransactionsRequest[0] != tx.Hash() { |
| 998 | + errc <- fmt.Errorf("requested unknown tx hash") |
| 999 | + return |
| 1000 | + } |
| 1001 | + |
| 1002 | + resp := eth.PooledTransactionsPacket{RequestId: req.RequestId, PooledTransactionsResponse: eth.PooledTransactionsResponse(types.Transactions{tx})} |
| 1003 | + if err := conn.Write(ethProto, eth.PooledTransactionsMsg, resp); err != nil { |
| 1004 | + errc <- fmt.Errorf("writing pooled tx response failed: %v", err) |
| 1005 | + return |
| 1006 | + } |
| 1007 | + if readUntilDisconnect(conn) { |
| 1008 | + errc <- fmt.Errorf("unexpected disconnect") |
| 1009 | + return |
| 1010 | + } |
| 1011 | + close(errc) |
| 1012 | + } |
| 1013 | + |
| 1014 | + if err := s.engine.sendForkchoiceUpdated(); err != nil { |
| 1015 | + t.Fatalf("send fcu failed: %v", err) |
| 1016 | + } |
| 1017 | + |
| 1018 | + go goodPeer() |
| 1019 | + go badPeer() |
| 1020 | + err := <-errc |
| 1021 | + if err != nil { |
| 1022 | + t.Fatalf("%v", err) |
| 1023 | + } |
| 1024 | +} |
0 commit comments