-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dot/rpc) implement
offchain_localStorageSet
and `offchain_loca…
…lStorageGet` (#1774) * feat: add offchain set and get RPC methods * chore: implement unit tests * chore: fix deepsource * chore: fix mocks deepsource warnings * chore: fix deepsource warns * chore: remove control param * chore: remove os.Exit() call outside main func * chore: increase the test coverage * chore: remove comment
- Loading branch information
1 parent
44b7216
commit a91de8b
Showing
13 changed files
with
380 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package modules | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/ChainSafe/gossamer/lib/common" | ||
) | ||
|
||
const ( | ||
offchainPersistent = "PERSISTENT" | ||
offchainLocal = "LOCAL" | ||
) | ||
|
||
// OffchainLocalStorageGet represents the request format to retrieve data from offchain storage | ||
type OffchainLocalStorageGet struct { | ||
Kind string | ||
Key string | ||
} | ||
|
||
// OffchainLocalStorageSet represents the request format to store data into offchain storage | ||
type OffchainLocalStorageSet struct { | ||
Kind string | ||
Key string | ||
Value string | ||
} | ||
|
||
// OffchainModule defines the RPC module to Offchain methods | ||
type OffchainModule struct { | ||
nodeStorage RuntimeStorageAPI | ||
} | ||
|
||
// NewOffchainModule creates a RPC module to Offchain methods | ||
func NewOffchainModule(ns RuntimeStorageAPI) *OffchainModule { | ||
return &OffchainModule{ | ||
nodeStorage: ns, | ||
} | ||
} | ||
|
||
// LocalStorageGet get offchain local storage under given key and prefix | ||
func (s *OffchainModule) LocalStorageGet(_ *http.Request, req *OffchainLocalStorageGet, res *StringResponse) error { | ||
var ( | ||
v []byte | ||
key []byte | ||
err error | ||
) | ||
|
||
if key, err = common.HexToBytes(req.Key); err != nil { | ||
return err | ||
} | ||
|
||
switch req.Kind { | ||
case offchainPersistent: | ||
v, err = s.nodeStorage.GetPersistent(key) | ||
case offchainLocal: | ||
v, err = s.nodeStorage.GetLocal(key) | ||
default: | ||
return fmt.Errorf("storage kind not found: %s", req.Kind) | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
*res = StringResponse(common.BytesToHex(v)) | ||
return nil | ||
} | ||
|
||
// LocalStorageSet set offchain local storage under given key and prefix | ||
func (s *OffchainModule) LocalStorageSet(_ *http.Request, req *OffchainLocalStorageSet, _ *StringResponse) error { | ||
var ( | ||
val []byte | ||
key []byte | ||
err error | ||
) | ||
|
||
if key, err = common.HexToBytes(req.Key); err != nil { | ||
return err | ||
} | ||
|
||
if val, err = common.HexToBytes(req.Value); err != nil { | ||
return err | ||
} | ||
|
||
switch req.Kind { | ||
case offchainPersistent: | ||
err = s.nodeStorage.SetPersistent(key, val) | ||
case offchainLocal: | ||
err = s.nodeStorage.SetLocal(key, val) | ||
default: | ||
return fmt.Errorf("storage kind not found: %s", req.Kind) | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.