@@ -33,8 +33,10 @@ import (
33
33
"github.com/ChainSafe/gossamer/lib/keystore"
34
34
"github.com/ChainSafe/gossamer/lib/runtime"
35
35
"github.com/ChainSafe/gossamer/lib/runtime/extrinsic"
36
+ "github.com/ChainSafe/gossamer/lib/runtime/storage"
36
37
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
37
38
"github.com/ChainSafe/gossamer/lib/transaction"
39
+ "github.com/ChainSafe/gossamer/lib/trie"
38
40
"github.com/ChainSafe/gossamer/lib/utils"
39
41
log "github.com/ChainSafe/log15"
40
42
"github.com/stretchr/testify/mock"
@@ -619,6 +621,173 @@ func TestService_HandleRuntimeChangesAfterCodeSubstitutes(t *testing.T) {
619
621
require .NotEqualf (t , codeHashBefore , rt .GetCodeHash (), "expected different code hash after runtime update" ) // codeHash should change after runtime change
620
622
}
621
623
624
+ func TestTryQueryStore_WhenThereIsDataToRetrieve (t * testing.T ) {
625
+ s := NewTestService (t , nil )
626
+ storageStateTrie , err := storage .NewTrieState (trie .NewTrie (nil ))
627
+
628
+ testKey , testValue := []byte ("to" ), []byte ("0x1723712318238AB12312" )
629
+ storageStateTrie .Set (testKey , testValue )
630
+ require .NoError (t , err )
631
+
632
+ header , err := types .NewHeader (s .blockState .GenesisHash (), storageStateTrie .MustRoot (),
633
+ common.Hash {}, big .NewInt (1 ), nil )
634
+ require .NoError (t , err )
635
+
636
+ err = s .storageState .StoreTrie (storageStateTrie , header )
637
+ require .NoError (t , err )
638
+
639
+ testBlock := & types.Block {
640
+ Header : header ,
641
+ Body : types .NewBody ([]byte {}),
642
+ }
643
+
644
+ err = s .blockState .AddBlock (testBlock )
645
+ require .NoError (t , err )
646
+
647
+ blockhash := testBlock .Header .Hash ()
648
+ hexKey := common .BytesToHex (testKey )
649
+ keys := []string {hexKey }
650
+
651
+ changes , err := s .tryQueryStorage (blockhash , keys ... )
652
+ require .NoError (t , err )
653
+
654
+ require .Equal (t , changes [hexKey ], common .BytesToHex (testValue ))
655
+ }
656
+
657
+ func TestTryQueryStore_WhenDoesNotHaveDataToRetrieve (t * testing.T ) {
658
+ s := NewTestService (t , nil )
659
+ storageStateTrie , err := storage .NewTrieState (trie .NewTrie (nil ))
660
+ require .NoError (t , err )
661
+
662
+ header , err := types .NewHeader (s .blockState .GenesisHash (), storageStateTrie .MustRoot (),
663
+ common.Hash {}, big .NewInt (1 ), nil )
664
+ require .NoError (t , err )
665
+
666
+ err = s .storageState .StoreTrie (storageStateTrie , header )
667
+ require .NoError (t , err )
668
+
669
+ testBlock := & types.Block {
670
+ Header : header ,
671
+ Body : types .NewBody ([]byte {}),
672
+ }
673
+
674
+ err = s .blockState .AddBlock (testBlock )
675
+ require .NoError (t , err )
676
+
677
+ testKey := []byte ("to" )
678
+ blockhash := testBlock .Header .Hash ()
679
+ hexKey := common .BytesToHex (testKey )
680
+ keys := []string {hexKey }
681
+
682
+ changes , err := s .tryQueryStorage (blockhash , keys ... )
683
+ require .NoError (t , err )
684
+
685
+ require .Empty (t , changes )
686
+ }
687
+
688
+ func TestTryQueryState_WhenDoesNotHaveStateRoot (t * testing.T ) {
689
+ s := NewTestService (t , nil )
690
+
691
+ header , err := types .NewHeader (s .blockState .GenesisHash (), common.Hash {}, common.Hash {}, big .NewInt (1 ), nil )
692
+ require .NoError (t , err )
693
+
694
+ testBlock := & types.Block {
695
+ Header : header ,
696
+ Body : types .NewBody ([]byte {}),
697
+ }
698
+
699
+ err = s .blockState .AddBlock (testBlock )
700
+ require .NoError (t , err )
701
+
702
+ testKey := []byte ("to" )
703
+ blockhash := testBlock .Header .Hash ()
704
+ hexKey := common .BytesToHex (testKey )
705
+ keys := []string {hexKey }
706
+
707
+ changes , err := s .tryQueryStorage (blockhash , keys ... )
708
+ require .Error (t , err )
709
+ require .Nil (t , changes )
710
+ }
711
+
712
+ func TestQueryStorate_WhenBlocksHasData (t * testing.T ) {
713
+ keys := []string {
714
+ common .BytesToHex ([]byte ("transfer.to" )),
715
+ common .BytesToHex ([]byte ("transfer.from" )),
716
+ common .BytesToHex ([]byte ("transfer.value" )),
717
+ }
718
+
719
+ s := NewTestService (t , nil )
720
+
721
+ firstKey , firstValue := []byte ("transfer.to" ), []byte ("some-address-herer" )
722
+ firstBlock := createNewBlockAndStoreDataAtBlock (
723
+ t , s , firstKey , firstValue , s .blockState .GenesisHash (), 1 ,
724
+ )
725
+
726
+ secondKey , secondValue := []byte ("transfer.from" ), []byte ("another-address-here" )
727
+ secondBlock := createNewBlockAndStoreDataAtBlock (
728
+ t , s , secondKey , secondValue , firstBlock .Header .Hash (), 2 ,
729
+ )
730
+
731
+ thirdKey , thirdValue := []byte ("transfer.value" ), []byte ("value-gigamegablaster" )
732
+ thirdBlock := createNewBlockAndStoreDataAtBlock (
733
+ t , s , thirdKey , thirdValue , secondBlock .Header .Hash (), 3 ,
734
+ )
735
+
736
+ from := firstBlock .Header .Hash ()
737
+ data , err := s .QueryStorage (from , common.Hash {}, keys ... )
738
+ require .NoError (t , err )
739
+ require .Len (t , data , 3 )
740
+
741
+ require .Equal (t , data [firstBlock .Header .Hash ()], QueryKeyValueChanges (
742
+ map [string ]string {
743
+ common .BytesToHex (firstKey ): common .BytesToHex (firstValue ),
744
+ },
745
+ ))
746
+
747
+ from = secondBlock .Header .Hash ()
748
+ to := thirdBlock .Header .Hash ()
749
+
750
+ data , err = s .QueryStorage (from , to , keys ... )
751
+ require .NoError (t , err )
752
+ require .Len (t , data , 2 )
753
+
754
+ require .Equal (t , data [secondBlock .Header .Hash ()], QueryKeyValueChanges (
755
+ map [string ]string {
756
+ common .BytesToHex (secondKey ): common .BytesToHex (secondValue ),
757
+ },
758
+ ))
759
+ require .Equal (t , data [thirdBlock .Header .Hash ()], QueryKeyValueChanges (
760
+ map [string ]string {
761
+ common .BytesToHex (thirdKey ): common .BytesToHex (thirdValue ),
762
+ },
763
+ ))
764
+ }
765
+
766
+ func createNewBlockAndStoreDataAtBlock (t * testing.T , s * Service , key , value []byte , parentHash common.Hash , number int64 ) * types.Block {
767
+ t .Helper ()
768
+
769
+ storageStateTrie , err := storage .NewTrieState (trie .NewTrie (nil ))
770
+ storageStateTrie .Set (key , value )
771
+ require .NoError (t , err )
772
+
773
+ header , err := types .NewHeader (parentHash , storageStateTrie .MustRoot (),
774
+ common.Hash {}, big .NewInt (number ), nil )
775
+ require .NoError (t , err )
776
+
777
+ err = s .storageState .StoreTrie (storageStateTrie , header )
778
+ require .NoError (t , err )
779
+
780
+ testBlock := & types.Block {
781
+ Header : header ,
782
+ Body : types .NewBody ([]byte {}),
783
+ }
784
+
785
+ err = s .blockState .AddBlock (testBlock )
786
+ require .NoError (t , err )
787
+
788
+ return testBlock
789
+ }
790
+
622
791
func TestDecodeSessionKeys (t * testing.T ) {
623
792
mockInstance := new (runtimemocks.MockInstance )
624
793
mockInstance .On ("DecodeSessionKeys" , mock .AnythingOfType ("[]uint8" )).Return ([]byte {}, nil ).Once ()
0 commit comments