From 8e084ebdc1ae18fe0b6b0b2441cd286cd10875ce Mon Sep 17 00:00:00 2001 From: Zakhar Petukhov Date: Tue, 17 Dec 2024 13:18:35 +0800 Subject: [PATCH] change the account source to a non-required field --- api/openapi.json | 3 +- api/openapi.yml | 23 ++++++++-------- pkg/api/account_handlers.go | 15 ++++++---- pkg/oas/oas_json_gen.go | 43 ++++++++++++++++++++++++++--- pkg/oas/oas_schemas_gen.go | 52 +++++++++++++++++++++++++++++++++-- pkg/oas/oas_validators_gen.go | 11 ++++++-- 6 files changed, 118 insertions(+), 29 deletions(-) diff --git a/api/openapi.json b/api/openapi.json index 99b48390..56be55dd 100644 --- a/api/openapi.json +++ b/api/openapi.json @@ -1551,8 +1551,7 @@ "code", "code_hash", "methods", - "compiler", - "source" + "compiler" ], "type": "object" }, diff --git a/api/openapi.yml b/api/openapi.yml index 206dc927..c862c733 100644 --- a/api/openapi.yml +++ b/api/openapi.yml @@ -7410,7 +7410,6 @@ components: - code_hash - methods - compiler - - source properties: code: type: string @@ -7480,17 +7479,17 @@ components: format: int64 example: 1668436763 ExtraCurrency: - type: object - required: - - amount - - preview - properties: - amount: - type: string - x-js-format: bigint - example: "1000000000" - preview: - $ref: '#/components/schemas/EcPreview' + type: object + required: + - amount + - preview + properties: + amount: + type: string + x-js-format: bigint + example: "1000000000" + preview: + $ref: '#/components/schemas/EcPreview' SourceFile: type: object required: diff --git a/pkg/api/account_handlers.go b/pkg/api/account_handlers.go index 1ba3568e..e3d10ee1 100644 --- a/pkg/api/account_handlers.go +++ b/pkg/api/account_handlers.go @@ -488,10 +488,7 @@ func (h *Handler) BlockchainAccountInspect(ctx context.Context, params oas.Block if err != nil { return nil, toError(http.StatusInternalServerError, err) } - source, err := h.verifierSource.GetAccountSource(account.ID) - if err != nil { - return nil, toError(http.StatusInternalServerError, err) - } + source, _ := h.verifierSource.GetAccountSource(account.ID) sourceFiles := make([]oas.SourceFile, len(source.Files)) for idx, file := range source.Files { sourceFiles[idx] = oas.SourceFile{ @@ -502,11 +499,17 @@ func (h *Handler) BlockchainAccountInspect(ctx context.Context, params oas.Block IncludeInCommand: file.IncludeInCommand, } } + compiler := oas.BlockchainAccountInspectCompilerFunc + if source.Compiler != "" { + compiler = oas.BlockchainAccountInspectCompiler(source.Compiler) + } resp := oas.BlockchainAccountInspect{ Code: hex.EncodeToString(rawAccount.Code), CodeHash: hex.EncodeToString(codeHash), - Compiler: oas.BlockchainAccountInspectCompiler(source.Compiler), - Source: oas.Source{Files: sourceFiles}, + Compiler: compiler, + } + if len(sourceFiles) > 0 { + resp.Source = oas.NewOptSource(oas.Source{Files: sourceFiles}) } for _, methodID := range methods { if method, ok := code.Methods[methodID]; ok { diff --git a/pkg/oas/oas_json_gen.go b/pkg/oas/oas_json_gen.go index 81d8e7da..a8d4a86e 100644 --- a/pkg/oas/oas_json_gen.go +++ b/pkg/oas/oas_json_gen.go @@ -4595,8 +4595,10 @@ func (s *BlockchainAccountInspect) encodeFields(e *jx.Encoder) { s.Compiler.Encode(e) } { - e.FieldStart("source") - s.Source.Encode(e) + if s.Source.Set { + e.FieldStart("source") + s.Source.Encode(e) + } } } @@ -4670,8 +4672,8 @@ func (s *BlockchainAccountInspect) Decode(d *jx.Decoder) error { return errors.Wrap(err, "decode field \"compiler\"") } case "source": - requiredBitSet[0] |= 1 << 4 if err := func() error { + s.Source.Reset() if err := s.Source.Decode(d); err != nil { return err } @@ -4689,7 +4691,7 @@ func (s *BlockchainAccountInspect) Decode(d *jx.Decoder) error { // Validate required fields. var failures []validate.FieldError for i, mask := range [1]uint8{ - 0b00011111, + 0b00001111, } { if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { // Mask only required fields and check equality to mask using XOR. @@ -31979,6 +31981,39 @@ func (s *OptSmartContractAction) UnmarshalJSON(data []byte) error { return s.Decode(d) } +// Encode encodes Source as json. +func (o OptSource) Encode(e *jx.Encoder) { + if !o.Set { + return + } + o.Value.Encode(e) +} + +// Decode decodes Source from json. +func (o *OptSource) Decode(d *jx.Decoder) error { + if o == nil { + return errors.New("invalid: unable to decode OptSource to nil") + } + o.Set = true + if err := o.Value.Decode(d); err != nil { + return err + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s OptSource) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *OptSource) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + // Encode encodes StateInit as json. func (o OptStateInit) Encode(e *jx.Encoder) { if !o.Set { diff --git a/pkg/oas/oas_schemas_gen.go b/pkg/oas/oas_schemas_gen.go index 2ec44190..be542dba 100644 --- a/pkg/oas/oas_schemas_gen.go +++ b/pkg/oas/oas_schemas_gen.go @@ -1946,7 +1946,7 @@ type BlockchainAccountInspect struct { CodeHash string `json:"code_hash"` Methods []Method `json:"methods"` Compiler BlockchainAccountInspectCompiler `json:"compiler"` - Source Source `json:"source"` + Source OptSource `json:"source"` } // GetCode returns the value of Code. @@ -1970,7 +1970,7 @@ func (s *BlockchainAccountInspect) GetCompiler() BlockchainAccountInspectCompile } // GetSource returns the value of Source. -func (s *BlockchainAccountInspect) GetSource() Source { +func (s *BlockchainAccountInspect) GetSource() OptSource { return s.Source } @@ -1995,7 +1995,7 @@ func (s *BlockchainAccountInspect) SetCompiler(val BlockchainAccountInspectCompi } // SetSource sets the value of Source. -func (s *BlockchainAccountInspect) SetSource(val Source) { +func (s *BlockchainAccountInspect) SetSource(val OptSource) { s.Source = val } @@ -13818,6 +13818,52 @@ func (o OptSmartContractAction) Or(d SmartContractAction) SmartContractAction { return d } +// NewOptSource returns new OptSource with value set to v. +func NewOptSource(v Source) OptSource { + return OptSource{ + Value: v, + Set: true, + } +} + +// OptSource is optional Source. +type OptSource struct { + Value Source + Set bool +} + +// IsSet returns true if OptSource was set. +func (o OptSource) IsSet() bool { return o.Set } + +// Reset unsets value. +func (o *OptSource) Reset() { + var v Source + o.Value = v + o.Set = false +} + +// SetTo sets value to v. +func (o *OptSource) SetTo(v Source) { + o.Set = true + o.Value = v +} + +// Get returns value and boolean that denotes whether value was set. +func (o OptSource) Get() (v Source, ok bool) { + if !o.Set { + return v, false + } + return o.Value, true +} + +// Or returns value if set, or given parameter if does not. +func (o OptSource) Or(d Source) Source { + if v, ok := o.Get(); ok { + return v + } + return d +} + // NewOptStateInit returns new OptStateInit with value set to v. func NewOptStateInit(v StateInit) OptStateInit { return OptStateInit{ diff --git a/pkg/oas/oas_validators_gen.go b/pkg/oas/oas_validators_gen.go index 7aaedc97..667446e5 100644 --- a/pkg/oas/oas_validators_gen.go +++ b/pkg/oas/oas_validators_gen.go @@ -909,8 +909,15 @@ func (s *BlockchainAccountInspect) Validate() error { }) } if err := func() error { - if err := s.Source.Validate(); err != nil { - return err + if value, ok := s.Source.Get(); ok { + if err := func() error { + if err := value.Validate(); err != nil { + return err + } + return nil + }(); err != nil { + return err + } } return nil }(); err != nil {