lint, cleanup

This commit is contained in:
Roy Crihfield 2023-04-04 11:42:41 +08:00
parent b275cef114
commit 12590d2045
2 changed files with 9 additions and 32 deletions

View File

@ -19,6 +19,7 @@ package statediff_test
import (
"bytes"
"context"
"errors"
"math/big"
"math/rand"
"reflect"
@ -206,7 +207,6 @@ func testErrorInBlockLoop(t *testing.T) {
}()
service.Loop(eventsChannel)
// defaultParams.ComputeWatchedAddressesLeafPaths()
if !reflect.DeepEqual(builder.Params, defaultParams) {
t.Error("Test failure:", t.Name())
t.Logf("Actual params does not equal expected.\nactual:%+v\nexpected: %+v", builder.Params, defaultParams)
@ -277,7 +277,6 @@ func TestGetStateDiffAt(t *testing.T) {
t.Error(err)
}
// defaultParams.ComputeWatchedAddressesLeafPaths()
if !reflect.DeepEqual(builder.Params, defaultParams) {
t.Error("Test failure:", t.Name())
t.Logf("Actual params does not equal expected.\nactual:%+v\nexpected: %+v", builder.Params, defaultParams)
@ -321,11 +320,11 @@ func subscribeWrites(ctx context.Context, svc *statediff.Service) (writeSub, err
}
client := rpc.DialInProc(server)
statusChan := make(chan statediff.JobStatus)
sub, err := client.Subscribe(context.Background(), "statediff", statusChan, "streamWrites")
sub, err := client.Subscribe(ctx, "statediff", statusChan, "streamWrites")
return writeSub{sub, statusChan, client}, err
}
func awaitJob(ws writeSub, job statediff.JobID, ctx context.Context) (bool, error) {
func awaitJob(ws writeSub, job statediff.JobID, timeout time.Duration) (bool, error) {
for {
select {
case err := <-ws.sub.Err():
@ -337,8 +336,8 @@ func awaitJob(ws writeSub, job statediff.JobID, ctx context.Context) (bool, erro
if status.ID == job {
return true, nil
}
case <-ctx.Done():
return false, ctx.Err()
case <-time.After(timeout):
return false, errors.New("timeout")
}
}
}
@ -358,15 +357,14 @@ func TestWriteStateDiffAt(t *testing.T) {
// delay to avoid subscription request being sent after statediff is written,
// and timeout to prevent hanging just in case it still happens
writeDelay := time.Second
writeDelay := 100 * time.Millisecond
jobTimeout := time.Second
ws, err := subscribeWrites(context.Background(), service)
require.NoError(t, err)
defer ws.close()
time.Sleep(writeDelay)
job := service.WriteStateDiffAt(testBlock1.NumberU64(), defaultParams)
ctx, _ := context.WithTimeout(context.Background(), jobTimeout)
ok, err := awaitJob(ws, job, ctx)
ok, err := awaitJob(ws, job, jobTimeout)
require.NoError(t, err)
require.True(t, ok)

View File

@ -30,31 +30,19 @@ var _ interfaces.StateDiffIndexer = &StateDiffIndexer{}
var _ interfaces.Batch = &batch{}
// StateDiffIndexer is a mock state diff indexer
type StateDiffIndexer struct {
StateNodes []sdtypes.StateLeafNode
IPLDs []sdtypes.IPLD
}
type StateDiffIndexer struct{}
type batch struct {
sdi *StateDiffIndexer
StateNodes []sdtypes.StateLeafNode
IPLDs []sdtypes.IPLD
}
type batch struct{}
func (sdi *StateDiffIndexer) PushBlock(block *types.Block, receipts types.Receipts, totalDifficulty *big.Int) (interfaces.Batch, error) {
return &batch{}, nil
}
func (sdi *StateDiffIndexer) PushStateNode(txi interfaces.Batch, stateNode sdtypes.StateLeafNode, headerID string) error {
tx := txi.(*batch)
tx.StateNodes = append(tx.StateNodes, stateNode)
return nil
}
func (sdi *StateDiffIndexer) PushIPLD(txi interfaces.Batch, ipld sdtypes.IPLD) error {
tx := txi.(*batch)
tx.IPLDs = append(tx.IPLDs, ipld)
return nil
}
@ -85,14 +73,5 @@ func (sdi *StateDiffIndexer) Close() error {
}
func (tx *batch) Submit(err error) error {
if err != nil {
return err
}
for _, sn := range tx.StateNodes {
tx.sdi.StateNodes = append(tx.sdi.StateNodes, sn)
}
for _, ipld := range tx.IPLDs {
tx.sdi.IPLDs = append(tx.sdi.IPLDs, ipld)
}
return nil
}