mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-25 03:56:34 +00:00
cd42b8b20c
This simple approach allows to improve the performance of BoltDB and LevelDB in both terms of speed and allocations for retrieving GasPerVote value from the storage. MemoryPS's speed suffers a bit, but we don't use it for production environment. Part of #2322. Benchmark results: name old time/op new time/op delta NEO_GetGASPerVote/MemPS_10RewardRecords_1RewardDistance-8 25.3µs ± 1% 26.4µs ± 9% +4.41% (p=0.043 n=10+9) NEO_GetGASPerVote/MemPS_10RewardRecords_10RewardDistance-8 27.9µs ± 1% 30.1µs ±15% +7.97% (p=0.000 n=10+9) NEO_GetGASPerVote/MemPS_10RewardRecords_100RewardDistance-8 55.1µs ± 1% 60.2µs ± 7% +9.27% (p=0.000 n=10+10) NEO_GetGASPerVote/MemPS_10RewardRecords_1000RewardDistance-8 353µs ± 2% 416µs ±13% +17.88% (p=0.000 n=8+8) NEO_GetGASPerVote/MemPS_100RewardRecords_1RewardDistance-8 195µs ± 1% 216µs ± 7% +10.42% (p=0.000 n=10+10) NEO_GetGASPerVote/MemPS_100RewardRecords_10RewardDistance-8 200µs ± 4% 214µs ± 9% +6.99% (p=0.002 n=9+8) NEO_GetGASPerVote/MemPS_100RewardRecords_100RewardDistance-8 223µs ± 2% 247µs ± 9% +10.60% (p=0.000 n=10+10) NEO_GetGASPerVote/MemPS_100RewardRecords_1000RewardDistance-8 612µs ±23% 855µs ±52% +39.60% (p=0.001 n=9+10) NEO_GetGASPerVote/MemPS_1000RewardRecords_1RewardDistance-8 11.3ms ±53% 10.7ms ±50% ~ (p=0.739 n=10+10) NEO_GetGASPerVote/MemPS_1000RewardRecords_10RewardDistance-8 12.0ms ±37% 10.4ms ±65% ~ (p=0.853 n=10+10) NEO_GetGASPerVote/MemPS_1000RewardRecords_100RewardDistance-8 11.3ms ±40% 10.4ms ±49% ~ (p=0.631 n=10+10) NEO_GetGASPerVote/MemPS_1000RewardRecords_1000RewardDistance-8 3.80ms ±45% 3.69ms ±27% ~ (p=0.931 n=6+5) NEO_GetGASPerVote/BoltPS_10RewardRecords_1RewardDistance-8 23.0µs ± 9% 22.6µs ± 4% ~ (p=0.059 n=8+9) NEO_GetGASPerVote/BoltPS_10RewardRecords_10RewardDistance-8 25.9µs ± 5% 24.8µs ± 4% -4.17% (p=0.006 n=10+8) NEO_GetGASPerVote/BoltPS_10RewardRecords_100RewardDistance-8 42.7µs ±13% 38.9µs ± 1% -8.85% (p=0.000 n=9+8) NEO_GetGASPerVote/BoltPS_10RewardRecords_1000RewardDistance-8 80.8µs ±12% 84.9µs ± 9% ~ (p=0.114 n=8+9) NEO_GetGASPerVote/BoltPS_100RewardRecords_1RewardDistance-8 64.3µs ±16% 22.1µs ±23% -65.64% (p=0.000 n=10+10) NEO_GetGASPerVote/BoltPS_100RewardRecords_10RewardDistance-8 61.0µs ±34% 23.2µs ± 8% -62.04% (p=0.000 n=10+9) NEO_GetGASPerVote/BoltPS_100RewardRecords_100RewardDistance-8 62.2µs ±14% 25.7µs ±13% -58.66% (p=0.000 n=9+10) NEO_GetGASPerVote/BoltPS_100RewardRecords_1000RewardDistance-8 359µs ±60% 325µs ±60% ~ (p=0.739 n=10+10) NEO_GetGASPerVote/BoltPS_1000RewardRecords_1RewardDistance-8 242µs ±21% 13µs ±28% -94.49% (p=0.000 n=10+8) NEO_GetGASPerVote/BoltPS_1000RewardRecords_10RewardDistance-8 229µs ±23% 18µs ±70% -92.02% (p=0.000 n=10+9) NEO_GetGASPerVote/BoltPS_1000RewardRecords_100RewardDistance-8 238µs ±28% 20µs ±109% -91.38% (p=0.000 n=10+9) NEO_GetGASPerVote/BoltPS_1000RewardRecords_1000RewardDistance-8 265µs ±20% 77µs ±62% -71.04% (p=0.000 n=10+10) NEO_GetGASPerVote/LevelPS_10RewardRecords_1RewardDistance-8 25.5µs ± 3% 24.7µs ± 7% ~ (p=0.143 n=10+10) NEO_GetGASPerVote/LevelPS_10RewardRecords_10RewardDistance-8 27.4µs ± 2% 27.9µs ± 6% ~ (p=0.280 n=10+10) NEO_GetGASPerVote/LevelPS_10RewardRecords_100RewardDistance-8 50.2µs ± 7% 47.4µs ±10% ~ (p=0.156 n=9+10) NEO_GetGASPerVote/LevelPS_10RewardRecords_1000RewardDistance-8 98.2µs ± 9% 94.6µs ±10% ~ (p=0.218 n=10+10) NEO_GetGASPerVote/LevelPS_100RewardRecords_1RewardDistance-8 82.9µs ±13% 32.1µs ±22% -61.30% (p=0.000 n=10+10) NEO_GetGASPerVote/LevelPS_100RewardRecords_10RewardDistance-8 92.2µs ±11% 33.7µs ±12% -63.42% (p=0.000 n=10+9) NEO_GetGASPerVote/LevelPS_100RewardRecords_100RewardDistance-8 88.3µs ±22% 39.4µs ±14% -55.36% (p=0.000 n=10+9) NEO_GetGASPerVote/LevelPS_100RewardRecords_1000RewardDistance-8 106µs ±18% 78µs ±24% -26.20% (p=0.000 n=9+10) NEO_GetGASPerVote/LevelPS_1000RewardRecords_1RewardDistance-8 360µs ±24% 29µs ±53% -91.91% (p=0.000 n=10+9) NEO_GetGASPerVote/LevelPS_1000RewardRecords_10RewardDistance-8 353µs ±16% 50µs ±70% -85.72% (p=0.000 n=10+10) NEO_GetGASPerVote/LevelPS_1000RewardRecords_100RewardDistance-8 381µs ±20% 47µs ±111% -87.64% (p=0.000 n=10+10) NEO_GetGASPerVote/LevelPS_1000RewardRecords_1000RewardDistance-8 434µs ±19% 113µs ±41% -74.04% (p=0.000 n=10+10) name old alloc/op new alloc/op delta NEO_GetGASPerVote/MemPS_10RewardRecords_1RewardDistance-8 4.82kB ± 0% 4.26kB ± 1% -11.62% (p=0.000 n=10+9) NEO_GetGASPerVote/MemPS_10RewardRecords_10RewardDistance-8 4.99kB ± 0% 4.41kB ± 1% -11.56% (p=0.000 n=10+10) NEO_GetGASPerVote/MemPS_10RewardRecords_100RewardDistance-8 8.45kB ± 0% 7.87kB ± 0% -6.88% (p=0.000 n=10+10) NEO_GetGASPerVote/MemPS_10RewardRecords_1000RewardDistance-8 55.0kB ± 0% 54.5kB ± 0% -0.81% (p=0.000 n=10+10) NEO_GetGASPerVote/MemPS_100RewardRecords_1RewardDistance-8 29.1kB ± 0% 21.7kB ± 2% -25.56% (p=0.000 n=9+9) NEO_GetGASPerVote/MemPS_100RewardRecords_10RewardDistance-8 29.3kB ± 1% 21.8kB ± 2% -25.74% (p=0.000 n=10+10) NEO_GetGASPerVote/MemPS_100RewardRecords_100RewardDistance-8 31.3kB ± 1% 23.6kB ± 1% -24.50% (p=0.000 n=10+10) NEO_GetGASPerVote/MemPS_100RewardRecords_1000RewardDistance-8 92.5kB ± 5% 84.7kB ± 3% -8.50% (p=0.000 n=10+9) NEO_GetGASPerVote/MemPS_1000RewardRecords_1RewardDistance-8 324kB ±29% 222kB ±44% -31.33% (p=0.007 n=10+10) NEO_GetGASPerVote/MemPS_1000RewardRecords_10RewardDistance-8 308kB ±32% 174kB ±14% -43.56% (p=0.000 n=10+8) NEO_GetGASPerVote/MemPS_1000RewardRecords_100RewardDistance-8 298kB ±23% 178kB ±36% -40.26% (p=0.000 n=10+10) NEO_GetGASPerVote/MemPS_1000RewardRecords_1000RewardDistance-8 362kB ± 6% 248kB ± 6% -31.54% (p=0.004 n=6+5) NEO_GetGASPerVote/BoltPS_10RewardRecords_1RewardDistance-8 5.15kB ± 3% 4.64kB ± 2% -9.92% (p=0.000 n=10+9) NEO_GetGASPerVote/BoltPS_10RewardRecords_10RewardDistance-8 5.36kB ± 1% 4.75kB ± 5% -11.42% (p=0.000 n=10+10) NEO_GetGASPerVote/BoltPS_10RewardRecords_100RewardDistance-8 8.15kB ± 4% 7.53kB ± 1% -7.62% (p=0.000 n=10+9) NEO_GetGASPerVote/BoltPS_10RewardRecords_1000RewardDistance-8 33.2kB ± 5% 33.2kB ± 7% ~ (p=0.829 n=8+10) NEO_GetGASPerVote/BoltPS_100RewardRecords_1RewardDistance-8 20.1kB ± 7% 5.8kB ±13% -70.90% (p=0.000 n=10+10) NEO_GetGASPerVote/BoltPS_100RewardRecords_10RewardDistance-8 19.8kB ±14% 6.2kB ± 5% -68.87% (p=0.000 n=10+9) NEO_GetGASPerVote/BoltPS_100RewardRecords_100RewardDistance-8 21.7kB ± 6% 8.0kB ± 7% -63.20% (p=0.000 n=9+10) NEO_GetGASPerVote/BoltPS_100RewardRecords_1000RewardDistance-8 98.5kB ±44% 81.8kB ±48% ~ (p=0.143 n=10+10) NEO_GetGASPerVote/BoltPS_1000RewardRecords_1RewardDistance-8 130kB ± 4% 4kB ± 9% -96.69% (p=0.000 n=10+10) NEO_GetGASPerVote/BoltPS_1000RewardRecords_10RewardDistance-8 131kB ± 4% 5kB ±21% -96.48% (p=0.000 n=9+9) NEO_GetGASPerVote/BoltPS_1000RewardRecords_100RewardDistance-8 132kB ± 4% 6kB ±10% -95.39% (p=0.000 n=10+8) NEO_GetGASPerVote/BoltPS_1000RewardRecords_1000RewardDistance-8 151kB ± 4% 26kB ±10% -82.46% (p=0.000 n=9+9) NEO_GetGASPerVote/LevelPS_10RewardRecords_1RewardDistance-8 5.92kB ± 3% 5.32kB ± 2% -10.01% (p=0.000 n=10+10) NEO_GetGASPerVote/LevelPS_10RewardRecords_10RewardDistance-8 6.09kB ± 2% 5.48kB ± 2% -10.00% (p=0.000 n=10+10) NEO_GetGASPerVote/LevelPS_10RewardRecords_100RewardDistance-8 9.61kB ± 1% 9.00kB ± 0% -6.29% (p=0.000 n=10+10) NEO_GetGASPerVote/LevelPS_10RewardRecords_1000RewardDistance-8 33.4kB ± 7% 32.2kB ± 5% -3.60% (p=0.037 n=10+10) NEO_GetGASPerVote/LevelPS_100RewardRecords_1RewardDistance-8 22.3kB ±10% 9.0kB ±16% -59.78% (p=0.000 n=10+10) NEO_GetGASPerVote/LevelPS_100RewardRecords_10RewardDistance-8 23.6kB ± 6% 8.5kB ±20% -63.76% (p=0.000 n=10+10) NEO_GetGASPerVote/LevelPS_100RewardRecords_100RewardDistance-8 24.2kB ± 9% 11.5kB ± 4% -52.34% (p=0.000 n=10+8) NEO_GetGASPerVote/LevelPS_100RewardRecords_1000RewardDistance-8 44.2kB ± 6% 30.8kB ± 9% -30.24% (p=0.000 n=10+10) NEO_GetGASPerVote/LevelPS_1000RewardRecords_1RewardDistance-8 144kB ± 4% 10kB ±24% -93.39% (p=0.000 n=9+10) NEO_GetGASPerVote/LevelPS_1000RewardRecords_10RewardDistance-8 146kB ± 1% 11kB ±37% -92.14% (p=0.000 n=7+10) NEO_GetGASPerVote/LevelPS_1000RewardRecords_100RewardDistance-8 149kB ± 3% 11kB ±12% -92.28% (p=0.000 n=10+9) NEO_GetGASPerVote/LevelPS_1000RewardRecords_1000RewardDistance-8 171kB ± 4% 34kB ±12% -80.00% (p=0.000 n=10+10) name old allocs/op new allocs/op delta NEO_GetGASPerVote/MemPS_10RewardRecords_1RewardDistance-8 95.0 ± 0% 74.0 ± 0% -22.11% (p=0.001 n=8+9) NEO_GetGASPerVote/MemPS_10RewardRecords_10RewardDistance-8 100 ± 0% 78 ± 1% -21.70% (p=0.000 n=10+10) NEO_GetGASPerVote/MemPS_10RewardRecords_100RewardDistance-8 153 ± 0% 131 ± 2% -14.25% (p=0.000 n=6+10) NEO_GetGASPerVote/MemPS_10RewardRecords_1000RewardDistance-8 799 ± 2% 797 ± 4% ~ (p=0.956 n=10+10) NEO_GetGASPerVote/MemPS_100RewardRecords_1RewardDistance-8 438 ± 6% 167 ± 0% -61.86% (p=0.000 n=10+9) NEO_GetGASPerVote/MemPS_100RewardRecords_10RewardDistance-8 446 ± 5% 172 ± 0% -61.38% (p=0.000 n=10+10) NEO_GetGASPerVote/MemPS_100RewardRecords_100RewardDistance-8 506 ± 4% 232 ± 1% -54.21% (p=0.000 n=10+10) NEO_GetGASPerVote/MemPS_100RewardRecords_1000RewardDistance-8 1.31k ± 5% 0.97k ± 4% -26.20% (p=0.000 n=10+10) NEO_GetGASPerVote/MemPS_1000RewardRecords_1RewardDistance-8 5.06k ± 1% 1.09k ± 2% -78.53% (p=0.000 n=10+10) NEO_GetGASPerVote/MemPS_1000RewardRecords_10RewardDistance-8 5.02k ± 3% 1.08k ± 0% -78.45% (p=0.000 n=10+8) NEO_GetGASPerVote/MemPS_1000RewardRecords_100RewardDistance-8 5.09k ± 3% 1.15k ± 2% -77.48% (p=0.000 n=10+10) NEO_GetGASPerVote/MemPS_1000RewardRecords_1000RewardDistance-8 5.83k ± 1% 1.87k ± 3% -68.02% (p=0.004 n=6+5) NEO_GetGASPerVote/BoltPS_10RewardRecords_1RewardDistance-8 103 ± 2% 82 ± 1% -20.83% (p=0.000 n=10+10) NEO_GetGASPerVote/BoltPS_10RewardRecords_10RewardDistance-8 107 ± 0% 86 ± 0% -19.63% (p=0.000 n=8+8) NEO_GetGASPerVote/BoltPS_10RewardRecords_100RewardDistance-8 164 ± 1% 139 ± 0% -15.45% (p=0.000 n=10+9) NEO_GetGASPerVote/BoltPS_10RewardRecords_1000RewardDistance-8 820 ± 1% 789 ± 1% -3.70% (p=0.000 n=9+10) NEO_GetGASPerVote/BoltPS_100RewardRecords_1RewardDistance-8 475 ± 0% 94 ± 3% -80.15% (p=0.000 n=10+9) NEO_GetGASPerVote/BoltPS_100RewardRecords_10RewardDistance-8 481 ± 0% 100 ± 2% -79.26% (p=0.000 n=9+9) NEO_GetGASPerVote/BoltPS_100RewardRecords_100RewardDistance-8 549 ± 0% 161 ± 2% -70.69% (p=0.000 n=10+10) NEO_GetGASPerVote/BoltPS_100RewardRecords_1000RewardDistance-8 1.61k ±19% 1.19k ±25% -26.05% (p=0.000 n=10+10) NEO_GetGASPerVote/BoltPS_1000RewardRecords_1RewardDistance-8 4.12k ± 0% 0.08k ± 2% -98.02% (p=0.000 n=10+10) NEO_GetGASPerVote/BoltPS_1000RewardRecords_10RewardDistance-8 4.14k ± 0% 0.09k ± 3% -97.90% (p=0.000 n=9+9) NEO_GetGASPerVote/BoltPS_1000RewardRecords_100RewardDistance-8 4.19k ± 0% 0.15k ± 3% -96.52% (p=0.000 n=9+9) NEO_GetGASPerVote/BoltPS_1000RewardRecords_1000RewardDistance-8 4.82k ± 1% 0.74k ± 1% -84.58% (p=0.000 n=10+9) NEO_GetGASPerVote/LevelPS_10RewardRecords_1RewardDistance-8 112 ± 4% 90 ± 3% -19.45% (p=0.000 n=10+10) NEO_GetGASPerVote/LevelPS_10RewardRecords_10RewardDistance-8 116 ± 2% 95 ± 2% -17.90% (p=0.000 n=10+10) NEO_GetGASPerVote/LevelPS_10RewardRecords_100RewardDistance-8 170 ± 3% 148 ± 3% -12.99% (p=0.000 n=10+10) NEO_GetGASPerVote/LevelPS_10RewardRecords_1000RewardDistance-8 800 ± 2% 772 ± 2% -3.50% (p=0.000 n=10+10) NEO_GetGASPerVote/LevelPS_100RewardRecords_1RewardDistance-8 480 ± 3% 118 ± 3% -75.32% (p=0.000 n=10+10) NEO_GetGASPerVote/LevelPS_100RewardRecords_10RewardDistance-8 479 ± 2% 123 ± 3% -74.33% (p=0.000 n=10+9) NEO_GetGASPerVote/LevelPS_100RewardRecords_100RewardDistance-8 542 ± 1% 183 ± 3% -66.34% (p=0.000 n=10+9) NEO_GetGASPerVote/LevelPS_100RewardRecords_1000RewardDistance-8 1.19k ± 1% 0.79k ± 1% -33.41% (p=0.000 n=10+10) NEO_GetGASPerVote/LevelPS_1000RewardRecords_1RewardDistance-8 4.21k ± 1% 0.13k ±21% -96.83% (p=0.000 n=10+10) NEO_GetGASPerVote/LevelPS_1000RewardRecords_10RewardDistance-8 4.23k ± 1% 0.15k ±17% -96.48% (p=0.000 n=10+10) NEO_GetGASPerVote/LevelPS_1000RewardRecords_100RewardDistance-8 4.27k ± 0% 0.19k ± 6% -95.51% (p=0.000 n=10+9) NEO_GetGASPerVote/LevelPS_1000RewardRecords_1000RewardDistance-8 4.89k ± 1% 0.79k ± 2% -83.80% (p=0.000 n=10+10)
1091 lines
32 KiB
Go
1091 lines
32 KiB
Go
package native
|
|
|
|
import (
|
|
"crypto/elliptic"
|
|
"encoding/binary"
|
|
"errors"
|
|
"fmt"
|
|
"math/big"
|
|
"sort"
|
|
"strings"
|
|
"sync/atomic"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/dao"
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/runtime"
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
)
|
|
|
|
// NEO represents NEO native contract.
|
|
type NEO struct {
|
|
nep17TokenNative
|
|
GAS *GAS
|
|
|
|
// gasPerBlock represents current value of generated gas per block.
|
|
// It is append-only and doesn't need to be copied when used.
|
|
gasPerBlock atomic.Value
|
|
gasPerBlockChanged atomic.Value
|
|
|
|
registerPrice atomic.Value
|
|
registerPriceChanged atomic.Value
|
|
|
|
votesChanged atomic.Value
|
|
nextValidators atomic.Value
|
|
validators atomic.Value
|
|
// committee contains cached committee members and their votes.
|
|
// It is updated once in a while depending on committee size
|
|
// (every 28 blocks for mainnet). It's value
|
|
// is always equal to value stored by `prefixCommittee`.
|
|
committee atomic.Value
|
|
// committeeHash contains script hash of the committee.
|
|
committeeHash atomic.Value
|
|
|
|
// gasPerVoteCache contains last updated value of GAS per vote reward for candidates.
|
|
// It is set in state-modifying methods only and read in `PostPersist` thus is not protected
|
|
// by any mutex.
|
|
gasPerVoteCache map[string]big.Int
|
|
}
|
|
|
|
const (
|
|
neoContractID = -5
|
|
// NEOTotalSupply is the total amount of NEO in the system.
|
|
NEOTotalSupply = 100000000
|
|
// DefaultRegisterPrice is default price for candidate register.
|
|
DefaultRegisterPrice = 1000 * GASFactor
|
|
// prefixCandidate is a prefix used to store validator's data.
|
|
prefixCandidate = 33
|
|
// prefixVotersCount is a prefix for storing total amount of NEO of voters.
|
|
prefixVotersCount = 1
|
|
// prefixVoterRewardPerCommittee is a prefix for storing committee GAS reward.
|
|
prefixVoterRewardPerCommittee = 23
|
|
// voterRewardFactor is a factor by which voter reward per committee is multiplied
|
|
// to make calculations more precise.
|
|
voterRewardFactor = 100_000_000
|
|
// prefixGASPerBlock is a prefix for storing amount of GAS generated per block.
|
|
prefixGASPerBlock = 29
|
|
// prefixRegisterPrice is a prefix for storing candidate register price.
|
|
prefixRegisterPrice = 13
|
|
// effectiveVoterTurnout represents minimal ratio of total supply to total amount voted value
|
|
// which is require to use non-standby validators.
|
|
effectiveVoterTurnout = 5
|
|
// neoHolderRewardRatio is a percent of generated GAS that is distributed to NEO holders.
|
|
neoHolderRewardRatio = 10
|
|
// neoHolderRewardRatio is a percent of generated GAS that is distributed to committee.
|
|
committeeRewardRatio = 10
|
|
// neoHolderRewardRatio is a percent of generated GAS that is distributed to voters.
|
|
voterRewardRatio = 80
|
|
)
|
|
|
|
var (
|
|
// prefixCommittee is a key used to store committee.
|
|
prefixCommittee = []byte{14}
|
|
|
|
bigCommitteeRewardRatio = big.NewInt(committeeRewardRatio)
|
|
bigVoterRewardRatio = big.NewInt(voterRewardRatio)
|
|
bigVoterRewardFactor = big.NewInt(voterRewardFactor)
|
|
bigEffectiveVoterTurnout = big.NewInt(effectiveVoterTurnout)
|
|
big100 = big.NewInt(100)
|
|
)
|
|
|
|
// makeValidatorKey creates a key from account script hash.
|
|
func makeValidatorKey(key *keys.PublicKey) []byte {
|
|
b := key.Bytes()
|
|
// Don't create a new buffer.
|
|
b = append(b, 0)
|
|
copy(b[1:], b[0:])
|
|
b[0] = prefixCandidate
|
|
return b
|
|
}
|
|
|
|
// newNEO returns NEO native contract.
|
|
func newNEO() *NEO {
|
|
n := &NEO{}
|
|
defer n.UpdateHash()
|
|
|
|
nep17 := newNEP17Native(nativenames.Neo, neoContractID)
|
|
nep17.symbol = "NEO"
|
|
nep17.decimals = 0
|
|
nep17.factor = 1
|
|
nep17.incBalance = n.increaseBalance
|
|
nep17.balFromBytes = n.balanceFromBytes
|
|
|
|
n.nep17TokenNative = *nep17
|
|
n.votesChanged.Store(true)
|
|
n.nextValidators.Store(keys.PublicKeys(nil))
|
|
n.validators.Store(keys.PublicKeys(nil))
|
|
n.committee.Store(keysWithVotes(nil))
|
|
n.committeeHash.Store(util.Uint160{})
|
|
n.registerPriceChanged.Store(true)
|
|
n.gasPerVoteCache = make(map[string]big.Int)
|
|
|
|
desc := newDescriptor("unclaimedGas", smartcontract.IntegerType,
|
|
manifest.NewParameter("account", smartcontract.Hash160Type),
|
|
manifest.NewParameter("end", smartcontract.IntegerType))
|
|
md := newMethodAndPrice(n.unclaimedGas, 1<<17, callflag.ReadStates)
|
|
n.AddMethod(md, desc)
|
|
|
|
desc = newDescriptor("registerCandidate", smartcontract.BoolType,
|
|
manifest.NewParameter("pubkey", smartcontract.PublicKeyType))
|
|
md = newMethodAndPrice(n.registerCandidate, 0, callflag.States)
|
|
n.AddMethod(md, desc)
|
|
|
|
desc = newDescriptor("unregisterCandidate", smartcontract.BoolType,
|
|
manifest.NewParameter("pubkey", smartcontract.PublicKeyType))
|
|
md = newMethodAndPrice(n.unregisterCandidate, 1<<16, callflag.States)
|
|
n.AddMethod(md, desc)
|
|
|
|
desc = newDescriptor("vote", smartcontract.BoolType,
|
|
manifest.NewParameter("account", smartcontract.Hash160Type),
|
|
manifest.NewParameter("voteTo", smartcontract.PublicKeyType))
|
|
md = newMethodAndPrice(n.vote, 1<<16, callflag.States)
|
|
n.AddMethod(md, desc)
|
|
|
|
desc = newDescriptor("getCandidates", smartcontract.ArrayType)
|
|
md = newMethodAndPrice(n.getCandidatesCall, 1<<22, callflag.ReadStates)
|
|
n.AddMethod(md, desc)
|
|
|
|
desc = newDescriptor("getAccountState", smartcontract.ArrayType,
|
|
manifest.NewParameter("account", smartcontract.Hash160Type))
|
|
md = newMethodAndPrice(n.getAccountState, 1<<15, callflag.ReadStates)
|
|
n.AddMethod(md, desc)
|
|
|
|
desc = newDescriptor("getCommittee", smartcontract.ArrayType)
|
|
md = newMethodAndPrice(n.getCommittee, 1<<16, callflag.ReadStates)
|
|
n.AddMethod(md, desc)
|
|
|
|
desc = newDescriptor("getNextBlockValidators", smartcontract.ArrayType)
|
|
md = newMethodAndPrice(n.getNextBlockValidators, 1<<16, callflag.ReadStates)
|
|
n.AddMethod(md, desc)
|
|
|
|
desc = newDescriptor("getGasPerBlock", smartcontract.IntegerType)
|
|
md = newMethodAndPrice(n.getGASPerBlock, 1<<15, callflag.ReadStates)
|
|
n.AddMethod(md, desc)
|
|
|
|
desc = newDescriptor("setGasPerBlock", smartcontract.VoidType,
|
|
manifest.NewParameter("gasPerBlock", smartcontract.IntegerType))
|
|
md = newMethodAndPrice(n.setGASPerBlock, 1<<15, callflag.States)
|
|
n.AddMethod(md, desc)
|
|
|
|
desc = newDescriptor("getRegisterPrice", smartcontract.IntegerType)
|
|
md = newMethodAndPrice(n.getRegisterPrice, 1<<15, callflag.ReadStates)
|
|
n.AddMethod(md, desc)
|
|
|
|
desc = newDescriptor("setRegisterPrice", smartcontract.VoidType,
|
|
manifest.NewParameter("registerPrice", smartcontract.IntegerType))
|
|
md = newMethodAndPrice(n.setRegisterPrice, 1<<15, callflag.States)
|
|
n.AddMethod(md, desc)
|
|
|
|
return n
|
|
}
|
|
|
|
// Initialize initializes NEO contract.
|
|
func (n *NEO) Initialize(ic *interop.Context) error {
|
|
if err := n.nep17TokenNative.Initialize(ic); err != nil {
|
|
return err
|
|
}
|
|
|
|
_, totalSupply := n.nep17TokenNative.getTotalSupply(ic.DAO)
|
|
if totalSupply.Sign() != 0 {
|
|
return errors.New("already initialized")
|
|
}
|
|
|
|
committee := ic.Chain.GetStandByCommittee()
|
|
cvs := toKeysWithVotes(committee)
|
|
err := n.updateCache(cvs, ic.Chain)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = ic.DAO.PutStorageItem(n.ID, prefixCommittee, cvs.Bytes())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
h, err := getStandbyValidatorsHash(ic)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
n.mint(ic, h, big.NewInt(NEOTotalSupply), false)
|
|
|
|
var index uint32
|
|
value := big.NewInt(5 * GASFactor)
|
|
err = n.putGASRecord(ic.DAO, index, value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
gr := &gasRecord{{Index: index, GASPerBlock: *value}}
|
|
n.gasPerBlock.Store(*gr)
|
|
n.gasPerBlockChanged.Store(false)
|
|
err = ic.DAO.PutStorageItem(n.ID, []byte{prefixVotersCount}, state.StorageItem{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = setIntWithKey(n.ID, ic.DAO, []byte{prefixRegisterPrice}, DefaultRegisterPrice)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
n.registerPrice.Store(int64(DefaultRegisterPrice))
|
|
n.registerPriceChanged.Store(false)
|
|
return nil
|
|
}
|
|
|
|
// InitializeCache initializes all NEO cache with the proper values from storage.
|
|
// Cache initialisation should be done apart from Initialize because Initialize is
|
|
// called only when deploying native contracts.
|
|
func (n *NEO) InitializeCache(bc interop.Ledger, d dao.DAO) error {
|
|
var committee = keysWithVotes{}
|
|
si := d.GetStorageItem(n.ID, prefixCommittee)
|
|
if err := committee.DecodeBytes(si); err != nil {
|
|
return err
|
|
}
|
|
if err := n.updateCache(committee, bc); err != nil {
|
|
return err
|
|
}
|
|
|
|
gr, err := n.getSortedGASRecordFromDAO(d)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
n.gasPerBlock.Store(gr)
|
|
n.gasPerBlockChanged.Store(false)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (n *NEO) updateCache(cvs keysWithVotes, bc interop.Ledger) error {
|
|
n.committee.Store(cvs)
|
|
|
|
var committee = n.GetCommitteeMembers()
|
|
script, err := smartcontract.CreateMajorityMultiSigRedeemScript(committee.Copy())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
n.committeeHash.Store(hash.Hash160(script))
|
|
|
|
nextVals := committee[:bc.GetConfig().ValidatorsCount].Copy()
|
|
sort.Sort(nextVals)
|
|
n.nextValidators.Store(nextVals)
|
|
return nil
|
|
}
|
|
|
|
func (n *NEO) updateCommittee(ic *interop.Context) error {
|
|
votesChanged := n.votesChanged.Load().(bool)
|
|
if !votesChanged {
|
|
// We need to put in storage anyway, as it affects dumps
|
|
committee := n.committee.Load().(keysWithVotes)
|
|
return ic.DAO.PutStorageItem(n.ID, prefixCommittee, committee.Bytes())
|
|
}
|
|
|
|
_, cvs, err := n.computeCommitteeMembers(ic.Chain, ic.DAO)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := n.updateCache(cvs, ic.Chain); err != nil {
|
|
return err
|
|
}
|
|
n.votesChanged.Store(false)
|
|
return ic.DAO.PutStorageItem(n.ID, prefixCommittee, cvs.Bytes())
|
|
}
|
|
|
|
// ShouldUpdateCommittee returns true if committee is updated at block h.
|
|
func ShouldUpdateCommittee(h uint32, bc interop.Ledger) bool {
|
|
cfg := bc.GetConfig()
|
|
r := len(cfg.StandbyCommittee)
|
|
return h%uint32(r) == 0
|
|
}
|
|
|
|
// OnPersist implements Contract interface.
|
|
func (n *NEO) OnPersist(ic *interop.Context) error {
|
|
if ShouldUpdateCommittee(ic.Block.Index, ic.Chain) {
|
|
if err := n.updateCommittee(ic); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// PostPersist implements Contract interface.
|
|
func (n *NEO) PostPersist(ic *interop.Context) error {
|
|
gas := n.GetGASPerBlock(ic.DAO, ic.Block.Index)
|
|
pubs := n.GetCommitteeMembers()
|
|
committeeSize := len(ic.Chain.GetConfig().StandbyCommittee)
|
|
index := int(ic.Block.Index) % committeeSize
|
|
committeeReward := new(big.Int).Mul(gas, bigCommitteeRewardRatio)
|
|
n.GAS.mint(ic, pubs[index].GetScriptHash(), committeeReward.Div(committeeReward, big100), false)
|
|
|
|
if ShouldUpdateCommittee(ic.Block.Index, ic.Chain) {
|
|
var voterReward = new(big.Int).Set(bigVoterRewardRatio)
|
|
voterReward.Mul(voterReward, gas)
|
|
voterReward.Mul(voterReward, big.NewInt(voterRewardFactor*int64(committeeSize)))
|
|
var validatorsCount = ic.Chain.GetConfig().ValidatorsCount
|
|
voterReward.Div(voterReward, big.NewInt(int64(committeeSize+validatorsCount)))
|
|
voterReward.Div(voterReward, big100)
|
|
|
|
var cs = n.committee.Load().(keysWithVotes)
|
|
var key = make([]byte, 38)
|
|
for i := range cs {
|
|
if cs[i].Votes.Sign() > 0 {
|
|
var tmp = new(big.Int)
|
|
if i < validatorsCount {
|
|
tmp.Set(intTwo)
|
|
} else {
|
|
tmp.Set(intOne)
|
|
}
|
|
tmp.Mul(tmp, voterReward)
|
|
tmp.Div(tmp, cs[i].Votes)
|
|
|
|
key = makeVoterKey([]byte(cs[i].Key), key)
|
|
|
|
var r *big.Int
|
|
if g, ok := n.gasPerVoteCache[cs[i].Key]; ok {
|
|
r = &g
|
|
} else {
|
|
reward := n.getGASPerVote(ic.DAO, key[:34], []uint32{ic.Block.Index + 1})
|
|
r = &reward[0]
|
|
}
|
|
tmp.Add(tmp, r)
|
|
|
|
binary.BigEndian.PutUint32(key[34:], ic.Block.Index+1)
|
|
n.gasPerVoteCache[cs[i].Key] = *tmp
|
|
|
|
if err := ic.DAO.PutStorageItem(n.ID, key, bigint.ToBytes(tmp)); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if n.gasPerBlockChanged.Load().(bool) {
|
|
gr, err := n.getSortedGASRecordFromDAO(ic.DAO)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
n.gasPerBlock.Store(gr)
|
|
n.gasPerBlockChanged.Store(false)
|
|
}
|
|
|
|
if n.registerPriceChanged.Load().(bool) {
|
|
p := getIntWithKey(n.ID, ic.DAO, []byte{prefixRegisterPrice})
|
|
n.registerPrice.Store(p)
|
|
n.registerPriceChanged.Store(false)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (n *NEO) getGASPerVote(d dao.DAO, key []byte, indexes []uint32) []big.Int {
|
|
sort.Slice(indexes, func(i, j int) bool {
|
|
return indexes[i] < indexes[j]
|
|
})
|
|
start := make([]byte, 4)
|
|
binary.BigEndian.PutUint32(start, indexes[len(indexes)-1])
|
|
|
|
need := len(indexes)
|
|
var reward = make([]big.Int, need)
|
|
collected := 0
|
|
d.Seek(n.ID, storage.SeekRange{
|
|
Prefix: key,
|
|
Start: start,
|
|
Backwards: true,
|
|
}, func(k, v []byte) bool {
|
|
if len(k) == 4 {
|
|
num := binary.BigEndian.Uint32(k)
|
|
for i, ind := range indexes {
|
|
if reward[i].Sign() == 0 && num <= ind {
|
|
reward[i] = *bigint.FromBytes(v)
|
|
collected++
|
|
}
|
|
}
|
|
}
|
|
return collected < need
|
|
})
|
|
return reward
|
|
}
|
|
|
|
func (n *NEO) increaseBalance(ic *interop.Context, h util.Uint160, si *state.StorageItem, amount *big.Int, checkBal *big.Int) error {
|
|
acc, err := state.NEOBalanceFromBytes(*si)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if (amount.Sign() == -1 && acc.Balance.CmpAbs(amount) == -1) ||
|
|
(amount.Sign() == 0 && checkBal != nil && acc.Balance.Cmp(checkBal) == -1) {
|
|
return errors.New("insufficient funds")
|
|
}
|
|
if err := n.distributeGas(ic, h, acc); err != nil {
|
|
return err
|
|
}
|
|
if amount.Sign() == 0 {
|
|
*si = acc.Bytes()
|
|
return nil
|
|
}
|
|
if err := n.ModifyAccountVotes(acc, ic.DAO, amount, false); err != nil {
|
|
return err
|
|
}
|
|
if acc.VoteTo != nil {
|
|
if err := n.modifyVoterTurnout(ic.DAO, amount); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
acc.Balance.Add(&acc.Balance, amount)
|
|
if acc.Balance.Sign() != 0 {
|
|
*si = acc.Bytes()
|
|
} else {
|
|
*si = nil
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (n *NEO) balanceFromBytes(si *state.StorageItem) (*big.Int, error) {
|
|
acc, err := state.NEOBalanceFromBytes(*si)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &acc.Balance, err
|
|
}
|
|
|
|
func (n *NEO) distributeGas(ic *interop.Context, h util.Uint160, acc *state.NEOBalance) error {
|
|
if ic.Block == nil || ic.Block.Index == 0 || ic.Block.Index == acc.BalanceHeight {
|
|
return nil
|
|
}
|
|
gen, err := n.calculateBonus(ic.DAO, acc.VoteTo, &acc.Balance, acc.BalanceHeight, ic.Block.Index)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
acc.BalanceHeight = ic.Block.Index
|
|
|
|
// Must store acc before GAS distribution to fix acc's BalanceHeight value in the storage for
|
|
// further acc's queries from `onNEP17Payment` if so, see https://github.com/nspcc-dev/neo-go/pull/2181.
|
|
key := makeAccountKey(h)
|
|
err = ic.DAO.PutStorageItem(n.ID, key, acc.Bytes())
|
|
if err != nil {
|
|
return fmt.Errorf("failed to store acc before gas distribution: %w", err)
|
|
}
|
|
|
|
n.GAS.mint(ic, h, gen, true)
|
|
return nil
|
|
}
|
|
|
|
func (n *NEO) unclaimedGas(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
u := toUint160(args[0])
|
|
end := uint32(toBigInt(args[1]).Int64())
|
|
gen, err := n.CalculateBonus(ic.DAO, u, end)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return stackitem.NewBigInteger(gen)
|
|
}
|
|
|
|
func (n *NEO) getGASPerBlock(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
|
|
gas := n.GetGASPerBlock(ic.DAO, ic.Block.Index)
|
|
return stackitem.NewBigInteger(gas)
|
|
}
|
|
|
|
func (n *NEO) getSortedGASRecordFromDAO(d dao.DAO) (gasRecord, error) {
|
|
grArr, err := d.GetStorageItemsWithPrefix(n.ID, []byte{prefixGASPerBlock})
|
|
if err != nil {
|
|
return gasRecord{}, fmt.Errorf("failed to get gas records from storage: %w", err)
|
|
}
|
|
var gr = make(gasRecord, len(grArr))
|
|
for i, kv := range grArr {
|
|
indexBytes, gasValue := kv.Key, kv.Item
|
|
gr[i] = gasIndexPair{
|
|
Index: binary.BigEndian.Uint32([]byte(indexBytes)),
|
|
GASPerBlock: *bigint.FromBytes(gasValue),
|
|
}
|
|
}
|
|
// GAS records should be sorted by index, but GetStorageItemsWithPrefix returns
|
|
// values sorted by BE bytes of index, so we're OK with that.
|
|
return gr, nil
|
|
}
|
|
|
|
// GetGASPerBlock returns gas generated for block with provided index.
|
|
func (n *NEO) GetGASPerBlock(d dao.DAO, index uint32) *big.Int {
|
|
var (
|
|
gr gasRecord
|
|
err error
|
|
)
|
|
if n.gasPerBlockChanged.Load().(bool) {
|
|
gr, err = n.getSortedGASRecordFromDAO(d)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
} else {
|
|
gr = n.gasPerBlock.Load().(gasRecord)
|
|
}
|
|
for i := len(gr) - 1; i >= 0; i-- {
|
|
if gr[i].Index <= index {
|
|
g := gr[i].GASPerBlock
|
|
return &g
|
|
}
|
|
}
|
|
panic("contract not initialized")
|
|
}
|
|
|
|
// GetCommitteeAddress returns address of the committee.
|
|
func (n *NEO) GetCommitteeAddress() util.Uint160 {
|
|
return n.committeeHash.Load().(util.Uint160)
|
|
}
|
|
|
|
func (n *NEO) checkCommittee(ic *interop.Context) bool {
|
|
ok, err := runtime.CheckHashedWitness(ic, n.GetCommitteeAddress())
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return ok
|
|
}
|
|
|
|
func (n *NEO) setGASPerBlock(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
gas := toBigInt(args[0])
|
|
err := n.SetGASPerBlock(ic, ic.Block.Index+1, gas)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return stackitem.Null{}
|
|
}
|
|
|
|
// SetGASPerBlock sets gas generated for blocks after index.
|
|
func (n *NEO) SetGASPerBlock(ic *interop.Context, index uint32, gas *big.Int) error {
|
|
if gas.Sign() == -1 || gas.Cmp(big.NewInt(10*GASFactor)) == 1 {
|
|
return errors.New("invalid value for GASPerBlock")
|
|
}
|
|
if !n.checkCommittee(ic) {
|
|
return errors.New("invalid committee signature")
|
|
}
|
|
n.gasPerBlockChanged.Store(true)
|
|
return n.putGASRecord(ic.DAO, index, gas)
|
|
}
|
|
|
|
func (n *NEO) getRegisterPrice(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
|
|
return stackitem.NewBigInteger(big.NewInt(n.getRegisterPriceInternal(ic.DAO)))
|
|
}
|
|
|
|
func (n *NEO) getRegisterPriceInternal(d dao.DAO) int64 {
|
|
if !n.registerPriceChanged.Load().(bool) {
|
|
return n.registerPrice.Load().(int64)
|
|
}
|
|
return getIntWithKey(n.ID, d, []byte{prefixRegisterPrice})
|
|
}
|
|
|
|
func (n *NEO) setRegisterPrice(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
price := toBigInt(args[0])
|
|
if price.Sign() <= 0 || !price.IsInt64() {
|
|
panic("invalid register price")
|
|
}
|
|
if !n.checkCommittee(ic) {
|
|
panic("invalid committee signature")
|
|
}
|
|
|
|
err := setIntWithKey(n.ID, ic.DAO, []byte{prefixRegisterPrice}, price.Int64())
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
n.registerPriceChanged.Store(true)
|
|
return stackitem.Null{}
|
|
}
|
|
|
|
func (n *NEO) dropCandidateIfZero(d dao.DAO, pub *keys.PublicKey, c *candidate) (bool, error) {
|
|
if c.Registered || c.Votes.Sign() != 0 {
|
|
return false, nil
|
|
}
|
|
if err := d.DeleteStorageItem(n.ID, makeValidatorKey(pub)); err != nil {
|
|
return true, err
|
|
}
|
|
|
|
var toRemove []string
|
|
voterKey := makeVoterKey(pub.Bytes())
|
|
d.Seek(n.ID, storage.SeekRange{Prefix: voterKey}, func(k, v []byte) bool {
|
|
toRemove = append(toRemove, string(k))
|
|
return true
|
|
})
|
|
for i := range toRemove {
|
|
if err := d.DeleteStorageItem(n.ID, []byte(toRemove[i])); err != nil {
|
|
return true, err
|
|
}
|
|
}
|
|
delete(n.gasPerVoteCache, string(voterKey))
|
|
|
|
return true, nil
|
|
}
|
|
|
|
func makeVoterKey(pub []byte, prealloc ...[]byte) []byte {
|
|
var key []byte
|
|
if len(prealloc) != 0 {
|
|
key = prealloc[0]
|
|
} else {
|
|
key = make([]byte, 34, 38)
|
|
}
|
|
key[0] = prefixVoterRewardPerCommittee
|
|
copy(key[1:], pub)
|
|
return key
|
|
}
|
|
|
|
// CalculateBonus calculates amount of gas generated for holding value NEO from start to end block
|
|
// and having voted for active committee member.
|
|
func (n *NEO) CalculateBonus(d dao.DAO, acc util.Uint160, end uint32) (*big.Int, error) {
|
|
key := makeAccountKey(acc)
|
|
si := d.GetStorageItem(n.ID, key)
|
|
if si == nil {
|
|
return nil, storage.ErrKeyNotFound
|
|
}
|
|
st, err := state.NEOBalanceFromBytes(si)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return n.calculateBonus(d, st.VoteTo, &st.Balance, st.BalanceHeight, end)
|
|
}
|
|
|
|
func (n *NEO) calculateBonus(d dao.DAO, vote *keys.PublicKey, value *big.Int, start, end uint32) (*big.Int, error) {
|
|
r, err := n.CalculateNEOHolderReward(d, value, start, end)
|
|
if err != nil || vote == nil {
|
|
return r, err
|
|
}
|
|
|
|
var key = makeVoterKey(vote.Bytes())
|
|
var reward = n.getGASPerVote(d, key, []uint32{start, end})
|
|
var tmp = (&reward[1]).Sub(&reward[1], &reward[0])
|
|
tmp.Mul(tmp, value)
|
|
tmp.Div(tmp, bigVoterRewardFactor)
|
|
tmp.Add(tmp, r)
|
|
return tmp, nil
|
|
}
|
|
|
|
// CalculateNEOHolderReward return GAS reward for holding `value` of NEO from start to end block.
|
|
func (n *NEO) CalculateNEOHolderReward(d dao.DAO, value *big.Int, start, end uint32) (*big.Int, error) {
|
|
if value.Sign() == 0 || start >= end {
|
|
return big.NewInt(0), nil
|
|
} else if value.Sign() < 0 {
|
|
return nil, errors.New("negative value")
|
|
}
|
|
var (
|
|
gr gasRecord
|
|
err error
|
|
)
|
|
if !n.gasPerBlockChanged.Load().(bool) {
|
|
gr = n.gasPerBlock.Load().(gasRecord)
|
|
} else {
|
|
gr, err = n.getSortedGASRecordFromDAO(d)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
var sum, tmp big.Int
|
|
for i := len(gr) - 1; i >= 0; i-- {
|
|
if gr[i].Index >= end {
|
|
continue
|
|
} else if gr[i].Index <= start {
|
|
tmp.SetInt64(int64(end - start))
|
|
tmp.Mul(&tmp, &gr[i].GASPerBlock)
|
|
sum.Add(&sum, &tmp)
|
|
break
|
|
}
|
|
tmp.SetInt64(int64(end - gr[i].Index))
|
|
tmp.Mul(&tmp, &gr[i].GASPerBlock)
|
|
sum.Add(&sum, &tmp)
|
|
end = gr[i].Index
|
|
}
|
|
res := new(big.Int).Mul(value, &sum)
|
|
res.Mul(res, tmp.SetInt64(neoHolderRewardRatio))
|
|
res.Div(res, tmp.SetInt64(100*NEOTotalSupply))
|
|
return res, nil
|
|
}
|
|
|
|
func (n *NEO) registerCandidate(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
pub := toPublicKey(args[0])
|
|
ok, err := runtime.CheckKeyedWitness(ic, pub)
|
|
if err != nil {
|
|
panic(err)
|
|
} else if !ok {
|
|
return stackitem.NewBool(false)
|
|
}
|
|
if !ic.VM.AddGas(n.getRegisterPriceInternal(ic.DAO)) {
|
|
panic("insufficient gas")
|
|
}
|
|
err = n.RegisterCandidateInternal(ic, pub)
|
|
return stackitem.NewBool(err == nil)
|
|
}
|
|
|
|
// RegisterCandidateInternal registers pub as a new candidate.
|
|
func (n *NEO) RegisterCandidateInternal(ic *interop.Context, pub *keys.PublicKey) error {
|
|
key := makeValidatorKey(pub)
|
|
si := ic.DAO.GetStorageItem(n.ID, key)
|
|
var c *candidate
|
|
if si == nil {
|
|
c = &candidate{Registered: true}
|
|
} else {
|
|
c = new(candidate).FromBytes(si)
|
|
c.Registered = true
|
|
}
|
|
return putConvertibleToDAO(n.ID, ic.DAO, key, c)
|
|
}
|
|
|
|
func (n *NEO) unregisterCandidate(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
pub := toPublicKey(args[0])
|
|
ok, err := runtime.CheckKeyedWitness(ic, pub)
|
|
if err != nil {
|
|
panic(err)
|
|
} else if !ok {
|
|
return stackitem.NewBool(false)
|
|
}
|
|
err = n.UnregisterCandidateInternal(ic, pub)
|
|
return stackitem.NewBool(err == nil)
|
|
}
|
|
|
|
// UnregisterCandidateInternal unregisters pub as a candidate.
|
|
func (n *NEO) UnregisterCandidateInternal(ic *interop.Context, pub *keys.PublicKey) error {
|
|
key := makeValidatorKey(pub)
|
|
si := ic.DAO.GetStorageItem(n.ID, key)
|
|
if si == nil {
|
|
return nil
|
|
}
|
|
n.validators.Store(keys.PublicKeys(nil))
|
|
c := new(candidate).FromBytes(si)
|
|
c.Registered = false
|
|
ok, err := n.dropCandidateIfZero(ic.DAO, pub, c)
|
|
if ok {
|
|
return err
|
|
}
|
|
return putConvertibleToDAO(n.ID, ic.DAO, key, c)
|
|
}
|
|
|
|
func (n *NEO) vote(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
acc := toUint160(args[0])
|
|
var pub *keys.PublicKey
|
|
if _, ok := args[1].(stackitem.Null); !ok {
|
|
pub = toPublicKey(args[1])
|
|
}
|
|
err := n.VoteInternal(ic, acc, pub)
|
|
return stackitem.NewBool(err == nil)
|
|
}
|
|
|
|
// VoteInternal votes from account h for validarors specified in pubs.
|
|
func (n *NEO) VoteInternal(ic *interop.Context, h util.Uint160, pub *keys.PublicKey) error {
|
|
ok, err := runtime.CheckHashedWitness(ic, h)
|
|
if err != nil {
|
|
return err
|
|
} else if !ok {
|
|
return errors.New("invalid signature")
|
|
}
|
|
key := makeAccountKey(h)
|
|
si := ic.DAO.GetStorageItem(n.ID, key)
|
|
if si == nil {
|
|
return errors.New("invalid account")
|
|
}
|
|
acc, err := state.NEOBalanceFromBytes(si)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// we should put it in storage anyway as it affects dumps
|
|
err = ic.DAO.PutStorageItem(n.ID, key, si)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if pub != nil {
|
|
valKey := makeValidatorKey(pub)
|
|
valSi := ic.DAO.GetStorageItem(n.ID, valKey)
|
|
if valSi == nil {
|
|
return errors.New("unknown validator")
|
|
}
|
|
cd := new(candidate).FromBytes(valSi)
|
|
// we should put it in storage anyway as it affects dumps
|
|
err = ic.DAO.PutStorageItem(n.ID, valKey, valSi)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !cd.Registered {
|
|
return errors.New("validator must be registered")
|
|
}
|
|
}
|
|
|
|
if (acc.VoteTo == nil) != (pub == nil) {
|
|
val := &acc.Balance
|
|
if pub == nil {
|
|
val = new(big.Int).Neg(val)
|
|
}
|
|
if err := n.modifyVoterTurnout(ic.DAO, val); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if err := n.distributeGas(ic, h, acc); err != nil {
|
|
return err
|
|
}
|
|
if err := n.ModifyAccountVotes(acc, ic.DAO, new(big.Int).Neg(&acc.Balance), false); err != nil {
|
|
return err
|
|
}
|
|
acc.VoteTo = pub
|
|
if err := n.ModifyAccountVotes(acc, ic.DAO, &acc.Balance, true); err != nil {
|
|
return err
|
|
}
|
|
return ic.DAO.PutStorageItem(n.ID, key, acc.Bytes())
|
|
}
|
|
|
|
// ModifyAccountVotes modifies votes of the specified account by value (can be negative).
|
|
// typ specifies if this modify is occurring during transfer or vote (with old or new validator).
|
|
func (n *NEO) ModifyAccountVotes(acc *state.NEOBalance, d dao.DAO, value *big.Int, isNewVote bool) error {
|
|
n.votesChanged.Store(true)
|
|
if acc.VoteTo != nil {
|
|
key := makeValidatorKey(acc.VoteTo)
|
|
si := d.GetStorageItem(n.ID, key)
|
|
if si == nil {
|
|
return errors.New("invalid validator")
|
|
}
|
|
cd := new(candidate).FromBytes(si)
|
|
cd.Votes.Add(&cd.Votes, value)
|
|
if !isNewVote {
|
|
ok, err := n.dropCandidateIfZero(d, acc.VoteTo, cd)
|
|
if ok {
|
|
return err
|
|
}
|
|
}
|
|
n.validators.Store(keys.PublicKeys(nil))
|
|
return putConvertibleToDAO(n.ID, d, key, cd)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (n *NEO) getCandidates(d dao.DAO, sortByKey bool) ([]keyWithVotes, error) {
|
|
siArr, err := d.GetStorageItemsWithPrefix(n.ID, []byte{prefixCandidate})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
arr := make([]keyWithVotes, 0, len(siArr))
|
|
for _, kv := range siArr {
|
|
c := new(candidate).FromBytes(kv.Item)
|
|
if c.Registered {
|
|
arr = append(arr, keyWithVotes{Key: string(kv.Key), Votes: &c.Votes})
|
|
}
|
|
}
|
|
if !sortByKey {
|
|
// sortByKey assumes to sort by serialized key bytes (that's the way keys
|
|
// are stored and retrieved from the storage by default). Otherwise, need
|
|
// to sort using big.Int comparator.
|
|
sort.Slice(arr, func(i, j int) bool {
|
|
// The most-voted validators should end up in the front of the list.
|
|
cmp := arr[i].Votes.Cmp(arr[j].Votes)
|
|
if cmp != 0 {
|
|
return cmp > 0
|
|
}
|
|
// Ties are broken with deserialized public keys.
|
|
// Sort by ECPoint's (X, Y) components: compare X first, and then compare Y.
|
|
cmpX := strings.Compare(arr[i].Key[1:], arr[j].Key[1:])
|
|
if cmpX != 0 {
|
|
return cmpX == -1
|
|
}
|
|
// The case when X components are the same is extremely rare, thus we perform
|
|
// key deserialization only if needed. No error can occur.
|
|
ki, _ := keys.NewPublicKeyFromBytes([]byte(arr[i].Key), elliptic.P256())
|
|
kj, _ := keys.NewPublicKeyFromBytes([]byte(arr[j].Key), elliptic.P256())
|
|
return ki.Y.Cmp(kj.Y) == -1
|
|
})
|
|
}
|
|
return arr, nil
|
|
}
|
|
|
|
// GetCandidates returns current registered validators list with keys
|
|
// and votes.
|
|
func (n *NEO) GetCandidates(d dao.DAO) ([]state.Validator, error) {
|
|
kvs, err := n.getCandidates(d, true)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
arr := make([]state.Validator, len(kvs))
|
|
for i := range kvs {
|
|
arr[i].Key, err = keys.NewPublicKeyFromBytes([]byte(kvs[i].Key), elliptic.P256())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
arr[i].Votes = kvs[i].Votes
|
|
}
|
|
return arr, nil
|
|
}
|
|
|
|
func (n *NEO) getCandidatesCall(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
|
|
validators, err := n.getCandidates(ic.DAO, true)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
arr := make([]stackitem.Item, len(validators))
|
|
for i := range validators {
|
|
arr[i] = stackitem.NewStruct([]stackitem.Item{
|
|
stackitem.NewByteArray([]byte(validators[i].Key)),
|
|
stackitem.NewBigInteger(validators[i].Votes),
|
|
})
|
|
}
|
|
return stackitem.NewArray(arr)
|
|
}
|
|
|
|
func (n *NEO) getAccountState(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
key := makeAccountKey(toUint160(args[0]))
|
|
si := ic.DAO.GetStorageItem(n.ID, key)
|
|
if len(si) == 0 {
|
|
return stackitem.Null{}
|
|
}
|
|
|
|
item, err := stackitem.Deserialize(si)
|
|
if err != nil {
|
|
panic(err) // no errors are expected but we better be sure
|
|
}
|
|
return item
|
|
}
|
|
|
|
// ComputeNextBlockValidators returns an actual list of current validators.
|
|
func (n *NEO) ComputeNextBlockValidators(bc interop.Ledger, d dao.DAO) (keys.PublicKeys, error) {
|
|
if vals := n.validators.Load().(keys.PublicKeys); vals != nil {
|
|
return vals.Copy(), nil
|
|
}
|
|
result, _, err := n.computeCommitteeMembers(bc, d)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result = result[:bc.GetConfig().ValidatorsCount]
|
|
sort.Sort(result)
|
|
n.validators.Store(result)
|
|
return result, nil
|
|
}
|
|
|
|
func (n *NEO) getCommittee(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
|
|
pubs := n.GetCommitteeMembers()
|
|
sort.Sort(pubs)
|
|
return pubsToArray(pubs)
|
|
}
|
|
|
|
func (n *NEO) modifyVoterTurnout(d dao.DAO, amount *big.Int) error {
|
|
key := []byte{prefixVotersCount}
|
|
si := d.GetStorageItem(n.ID, key)
|
|
if si == nil {
|
|
return errors.New("voters count not found")
|
|
}
|
|
votersCount := bigint.FromBytes(si)
|
|
votersCount.Add(votersCount, amount)
|
|
si = bigint.ToPreallocatedBytes(votersCount, si)
|
|
return d.PutStorageItem(n.ID, key, si)
|
|
}
|
|
|
|
// GetCommitteeMembers returns public keys of nodes in committee using cached value.
|
|
func (n *NEO) GetCommitteeMembers() keys.PublicKeys {
|
|
var cvs = n.committee.Load().(keysWithVotes)
|
|
var committee = make(keys.PublicKeys, len(cvs))
|
|
var err error
|
|
for i := range committee {
|
|
committee[i], err = cvs[i].PublicKey()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
return committee
|
|
}
|
|
|
|
func toKeysWithVotes(pubs keys.PublicKeys) keysWithVotes {
|
|
ks := make(keysWithVotes, len(pubs))
|
|
for i := range pubs {
|
|
ks[i].UnmarshaledKey = pubs[i]
|
|
ks[i].Key = string(pubs[i].Bytes())
|
|
ks[i].Votes = big.NewInt(0)
|
|
}
|
|
return ks
|
|
}
|
|
|
|
// computeCommitteeMembers returns public keys of nodes in committee.
|
|
func (n *NEO) computeCommitteeMembers(bc interop.Ledger, d dao.DAO) (keys.PublicKeys, keysWithVotes, error) {
|
|
key := []byte{prefixVotersCount}
|
|
si := d.GetStorageItem(n.ID, key)
|
|
if si == nil {
|
|
return nil, nil, errors.New("voters count not found")
|
|
}
|
|
votersCount := bigint.FromBytes(si)
|
|
// votersCount / totalSupply must be >= 0.2
|
|
votersCount.Mul(votersCount, bigEffectiveVoterTurnout)
|
|
_, totalSupply := n.getTotalSupply(d)
|
|
voterTurnout := votersCount.Div(votersCount, totalSupply)
|
|
|
|
sbVals := bc.GetStandByCommittee()
|
|
count := len(sbVals)
|
|
cs, err := n.getCandidates(d, false)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
if voterTurnout.Sign() != 1 || len(cs) < count {
|
|
kvs := make(keysWithVotes, count)
|
|
for i := range kvs {
|
|
kvs[i].UnmarshaledKey = sbVals[i]
|
|
kvs[i].Key = string(sbVals[i].Bytes())
|
|
votes := big.NewInt(0)
|
|
for j := range cs {
|
|
if cs[j].Key == kvs[i].Key {
|
|
votes = cs[j].Votes
|
|
break
|
|
}
|
|
}
|
|
kvs[i].Votes = votes
|
|
}
|
|
return sbVals, kvs, nil
|
|
}
|
|
pubs := make(keys.PublicKeys, count)
|
|
for i := range pubs {
|
|
pubs[i], err = cs[i].PublicKey()
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
}
|
|
return pubs, cs[:count], nil
|
|
}
|
|
|
|
func (n *NEO) getNextBlockValidators(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
|
|
result := n.GetNextBlockValidatorsInternal()
|
|
return pubsToArray(result)
|
|
}
|
|
|
|
// GetNextBlockValidatorsInternal returns next block validators.
|
|
func (n *NEO) GetNextBlockValidatorsInternal() keys.PublicKeys {
|
|
return n.nextValidators.Load().(keys.PublicKeys).Copy()
|
|
}
|
|
|
|
// BalanceOf returns native NEO token balance for the acc.
|
|
func (n *NEO) BalanceOf(d dao.DAO, acc util.Uint160) (*big.Int, uint32) {
|
|
key := makeAccountKey(acc)
|
|
si := d.GetStorageItem(n.ID, key)
|
|
if si == nil {
|
|
return big.NewInt(0), 0
|
|
}
|
|
st, err := state.NEOBalanceFromBytes(si)
|
|
if err != nil {
|
|
panic(fmt.Errorf("failed to decode NEO balance state: %w", err))
|
|
}
|
|
return &st.Balance, st.BalanceHeight
|
|
}
|
|
|
|
func pubsToArray(pubs keys.PublicKeys) stackitem.Item {
|
|
arr := make([]stackitem.Item, len(pubs))
|
|
for i := range pubs {
|
|
arr[i] = stackitem.NewByteArray(pubs[i].Bytes())
|
|
}
|
|
return stackitem.NewArray(arr)
|
|
}
|
|
|
|
func toPublicKey(s stackitem.Item) *keys.PublicKey {
|
|
buf, err := s.TryBytes()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
pub := new(keys.PublicKey)
|
|
if err := pub.DecodeBytes(buf); err != nil {
|
|
panic(err)
|
|
}
|
|
return pub
|
|
}
|
|
|
|
// putGASRecord is a helper which creates key and puts GASPerBlock value into the storage.
|
|
func (n *NEO) putGASRecord(dao dao.DAO, index uint32, value *big.Int) error {
|
|
key := make([]byte, 5)
|
|
key[0] = prefixGASPerBlock
|
|
binary.BigEndian.PutUint32(key[1:], index)
|
|
return dao.PutStorageItem(n.ID, key, bigint.ToBytes(value))
|
|
}
|