-
Notifications
You must be signed in to change notification settings - Fork 694
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
Marshal blocks and transactions inside API calls #2153
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ import ( | |
"math" | ||
"net/http" | ||
|
||
stdjson "encoding/json" | ||
|
||
"go.uber.org/zap" | ||
|
||
"github.com/ava-labs/avalanchego/api" | ||
|
@@ -79,6 +81,7 @@ func (s *Service) GetBlock(_ *http.Request, args *api.GetBlockArgs, reply *api.G | |
} | ||
reply.Encoding = args.Encoding | ||
|
||
var result any | ||
if args.Encoding == formatting.JSON { | ||
block.InitCtx(s.vm.ctx) | ||
for _, tx := range block.Txs() { | ||
|
@@ -92,16 +95,16 @@ func (s *Service) GetBlock(_ *http.Request, args *api.GetBlockArgs, reply *api.G | |
return err | ||
} | ||
} | ||
reply.Block = block | ||
return nil | ||
} | ||
|
||
reply.Block, err = formatting.Encode(args.Encoding, block.Bytes()) | ||
if err != nil { | ||
return fmt.Errorf("couldn't encode block %s as string: %w", args.BlockID, err) | ||
result = block | ||
} else { | ||
result, err = formatting.Encode(args.Encoding, block.Bytes()) | ||
if err != nil { | ||
return fmt.Errorf("couldn't encode block %s as string: %w", args.BlockID, err) | ||
} | ||
} | ||
|
||
return nil | ||
reply.Block, err = stdjson.Marshal(result) | ||
return err | ||
} | ||
|
||
// GetBlockByHeight returns the block at the given height. | ||
|
@@ -130,6 +133,7 @@ func (s *Service) GetBlockByHeight(_ *http.Request, args *api.GetBlockByHeightAr | |
return fmt.Errorf("couldn't get block with id %s: %w", blockID, err) | ||
} | ||
|
||
var result any | ||
if args.Encoding == formatting.JSON { | ||
block.InitCtx(s.vm.ctx) | ||
for _, tx := range block.Txs() { | ||
|
@@ -143,16 +147,16 @@ func (s *Service) GetBlockByHeight(_ *http.Request, args *api.GetBlockByHeightAr | |
return err | ||
} | ||
} | ||
reply.Block = block | ||
return nil | ||
} | ||
|
||
reply.Block, err = formatting.Encode(args.Encoding, block.Bytes()) | ||
if err != nil { | ||
return fmt.Errorf("couldn't encode block %s as string: %w", blockID, err) | ||
result = block | ||
} else { | ||
result, err = formatting.Encode(args.Encoding, block.Bytes()) | ||
if err != nil { | ||
return fmt.Errorf("couldn't encode block %s as string: %w", blockID, err) | ||
} | ||
Comment on lines
+153
to
+155
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: consider mogin this error check and the one on lines 146-149 outside of the if/else scope |
||
} | ||
|
||
return nil | ||
reply.Block, err = stdjson.Marshal(result) | ||
return err | ||
} | ||
|
||
// GetHeight returns the height of the last accepted block. | ||
|
@@ -320,23 +324,26 @@ func (s *Service) GetTx(_ *http.Request, args *api.GetTxArgs, reply *api.GetTxRe | |
if err != nil { | ||
return err | ||
} | ||
|
||
reply.Encoding = args.Encoding | ||
|
||
var result any | ||
if args.Encoding == formatting.JSON { | ||
reply.Tx = tx | ||
return tx.Unsigned.Visit(&txInit{ | ||
err = tx.Unsigned.Visit(&txInit{ | ||
tx: tx, | ||
ctx: s.vm.ctx, | ||
typeToFxIndex: s.vm.typeToFxIndex, | ||
fxs: s.vm.fxs, | ||
}) | ||
result = tx | ||
} else { | ||
result, err = formatting.Encode(args.Encoding, tx.Bytes()) | ||
} | ||
|
||
reply.Tx, err = formatting.Encode(args.Encoding, tx.Bytes()) | ||
if err != nil { | ||
return fmt.Errorf("couldn't encode tx as string: %w", err) | ||
return err | ||
} | ||
return nil | ||
|
||
reply.Tx, err = stdjson.Marshal(result) | ||
return err | ||
} | ||
|
||
// GetUTXOs gets all utxos for passed in addresses | ||
|
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: we could move this error and the one above (line 94-96) outside of the if else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The above error is in a for loop - so I don't think it makes sense to refactor this instance