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
Show file tree
Hide file tree
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
WIP: unit-tests
  • Loading branch information
algonathan committed Apr 14, 2022
commit 58c8d0065a71b832b3d5e337962c5ad49549bf15
6 changes: 4 additions & 2 deletions daemon/algod/api/server/v2/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1169,10 +1169,12 @@ func (v2 *Handlers) StateProof(ctx echo.Context, round uint64) error {

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)
return notFound(ctx, err, fmt.Sprintf("could not retrieve consensus information for round (%d)", round), v2.Log)
}

for current := round; current > round-consensus.CompactCertRounds; current-- {
// as a start we presume the rounds given are only from current rounds
// assuming i'm starting from rounds where a compcert are valid.
for current := round; current > round-consensus.CompactCertRounds && current > 0; 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)
Expand Down
71 changes: 71 additions & 0 deletions daemon/algod/api/server/v2/test/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,3 +797,74 @@ func TestAppendParticipationKeys(t *testing.T) {
require.Contains(t, rec.Body.String(), expectedErr.Error())
})
}

func newEmptyBlock(a *require.Assertions, l v2.LedgerForAPI) bookkeeping.Block {
genBlk, err := l.Block(0)
a.NoError(err)

totalsRound, totals, err := l.LatestTotals()
a.NoError(err)
a.Equal(l.Latest(), totalsRound)

totalRewardUnits := totals.RewardUnits()
poolBal, _, _, err := l.LookupLatest(poolAddr)
a.NoError(err)

latestBlock, err := l.Block(l.Latest())
a.NoError(err)

var blk bookkeeping.Block
blk.BlockHeader = bookkeeping.BlockHeader{
GenesisID: genBlk.GenesisID(),
GenesisHash: genBlk.GenesisHash(),
Round: l.Latest() + 1,
Branch: latestBlock.Hash(),
RewardsState: latestBlock.NextRewardsState(l.Latest()+1, proto, poolBal.MicroAlgos, totalRewardUnits, logging.Base()),
UpgradeState: latestBlock.UpgradeState,
}

blk.BlockHeader.TxnCounter = latestBlock.TxnCounter

blk.RewardsPool = latestBlock.RewardsPool
blk.FeeSink = latestBlock.FeeSink
blk.CurrentProtocol = latestBlock.CurrentProtocol
blk.TimeStamp = latestBlock.TimeStamp + 1

blk.BlockHeader.TxnCounter++
blk.TxnRoot, err = blk.PaysetCommit()
a.NoError(err)

return blk
}

func TestStateProofOutOfBoundsRound(t *testing.T) {
// TODO:find a way to create enough blocks to generate comp-cert.
a := require.New(t)

handler, ctx, responseRecorder, _, _, releasefunc := setupTestForMethodGet(t)
defer releasefunc()

// we only have block for round 0:
a.NoError(handler.StateProof(ctx, 1))
a.Equal(404, responseRecorder.Code)
}

func TestStateProof404(t *testing.T) {
// TODO: we have an overflow in the test.
a := require.New(t)

handler, ctx, responseRecorder, _, _, releasefunc := setupTestForMethodGet(t)
defer releasefunc()

ldger := handler.Node.LedgerForAPI()

for i := 0; i < 2; i++ {
blk := newEmptyBlock(a, ldger)
blk.BlockHeader.CurrentProtocol = protocol.ConsensusFuture
a.NoError(ldger.(*data.Ledger).AddBlock(blk, agreement.Certificate{}))
}

// we didn't add any certificate
a.NoError(handler.StateProof(ctx, 2))
algonathan marked this conversation as resolved.
Show resolved Hide resolved
a.Equal(404, responseRecorder.Code)
}