Skip to content

Commit

Permalink
enable few more unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
tsachiherman committed Oct 8, 2021
1 parent 925707f commit b645bac
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 116 deletions.
2 changes: 1 addition & 1 deletion ledger/accountdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ func TestAccountsDbQueriesCreateClose(t *testing.T) {

func benchmarkWriteCatchpointStagingBalancesSub(b *testing.B, ascendingOrder bool) {
proto := config.Consensus[protocol.ConsensusCurrentVersion]
genesisInitState, _ := testGenerateInitState(b, protocol.ConsensusCurrentVersion, 100)
genesisInitState, _ := ledgertesting.GenerateInitState(b, protocol.ConsensusCurrentVersion, 100)
const inMem = false
log := logging.TestingLog(b)
cfg := config.GetDefaultLocal()
Expand Down
11 changes: 6 additions & 5 deletions ledger/catchupaccessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/algorand/go-algorand/data/basics"
"github.com/algorand/go-algorand/data/bookkeeping"
"github.com/algorand/go-algorand/ledger/ledgercore"
ledgertesting "github.com/algorand/go-algorand/ledger/testing"
"github.com/algorand/go-algorand/logging"
"github.com/algorand/go-algorand/protocol"
"github.com/algorand/go-algorand/test/partitiontest"
Expand Down Expand Up @@ -69,7 +70,7 @@ func createTestingEncodedChunks(accountsCount uint64) (encodedAccountChunks [][]
}

func benchmarkRestoringFromCatchpointFileHelper(b *testing.B) {
genesisInitState, _ := testGenerateInitState(b, protocol.ConsensusCurrentVersion, 100)
genesisInitState, _ := ledgertesting.GenerateInitState(b, protocol.ConsensusCurrentVersion, 100)
const inMem = false
log := logging.TestingLog(b)
cfg := config.GetDefaultLocal()
Expand Down Expand Up @@ -145,7 +146,7 @@ func TestCatchupAcessorFoo(t *testing.T) {
log := logging.TestingLog(t)
dbBaseFileName := t.Name()
const inMem = true
genesisInitState, _ /* initKeys */ := testGenerateInitState(t, protocol.ConsensusCurrentVersion, 100)
genesisInitState, _ /* initKeys */ := ledgertesting.GenerateInitState(t, protocol.ConsensusCurrentVersion, 100)
cfg := config.GetDefaultLocal()
l, err := OpenLedger(log, dbBaseFileName, inMem, genesisInitState, cfg)
require.NoError(t, err, "could not open ledger")
Expand Down Expand Up @@ -201,7 +202,7 @@ func TestBuildMerkleTrie(t *testing.T) {
log := logging.TestingLog(t)
dbBaseFileName := t.Name()
const inMem = true
genesisInitState, initKeys := testGenerateInitState(t, protocol.ConsensusCurrentVersion, 100)
genesisInitState, initKeys := ledgertesting.GenerateInitState(t, protocol.ConsensusCurrentVersion, 100)
cfg := config.GetDefaultLocal()
l, err := OpenLedger(log, dbBaseFileName, inMem, genesisInitState, cfg)
require.NoError(t, err, "could not open ledger")
Expand Down Expand Up @@ -300,7 +301,7 @@ func TestCatchupAccessorBlockdb(t *testing.T) {
log := logging.TestingLog(t)
dbBaseFileName := t.Name()
const inMem = true
genesisInitState, _ /*initKeys*/ := testGenerateInitState(t, protocol.ConsensusCurrentVersion, 100)
genesisInitState, _ /*initKeys*/ := ledgertesting.GenerateInitState(t, protocol.ConsensusCurrentVersion, 100)
cfg := config.GetDefaultLocal()
l, err := OpenLedger(log, dbBaseFileName, inMem, genesisInitState, cfg)
require.NoError(t, err, "could not open ledger")
Expand All @@ -326,7 +327,7 @@ func TestVerifyCatchpoint(t *testing.T) {
log := logging.TestingLog(t)
dbBaseFileName := t.Name()
const inMem = true
genesisInitState, _ /*initKeys*/ := testGenerateInitState(t, protocol.ConsensusCurrentVersion, 100)
genesisInitState, _ /*initKeys*/ := ledgertesting.GenerateInitState(t, protocol.ConsensusCurrentVersion, 100)
cfg := config.GetDefaultLocal()
l, err := OpenLedger(log, dbBaseFileName, inMem, genesisInitState, cfg)
require.NoError(t, err, "could not open ledger")
Expand Down
13 changes: 7 additions & 6 deletions ledger/internal/applications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ return`
program := ops.Program
proto := config.Consensus[protocol.ConsensusCurrentVersion]
genesisInitState, initKeys := testGenerateInitState(t, protocol.ConsensusCurrentVersion, 100)
genesisInitState, initKeys := ledgertesting.GenerateInitState(t, protocol.ConsensusCurrentVersion, 100)
creator, err := basics.UnmarshalChecksumAddress("3LN5DBFC2UTPD265LQDP3LMTLGZCQ5M3JV7XTVTGRH5CKSVNQVDFPN6FG4")
a.NoError(err)
Expand Down Expand Up @@ -596,8 +596,9 @@ return`
LocalDeltas: map[uint64]basics.StateDelta{0: {"lk": basics.ValueDelta{Action: basics.SetBytesAction, Bytes: "local"}}}},
})
a.NoError(err)
}
}*/

/*
func TestAppAccountDelta(t *testing.T) {
partitiontest.PartitionTest(t)
Expand Down Expand Up @@ -653,7 +654,7 @@ return`
program := ops.Program
proto := config.Consensus[protocol.ConsensusCurrentVersion]
genesisInitState, initKeys := testGenerateInitState(t, protocol.ConsensusCurrentVersion, 100)
genesisInitState, initKeys := ledgertesting.GenerateInitState(t, protocol.ConsensusCurrentVersion, 100)
creator, err := basics.UnmarshalChecksumAddress("3LN5DBFC2UTPD265LQDP3LMTLGZCQ5M3JV7XTVTGRH5CKSVNQVDFPN6FG4")
a.NoError(err)
Expand Down Expand Up @@ -908,7 +909,7 @@ return`
program := ops.Program
proto := config.Consensus[protocol.ConsensusCurrentVersion]
genesisInitState, initKeys := testGenerateInitState(t, protocol.ConsensusCurrentVersion, 100)
genesisInitState, initKeys := ledgertesting.GenerateInitState(t, protocol.ConsensusCurrentVersion, 100)
creator, err := basics.UnmarshalChecksumAddress("3LN5DBFC2UTPD265LQDP3LMTLGZCQ5M3JV7XTVTGRH5CKSVNQVDFPN6FG4")
a.NoError(err)
Expand Down Expand Up @@ -1067,7 +1068,7 @@ return`
program := ops.Program
proto := config.Consensus[protocol.ConsensusCurrentVersion]
genesisInitState, initKeys := testGenerateInitState(t, protocol.ConsensusCurrentVersion, 100)
genesisInitState, initKeys := ledgertesting.GenerateInitState(t, protocol.ConsensusCurrentVersion, 100)
creator, err := basics.UnmarshalChecksumAddress("3LN5DBFC2UTPD265LQDP3LMTLGZCQ5M3JV7XTVTGRH5CKSVNQVDFPN6FG4")
a.NoError(err)
Expand Down Expand Up @@ -1269,7 +1270,7 @@ func testAppAccountDeltaIndicesCompatibility(t *testing.T, source string, accoun
// explicitly trigger compatibility mode
proto := config.Consensus[protocol.ConsensusV24]
genesisInitState, initKeys := testGenerateInitState(t, protocol.ConsensusV24, 100)
genesisInitState, initKeys := ledgertesting.GenerateInitState(t, protocol.ConsensusV24, 100)
creator, err := basics.UnmarshalChecksumAddress("3LN5DBFC2UTPD265LQDP3LMTLGZCQ5M3JV7XTVTGRH5CKSVNQVDFPN6FG4")
a.NoError(err)
Expand Down
20 changes: 8 additions & 12 deletions ledger/internal/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ func TestPrivateTransactionGroup(t *testing.T) {

// BlockEvaluator.workaroundOverspentRewards() fixed a couple issues on testnet.
// This is now part of history and has to be re-created when running catchup on testnet. So, test to ensure it keeps happenning.
/*func TestTestnetFixup(t *testing.T) {
func TestTestnetFixup(t *testing.T) {
partitiontest.PartitionTest(t)

eval := &BlockEvaluator{}
Expand Down Expand Up @@ -779,19 +779,16 @@ func testnetFixupExecution(t *testing.T, headerRound basics.Round, poolBonus uin
genesisInitState.Block.BlockHeader.GenesisID = "testnet"
genesisInitState.GenesisHash = testnetGenesisHash

// for addr, adata := range genesisInitState.Accounts {
// t.Logf("%s: %+v", addr.String(), adata)
// }
rewardPoolBalance := genesisInitState.Accounts[testPoolAddr]
nextPoolBalance := rewardPoolBalance.MicroAlgos.Raw + poolBonus

dbName := fmt.Sprintf("%s.%d", t.Name(), crypto.RandUint64())
const inMem = true
cfg := config.GetDefaultLocal()
cfg.Archival = true
l, err := OpenLedger(logging.Base(), dbName, inMem, genesisInitState, cfg)
require.NoError(t, err)
defer l.Close()
l := newTestLedger(t, bookkeeping.GenesisBalances{
Balances: genesisInitState.Accounts,
FeeSink: testSinkAddr,
RewardsPool: testPoolAddr,
})
l.blocks[0] = genesisInitState.Block
l.genesisHash = genesisInitState.GenesisHash

newBlock := bookkeeping.MakeBlock(genesisInitState.Block.BlockHeader)
eval, err := l.StartEvaluator(newBlock.BlockHeader, 0, 0)
Expand Down Expand Up @@ -828,7 +825,6 @@ func testnetFixupExecution(t *testing.T, headerRound basics.Round, poolBonus uin
require.Equal(t, nextPoolBalance, poolOld.MicroAlgos.Raw)
require.NoError(t, err)
}
*/

// Test that ModifiedAssetHoldings in StateDelta is set correctly.
func TestModifiedAssetHoldings(t *testing.T) {
Expand Down
105 changes: 13 additions & 92 deletions ledger/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,13 @@ import (
"github.com/algorand/go-algorand/data/transactions/logic"
"github.com/algorand/go-algorand/data/transactions/verify"
"github.com/algorand/go-algorand/ledger/ledgercore"
ledgertesting "github.com/algorand/go-algorand/ledger/testing"
"github.com/algorand/go-algorand/logging"
"github.com/algorand/go-algorand/protocol"
"github.com/algorand/go-algorand/test/partitiontest"
"github.com/algorand/go-algorand/util/execpool"
)

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)
}

func sign(secrets map[basics.Address]*crypto.SignatureSecrets, t transactions.Transaction) transactions.SignedTxn {
var sig crypto.Signature
_, ok := secrets[t.Sender]
Expand All @@ -68,72 +55,6 @@ func sign(secrets map[basics.Address]*crypto.SignatureSecrets, t transactions.Tr
}
}

func testGenerateInitState(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
}

func (l *Ledger) appendUnvalidated(blk bookkeeping.Block) error {
backlogPool := execpool.MakeBacklog(nil, 0, execpool.LowPriority, nil)
defer backlogPool.Shutdown()
Expand Down Expand Up @@ -258,7 +179,7 @@ func (l *Ledger) addBlockTxns(t *testing.T, accounts map[basics.Address]basics.A
func TestLedgerBasic(t *testing.T) {
partitiontest.PartitionTest(t)

genesisInitState, _ := testGenerateInitState(t, protocol.ConsensusCurrentVersion, 100)
genesisInitState, _ := ledgertesting.GenerateInitState(t, protocol.ConsensusCurrentVersion, 100)
const inMem = true
cfg := config.GetDefaultLocal()
cfg.Archival = true
Expand All @@ -273,7 +194,7 @@ func TestLedgerBlockHeaders(t *testing.T) {

a := require.New(t)

genesisInitState, _ := testGenerateInitState(t, protocol.ConsensusCurrentVersion, 100)
genesisInitState, _ := ledgertesting.GenerateInitState(t, protocol.ConsensusCurrentVersion, 100)
const inMem = true
cfg := config.GetDefaultLocal()
cfg.Archival = true
Expand Down Expand Up @@ -419,7 +340,7 @@ func TestLedgerSingleTx(t *testing.T) {
// V15 is the earliest protocol version in active use.
// The genesis for betanet and testnet is at V15
// The genesis for mainnet is at V17
genesisInitState, initSecrets := testGenerateInitState(t, protocol.ConsensusV15, 100)
genesisInitState, initSecrets := ledgertesting.GenerateInitState(t, protocol.ConsensusV15, 100)
const inMem = true
log := logging.TestingLog(t)
cfg := config.GetDefaultLocal()
Expand Down Expand Up @@ -622,7 +543,7 @@ func TestLedgerSingleTxV24(t *testing.T) {
a := require.New(t)

protoName := protocol.ConsensusV24
genesisInitState, initSecrets := testGenerateInitState(t, protoName, 100)
genesisInitState, initSecrets := ledgertesting.GenerateInitState(t, protoName, 100)
const inMem = true
log := logging.TestingLog(t)
cfg := config.GetDefaultLocal()
Expand Down Expand Up @@ -793,7 +714,7 @@ func TestLedgerAppCrossRoundWrites(t *testing.T) {
a := require.New(t)

protoName := protocol.ConsensusV24
genesisInitState, initSecrets := testGenerateInitState(t, protoName, 100)
genesisInitState, initSecrets := ledgertesting.GenerateInitState(t, protoName, 100)
const inMem = true
log := logging.TestingLog(t)
cfg := config.GetDefaultLocal()
Expand Down Expand Up @@ -931,7 +852,7 @@ func TestLedgerAppMultiTxnWrites(t *testing.T) {
a := require.New(t)

protoName := protocol.ConsensusV24
genesisInitState, initSecrets := testGenerateInitState(t, protoName, 100)
genesisInitState, initSecrets := ledgertesting.GenerateInitState(t, protoName, 100)
const inMem = true
log := logging.TestingLog(t)
cfg := config.GetDefaultLocal()
Expand Down Expand Up @@ -1096,7 +1017,7 @@ func testLedgerSingleTxApplyData(t *testing.T, version protocol.ConsensusVersion
backlogPool := execpool.MakeBacklog(nil, 0, execpool.LowPriority, nil)
defer backlogPool.Shutdown()

genesisInitState, initSecrets := testGenerateInitState(t, version, 100)
genesisInitState, initSecrets := ledgertesting.GenerateInitState(t, version, 100)
const inMem = true
log := logging.TestingLog(t)
cfg := config.GetDefaultLocal()
Expand Down Expand Up @@ -1368,7 +1289,7 @@ func TestLedgerRegressionFaultyLeaseFirstValidCheckFuture(t *testing.T) {
func testLedgerRegressionFaultyLeaseFirstValidCheck2f3880f7(t *testing.T, version protocol.ConsensusVersion) {
a := require.New(t)

genesisInitState, initSecrets := testGenerateInitState(t, version, 100)
genesisInitState, initSecrets := ledgertesting.GenerateInitState(t, version, 100)
const inMem = true
cfg := config.GetDefaultLocal()
cfg.Archival = true
Expand Down Expand Up @@ -1494,7 +1415,7 @@ func TestGetLastCatchpointLabel(t *testing.T) {
partitiontest.PartitionTest(t)

//initLedger
genesisInitState, _ := testGenerateInitState(t, protocol.ConsensusCurrentVersion, 100)
genesisInitState, _ := ledgertesting.GenerateInitState(t, protocol.ConsensusCurrentVersion, 100)
const inMem = true
log := logging.TestingLog(t)
cfg := config.GetDefaultLocal()
Expand Down Expand Up @@ -1573,7 +1494,7 @@ func TestListAssetsAndApplications(t *testing.T) {
numElementsPerSegement := 10 // This is multiplied by 10. see randomCreatables

//initLedger
genesisInitState, _ := testGenerateInitState(t, protocol.ConsensusCurrentVersion, 100)
genesisInitState, _ := ledgertesting.GenerateInitState(t, protocol.ConsensusCurrentVersion, 100)
const inMem = true
log := logging.TestingLog(t)
cfg := config.GetDefaultLocal()
Expand Down Expand Up @@ -1632,7 +1553,7 @@ func TestLedgerMemoryLeak(t *testing.T) {

t.Skip() // for manual runs only
dbName := fmt.Sprintf("%s.%d", t.Name(), crypto.RandUint64())
genesisInitState, initKeys := testGenerateInitState(t, protocol.ConsensusCurrentVersion, 10000000000)
genesisInitState, initKeys := ledgertesting.GenerateInitState(t, protocol.ConsensusCurrentVersion, 10000000000)
const inMem = false
cfg := config.GetDefaultLocal()
cfg.Archival = true
Expand Down Expand Up @@ -1756,7 +1677,7 @@ func BenchmarkLedgerStartup(b *testing.B) {
log := logging.TestingLog(b)
tmpDir, err := ioutil.TempDir(os.TempDir(), "BenchmarkLedgerStartup")
require.NoError(b, err)
genesisInitState, _ := testGenerateInitState(b, protocol.ConsensusCurrentVersion, 100)
genesisInitState, _ := ledgertesting.GenerateInitState(b, protocol.ConsensusCurrentVersion, 100)

cfg := config.GetDefaultLocal()
cfg.Archival = false
Expand Down
Loading

0 comments on commit b645bac

Please sign in to comment.