Skip to content

Commit 43cb563

Browse files
authored
AVM: Provide access to some more block header values (algorand#6107)
1 parent 0da0e99 commit 43cb563

File tree

13 files changed

+86
-26
lines changed

13 files changed

+86
-26
lines changed

data/transactions/logic/TEAL_opcodes_v10.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1638,7 +1638,7 @@ Fields
16381638

16391639
| Index | Name | Type | Notes |
16401640
| - | ------ | -- | --------- |
1641-
| 0 | BlkSeed | []byte | |
1641+
| 0 | BlkSeed | [32]byte | |
16421642
| 1 | BlkTimestamp | uint64 | |
16431643

16441644

data/transactions/logic/TEAL_opcodes_v7.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,6 @@ Fields
14761476

14771477
| Index | Name | Type | Notes |
14781478
| - | ------ | -- | --------- |
1479-
| 0 | BlkSeed | []byte | |
1479+
| 0 | BlkSeed | [32]byte | |
14801480
| 1 | BlkTimestamp | uint64 | |
14811481

data/transactions/logic/TEAL_opcodes_v8.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1635,6 +1635,6 @@ Fields
16351635

16361636
| Index | Name | Type | Notes |
16371637
| - | ------ | -- | --------- |
1638-
| 0 | BlkSeed | []byte | |
1638+
| 0 | BlkSeed | [32]byte | |
16391639
| 1 | BlkTimestamp | uint64 | |
16401640

data/transactions/logic/TEAL_opcodes_v9.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1635,6 +1635,6 @@ Fields
16351635

16361636
| Index | Name | Type | Notes |
16371637
| - | ------ | -- | --------- |
1638-
| 0 | BlkSeed | []byte | |
1638+
| 0 | BlkSeed | [32]byte | |
16391639
| 1 | BlkTimestamp | uint64 | |
16401640

data/transactions/logic/assembler_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,11 +1700,21 @@ global AssetCreateMinBalance
17001700
global AssetOptInMinBalance
17011701
global GenesisHash
17021702
pushint 1
1703+
block BlkBranch
1704+
pushint 1
1705+
block BlkFeeSink
1706+
pushint 1
1707+
block BlkProtocol
1708+
pushint 1
1709+
block BlkTxnCounter
1710+
pushint 1
17031711
block BlkProposer
17041712
pushint 1
17051713
block BlkFeesCollected
17061714
pushint 1
17071715
block BlkBonus
1716+
pushint 1
1717+
block BlkProposerPayout
17081718
global PayoutsEnabled
17091719
global PayoutsGoOnlineFee
17101720
global PayoutsPercent

data/transactions/logic/eval.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5771,12 +5771,24 @@ func opBlock(cx *EvalContext) error {
57715771
return fmt.Errorf("block(%d) timestamp %d < 0", round, hdr.TimeStamp)
57725772
}
57735773
cx.Stack[last] = stackValue{Uint: uint64(hdr.TimeStamp)}
5774+
5775+
case BlkBranch:
5776+
cx.Stack[last].Bytes = hdr.Branch[:]
5777+
case BlkFeeSink:
5778+
cx.Stack[last].Bytes = hdr.FeeSink[:]
5779+
case BlkProtocol:
5780+
cx.Stack[last].Bytes = []byte(hdr.CurrentProtocol)
5781+
case BlkTxnCounter:
5782+
cx.Stack[last] = stackValue{Uint: hdr.TxnCounter}
5783+
57745784
case BlkProposer:
57755785
cx.Stack[last].Bytes = hdr.Proposer[:]
57765786
case BlkFeesCollected:
57775787
cx.Stack[last] = stackValue{Uint: hdr.FeesCollected.Raw}
57785788
case BlkBonus:
57795789
cx.Stack[last] = stackValue{Uint: hdr.Bonus.Raw}
5790+
case BlkProposerPayout:
5791+
cx.Stack[last] = stackValue{Uint: hdr.ProposerPayout.Raw}
57805792
default:
57815793
return fmt.Errorf("invalid block field %s", fs.field)
57825794
}

data/transactions/logic/fields.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,16 @@ const (
997997
BlkFeesCollected
998998
// BlkBonus is the extra amount to be paid for the given block (from FeeSink)
999999
BlkBonus
1000+
// BlkBranch is the hash of the previous block
1001+
BlkBranch
1002+
// BlkFeeSink is the fee sink for the given round
1003+
BlkFeeSink
1004+
// BlkProtocol is the ConsensusVersion of the block.
1005+
BlkProtocol
1006+
// BlkTxnCounter is the number of the next transaction after the block
1007+
BlkTxnCounter
1008+
// BlkProposerPayout is the actual amount moved from feesink to proposer
1009+
BlkProposerPayout
10001010

10011011
invalidBlockField // compile-time constant for number of fields
10021012
)
@@ -1010,11 +1020,16 @@ type blockFieldSpec struct {
10101020
}
10111021

10121022
var blockFieldSpecs = [...]blockFieldSpec{
1013-
{BlkSeed, StackBytes, randomnessVersion},
1023+
{BlkSeed, StackBytes32, randomnessVersion},
10141024
{BlkTimestamp, StackUint64, randomnessVersion},
10151025
{BlkProposer, StackAddress, incentiveVersion},
10161026
{BlkFeesCollected, StackUint64, incentiveVersion},
10171027
{BlkBonus, StackUint64, incentiveVersion},
1028+
{BlkBranch, StackBytes32, incentiveVersion},
1029+
{BlkFeeSink, StackAddress, incentiveVersion},
1030+
{BlkProtocol, StackBytes, incentiveVersion},
1031+
{BlkTxnCounter, StackUint64, incentiveVersion},
1032+
{BlkProposerPayout, StackUint64, incentiveVersion},
10181033
}
10191034

10201035
func blockFieldSpecByField(r BlockField) (blockFieldSpec, bool) {

data/transactions/logic/fields_string.go

Lines changed: 8 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

data/transactions/logic/langspec_v10.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4601,7 +4601,7 @@
46014601
"BlkTimestamp"
46024602
],
46034603
"ArgEnumTypes": [
4604-
"[]byte",
4604+
"[32]byte",
46054605
"uint64"
46064606
],
46074607
"DocCost": "1",

data/transactions/logic/langspec_v7.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4249,7 +4249,7 @@
42494249
"BlkTimestamp"
42504250
],
42514251
"ArgEnumTypes": [
4252-
"[]byte",
4252+
"[32]byte",
42534253
"uint64"
42544254
],
42554255
"DocCost": "1",

data/transactions/logic/langspec_v8.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4595,7 +4595,7 @@
45954595
"BlkTimestamp"
45964596
],
45974597
"ArgEnumTypes": [
4598-
"[]byte",
4598+
"[32]byte",
45994599
"uint64"
46004600
],
46014601
"DocCost": "1",

data/transactions/logic/langspec_v9.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4595,7 +4595,7 @@
45954595
"BlkTimestamp"
45964596
],
45974597
"ArgEnumTypes": [
4598-
"[]byte",
4598+
"[32]byte",
45994599
"uint64"
46004600
],
46014601
"DocCost": "1",

test/scripts/e2e_subs/hdr-access.py

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#!/usr/bin/env python
22

3+
import base64
34
import os
45
import sys
56
from goal import Goal
7+
import algosdk.encoding as enc
68

79
from datetime import datetime
810

@@ -43,13 +45,10 @@
4345
txinfo, err = goal.app_create(joe, goal.assemble(teal))
4446
assert "not available" in str(err), err
4547

46-
# We want to manipulate lastvalid, so we need to turn off autosend
47-
goal.autosend = False
48-
49-
# We will be able to access two blocks, by setting lv explcitly. So we
50-
# test that the block timestamp from two blocks ago is between 2 and 5
51-
# (inclusive) seconds before the previous block timestamp. devMode
52-
# might mess this test up.
48+
# We will be able to access more than one previous block by using a
49+
# shorter tx liftetime. So we test that the block timestamp from two
50+
# blocks ago is between 2 and 5 (inclusive) seconds before the
51+
# previous block timestamp. devMode might mess this test up.
5352
teal = """
5453
#pragma version 7
5554
txn FirstValid
@@ -73,10 +72,7 @@
7372
int 6
7473
<
7574
"""
76-
checktimes = goal.assemble(teal)
77-
tx = goal.app_create(joe, goal.assemble(teal))
78-
tx.last_valid_round = tx.last_valid_round - 800
79-
txinfo, err = goal.send(tx)
75+
txinfo, err = goal.app_create(joe, goal.assemble(teal), lifetime=100)
8076
assert not err, err
8177

8278
# block 0 is not accessible even with a low LastValid
@@ -85,11 +81,33 @@
8581
int 0
8682
block BlkTimestamp
8783
"""
88-
tx = goal.app_create(joe, goal.assemble(teal))
89-
tx.last_valid_round = tx.last_valid_round - 800
90-
txinfo, err = goal.send(tx)
84+
txinfo, err = goal.app_create(joe, goal.assemble(teal), lifetime=100)
9185
assert "round 0 is not available" in str(err), err
9286
assert "outside [1-" in str(err), err # confirms that we can look back to 1
9387

88+
89+
# Get FeeSink from `block` opcode, compare to REST API
90+
teal = """
91+
#pragma version 11
92+
txn FirstValid
93+
int 2
94+
-
95+
block BlkFeeSink
96+
log
97+
int 1
98+
return
99+
"""
100+
txinfo, err = goal.app_create(joe, goal.assemble(teal), lifetime=100)
101+
assert not err, err
102+
assert len(txinfo["logs"]) == 1
103+
opcode = txinfo["logs"][0]
104+
105+
block = goal.algod.block_info(txinfo['confirmed-round']-2)['block']
106+
api = base64.b64encode(enc.decode_address(block['fees'])).decode("utf-8")
107+
108+
print(opcode, api)
109+
110+
assert opcode == api
111+
94112
stamp = datetime.now().strftime("%Y%m%d_%H%M%S")
95113
print(f"{os.path.basename(sys.argv[0])} OK {stamp}")

0 commit comments

Comments
 (0)