diff options
Diffstat (limited to 'ledger/testing/initState.go')
-rw-r--r-- | ledger/testing/initState.go | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/ledger/testing/initState.go b/ledger/testing/initState.go new file mode 100644 index 000000000..ad96e1f76 --- /dev/null +++ b/ledger/testing/initState.go @@ -0,0 +1,111 @@ +// Copyright (C) 2019-2021 Algorand, Inc. +// This file is part of go-algorand +// +// go-algorand is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// go-algorand is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with go-algorand. If not, see <https://www.gnu.org/licenses/>. + +package testing + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/algorand/go-algorand/config" + "github.com/algorand/go-algorand/crypto" + "github.com/algorand/go-algorand/data/basics" + "github.com/algorand/go-algorand/data/bookkeeping" + "github.com/algorand/go-algorand/ledger/ledgercore" + "github.com/algorand/go-algorand/protocol" +) + +var poolSecret, sinkSecret *crypto.SignatureSecrets + +func init() { + var seed crypto.Seed + + incentivePoolName := []byte("incentive pool") + copy(seed[:], incentivePoolName) + poolSecret = crypto.GenerateSignatureSecrets(seed) + + feeSinkName := []byte("fee sink") + copy(seed[:], feeSinkName) + sinkSecret = crypto.GenerateSignatureSecrets(seed) +} + +// GenerateInitState generates testing init state +func GenerateInitState(tb testing.TB, proto protocol.ConsensusVersion, baseAlgoPerAccount int) (genesisInitState ledgercore.InitState, initKeys map[basics.Address]*crypto.SignatureSecrets) { + params := config.Consensus[proto] + poolAddr := testPoolAddr + sinkAddr := testSinkAddr + + var zeroSeed crypto.Seed + var genaddrs [10]basics.Address + var gensecrets [10]*crypto.SignatureSecrets + for i := range genaddrs { + seed := zeroSeed + seed[0] = byte(i) + x := crypto.GenerateSignatureSecrets(seed) + genaddrs[i] = basics.Address(x.SignatureVerifier) + gensecrets[i] = x + } + + initKeys = make(map[basics.Address]*crypto.SignatureSecrets) + initAccounts := make(map[basics.Address]basics.AccountData) + for i := range genaddrs { + initKeys[genaddrs[i]] = gensecrets[i] + // Give each account quite a bit more balance than MinFee or MinBalance + initAccounts[genaddrs[i]] = basics.MakeAccountData(basics.Online, basics.MicroAlgos{Raw: uint64((i + baseAlgoPerAccount) * 100000)}) + } + initKeys[poolAddr] = poolSecret + initAccounts[poolAddr] = basics.MakeAccountData(basics.NotParticipating, basics.MicroAlgos{Raw: 1234567}) + initKeys[sinkAddr] = sinkSecret + initAccounts[sinkAddr] = basics.MakeAccountData(basics.NotParticipating, basics.MicroAlgos{Raw: 7654321}) + + incentivePoolBalanceAtGenesis := initAccounts[poolAddr].MicroAlgos + var initialRewardsPerRound uint64 + if params.InitialRewardsRateCalculation { + initialRewardsPerRound = basics.SubSaturate(incentivePoolBalanceAtGenesis.Raw, params.MinBalance) / uint64(params.RewardsRateRefreshInterval) + } else { + initialRewardsPerRound = incentivePoolBalanceAtGenesis.Raw / uint64(params.RewardsRateRefreshInterval) + } + + initBlock := bookkeeping.Block{ + BlockHeader: bookkeeping.BlockHeader{ + GenesisID: tb.Name(), + Round: 0, + RewardsState: bookkeeping.RewardsState{ + RewardsRate: initialRewardsPerRound, + RewardsPool: poolAddr, + FeeSink: sinkAddr, + }, + UpgradeState: bookkeeping.UpgradeState{ + CurrentProtocol: proto, + }, + }, + } + + var err error + initBlock.TxnRoot, err = initBlock.PaysetCommit() + require.NoError(tb, err) + + if params.SupportGenesisHash { + initBlock.BlockHeader.GenesisHash = crypto.Hash([]byte(tb.Name())) + } + + genesisInitState.Block = initBlock + genesisInitState.Accounts = initAccounts + genesisInitState.GenesisHash = crypto.Hash([]byte(tb.Name())) + + return +} |