Skip to content
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

algod:Stateproof server api #3888

Merged
merged 19 commits into from
May 2, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
relocated code inside file
  • Loading branch information
algonathan committed Apr 14, 2022
commit de2748b885783d36ef1107b3eab64e104fa96761
90 changes: 45 additions & 45 deletions daemon/algod/api/server/v2/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,51 +58,6 @@ type Handlers struct {
Shutdown <-chan struct{}
}

var errNilLedger = errors.New("could not contact ledger")
var errNoStateProofInRange = errors.New("no state proof in range")

// StateProof returns the state proof for a given round.
// (GET /v2/transaction/state-proof/{round})
func (v2 *Handlers) StateProof(ctx echo.Context, round uint64) error {
ledger := v2.Node.LedgerForAPI()
if ledger == nil {
return internalError(ctx, errNilLedger, errNilLedger.Error(), v2.Log)
}

consensus, err := ledger.ConsensusParams(basics.Round(round))
if err != nil {
return internalError(ctx, err, fmt.Sprintf("could not retrieve consensus information for round (%d)", round), v2.Log)
}

for current := round; current > round-consensus.CompactCertRounds; current-- {
block, err := ledger.Block(basics.Round(current))
if err != nil {
return internalError(ctx, err, "couldn't retrieve block, and locate state-proof", v2.Log)
}

txn := searchForCompactCert(block)
if txn == nil {
continue
}

response := generated.StateProofResponse{
StateProofMessage: protocol.Encode(&txn.CertMsg),
StateProof: protocol.Encode(&txn.Cert),
}
return ctx.JSON(http.StatusOK, response)
}
return notFound(ctx, errNoStateProofInRange, fmt.Sprintf("could not find state-proof for round (%d)", round), v2.Log)
}

func searchForCompactCert(block bookkeeping.Block) *transactions.Transaction {
for _, txn := range block.Payset {
if txn.Txn.Sender == transactions.CompactCertSender {
return &txn.Txn
}
}
return nil
}

// StateProof returns a wanted Algorand state proof.
// (GET /v2/transactions/state-proof)

Expand Down Expand Up @@ -1200,3 +1155,48 @@ func (v2 *Handlers) TealCompile(ctx echo.Context) error {
}
return ctx.JSON(http.StatusOK, response)
}

var errNilLedger = errors.New("could not contact ledger")
var errNoStateProofInRange = errors.New("no state proof in range")
algonathan marked this conversation as resolved.
Show resolved Hide resolved

// StateProof returns the state proof for a given round.
// (GET /v2/transaction/state-proof/{round})
func (v2 *Handlers) StateProof(ctx echo.Context, round uint64) error {
ledger := v2.Node.LedgerForAPI()
if ledger == nil {
return internalError(ctx, errNilLedger, errNilLedger.Error(), v2.Log)
}

consensus, err := ledger.ConsensusParams(basics.Round(round))
if err != nil {
return internalError(ctx, err, fmt.Sprintf("could not retrieve consensus information for round (%d)", round), v2.Log)
}

for current := round; current > round-consensus.CompactCertRounds; current-- {
block, err := ledger.Block(basics.Round(current))
if err != nil {
return internalError(ctx, err, "couldn't retrieve block, and locate state-proof", v2.Log)
}

txn := searchForCompactCert(block)
if txn == nil {
continue
}

response := generated.StateProofResponse{
StateProofMessage: protocol.Encode(&txn.CertMsg),
StateProof: protocol.Encode(&txn.Cert),
}
return ctx.JSON(http.StatusOK, response)
}
return notFound(ctx, errNoStateProofInRange, fmt.Sprintf("could not find state-proof for round (%d)", round), v2.Log)
}

func searchForCompactCert(block bookkeeping.Block) *transactions.Transaction {
for _, txn := range block.Payset {
if txn.Txn.Sender == transactions.CompactCertSender {
return &txn.Txn
}
}
return nil
}