Skip to content

Commit

Permalink
fix another case of misaligned initialBlocks in stores of the same st…
Browse files Browse the repository at this point in the history
…age, only in dev mode
  • Loading branch information
sduchesneau committed Sep 18, 2024
1 parent 3200e72 commit 356d34d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 11 deletions.
2 changes: 1 addition & 1 deletion orchestrator/stage/modstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s *StoreModuleState) getStore(ctx context.Context, exclusiveEndBlock uint6
}
loadStore := s.storeConfig.NewFullKV(s.logger)
moduleInitBlock := s.storeConfig.ModuleInitialBlock()
if moduleInitBlock != exclusiveEndBlock {
if moduleInitBlock < exclusiveEndBlock { // note that 'endBlock' in a fullKV means the last block inserted in this store, from where we can start
fullKVFile := store.NewCompleteFileInfo(s.name, moduleInitBlock, exclusiveEndBlock)
err := loadStore.Load(ctx, fullKVFile)
if err != nil {
Expand Down
17 changes: 16 additions & 1 deletion test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,6 @@ func TestOneStoreOneMap(t *testing.T) {
mapOutput := run.MapOutput("assert_test_store_add_i64")
assert.Contains(t, mapOutput, `assert_test_store_add_i64: 0801`)

fmt.Println(mapOutput)
assert.Equal(t, test.expectedResponseCount, strings.Count(mapOutput, "\n"))

withZST := func(s []string) []string {
Expand Down Expand Up @@ -411,6 +410,22 @@ func TestMultipleStoresDifferentStartBlocks(t *testing.T) {

require.NoError(t, run2.RunWithTempDir(t, "pass two", run.TempDir))
}

func TestMultipleStoresUnalignedStartBlocksDevMode(t *testing.T) {
manifest.TestUseSimpleHash = true
// dev mode
run2 := newTestRun(t, 23, 999, 30, 0, "multi_store_different_23", "./testdata/complex_substreams/complex-substreams-v0.1.0.spkg")
require.NoError(t, run2.Run(t, "dev_mode"))
fmt.Println(run2.MapOutput("multi_store_different_23"))
}
func TestMultipleStoresUnalignedStartBlocksProdMode(t *testing.T) {
manifest.TestUseSimpleHash = true
run2 := newTestRun(t, 23, 999, 30, 0, "multi_store_different_23", "./testdata/complex_substreams/complex-substreams-v0.1.0.spkg")
run2.ProductionMode = true
require.NoError(t, run2.Run(t, "prod_mode"))
fmt.Println(run2.MapOutput("multi_store_different_23"))
}

func TestStoreDeletePrefix(t *testing.T) {
run := newTestRun(t, 30, 40, 42, 0, "assert_test_store_delete_prefix", "./testdata/simple_substreams/substreams-test-v0.1.0.spkg")
run.BlockProcessedCallback = func(ctx *execContext) {
Expand Down
Binary file modified test/testdata/complex_substreams/complex-substreams-v0.1.0.spkg
Binary file not shown.
35 changes: 26 additions & 9 deletions test/testdata/complex_substreams/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ fn first_store_init_20(block: test::Block, first_store: StoreAddInt64) {
first_store.add(0, "block_counter", 1);
}

#[substreams::handlers::store]
fn first_store_init_23(block: test::Block, first_store: StoreAddInt64) {
first_store.add(0, "block_counter", 1);
}

#[substreams::handlers::store]
fn second_store_init_30(block: test::Block, first_store: StoreGetInt64, second_store: StoreSetInt64) {
let block_counter = first_store.get_last("block_counter").unwrap();
Expand Down Expand Up @@ -73,8 +78,20 @@ fn assert_first_store_init_20(block: test::Block, first_store: StoreGetInt64) ->
}

#[substreams::handlers::map]
fn multi_store_different_40(first_store: store::Deltas<DeltaInt64>, second_store: store::Deltas<DeltaInt64>) -> Result<test::Block, Error> {
fn assert_first_store_init_23(block: test::Block, first_store: StoreGetInt64) -> Result<test::Boolean, Error> {
let block_counter = first_store.get_last("block_counter");

if block.number < 23 {
assert!(block_counter.is_none());
return Ok(test::Boolean { result: true })
}

assert_eq!(block_counter.unwrap(), (block.number - 22) as i64);
Ok(test::Boolean { result: true })
}

#[substreams::handlers::map]
fn multi_store_different_40(first_store: store::Deltas<DeltaInt64>, second_store: store::Deltas<DeltaString>) -> Result<test::Block, Error> {
first_store
.deltas
.iter()
Expand All @@ -83,20 +100,20 @@ fn multi_store_different_40(first_store: store::Deltas<DeltaInt64>, second_store
x => panic!("unhandled key {}", x),
});

second_store
.deltas
.iter()
.for_each(|delta| match delta.key.as_str() {
"block_counter" => log::info!("second_store: block_counter: {}", delta.new_value),
x => panic!("unhandled key {}", x),
});

Ok(test::Block {
id: "1".to_string(),
number: 1,
})
}

#[substreams::handlers::map]
fn multi_store_different_23(first_store: store::Deltas<DeltaInt64>, second_store: store::Deltas<DeltaString>) -> Result<test::Block, Error> {
Ok(test::Block {
id: "hehehehe".to_string(),
number: 5,
})
}

#[substreams::handlers::map]
fn assert_first_store_deltas_init_20(block: test::Block, first_store: store::Deltas<DeltaInt64>) -> Result<test::Boolean, Error> {
let mut block_counter = None;
Expand Down
30 changes: 30 additions & 0 deletions test/testdata/complex_substreams/substreams.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ modules:
output:
type: proto:sf.substreams.v1.test.Boolean


- name: first_store_init_23
kind: store
initialBlock: 23
updatePolicy: add
valueType: int64
inputs:
- source: sf.substreams.v1.test.Block

- name: assert_first_store_init_23
kind: map
initialBlock: 23
inputs:
- source: sf.substreams.v1.test.Block
- store: first_store_init_23
output:
type: proto:sf.substreams.v1.test.Boolean

- name: second_store_init_30
kind: store
initialBlock: 30
Expand Down Expand Up @@ -213,7 +231,19 @@ modules:
initialBlock: 40
inputs:
- store: first_store_init_20
mode: deltas
- store: set_sum_store_init_0
mode: deltas
output:
type: proto:sf.substreams.v1.test.Block

- name: multi_store_different_23
kind: map
initialBlock: 23
inputs:
- store: first_store_init_23
mode: deltas
- store: set_sum_store_init_0
mode: deltas
output:
type: proto:sf.substreams.v1.test.Block

0 comments on commit 356d34d

Please sign in to comment.