Skip to content

Commit 8833a05

Browse files
committed
feat: add v11 block functions
1 parent ac5bdf5 commit 8833a05

File tree

3 files changed

+88
-45
lines changed

3 files changed

+88
-45
lines changed

src/impl/block.ts

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,62 @@
1-
import { Account, bytes, internal, uint64 } from '@algorandfoundation/algorand-typescript'
1+
import { Account, bytes, internal, Uint64, uint64 } from '@algorandfoundation/algorand-typescript'
22
import { lazyContext } from '../context-helpers/internal-context'
3-
import { asUint64 } from '../util'
4-
import { itob } from './pure'
3+
import { asUint64, getRandomBytes } from '../util'
4+
5+
export class BlockData {
6+
seed: bytes
7+
timestamp: uint64
8+
proposer: Account
9+
feesCollected: uint64
10+
bonus: uint64
11+
branch: bytes
12+
feeSink: Account
13+
protocol: bytes
14+
txnCounter: uint64
15+
proposerPayout: uint64
16+
17+
constructor() {
18+
this.seed = getRandomBytes(32).asAlgoTs()
19+
this.timestamp = asUint64(Date.now())
20+
this.proposer = Account()
21+
this.feesCollected = Uint64(0)
22+
this.bonus = Uint64(0)
23+
this.branch = getRandomBytes(32).asAlgoTs()
24+
this.feeSink = Account()
25+
this.protocol = getRandomBytes(32).asAlgoTs()
26+
this.txnCounter = Uint64(0)
27+
this.proposerPayout = Uint64(0)
28+
}
29+
}
530

631
export const Block: internal.opTypes.BlockType = {
732
blkSeed: function (a: internal.primitives.StubUint64Compat): bytes {
8-
return itob(lazyContext.ledger.getBlockContent(a).seed)
33+
return lazyContext.ledger.getBlockData(a).seed
934
},
1035
blkTimestamp: function (a: internal.primitives.StubUint64Compat): uint64 {
11-
return asUint64(lazyContext.ledger.getBlockContent(a).timestamp)
36+
return lazyContext.ledger.getBlockData(a).timestamp
1237
},
13-
// TODO: implement v11 methods
14-
blkProposer: function (_a: uint64): Account {
15-
throw new Error('Function not implemented.')
38+
blkProposer: function (a: uint64): Account {
39+
return lazyContext.ledger.getBlockData(a).proposer
1640
},
17-
blkFeesCollected: function (_a: uint64): uint64 {
18-
throw new Error('Function not implemented.')
41+
blkFeesCollected: function (a: uint64): uint64 {
42+
return lazyContext.ledger.getBlockData(a).feesCollected
1943
},
20-
blkBonus: function (_a: uint64): uint64 {
21-
throw new Error('Function not implemented.')
44+
blkBonus: function (a: uint64): uint64 {
45+
return lazyContext.ledger.getBlockData(a).bonus
2246
},
23-
blkBranch: function (_a: uint64): bytes {
24-
throw new Error('Function not implemented.')
47+
blkBranch: function (a: uint64): bytes {
48+
return lazyContext.ledger.getBlockData(a).branch
2549
},
26-
blkFeeSink: function (_a: uint64): Account {
27-
throw new Error('Function not implemented.')
50+
blkFeeSink: function (a: uint64): Account {
51+
return lazyContext.ledger.getBlockData(a).feeSink
2852
},
29-
blkProtocol: function (_a: uint64): bytes {
30-
throw new Error('Function not implemented.')
53+
blkProtocol: function (a: uint64): bytes {
54+
return lazyContext.ledger.getBlockData(a).protocol
3155
},
32-
blkTxnCounter: function (_a: uint64): uint64 {
33-
throw new Error('Function not implemented.')
56+
blkTxnCounter: function (a: uint64): uint64 {
57+
return lazyContext.ledger.getBlockData(a).txnCounter
3458
},
35-
blkProposerPayout: function (_a: uint64): uint64 {
36-
throw new Error('Function not implemented.')
59+
blkProposerPayout: function (a: uint64): uint64 {
60+
return lazyContext.ledger.getBlockData(a).proposerPayout
3761
},
3862
}

src/subcontexts/ledger-context.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@ import { MAX_UINT64 } from '../constants'
44
import { AccountData, AssetHolding } from '../impl/account'
55
import { ApplicationData } from '../impl/application'
66
import { AssetData } from '../impl/asset'
7+
import { BlockData } from '../impl/block'
78
import { GlobalData } from '../impl/global'
89
import { GlobalStateCls } from '../impl/state'
910
import { asBigInt, asMaybeBytesCls, asMaybeUint64Cls, asUint64, asUint64Cls, iterBigInt } from '../util'
1011

11-
interface BlockData {
12-
seed: bigint
13-
timestamp: bigint
14-
}
15-
1612
export class LedgerContext {
1713
appIdIter = iterBigInt(1001n, MAX_UINT64)
1814
assetIdIter = iterBigInt(1001n, MAX_UINT64)
@@ -124,19 +120,16 @@ export class LedgerContext {
124120
})
125121
}
126122

127-
setBlock(
128-
index: internal.primitives.StubUint64Compat,
129-
seed: internal.primitives.StubUint64Compat,
130-
timestamp: internal.primitives.StubUint64Compat,
131-
): void {
132-
const i = asBigInt(index)
133-
const s = asBigInt(seed)
134-
const t = asBigInt(timestamp)
135-
136-
this.blocks.set(i, { seed: s, timestamp: t })
123+
patchBlockData(index: internal.primitives.StubUint64Compat, data: Partial<BlockData>): void {
124+
const i = asUint64(index)
125+
const blockData = this.blocks.get(i) ?? new BlockData()
126+
this.blocks.set(i, {
127+
...blockData,
128+
...data,
129+
})
137130
}
138131

139-
getBlockContent(index: internal.primitives.StubUint64Compat): BlockData {
132+
getBlockData(index: internal.primitives.StubUint64Compat): BlockData {
140133
const i = asBigInt(index)
141134
if (this.blocks.has(i)) {
142135
return this.blocks.get(i)!

tests/state-op-codes.spec.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { AccountCls } from '../src/impl/account'
1212
import { InnerTxn } from '../src/impl/itxn'
1313
import { ApplicationTransaction } from '../src/impl/transactions'
1414
import { DeliberateAny } from '../src/typescript-helpers'
15-
import { asBigInt, asNumber, asUint64Cls, asUint8Array, encodeAddress, getRandomBytes } from '../src/util'
15+
import { asBigInt, asBigUintCls, asNumber, asUint64Cls, asUint8Array, encodeAddress, getRandomBytes } from '../src/util'
1616
import { AppExpectingEffects } from './artifacts/created-app-asset/contract.algo'
1717
import {
1818
ItxnDemoContract,
@@ -491,14 +491,40 @@ describe('State op codes', async () => {
491491
describe('Block', async () => {
492492
it('should return the correct field value of the block', async () => {
493493
const index = 42
494-
const seed = 123
494+
const seed = asBigUintCls(123n).toBytes().asAlgoTs()
495495
const timestamp = 1234567890
496-
ctx.ledger.setBlock(index, seed, timestamp)
497-
const seedResult = op.btoi(Block.blkSeed(Uint64(index)))
498-
const timestampResult = Block.blkTimestamp(Uint64(index))
496+
const proposer = ctx.any.account()
497+
const feesCollected = 1000
498+
const bonus = 12
499+
const branch = getRandomBytes(32).asAlgoTs()
500+
const feeSink = ctx.any.account()
501+
const protocol = getRandomBytes(32).asAlgoTs()
502+
const txnCounter = 32
503+
const proposerPayout = 42
504+
505+
ctx.ledger.patchBlockData(index, {
506+
seed,
507+
timestamp,
508+
proposer,
509+
feesCollected,
510+
bonus,
511+
branch,
512+
feeSink,
513+
protocol,
514+
txnCounter,
515+
proposerPayout,
516+
})
499517

500-
expect(seedResult).toEqual(Uint64(seed))
501-
expect(timestampResult).toEqual(Uint64(timestamp))
518+
expect(Block.blkSeed(index)).toEqual(seed)
519+
expect(Block.blkTimestamp(index)).toEqual(timestamp)
520+
expect(Block.blkProposer(index)).toEqual(proposer)
521+
expect(Block.blkFeesCollected(index)).toEqual(feesCollected)
522+
expect(Block.blkBonus(index)).toEqual(bonus)
523+
expect(Block.blkBranch(index)).toEqual(branch)
524+
expect(Block.blkFeeSink(index)).toEqual(feeSink)
525+
expect(Block.blkProtocol(index)).toEqual(protocol)
526+
expect(Block.blkTxnCounter(index)).toEqual(txnCounter)
527+
expect(Block.blkProposerPayout(index)).toEqual(proposerPayout)
502528
})
503529
it('should throw error if the block is not set', async () => {
504530
const index = 42

0 commit comments

Comments
 (0)