Skip to content

Commit

Permalink
development: tool to convert validated blocks. (#5048)
Browse files Browse the repository at this point in the history
  • Loading branch information
winder authored Jan 26, 2023
1 parent 2ba3f4b commit 008d23c
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 14 deletions.
7 changes: 5 additions & 2 deletions daemon/algod/api/server/v2/delta.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ func convertAssetResourceRecordToGenerated(asset ledgercore.AssetResourceRecord)
}
}

// stateDeltaToLedgerDelta converts ledgercore.StateDelta to v2.model.LedgerStateDelta
func stateDeltaToLedgerDelta(sDelta ledgercore.StateDelta, consensus config.ConsensusParams, rewardsLevel uint64, round uint64) (response model.LedgerStateDelta, err error) {
// StateDeltaToLedgerDelta converts ledgercore.StateDelta to v2.model.LedgerStateDelta
func StateDeltaToLedgerDelta(sDelta ledgercore.StateDelta, consensus config.ConsensusParams) (response model.LedgerStateDelta, err error) {
rewardsLevel := sDelta.Hdr.RewardsLevel
round := sDelta.Hdr.Round

var accts []model.AccountBalanceRecord
var apps []model.AppResourceRecord
var assets []model.AssetResourceRecord
Expand Down
2 changes: 1 addition & 1 deletion daemon/algod/api/server/v2/delta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestDelta(t *testing.T) {
Totals: ledgercore.AccountTotals{},
}

converted, err := stateDeltaToLedgerDelta(original, config.Consensus[protocol.ConsensusCurrentVersion], 25, 4)
converted, err := StateDeltaToLedgerDelta(original, config.Consensus[protocol.ConsensusCurrentVersion])
require.NoError(t, err)
require.Equal(t, original.Accts.Len(), len(*converted.Accts.Accounts))
expAccDelta := original.Accts.Accts[0]
Expand Down
13 changes: 2 additions & 11 deletions daemon/algod/api/server/v2/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1083,20 +1083,11 @@ func (v2 *Handlers) GetLedgerStateDelta(ctx echo.Context, round uint64) error {
if err != nil {
return internalError(ctx, err, errFailedRetrievingStateDelta, v2.Log)
}
consensusParams, err := v2.Node.LedgerForAPI().ConsensusParams(basics.Round(round))
if err != nil {
return internalError(ctx, fmt.Errorf("unable to retrieve consensus params for round %d", round), errInternalFailure, v2.Log)
}
hdr, err := v2.Node.LedgerForAPI().BlockHdr(basics.Round(round))
if err != nil {
return internalError(ctx, fmt.Errorf("unable to retrieve block header for round %d", round), errInternalFailure, v2.Log)
}

response, err := stateDeltaToLedgerDelta(sDelta, consensusParams, hdr.RewardsLevel, round)
consensusParams := config.Consensus[sDelta.Hdr.CurrentProtocol]
response, err := StateDeltaToLedgerDelta(sDelta, consensusParams)
if err != nil {
return internalError(ctx, err, errInternalFailure, v2.Log)
}

return ctx.JSON(http.StatusOK, response)
}

Expand Down
4 changes: 4 additions & 0 deletions tools/debug/vbconvert/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# vbconvert

Utility tool to assist converting ledgercore.ValidatedBlock objects into a
format that can be parsed using types in the Algorand Go SDK.
125 changes: 125 additions & 0 deletions tools/debug/vbconvert/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Copyright (C) 2019-2023 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
package main

import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"

"github.com/algorand/go-codec/codec"

"github.com/algorand/go-algorand/config"
v2 "github.com/algorand/go-algorand/daemon/algod/api/server/v2"
"github.com/algorand/go-algorand/daemon/algod/api/server/v2/generated/model"
"github.com/algorand/go-algorand/data/bookkeeping"
"github.com/algorand/go-algorand/ledger/ledgercore"
"github.com/algorand/go-algorand/protocol"
)

type arguments struct {
inputFile string
outputFile string
format string
}

type algodVB struct {
Blk bookkeeping.Block
Delta ledgercore.StateDelta
}

type conduitVB struct {
Blk bookkeeping.Block
Delta model.LedgerStateDelta
}

func run(args arguments) {
var algodType algodVB

// Read
data, err := os.ReadFile(args.inputFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to read input file '%s': %s\n", args.inputFile, err)
os.Exit(1)
}

err = protocol.DecodeReflect(data, &algodType)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to decode input file '%s': %s\n", args.inputFile, err)
os.Exit(1)
}

// Convert
consensusParams := config.Consensus[algodType.Delta.Hdr.CurrentProtocol]
modelDelta, err := v2.StateDeltaToLedgerDelta(algodType.Delta, consensusParams)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to convert ledgercore.StateDelta from input file '%s': %s\n", args.inputFile, err)
os.Exit(1)
}

// Write
outputFile, err := os.Create(args.outputFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to open output file '%s': %s\n", args.outputFile, err)
os.Exit(1)
}

var enc *codec.Encoder
switch strings.ToLower(args.format) {
case "json":
enc = protocol.NewJSONEncoder(outputFile)
case "msgp":
enc = protocol.NewEncoder(outputFile)
default:
fmt.Fprintf(os.Stderr, "Unknown encoder type '%s', valid encoders: json, msgp.\n", args.format)
os.Exit(1)
}

conduitType := conduitVB{
Blk: algodType.Blk,
Delta: modelDelta,
}
err = enc.Encode(conduitType)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to decode input file '%s': %s\n", args.outputFile, err)
os.Exit(1)
}
}

func main() {
var args arguments

command := &cobra.Command{
Use: "vbconvert",
Long: "Convert a ledgercore.ValidatedBlock into the conduit version of a ValidatedBlock.",
Run: func(_ *cobra.Command, _ []string) {
run(args)
},
}

command.Flags().StringVarP(&args.inputFile, "input", "i", "", "Input filename.")
command.Flags().StringVarP(&args.outputFile, "output", "o", "", "Optional output filename. If not present a default <filename>.convert is created.")
command.Flags().StringVarP(&args.format, "format", "f", "json", "Optional output format. Valid formats are 'json' and 'msgp'.")
command.MarkFlagRequired("input")
command.MarkFlagRequired("output")

if err := command.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "An error occurred while running vbconvert: %s.\n", err)
os.Exit(1)
}
}

0 comments on commit 008d23c

Please sign in to comment.