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

AVM: Provide access to some more block header values #6107

Merged
merged 4 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion data/transactions/logic/TEAL_opcodes_v10.md
Original file line number Diff line number Diff line change
Expand Up @@ -1638,7 +1638,7 @@ Fields

| Index | Name | Type | Notes |
| - | ------ | -- | --------- |
| 0 | BlkSeed | []byte | |
| 0 | BlkSeed | [32]byte | |
| 1 | BlkTimestamp | uint64 | |


Expand Down
2 changes: 1 addition & 1 deletion data/transactions/logic/TEAL_opcodes_v7.md
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,6 @@ Fields

| Index | Name | Type | Notes |
| - | ------ | -- | --------- |
| 0 | BlkSeed | []byte | |
| 0 | BlkSeed | [32]byte | |
| 1 | BlkTimestamp | uint64 | |

2 changes: 1 addition & 1 deletion data/transactions/logic/TEAL_opcodes_v8.md
Original file line number Diff line number Diff line change
Expand Up @@ -1635,6 +1635,6 @@ Fields

| Index | Name | Type | Notes |
| - | ------ | -- | --------- |
| 0 | BlkSeed | []byte | |
| 0 | BlkSeed | [32]byte | |
| 1 | BlkTimestamp | uint64 | |

2 changes: 1 addition & 1 deletion data/transactions/logic/TEAL_opcodes_v9.md
Original file line number Diff line number Diff line change
Expand Up @@ -1635,6 +1635,6 @@ Fields

| Index | Name | Type | Notes |
| - | ------ | -- | --------- |
| 0 | BlkSeed | []byte | |
| 0 | BlkSeed | [32]byte | |
| 1 | BlkTimestamp | uint64 | |

10 changes: 10 additions & 0 deletions data/transactions/logic/assembler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1700,11 +1700,21 @@ global AssetCreateMinBalance
global AssetOptInMinBalance
global GenesisHash
pushint 1
block BlkBranch
pushint 1
block BlkFeeSink
pushint 1
block BlkProtocol
pushint 1
block BlkTxnCounter
pushint 1
block BlkProposer
pushint 1
block BlkFeesCollected
pushint 1
block BlkBonus
pushint 1
block BlkProposerPayout
global PayoutsEnabled
global PayoutsGoOnlineFee
global PayoutsPercent
Expand Down
12 changes: 12 additions & 0 deletions data/transactions/logic/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -5771,12 +5771,24 @@ func opBlock(cx *EvalContext) error {
return fmt.Errorf("block(%d) timestamp %d < 0", round, hdr.TimeStamp)
}
cx.Stack[last] = stackValue{Uint: uint64(hdr.TimeStamp)}

case BlkBranch:
cx.Stack[last].Bytes = hdr.Branch[:]
case BlkFeeSink:
cx.Stack[last].Bytes = hdr.FeeSink[:]
case BlkProtocol:
cx.Stack[last].Bytes = []byte(hdr.CurrentProtocol)
case BlkTxnCounter:
cx.Stack[last] = stackValue{Uint: hdr.TxnCounter}

case BlkProposer:
cx.Stack[last].Bytes = hdr.Proposer[:]
case BlkFeesCollected:
cx.Stack[last] = stackValue{Uint: hdr.FeesCollected.Raw}
case BlkBonus:
cx.Stack[last] = stackValue{Uint: hdr.Bonus.Raw}
case BlkProposerPayout:
cx.Stack[last] = stackValue{Uint: hdr.ProposerPayout.Raw}
default:
return fmt.Errorf("invalid block field %s", fs.field)
}
Expand Down
17 changes: 16 additions & 1 deletion data/transactions/logic/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,16 @@ const (
BlkFeesCollected
// BlkBonus is the extra amount to be paid for the given block (from FeeSink)
BlkBonus
// BlkBranch is the hash of the previous block
BlkBranch
// BlkFeeSink is the fee sink for the given round
BlkFeeSink
// BlkProtocol is the ConsensusVersion of the block.
BlkProtocol
// BlkTxnCounter is the number of the next transaction after the block
BlkTxnCounter
jannotti marked this conversation as resolved.
Show resolved Hide resolved
// BlkProposerPayout is the actual amount moved from feesink to proposer
BlkProposerPayout

invalidBlockField // compile-time constant for number of fields
)
Expand All @@ -1010,11 +1020,16 @@ type blockFieldSpec struct {
}

var blockFieldSpecs = [...]blockFieldSpec{
{BlkSeed, StackBytes, randomnessVersion},
{BlkSeed, StackBytes32, randomnessVersion},
{BlkTimestamp, StackUint64, randomnessVersion},
{BlkProposer, StackAddress, incentiveVersion},
{BlkFeesCollected, StackUint64, incentiveVersion},
{BlkBonus, StackUint64, incentiveVersion},
{BlkBranch, StackBytes32, incentiveVersion},
{BlkFeeSink, StackAddress, incentiveVersion},
{BlkProtocol, StackBytes, incentiveVersion},
{BlkTxnCounter, StackUint64, incentiveVersion},
{BlkProposerPayout, StackUint64, incentiveVersion},
}

func blockFieldSpecByField(r BlockField) (blockFieldSpec, bool) {
Expand Down
11 changes: 8 additions & 3 deletions data/transactions/logic/fields_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion data/transactions/logic/langspec_v10.json
Original file line number Diff line number Diff line change
Expand Up @@ -4601,7 +4601,7 @@
"BlkTimestamp"
],
"ArgEnumTypes": [
"[]byte",
"[32]byte",
"uint64"
],
"DocCost": "1",
Expand Down
2 changes: 1 addition & 1 deletion data/transactions/logic/langspec_v7.json
Original file line number Diff line number Diff line change
Expand Up @@ -4249,7 +4249,7 @@
"BlkTimestamp"
],
"ArgEnumTypes": [
"[]byte",
"[32]byte",
"uint64"
],
"DocCost": "1",
Expand Down
2 changes: 1 addition & 1 deletion data/transactions/logic/langspec_v8.json
Original file line number Diff line number Diff line change
Expand Up @@ -4595,7 +4595,7 @@
"BlkTimestamp"
],
"ArgEnumTypes": [
"[]byte",
"[32]byte",
"uint64"
],
"DocCost": "1",
Expand Down
2 changes: 1 addition & 1 deletion data/transactions/logic/langspec_v9.json
Original file line number Diff line number Diff line change
Expand Up @@ -4595,7 +4595,7 @@
"BlkTimestamp"
],
"ArgEnumTypes": [
"[]byte",
"[32]byte",
"uint64"
],
"DocCost": "1",
Expand Down
46 changes: 32 additions & 14 deletions test/scripts/e2e_subs/hdr-access.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env python

import base64
import os
import sys
from goal import Goal
import algosdk.encoding as enc

from datetime import datetime

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

# We want to manipulate lastvalid, so we need to turn off autosend
goal.autosend = False

# We will be able to access two blocks, by setting lv explcitly. So we
# test that the block timestamp from two blocks ago is between 2 and 5
# (inclusive) seconds before the previous block timestamp. devMode
# might mess this test up.
# We will be able to access more than one previous block by using a
# shorter tx liftetime. So we test that the block timestamp from two
# blocks ago is between 2 and 5 (inclusive) seconds before the
# previous block timestamp. devMode might mess this test up.
teal = """
#pragma version 7
txn FirstValid
Expand All @@ -73,10 +72,7 @@
int 6
<
"""
checktimes = goal.assemble(teal)
tx = goal.app_create(joe, goal.assemble(teal))
tx.last_valid_round = tx.last_valid_round - 800
txinfo, err = goal.send(tx)
txinfo, err = goal.app_create(joe, goal.assemble(teal), lifetime=100)
assert not err, err

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


# Get FeeSink from `block` opcode, compare to REST API
teal = """
#pragma version 11
txn FirstValid
int 2
-
block BlkFeeSink
log
int 1
return
"""
txinfo, err = goal.app_create(joe, goal.assemble(teal), lifetime=100)
assert not err, err
assert len(txinfo["logs"]) == 1
opcode = txinfo["logs"][0]

block = goal.algod.block_info(txinfo['confirmed-round']-2)['block']
api = base64.b64encode(enc.decode_address(block['fees'])).decode("utf-8")

print(opcode, api)
jannotti marked this conversation as resolved.
Show resolved Hide resolved

assert opcode == api

stamp = datetime.now().strftime("%Y%m%d_%H%M%S")
print(f"{os.path.basename(sys.argv[0])} OK {stamp}")
Loading