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

feat(scripts): remove deprecated trie-state script #4270

Merged
merged 6 commits into from
Oct 22, 2024
Merged
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
1 change: 0 additions & 1 deletion dot/rpc/modules/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ var (
"state_getPairs",
"state_getKeysPaged",
"state_queryStorage",
"state_trie",
}

// AliasesMethods is a map that links the original methods to their aliases
Expand Down
47 changes: 0 additions & 47 deletions dot/rpc/modules/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/ChainSafe/gossamer/pkg/trie"
)

// StateGetReadProofRequest json fields
Expand Down Expand Up @@ -83,10 +82,6 @@ type StateStorageQueryAtRequest struct {
At common.Hash `json:"at"`
}

type StateTrieAtRequest struct {
At *common.Hash `json:"at"`
}

// StateStorageKeysQuery field to store storage keys
type StateStorageKeysQuery [][]byte

Expand Down Expand Up @@ -117,8 +112,6 @@ type StateStorageResponse string
// StatePairResponse is a key values
type StatePairResponse []interface{}

type StateTrieResponse []string

// StateStorageKeysResponse field for storage keys
type StateStorageKeysResponse []string

Expand Down Expand Up @@ -252,46 +245,6 @@ func (sm *StateModule) GetPairs(_ *http.Request, req *StatePairRequest, res *Sta
return nil
}

// Trie RPC method returns a list of scale encoded trie.Entry{Key byte, Value byte} representing
// all the entries in a trie for a block hash, if no block hash is given then it uses the best block hash
func (sm *StateModule) Trie(_ *http.Request, req *StateTrieAtRequest, res *StateTrieResponse) error {
var blockHash common.Hash

if req.At != nil {
blockHash = *req.At
} else {
blockHash = sm.blockAPI.BestBlockHash()
}

blockHeader, err := sm.blockAPI.GetHeader(blockHash)
if err != nil {
return fmt.Errorf("getting header: %w", err)
}

entries, err := sm.storageAPI.Entries(&blockHeader.StateRoot)
if err != nil {
return fmt.Errorf("getting entries: %w", err)
}

entriesArr := make([]string, 0, len(entries))
for key, value := range entries {
entry := trie.Entry{
Key: []byte(key),
Value: value,
}

encodedEntry, err := scale.Marshal(entry)
if err != nil {
return fmt.Errorf("scale encoding entry: %w", err)
}

entriesArr = append(entriesArr, common.BytesToHex(encodedEntry))
}

*res = entriesArr
return nil
}

// Call makes a call to the runtime.
func (sm *StateModule) Call(_ *http.Request, req *StateCallRequest, res *StateCallResponse) error {
var blockHash common.Hash
Expand Down
93 changes: 0 additions & 93 deletions dot/rpc/modules/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ package modules
import (
"errors"
"net/http"
"slices"
"testing"

wazero_runtime "github.com/ChainSafe/gossamer/lib/runtime/wazero"
"github.com/ChainSafe/gossamer/pkg/trie"

"github.com/ChainSafe/gossamer/dot/rpc/modules/mocks"
testdata "github.com/ChainSafe/gossamer/dot/rpc/modules/test_data"
Expand All @@ -32,7 +30,6 @@ import (
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

Expand Down Expand Up @@ -311,96 +308,6 @@ func TestCall(t *testing.T) {
assert.NotEmpty(t, res)
}

func TestStateTrie(t *testing.T) {
expecificBlockHash := common.Hash([32]byte{6, 6, 6, 6, 6, 6})
var expectedEncodedSlice []string
entries := []trie.Entry{
{Key: []byte("entry-1"), Value: []byte{0, 1, 2, 3}},
{Key: []byte("entry-2"), Value: []byte{3, 4, 5, 6}},
}

for _, entry := range entries {
expectedEncodedSlice = append(expectedEncodedSlice, common.BytesToHex(scale.MustMarshal(entry)))
}

testcases := map[string]struct {
request StateTrieAtRequest
newStateModule func(t *testing.T) *StateModule
expected StateTrieResponse
}{
"blockhash_parameter_nil": {
request: StateTrieAtRequest{At: nil},
expected: expectedEncodedSlice,
newStateModule: func(t *testing.T) *StateModule {
ctrl := gomock.NewController(t)

bestBlockHash := common.Hash([32]byte{1, 0, 1, 0, 1})
blockAPIMock := NewMockBlockAPI(ctrl)
blockAPIMock.EXPECT().BestBlockHash().Return(bestBlockHash)

fakeStateRoot := common.Hash([32]byte{5, 5, 5, 5, 5})
fakeBlockHeader := types.NewHeader(common.EmptyHash, fakeStateRoot,
common.EmptyHash, 1, nil)

blockAPIMock.EXPECT().GetHeader(bestBlockHash).Return(fakeBlockHeader, nil)

fakeEntries := map[string][]byte{
"entry-1": {0, 1, 2, 3},
"entry-2": {3, 4, 5, 6},
}
storageAPIMock := NewMockStorageAPI(ctrl)
storageAPIMock.EXPECT().Entries(&fakeStateRoot).
Return(fakeEntries, nil)

sm := NewStateModule(nil, storageAPIMock, nil, blockAPIMock)
return sm
},
},
"blockhash_parameter_not_nil": {
request: StateTrieAtRequest{At: &expecificBlockHash},
expected: expectedEncodedSlice,
newStateModule: func(t *testing.T) *StateModule {
ctrl := gomock.NewController(t)
blockAPIMock := NewMockBlockAPI(ctrl)

fakeStateRoot := common.Hash([32]byte{5, 5, 5, 5, 5})
fakeBlockHeader := types.NewHeader(common.EmptyHash, fakeStateRoot,
common.EmptyHash, 1, nil)

blockAPIMock.EXPECT().GetHeader(expecificBlockHash).
Return(fakeBlockHeader, nil)

fakeEntries := map[string][]byte{
"entry-1": {0, 1, 2, 3},
"entry-2": {3, 4, 5, 6},
}
storageAPIMock := NewMockStorageAPI(ctrl)
storageAPIMock.EXPECT().Entries(&fakeStateRoot).
Return(fakeEntries, nil)

sm := NewStateModule(nil, storageAPIMock, nil, blockAPIMock)
return sm
},
},
}

for tname, tt := range testcases {
tt := tt

t.Run(tname, func(t *testing.T) {
sm := tt.newStateModule(t)

var res StateTrieResponse
err := sm.Trie(nil, &tt.request, &res)
require.NoError(t, err)

slices.Sort(tt.expected)
slices.Sort(res)
require.Equal(t, tt.expected, res)
})
}
}

func TestStateModuleGetMetadata(t *testing.T) {
ctrl := gomock.NewController(t)

Expand Down
139 changes: 0 additions & 139 deletions scripts/trie_state/trie_state_script.go

This file was deleted.

Loading
Loading