|
| 1 | +package ring |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + "google.golang.org/grpc" |
| 9 | + "google.golang.org/protobuf/proto" |
| 10 | + "google.golang.org/protobuf/types/known/emptypb" |
| 11 | + |
| 12 | + "github.com/smartcontractkit/chainlink-common/pkg/logger" |
| 13 | + "github.com/smartcontractkit/chainlink-common/pkg/workflows/ring/pb" |
| 14 | + "github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3types" |
| 15 | + "github.com/smartcontractkit/libocr/offchainreporting2plus/types" |
| 16 | +) |
| 17 | + |
| 18 | +type mockArbiterScaler struct { |
| 19 | + called bool |
| 20 | + nShards uint32 |
| 21 | +} |
| 22 | + |
| 23 | +func (m *mockArbiterScaler) Status(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*pb.ReplicaStatus, error) { |
| 24 | + return &pb.ReplicaStatus{}, nil |
| 25 | +} |
| 26 | + |
| 27 | +func (m *mockArbiterScaler) ConsensusWantShards(ctx context.Context, req *pb.ConsensusWantShardsRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) { |
| 28 | + m.called = true |
| 29 | + m.nShards = req.NShards |
| 30 | + return &emptypb.Empty{}, nil |
| 31 | +} |
| 32 | + |
| 33 | +func TestTransmitter_NewTransmitter(t *testing.T) { |
| 34 | + lggr := logger.Test(t) |
| 35 | + store := NewStore() |
| 36 | + tx := NewTransmitter(lggr, store, nil, "test-account") |
| 37 | + require.NotNil(t, tx) |
| 38 | +} |
| 39 | + |
| 40 | +func TestTransmitter_FromAccount(t *testing.T) { |
| 41 | + lggr := logger.Test(t) |
| 42 | + store := NewStore() |
| 43 | + tx := NewTransmitter(lggr, store, nil, "my-account") |
| 44 | + |
| 45 | + account, err := tx.FromAccount(context.Background()) |
| 46 | + require.NoError(t, err) |
| 47 | + require.Equal(t, types.Account("my-account"), account) |
| 48 | +} |
| 49 | + |
| 50 | +func TestTransmitter_Transmit(t *testing.T) { |
| 51 | + lggr := logger.Test(t) |
| 52 | + store := NewStore() |
| 53 | + mock := &mockArbiterScaler{} |
| 54 | + tx := NewTransmitter(lggr, store, mock, "test-account") |
| 55 | + |
| 56 | + outcome := &pb.Outcome{ |
| 57 | + State: &pb.RoutingState{ |
| 58 | + Id: 1, |
| 59 | + State: &pb.RoutingState_RoutableShards{RoutableShards: 3}, |
| 60 | + }, |
| 61 | + Routes: map[string]*pb.WorkflowRoute{ |
| 62 | + "wf-1": {Shard: 0}, |
| 63 | + "wf-2": {Shard: 1}, |
| 64 | + }, |
| 65 | + } |
| 66 | + outcomeBytes, err := proto.Marshal(outcome) |
| 67 | + require.NoError(t, err) |
| 68 | + |
| 69 | + report := ocr3types.ReportWithInfo[[]byte]{Report: outcomeBytes} |
| 70 | + err = tx.Transmit(context.Background(), types.ConfigDigest{}, 0, report, nil) |
| 71 | + require.NoError(t, err) |
| 72 | + |
| 73 | + // Verify arbiter was notified |
| 74 | + require.True(t, mock.called) |
| 75 | + require.Equal(t, uint32(3), mock.nShards) |
| 76 | + |
| 77 | + // Verify store was updated |
| 78 | + require.Equal(t, uint32(3), store.GetRoutingState().GetRoutableShards()) |
| 79 | + routes := store.GetAllRoutingState() |
| 80 | + require.Equal(t, uint32(0), routes["wf-1"]) |
| 81 | + require.Equal(t, uint32(1), routes["wf-2"]) |
| 82 | +} |
| 83 | + |
| 84 | +func TestTransmitter_Transmit_NilArbiter(t *testing.T) { |
| 85 | + lggr := logger.Test(t) |
| 86 | + store := NewStore() |
| 87 | + tx := NewTransmitter(lggr, store, nil, "test-account") |
| 88 | + |
| 89 | + outcome := &pb.Outcome{ |
| 90 | + State: &pb.RoutingState{ |
| 91 | + Id: 1, |
| 92 | + State: &pb.RoutingState_RoutableShards{RoutableShards: 2}, |
| 93 | + }, |
| 94 | + Routes: map[string]*pb.WorkflowRoute{"wf-1": {Shard: 0}}, |
| 95 | + } |
| 96 | + outcomeBytes, _ := proto.Marshal(outcome) |
| 97 | + |
| 98 | + err := tx.Transmit(context.Background(), types.ConfigDigest{}, 0, ocr3types.ReportWithInfo[[]byte]{Report: outcomeBytes}, nil) |
| 99 | + require.NoError(t, err) |
| 100 | +} |
| 101 | + |
| 102 | +func TestTransmitter_Transmit_TransitionState(t *testing.T) { |
| 103 | + lggr := logger.Test(t) |
| 104 | + store := NewStore() |
| 105 | + mock := &mockArbiterScaler{} |
| 106 | + tx := NewTransmitter(lggr, store, mock, "test-account") |
| 107 | + |
| 108 | + outcome := &pb.Outcome{ |
| 109 | + State: &pb.RoutingState{ |
| 110 | + Id: 1, |
| 111 | + State: &pb.RoutingState_Transition{ |
| 112 | + Transition: &pb.Transition{WantShards: 5}, |
| 113 | + }, |
| 114 | + }, |
| 115 | + } |
| 116 | + outcomeBytes, _ := proto.Marshal(outcome) |
| 117 | + |
| 118 | + err := tx.Transmit(context.Background(), types.ConfigDigest{}, 0, ocr3types.ReportWithInfo[[]byte]{Report: outcomeBytes}, nil) |
| 119 | + require.NoError(t, err) |
| 120 | + require.Equal(t, uint32(5), mock.nShards) |
| 121 | +} |
0 commit comments