Remove hard-coded record types #132

Merged
ashwin merged 17 commits from deep-stack/laconicd-deprecated:ng-rm-record-types into main 2024-01-15 04:20:39 +00:00
8 changed files with 39 additions and 39 deletions
Showing only changes of commit a9c04a7980 - Show all commits

View File

@ -163,7 +163,7 @@ func GetGQLAuction(auction *auctiontypes.Auction, bids []*auctiontypes.Bid) (*Au
return &gqlAuction, nil return &gqlAuction, nil
} }
func getReferences(ctx context.Context, resolver QueryResolver, r *registrytypes.RecordEncodable) ([]*Record, error) { func getReferences(ctx context.Context, resolver QueryResolver, r *registrytypes.ReadableRecord) ([]*Record, error) {
var ids []string var ids []string
// #nosec G705 // #nosec G705
@ -184,7 +184,7 @@ func getReferences(ctx context.Context, resolver QueryResolver, r *registrytypes
return resolver.GetRecordsByIds(ctx, ids) return resolver.GetRecordsByIds(ctx, ids)
} }
func getAttributes(r *registrytypes.RecordEncodable) ([]*KeyValue, error) { func getAttributes(r *registrytypes.ReadableRecord) ([]*KeyValue, error) {
return mapToKeyValuePairs(r.Attributes) return mapToKeyValuePairs(r.Attributes)
} }

View File

@ -161,7 +161,7 @@ $ %s query %s list
} }
recordsList := res.GetRecords() recordsList := res.GetRecords()
records := make([]types.RecordEncodable, len(recordsList)) records := make([]types.ReadableRecord, len(recordsList))
for i, record := range res.GetRecords() { for i, record := range res.GetRecords() {
records[i] = record.ToReadableRecord() records[i] = record.ToReadableRecord()
} }

View File

@ -370,8 +370,8 @@ $ %s tx %s delete-name [crn]
} }
// GetPayloadFromFile Load payload object from YAML file. // GetPayloadFromFile Load payload object from YAML file.
func GetPayloadFromFile(filePath string) (*types.PayloadEncodable, error) { func GetPayloadFromFile(filePath string) (*types.ReadablePayload, error) {
var payload types.PayloadEncodable var payload types.ReadablePayload
data, err := os.ReadFile(filePath) // #nosec G304 data, err := os.ReadFile(filePath) // #nosec G304
if err != nil { if err != nil {

View File

@ -435,7 +435,7 @@ func (s *IntegrationTestSuite) TestGRPCQueryGetRecordByID() {
} }
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args) out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
sr.NoError(err) sr.NoError(err)
var records []nstypes.RecordEncodable var records []nstypes.ReadableRecord
err = json.Unmarshal(out.Bytes(), &records) err = json.Unmarshal(out.Bytes(), &records)
sr.NoError(err) sr.NoError(err)
return records[0].ID return records[0].ID

View File

@ -114,7 +114,7 @@ func (s *IntegrationTestSuite) TestGetCmdQueryForRecords() {
sr.Error(err) sr.Error(err)
} else { } else {
sr.NoError(err) sr.NoError(err)
var records []types.RecordEncodable var records []types.ReadableRecord
err := json.Unmarshal(out.Bytes(), &records) err := json.Unmarshal(out.Bytes(), &records)
sr.NoError(err) sr.NoError(err)
sr.Equal(tc.noOfRecords, len(records)) sr.Equal(tc.noOfRecords, len(records))

View File

@ -606,7 +606,7 @@ func (s *IntegrationTestSuite) TestGetCmdDissociateBond() {
cmd = cli.GetCmdList() cmd = cli.GetCmdList()
out, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args) out, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
sr.NoError(err) sr.NoError(err)
var records []nstypes.RecordEncodable var records []nstypes.ReadableRecord
err = json.Unmarshal(out.Bytes(), &records) err = json.Unmarshal(out.Bytes(), &records)
sr.NoError(err) sr.NoError(err)
return records[0].ID return records[0].ID
@ -848,7 +848,7 @@ func (s *IntegrationTestSuite) TestGetCmdAssociateBond() {
cmd = cli.GetCmdList() cmd = cli.GetCmdList()
out, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args) out, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
sr.NoError(err) sr.NoError(err)
var records []nstypes.RecordEncodable var records []nstypes.ReadableRecord
err = json.Unmarshal(out.Bytes(), &records) err = json.Unmarshal(out.Bytes(), &records)
sr.NoError(err) sr.NoError(err)

View File

@ -242,9 +242,9 @@ func (k Keeper) GetRecordExpiryQueue(ctx sdk.Context) []*types.ExpiryQueueRecord
} }
// ProcessSetRecord creates a record. // ProcessSetRecord creates a record.
func (k Keeper) ProcessSetRecord(ctx sdk.Context, msg types.MsgSetRecord) (*types.RecordEncodable, error) { func (k Keeper) ProcessSetRecord(ctx sdk.Context, msg types.MsgSetRecord) (*types.ReadableRecord, error) {
payload := msg.Payload.ToReadablePayload() payload := msg.Payload.ToReadablePayload()
record := types.RecordEncodable{Attributes: payload.Record, BondID: msg.BondId} record := types.ReadableRecord{Attributes: payload.Record, BondID: msg.BondId}
// Check signatures. // Check signatures.
resourceSignBytes, _ := record.GetSignBytes() resourceSignBytes, _ := record.GetSignBytes()
@ -285,7 +285,7 @@ func (k Keeper) ProcessSetRecord(ctx sdk.Context, msg types.MsgSetRecord) (*type
return &record, nil return &record, nil
} }
func (k Keeper) processRecord(ctx sdk.Context, record *types.RecordEncodable, isRenewal bool) error { func (k Keeper) processRecord(ctx sdk.Context, record *types.ReadableRecord, isRenewal bool) error {
params := k.GetParams(ctx) params := k.GetParams(ctx)
rent := params.RecordRent rent := params.RecordRent

View File

@ -17,15 +17,27 @@ const (
// become specific to content records. schema records will either occupy a new message or have new // become specific to content records. schema records will either occupy a new message or have new
// more general purpose helper types. // more general purpose helper types.
// PayloadEncodable represents a signed record payload that can be serialized from/to YAML. // ReadablePayload represents a signed record payload that can be serialized from/to YAML.
type PayloadEncodable struct { type ReadablePayload struct {
Record map[string]interface{} `json:"record"` Record map[string]interface{} `json:"record"`
Signatures []Signature `json:"signatures"` Signatures []Signature `json:"signatures"`
} }
// ReadableRecord represents a WNS record.
type ReadableRecord struct {
ID string `json:"id,omitempty"`
Names []string `json:"names,omitempty"`
BondID string `json:"bondId,omitempty"`
CreateTime string `json:"createTime,omitempty"`
ExpiryTime string `json:"expiryTime,omitempty"`
Deleted bool `json:"deleted,omitempty"`
Owners []string `json:"owners,omitempty"`
Attributes map[string]interface{} `json:"attributes,omitempty"`
}
// ToPayload converts PayloadEncodable to Payload object. // ToPayload converts PayloadEncodable to Payload object.
// Why? Because go-amino can't handle maps: https://github.com/tendermint/go-amino/issues/4. // Why? Because go-amino can't handle maps: https://github.com/tendermint/go-amino/issues/4.
func (payloadObj *PayloadEncodable) ToPayload() Payload { func (payloadObj *ReadablePayload) ToPayload() Payload {
// Note: record directly contains the attributes here // Note: record directly contains the attributes here
attributes := helpers.MarshalMapToJSONBytes(payloadObj.Record) attributes := helpers.MarshalMapToJSONBytes(payloadObj.Record)
payload := Payload{ payload := Payload{
@ -41,8 +53,8 @@ func (payloadObj *PayloadEncodable) ToPayload() Payload {
} }
// ToReadablePayload converts Payload to a serializable object // ToReadablePayload converts Payload to a serializable object
func (payload Payload) ToReadablePayload() PayloadEncodable { func (payload Payload) ToReadablePayload() ReadablePayload {
var encodable PayloadEncodable var encodable ReadablePayload
encodable.Record = helpers.UnMarshalMapFromJSONBytes(payload.Record.Attributes) encodable.Record = helpers.UnMarshalMapFromJSONBytes(payload.Record.Attributes)
encodable.Signatures = payload.Signatures encodable.Signatures = payload.Signatures
@ -51,8 +63,8 @@ func (payload Payload) ToReadablePayload() PayloadEncodable {
} }
// ToReadableRecord converts Record to a serializable object // ToReadableRecord converts Record to a serializable object
func (r *Record) ToReadableRecord() RecordEncodable { func (r *Record) ToReadableRecord() ReadableRecord {
var resourceObj RecordEncodable var resourceObj ReadableRecord
resourceObj.ID = r.Id resourceObj.ID = r.Id
resourceObj.BondID = r.BondId resourceObj.BondID = r.BondId
@ -66,21 +78,9 @@ func (r *Record) ToReadableRecord() RecordEncodable {
return resourceObj return resourceObj
} }
// RecordEncodable represents a WNS record.
type RecordEncodable struct {
ID string `json:"id,omitempty"`
Names []string `json:"names,omitempty"`
BondID string `json:"bondId,omitempty"`
CreateTime string `json:"createTime,omitempty"`
ExpiryTime string `json:"expiryTime,omitempty"`
Deleted bool `json:"deleted,omitempty"`
Owners []string `json:"owners,omitempty"`
Attributes map[string]interface{} `json:"attributes,omitempty"`
}
// ToRecordObj converts Record to RecordObj. // ToRecordObj converts Record to RecordObj.
// Why? Because go-amino can't handle maps: https://github.com/tendermint/go-amino/issues/4. // Why? Because go-amino can't handle maps: https://github.com/tendermint/go-amino/issues/4.
func (r *RecordEncodable) ToRecordObj() (Record, error) { func (r *ReadableRecord) ToRecordObj() (Record, error) {
attributes := helpers.MarshalMapToJSONBytes(r.Attributes) attributes := helpers.MarshalMapToJSONBytes(r.Attributes)
var resourceObj Record var resourceObj Record
@ -97,7 +97,7 @@ func (r *RecordEncodable) ToRecordObj() (Record, error) {
} }
// CanonicalJSON returns the canonical JSON representation of the record. // CanonicalJSON returns the canonical JSON representation of the record.
func (r *RecordEncodable) CanonicalJSON() []byte { func (r *ReadableRecord) CanonicalJSON() []byte {
bytes, err := canonicalJson.Marshal(r.Attributes) bytes, err := canonicalJson.Marshal(r.Attributes)
if err != nil { if err != nil {
panic("Record marshal error.") panic("Record marshal error.")
@ -107,7 +107,7 @@ func (r *RecordEncodable) CanonicalJSON() []byte {
} }
// GetSignBytes generates a record hash to be signed. // GetSignBytes generates a record hash to be signed.
func (r *RecordEncodable) GetSignBytes() ([]byte, []byte) { func (r *ReadableRecord) GetSignBytes() ([]byte, []byte) {
// Double SHA256 hash. // Double SHA256 hash.
// Input to the first round of hashing. // Input to the first round of hashing.
@ -127,7 +127,7 @@ func (r *RecordEncodable) GetSignBytes() ([]byte, []byte) {
} }
// GetCID gets the record CID. // GetCID gets the record CID.
func (r *RecordEncodable) GetCID() (string, error) { func (r *ReadableRecord) GetCID() (string, error) {
id, err := helpers.GetCid(r.CanonicalJSON()) id, err := helpers.GetCid(r.CanonicalJSON())
if err != nil { if err != nil {
return "", err return "", err