|
| 1 | +// Copyright 2021 github.com/gagliardetto |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package sysvar |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/binary" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + bin "github.com/gagliardetto/binary" |
| 22 | + solana "github.com/gagliardetto/solana-go" |
| 23 | +) |
| 24 | + |
| 25 | +// EpochRewardsSize is the serialized size, in bytes, of the EpochRewards sysvar. |
| 26 | +const EpochRewardsSize = 81 |
| 27 | + |
| 28 | +// EpochRewards is the data of the EpochRewards sysvar (account |
| 29 | +// solana.SysVarEpochRewardsPubkey): the state of the partitioned epoch-rewards |
| 30 | +// distribution. Mirrors solana-sdk epoch_rewards::EpochRewards. |
| 31 | +type EpochRewards struct { |
| 32 | + // DistributionStartingBlockHeight is the block height at which rewards |
| 33 | + // distribution started for the current epoch. |
| 34 | + DistributionStartingBlockHeight uint64 |
| 35 | + // NumPartitions is the number of partitions rewards are split across. |
| 36 | + NumPartitions uint64 |
| 37 | + // ParentBlockhash is the blockhash of the epoch's last block, used to seed |
| 38 | + // the partition shuffle. |
| 39 | + ParentBlockhash solana.Hash |
| 40 | + // TotalPoints is the total rewards points (u128) calculated for the epoch. |
| 41 | + TotalPoints bin.Uint128 |
| 42 | + // TotalRewards is the total rewards, in lamports, for the epoch. |
| 43 | + TotalRewards uint64 |
| 44 | + // DistributedRewards is the rewards, in lamports, distributed so far. |
| 45 | + DistributedRewards uint64 |
| 46 | + // Active reports whether rewards distribution is in progress. |
| 47 | + Active bool |
| 48 | +} |
| 49 | + |
| 50 | +// Distribute records that amount more lamports of rewards have been distributed. |
| 51 | +// It returns an error if that would push DistributedRewards above TotalRewards |
| 52 | +// (or overflow), mirroring the assertion in EpochRewards::distribute. |
| 53 | +func (e *EpochRewards) Distribute(amount uint64) error { |
| 54 | + newDistributed := e.DistributedRewards + amount |
| 55 | + if newDistributed < e.DistributedRewards || newDistributed > e.TotalRewards { |
| 56 | + return fmt.Errorf("epoch rewards: distributing %d would exceed total rewards %d", amount, e.TotalRewards) |
| 57 | + } |
| 58 | + e.DistributedRewards = newDistributed |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +func (e EpochRewards) MarshalWithEncoder(encoder *bin.Encoder) error { |
| 63 | + if err := encoder.WriteUint64(e.DistributionStartingBlockHeight, binary.LittleEndian); err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + if err := encoder.WriteUint64(e.NumPartitions, binary.LittleEndian); err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + if err := encoder.WriteBytes(e.ParentBlockhash[:], false); err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + if err := encoder.WriteUint128(e.TotalPoints, binary.LittleEndian); err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + if err := encoder.WriteUint64(e.TotalRewards, binary.LittleEndian); err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + if err := encoder.WriteUint64(e.DistributedRewards, binary.LittleEndian); err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + return encoder.WriteBool(e.Active) |
| 82 | +} |
| 83 | + |
| 84 | +func (e *EpochRewards) UnmarshalWithDecoder(decoder *bin.Decoder) error { |
| 85 | + var err error |
| 86 | + if e.DistributionStartingBlockHeight, err = decoder.ReadUint64(binary.LittleEndian); err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + if e.NumPartitions, err = decoder.ReadUint64(binary.LittleEndian); err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + buf, err := decoder.ReadNBytes(32) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + copy(e.ParentBlockhash[:], buf) |
| 97 | + if e.TotalPoints, err = decoder.ReadUint128(binary.LittleEndian); err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + if e.TotalRewards, err = decoder.ReadUint64(binary.LittleEndian); err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + if e.DistributedRewards, err = decoder.ReadUint64(binary.LittleEndian); err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + e.Active, err = decoder.ReadBool() |
| 107 | + return err |
| 108 | +} |
| 109 | + |
| 110 | +func (e EpochRewards) MarshalBinary() ([]byte, error) { return encodeSysvar(e) } |
| 111 | +func (e *EpochRewards) UnmarshalBinary(data []byte) error { |
| 112 | + return e.UnmarshalWithDecoder(bin.NewBinDecoder(data)) |
| 113 | +} |
| 114 | + |
| 115 | +// DecodeEpochRewards decodes EpochRewards sysvar account data. |
| 116 | +func DecodeEpochRewards(data []byte) (*EpochRewards, error) { |
| 117 | + var e EpochRewards |
| 118 | + if err := e.UnmarshalBinary(data); err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + return &e, nil |
| 122 | +} |
0 commit comments