Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BCFR-476] [common] - CR methods should throw an error if service is unstarted #874

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -310,6 +311,14 @@ func (it *fakeContractReaderInterfaceTester) Setup(_ *testing.T) {
}
}

func (it *fakeContractReaderInterfaceTester) StartServices(ctx context.Context, t *testing.T) {
require.NoError(t, it.impl.Start(ctx))
}

func (it *fakeContractReaderInterfaceTester) CloseServices(t *testing.T) {
require.NoError(t, it.impl.Close())
}

func (it *fakeContractReaderInterfaceTester) GetContractReader(_ *testing.T) types.ContractReader {
return it.impl
}
Expand Down Expand Up @@ -358,8 +367,11 @@ type fakeContractReader struct {
stored map[string][]TestStruct
batchStored BatchCallEntry
lock sync.Mutex
isStarted atomic.Bool
}

var errServiceNotStarted = errors.New("ContractReader service not started")

type fakeChainWriter struct {
types.ChainWriter
cr *fakeContractReader
Expand Down Expand Up @@ -407,9 +419,15 @@ func (f *fakeChainWriter) GetFeeComponents(ctx context.Context) (*types.ChainFee
return &types.ChainFeeComponents{}, nil
}

func (f *fakeContractReader) Start(_ context.Context) error { return nil }
func (f *fakeContractReader) Start(_ context.Context) error {
f.isStarted.Store(true)
return nil
}

func (f *fakeContractReader) Close() error { return nil }
func (f *fakeContractReader) Close() error {
f.isStarted.Store(false)
return nil
}

func (f *fakeContractReader) Ready() error { panic("unimplemented") }

Expand Down Expand Up @@ -453,6 +471,9 @@ func (f *fakeContractReader) SetBatchLatestValues(batchCallEntry BatchCallEntry)
}

func (f *fakeContractReader) GetLatestValue(_ context.Context, readIdentifier string, confidenceLevel primitives.ConfidenceLevel, params, returnVal any) error {
if !f.isStarted.Load() {
return errServiceNotStarted
}
split := strings.Split(readIdentifier, "-")
contractName := strings.Join([]string{split[0], split[1]}, "-")
if strings.HasSuffix(readIdentifier, MethodReturningAlterableUint64) {
Expand Down Expand Up @@ -546,6 +567,9 @@ func (f *fakeContractReader) GetLatestValue(_ context.Context, readIdentifier st
}

func (f *fakeContractReader) BatchGetLatestValues(_ context.Context, request types.BatchGetLatestValuesRequest) (types.BatchGetLatestValuesResult, error) {
if !f.isStarted.Load() {
return nil, errServiceNotStarted
}
result := make(types.BatchGetLatestValuesResult)
for requestContract, requestContractBatch := range request {
storedContractBatch := f.batchStored[requestContract]
Expand Down Expand Up @@ -599,6 +623,9 @@ func (f *fakeContractReader) BatchGetLatestValues(_ context.Context, request typ
}

func (f *fakeContractReader) QueryKey(_ context.Context, bc types.BoundContract, filter query.KeyFilter, limitAndSort query.LimitAndSort, sequenceType any) ([]types.Sequence, error) {
if !f.isStarted.Load() {
return nil, errServiceNotStarted
}
_, isValueType := sequenceType.(*values.Value)
if filter.Key == EventName {
f.lock.Lock()
Expand Down Expand Up @@ -838,7 +865,8 @@ func runContractReaderByIDGetLatestValue(t *testing.T) {
toBind := make(map[string]types.BoundContract)
ctx := tests.Context(t)
cr := chainreader.WrapContractReaderByIDs(tester.GetContractReader(t))

tester.StartServices(ctx, t)
defer tester.CloseServices(t)
anyContract := BindingsByName(tester.GetBindings(t), AnyContractName)[0]
anySecondContract := BindingsByName(tester.GetBindings(t), AnySecondContractName)[0]

Expand All @@ -864,7 +892,8 @@ func runContractReaderByIDGetLatestValue(t *testing.T) {
toBind := make(map[string]types.BoundContract)
ctx := tests.Context(t)
cr := chainreader.WrapContractReaderByIDs(tester.GetContractReader(t))

tester.StartServices(ctx, t)
defer tester.CloseServices(t)
anyContracts := BindingsByName(tester.GetBindings(t), AnyContractName)
anyContract1, anyContract2 := anyContracts[0], anyContracts[1]
anyContractID1, anyContractID2 := "1-"+anyContract1.String(), "2-"+anyContract2.String()
Expand Down Expand Up @@ -903,7 +932,8 @@ func runContractReaderByIDBatchGetLatestValues(t *testing.T) {
toBind := make(map[string]types.BoundContract)
ctx := tests.Context(t)
cr := chainreader.WrapContractReaderByIDs(tester.GetContractReader(t))

tester.StartServices(ctx, t)
defer tester.CloseServices(t)
anyContract := BindingsByName(tester.GetBindings(t), AnyContractName)[0]
anyContractID := "1-" + anyContract.String()
toBind[anyContractID] = anyContract
Expand Down Expand Up @@ -940,7 +970,8 @@ func runContractReaderByIDBatchGetLatestValues(t *testing.T) {
toBind := make(map[string]types.BoundContract)
ctx := tests.Context(t)
cr := chainreader.WrapContractReaderByIDs(tester.GetContractReader(t))

tester.StartServices(ctx, t)
defer tester.CloseServices(t)
anyContracts := BindingsByName(tester.GetBindings(t), AnyContractName)
anyContract1, anyContract2 := anyContracts[0], anyContracts[1]
anyContractID1, anyContractID2 := "1-"+anyContract1.String(), "2-"+anyContract2.String()
Expand Down Expand Up @@ -1004,7 +1035,8 @@ func runContractReaderByIDQueryKey(t *testing.T) {
toBind := make(map[string]types.BoundContract)
ctx := tests.Context(t)
cr := chainreader.WrapContractReaderByIDs(tester.GetContractReader(t))

tester.StartServices(ctx, t)
defer tester.CloseServices(t)
anyContract := BindingsByName(tester.GetBindings(t), AnyContractName)[0]
anyContractID := "1-" + anyContract.String()
toBind[anyContractID] = anyContract
Expand Down Expand Up @@ -1047,7 +1079,8 @@ func runContractReaderByIDQueryKey(t *testing.T) {
toBind := make(map[string]types.BoundContract)
ctx := tests.Context(t)
cr := chainreader.WrapContractReaderByIDs(tester.GetContractReader(t))

tester.StartServices(ctx, t)
defer tester.CloseServices(t)
anyContract1 := BindingsByName(tester.GetBindings(t), AnyContractName)[0]
anyContract2 := types.BoundContract{Address: "new-" + anyContract1.Address, Name: anyContract1.Name}
anyContractID1, anyContractID2 := "1-"+anyContract1.String(), "2-"+anyContract2.String()
Expand Down
Loading
Loading