Skip to content

Commit

Permalink
Merge branch 'sidechannel' of github.com:maticnetwork/heimdall into f…
Browse files Browse the repository at this point in the history
…ix-power-conversion
  • Loading branch information
mankenavenkatesh committed May 17, 2020
2 parents f4c89c7 + b9b9f83 commit 303c05f
Show file tree
Hide file tree
Showing 14 changed files with 283 additions and 80 deletions.
2 changes: 1 addition & 1 deletion checkpoint/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func GetLastNoACK(cdc *codec.Codec) *cobra.Command {
}

var lastNoAck uint64
if err := cliCtx.Codec.UnmarshalJSON(res, &lastNoAck); err != nil {
if err := json.Unmarshal(res, &lastNoAck); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion checkpoint/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func noackHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
}

var lastAckTime uint64
if err := cliCtx.Codec.UnmarshalJSON(res, &lastAckTime); err != nil {
if err := json.Unmarshal(res, &lastAckTime); err != nil {
hmRest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
Expand Down
65 changes: 50 additions & 15 deletions clerk/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rest
import (
"fmt"
"net/http"
"time"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/types/rest"
Expand All @@ -18,7 +19,6 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
"/clerk/event-record/list",
recordListHandlerFn(cliCtx),
).Methods("GET")

r.HandleFunc(
"/clerk/event-record/{recordId}",
recordHandlerFn(cliCtx),
Expand Down Expand Up @@ -72,32 +72,67 @@ func recordListHandlerFn(
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := r.URL.Query()
var queryParams []byte
var err error
var query string

cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}

// get page
page, ok := rest.ParseUint64OrReturnBadRequest(w, vars.Get("page"))
if !ok {
return
page := uint64(1) // default page
if vars.Get("page") != "" {
_page, ok := rest.ParseUint64OrReturnBadRequest(w, vars.Get("page"))
if !ok {
return
}

page = _page
}

// get limit
limit, ok := rest.ParseUint64OrReturnBadRequest(w, vars.Get("limit"))
if !ok {
return
limit := uint64(50) // default limit
if vars.Get("limit") != "" {
_limit, ok := rest.ParseUint64OrReturnBadRequest(w, vars.Get("limit"))
if !ok {
return
}

limit = _limit
}

// get query params
queryParams, err := cliCtx.Codec.MarshalJSON(hmTypes.NewQueryPaginationParams(page, limit))
if err != nil {
return
if vars.Get("from-time") != "" && vars.Get("to-time") != "" {
// get from time (epoch)
fromTime, ok := rest.ParseInt64OrReturnBadRequest(w, vars.Get("from-time"))
if !ok {
return
}

// get to time (epoch)
toTime, ok := rest.ParseInt64OrReturnBadRequest(w, vars.Get("to-time"))
if !ok {
return
}

// get query params
queryParams, err = cliCtx.Codec.MarshalJSON(types.NewQueryTimeRangePaginationParams(time.Unix(fromTime, 0), time.Unix(toTime, 0), page, limit))
if err != nil {
return
}

query = types.QueryRecordListWithTime
} else {
// get query params
queryParams, err = cliCtx.Codec.MarshalJSON(hmTypes.NewQueryPaginationParams(page, limit))
if err != nil {
return
}

query = types.QueryRecordList
}

// query records
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryRecordList), queryParams)
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, query), queryParams)
if err != nil {
hmRest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
Expand All @@ -112,7 +147,7 @@ func recordListHandlerFn(
}
}

// Returns deposit tx status information
// DepositTxStatusHandlerFn returns deposit tx status information
func DepositTxStatusHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := r.URL.Query()
Expand Down
2 changes: 1 addition & 1 deletion clerk/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (suite *GenesisTestSuite) TestInitExportGenesis() {
for i := range eventRecords {
hAddr := hmTypes.BytesToHeimdallAddress([]byte(strconv.Itoa(simulation.RandIntBetween(r1, 1000, 100000))))
hHash := hmTypes.BytesToHeimdallHash([]byte(strconv.Itoa(simulation.RandIntBetween(r1, 1000, 100000))))
testEventRecord := types.NewEventRecord(hHash, uint64(i), uint64(i), hAddr, make([]byte, 0), strconv.Itoa(simulation.RandIntBetween(r1, 1000, 100000)))
testEventRecord := types.NewEventRecord(hHash, uint64(i), uint64(i), hAddr, make([]byte, 0), strconv.Itoa(simulation.RandIntBetween(r1, 1000, 100000)), time.Now())
eventRecords[i] = &testEventRecord
}
genesisState := types.GenesisState{
Expand Down
1 change: 1 addition & 0 deletions clerk/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func (suite *HandlerTestSuite) TestHandleMsgEventRecord() {
msg.ContractAddress,
msg.Data,
msg.ChainID,
time.Now(),
),
)

Expand Down
147 changes: 114 additions & 33 deletions clerk/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package clerk
import (
"errors"
"strconv"
"time"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -22,6 +23,8 @@ var (

// RecordSequencePrefixKey represents record sequence prefix key
RecordSequencePrefixKey = []byte{0x12}

StateRecordPrefixKeyWithTime = []byte{0x13} // prefix key for when storing state with time
)

// Keeper stores all related data
Expand Down Expand Up @@ -65,36 +68,66 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", types.ModuleName)
}

// SetEventRecord adds record to store
func (k *Keeper) SetEventRecord(ctx sdk.Context, record types.EventRecord) error {
store := ctx.KVStore(k.storeKey)
key := GetEventRecordKey(record.ID)

// check if already set
if store.Has(key) {
return errors.New("State record already exists")
// SetEventRecordWithTime sets event record id with time
func (k *Keeper) SetEventRecordWithTime(ctx sdk.Context, record types.EventRecord) error {
key := GetEventRecordKeyWithTime(record.ID, record.RecordTime)
value, err := k.cdc.MarshalBinaryBare(record.ID)
if err != nil {
k.Logger(ctx).Error("Error marshalling record", "error", err)
return err
}

// TODO check state from mainchain
if err := k.setEventRecordStore(ctx, key, value); err != nil {
return err
}
return nil
}

// create Checkpoint block and marshall
out, err := k.cdc.MarshalBinaryBare(record)
// SetEventRecordWithID adds record to store with ID
func (k *Keeper) SetEventRecordWithID(ctx sdk.Context, record types.EventRecord) error {
key := GetEventRecordKey(record.ID)
value, err := k.cdc.MarshalBinaryBare(record)
if err != nil {
k.Logger(ctx).Error("Error marshalling record", "error", err)
return err
}

// store in key provided
store.Set(key, out)
if err := k.setEventRecordStore(ctx, key, value); err != nil {
return err
}
return nil
}

// setEventRecordStore adds value to store by key
func (k *Keeper) setEventRecordStore(ctx sdk.Context, key, value []byte) error {
store := ctx.KVStore(k.storeKey)
// check if already set
if store.Has(key) {
return errors.New("Key already exists")
}

// store value in provided key
store.Set(key, value)

// return
return nil
}

// SetEventRecord adds record to store
func (k *Keeper) SetEventRecord(ctx sdk.Context, record types.EventRecord) error {
if err := k.SetEventRecordWithID(ctx, record); err != nil {
return err
}
if err := k.SetEventRecordWithTime(ctx, record); err != nil {
return err
}
return nil
}

// GetEventRecord returns record from store
func (k *Keeper) GetEventRecord(ctx sdk.Context, stateId uint64) (*types.EventRecord, error) {
func (k *Keeper) GetEventRecord(ctx sdk.Context, stateID uint64) (*types.EventRecord, error) {
store := ctx.KVStore(k.storeKey)
key := GetEventRecordKey(stateId)
key := GetEventRecordKey(stateID)

// check store has data
if store.Has(key) {
Expand Down Expand Up @@ -138,14 +171,14 @@ func (k *Keeper) GetEventRecordList(ctx sdk.Context, page uint64, limit uint64)
var records []types.EventRecord

// have max limit
if limit > 20 {
limit = 20
if limit > 50 {
limit = 50
}

// get paginated iterator
iterator := hmTypes.KVStorePrefixIteratorPaginated(store, StateRecordPrefixKey, uint(page), uint(limit))

// loop through validators to get valid validators
// loop through records to get valid records
for ; iterator.Valid(); iterator.Next() {
var record types.EventRecord
if err := k.cdc.UnmarshalBinaryBare(iterator.Value(), &record); err == nil {
Expand All @@ -156,6 +189,43 @@ func (k *Keeper) GetEventRecordList(ctx sdk.Context, page uint64, limit uint64)
return records, nil
}

// GetEventRecordListWithTime returns all records with params like fromTime and toTime
func (k *Keeper) GetEventRecordListWithTime(ctx sdk.Context, fromTime, toTime time.Time, page, limit uint64) ([]types.EventRecord, error) {
var iterator sdk.Iterator
store := ctx.KVStore(k.storeKey)

// create records
var records []types.EventRecord

// have max limit
if limit > 50 {
limit = 50
}

if page == 0 && limit == 0 {
iterator = store.Iterator(GetEventRecordKeyWithTimePrefix(fromTime), GetEventRecordKeyWithTimePrefix(toTime))
} else {
iterator = hmTypes.KVStorePrefixRangeIteratorPaginated(store, uint(page), uint(limit), GetEventRecordKeyWithTimePrefix(fromTime), GetEventRecordKeyWithTimePrefix(toTime))
}

// get range iterator
defer iterator.Close()
// loop through records to get valid records
for ; iterator.Valid(); iterator.Next() {
var stateID uint64
if err := k.cdc.UnmarshalBinaryBare(iterator.Value(), &stateID); err == nil {
record, err := k.GetEventRecord(ctx, stateID)
if err != nil {
k.Logger(ctx).Error("GetEventRecordListWithTime | GetEventRecord", "error", err)
continue
}
records = append(records, *record)
}
}

return records, nil
}

//
// GetEventRecordKey returns key for state record
//
Expand All @@ -166,6 +236,23 @@ func GetEventRecordKey(stateID uint64) []byte {
return append(StateRecordPrefixKey, stateIDBytes...)
}

// GetEventRecordKeyWithTime appends prefix to state id and record time
func GetEventRecordKeyWithTime(stateID uint64, recordTime time.Time) []byte {
stateIDBytes := []byte(strconv.FormatUint(stateID, 10))
return append(GetEventRecordKeyWithTimePrefix(recordTime), stateIDBytes...)
}

// GetEventRecordKeyWithTimePrefix gives prefix for record time key
func GetEventRecordKeyWithTimePrefix(recordTime time.Time) []byte {
recordTimeBytes := sdk.FormatTimeBytes(recordTime)
return append(StateRecordPrefixKeyWithTime, recordTimeBytes...)
}

// GetRecordSequenceKey returns record sequence key
func GetRecordSequenceKey(sequence string) []byte {
return append(RecordSequencePrefixKey, []byte(sequence)...)
}

//
// Utils
//
Expand Down Expand Up @@ -193,24 +280,18 @@ func (k *Keeper) IterateRecordsAndApplyFn(ctx sdk.Context, f func(record types.E
}
}

// Sequence
// GetRecordSequenceKey returns record sequence key
func GetRecordSequenceKey(sequence string) []byte {
return append(RecordSequencePrefixKey, []byte(sequence)...)
}

// GetRecordSequences checks if record already exists
func (keeper Keeper) GetRecordSequences(ctx sdk.Context) (sequences []string) {
keeper.IterateRecordSequencesAndApplyFn(ctx, func(sequence string) error {
func (k *Keeper) GetRecordSequences(ctx sdk.Context) (sequences []string) {
k.IterateRecordSequencesAndApplyFn(ctx, func(sequence string) error {
sequences = append(sequences, sequence)
return nil
})
return
}

// IterateRecordSequencesAndApplyFn interate validators and apply the given function.
func (keeper Keeper) IterateRecordSequencesAndApplyFn(ctx sdk.Context, f func(sequence string) error) {
store := ctx.KVStore(keeper.storeKey)
// IterateRecordSequencesAndApplyFn interate records and apply the given function.
func (k *Keeper) IterateRecordSequencesAndApplyFn(ctx sdk.Context, f func(sequence string) error) {
store := ctx.KVStore(k.storeKey)

// get sequence iterator
iterator := sdk.KVStorePrefixIterator(store, RecordSequencePrefixKey)
Expand All @@ -228,13 +309,13 @@ func (keeper Keeper) IterateRecordSequencesAndApplyFn(ctx sdk.Context, f func(se
}

// SetRecordSequence sets mapping for sequence id to bool
func (keeper Keeper) SetRecordSequence(ctx sdk.Context, sequence string) {
store := ctx.KVStore(keeper.storeKey)
func (k *Keeper) SetRecordSequence(ctx sdk.Context, sequence string) {
store := ctx.KVStore(k.storeKey)
store.Set(GetRecordSequenceKey(sequence), DefaultValue)
}

// HasRecordSequence checks if record already exists
func (keeper Keeper) HasRecordSequence(ctx sdk.Context, sequence string) bool {
store := ctx.KVStore(keeper.storeKey)
func (k *Keeper) HasRecordSequence(ctx sdk.Context, sequence string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(GetRecordSequenceKey(sequence))
}
Loading

0 comments on commit 303c05f

Please sign in to comment.