Skip to content

Commit

Permalink
Add some extra checks on cheap heartbeats
Browse files Browse the repository at this point in the history
  • Loading branch information
jannotti committed Nov 21, 2024
1 parent 1f93f49 commit 75f9afb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
15 changes: 14 additions & 1 deletion ledger/apply/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,24 @@ func Heartbeat(hb transactions.HeartbeatTxnFields, header transactions.Header, b
if header.Fee.Raw > 0 {
kind = "cheap"
}

// These first checks are a little draconian. The idea is not let these
// free transactions do anything except their exact intended purpose.
if len(header.Note) > 0 {
return fmt.Errorf("%s heartbeat is not allowed to have a note", kind)
}
if header.Lease != [32]byte{} {
return fmt.Errorf("%s heartbeat is not allowed to have a lease", kind)
}
if !header.RekeyTo.IsZero() {
return fmt.Errorf("%s heartbeat is not allowed to rekey", kind)
}

if account.Status != basics.Online {
return fmt.Errorf("%s heartbeat is not allowed for %s %+v", kind, account.Status, hb.HbAddress)
}
if !account.IncentiveEligible {
return fmt.Errorf("%s heartbeat is not allowed for ineligible %+v", kind, hb.HbAddress)
return fmt.Errorf("%s heartbeat is not allowed when not IncentiveEligible %+v", kind, hb.HbAddress)
}
ch := FindChallenge(proto.Payouts, round, provider, ChRisky)
if ch.round == 0 {
Expand Down
25 changes: 17 additions & 8 deletions ledger/apply/heartbeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,28 @@ func TestCheapRules(t *testing.T) {
addrStart byte
status basics.Status
incentiveEligble bool
note []byte
lease [32]byte
rekey [32]byte
err string
}

empty := [32]byte{}
// Grace period is 200. For the second half of the grace period (1101-1200),
// the heartbeat is free for online, incentive eligible, challenged accounts.
cases := []tcase{
// test of range
{1100, 0x01, basics.Online, true, "no challenge"},
{1101, 0x01, basics.Online, true, ""},
{1200, 0x01, basics.Online, true, ""},
{1201, 0x01, basics.Online, true, "no challenge"},
{1100, 0x01, basics.Online, true, nil, empty, empty, "no challenge"},
{1101, 0x01, basics.Online, true, nil, empty, empty, ""},
{1200, 0x01, basics.Online, true, nil, empty, empty, ""},
{1201, 0x01, basics.Online, true, nil, empty, empty, "no challenge"},

// test of the other requirements
{1101, 0xf1, basics.Online, true, "not challenged by"},
{1101, 0x01, basics.Offline, true, "not allowed for Offline"},
{1101, 0x01, basics.Online, false, "not allowed for ineligible"},
{1101, 0x01, basics.Online, true, []byte("note"), empty, empty, "not allowed to have a note"},
{1101, 0x01, basics.Online, true, nil, [32]byte{'l', 'e', 'a', 's', 'e'}, empty, "not allowed to have a lease"},
{1101, 0x01, basics.Online, true, nil, empty, [32]byte{'r', 'e', 'k', 'e', 'y'}, "not allowed to rekey"},
{1101, 0xf1, basics.Online, true, nil, empty, empty, "not challenged by"},
{1101, 0x01, basics.Offline, true, nil, empty, empty, "not allowed for Offline"},
{1101, 0x01, basics.Online, false, nil, empty, empty, "not allowed when not IncentiveEligible"},
}
for _, tc := range cases {
const keyDilution = 777
Expand Down Expand Up @@ -164,6 +170,9 @@ func TestCheapRules(t *testing.T) {
Fee: basics.MicroAlgos{Raw: 1},
FirstValid: tc.rnd - 10,
LastValid: tc.rnd + 10,
Lease: tc.lease,
Note: tc.note,
RekeyTo: tc.rekey,
HbAddress: voter,
HbProof: otss.Sign(id, seed),
HbSeed: seed,
Expand Down

0 comments on commit 75f9afb

Please sign in to comment.