Skip to content

Commit

Permalink
Merge pull request #3135 from oasisprotocol/yawning/feature/3110
Browse files Browse the repository at this point in the history
Kill `tests/fixture-data` with fire
  • Loading branch information
Yawning authored Jul 29, 2020
2 parents 683eb0d + fd08527 commit 0a8e82c
Show file tree
Hide file tree
Showing 29 changed files with 565 additions and 941 deletions.
16 changes: 14 additions & 2 deletions .buildkite/scripts/test_consim.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,25 @@ WORKDIR=$PWD

echo ${WORKDIR}

# Run the consensusm simulator.
CONSIM_GENESIS=/tmp/consim-genesis.json

# Generate the consensus sim genesis document.
${WORKDIR}/go/oasis-node/oasis-node \
genesis init \
--debug.test_entity \
--debug.dont_blame_oasis \
--chain.id test \
--debug.allow_test_keys \
--staking.token_symbol BUF \
-g ${CONSIM_GENESIS}

# Run the consensus simulator.
${WORKDIR}/go/oasis-node/oasis-node \
debug consim \
--datadir /tmp/consim-datadir \
--log.level DEBUG \
--log.file /tmp/consim-datadir/consim.log \
-g ${WORKDIR}/tests/fixture-data/consim/genesis.json \
-g ${CONSIM_GENESIS} \
--debug.dont_blame_oasis \
--debug.allow_test_keys \
--consim.workload xfer \
Expand Down
1 change: 1 addition & 0 deletions .changelog/3110.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/oasis-test-runner/oasis: Purge most of tests/fixture-data
21 changes: 11 additions & 10 deletions go/genesis/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
memorySigner "github.com/oasisprotocol/oasis-core/go/common/crypto/signature/signers/memory"
"github.com/oasisprotocol/oasis-core/go/common/entity"
"github.com/oasisprotocol/oasis-core/go/common/node"
"github.com/oasisprotocol/oasis-core/go/common/quantity"
consensus "github.com/oasisprotocol/oasis-core/go/consensus/genesis"
tendermint "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/api"
epochtime "github.com/oasisprotocol/oasis-core/go/epochtime/api"
Expand Down Expand Up @@ -679,42 +680,42 @@ func TestGenesisSanityCheck(t *testing.T) {
// NOTE: There doesn't seem to be a way to generate invalid Quantities, so
// we're just going to test the code that checks if things add up.
d = *testDoc
d.Staking.TotalSupply = stakingTests.QtyFromInt(100)
d.Staking.TotalSupply = *quantity.NewFromUint64(100)
require.Error(d.SanityCheck(), "invalid total supply should be rejected")

d = *testDoc
d.Staking.CommonPool = stakingTests.QtyFromInt(100)
d.Staking.CommonPool = *quantity.NewFromUint64(100)
require.Error(d.SanityCheck(), "invalid common pool should be rejected")

d = *testDoc
d.Staking.LastBlockFees = stakingTests.QtyFromInt(100)
d.Staking.LastBlockFees = *quantity.NewFromUint64(100)
require.Error(d.SanityCheck(), "invalid last block fees should be rejected")

d = *testDoc
d.Staking.Ledger[stakingTests.DebugStateSrcAddress].General.Balance = stakingTests.QtyFromInt(100)
d.Staking.Ledger[stakingTests.DebugStateSrcAddress].General.Balance = *quantity.NewFromUint64(100)
require.Error(d.SanityCheck(), "invalid general balance should be rejected")

d = *testDoc
d.Staking.Ledger[stakingTests.DebugStateSrcAddress].Escrow.Active.Balance = stakingTests.QtyFromInt(100)
d.Staking.Ledger[stakingTests.DebugStateSrcAddress].Escrow.Active.Balance = *quantity.NewFromUint64(100)
require.Error(d.SanityCheck(), "invalid escrow active balance should be rejected")

d = *testDoc
d.Staking.Ledger[stakingTests.DebugStateSrcAddress].Escrow.Debonding.Balance = stakingTests.QtyFromInt(100)
d.Staking.Ledger[stakingTests.DebugStateSrcAddress].Escrow.Debonding.Balance = *quantity.NewFromUint64(100)
require.Error(d.SanityCheck(), "invalid escrow debonding balance should be rejected")

d = *testDoc
d.Staking.Ledger[stakingTests.DebugStateSrcAddress].Escrow.Active.TotalShares = stakingTests.QtyFromInt(1)
d.Staking.Ledger[stakingTests.DebugStateSrcAddress].Escrow.Active.TotalShares = *quantity.NewFromUint64(1)
require.Error(d.SanityCheck(), "invalid escrow active total shares should be rejected")

d = *testDoc
d.Staking.Ledger[stakingTests.DebugStateSrcAddress].Escrow.Debonding.TotalShares = stakingTests.QtyFromInt(1)
d.Staking.Ledger[stakingTests.DebugStateSrcAddress].Escrow.Debonding.TotalShares = *quantity.NewFromUint64(1)
require.Error(d.SanityCheck(), "invalid escrow debonding total shares should be rejected")

d = *testDoc
d.Staking.Delegations = map[staking.Address]map[staking.Address]*staking.Delegation{
stakingTests.DebugStateSrcAddress: {
stakingTests.DebugStateDestAddress: {
Shares: stakingTests.QtyFromInt(1),
Shares: *quantity.NewFromUint64(1),
},
},
}
Expand All @@ -725,7 +726,7 @@ func TestGenesisSanityCheck(t *testing.T) {
stakingTests.DebugStateSrcAddress: {
stakingTests.DebugStateDestAddress: {
{
Shares: stakingTests.QtyFromInt(1),
Shares: *quantity.NewFromUint64(1),
DebondEndTime: 10,
},
},
Expand Down
4 changes: 2 additions & 2 deletions go/oasis-net-runner/fixtures/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ func newDefaultFixture() (*oasis.NetworkFixture, error) {
AdmissionPolicy: registry.RuntimeAdmissionPolicy{
AnyNode: &registry.AnyNodeRuntimeAdmissionPolicy{},
},
GenesisState: viper.GetString(cfgRuntimeGenesisState),
GenesisRound: 0,
GenesisStatePath: viper.GetString(cfgRuntimeGenesisState),
GenesisRound: 0,
},
},
Validators: []oasis.ValidatorFixture{
Expand Down
19 changes: 0 additions & 19 deletions go/oasis-net-runner/fixtures/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import (
"github.com/stretchr/testify/require"
)

const (
defaultFixturePath = "../../../tests/fixture-data/net-runner/default.json"
)

func TestDefaultFixture(t *testing.T) {
f, err := newDefaultFixture()
require.Nil(t, err)
Expand All @@ -19,21 +15,6 @@ func TestDefaultFixture(t *testing.T) {
data, err := DumpFixture(f)
require.Nil(t, err)
require.NotNil(t, data)

// As cool as having tests cases is, having to regenerate test data
// every single time the default fixture changes is incredibly
// annoying.
//
// May this pearl of wisdom serve as a guiding light for the next
// unfortunate victim.
//
// $ ./oasis-net-runner dump-fixture > /tmp/fuckfuckfuckfuckfuck

storedData, err := ioutil.ReadFile(defaultFixturePath)
require.Nil(t, err)
require.NotNil(t, storedData)

require.EqualValues(t, storedData, data)
}

func TestCustomFixture(t *testing.T) {
Expand Down
144 changes: 144 additions & 0 deletions go/oasis-node/cmd/common/genesis/staking.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Package genesis implements common genesis document manipulation
// routines.
package genesis

import (
"encoding/json"
"fmt"
"io/ioutil"
"math/big"

"github.com/oasisprotocol/oasis-core/go/common/entity"
"github.com/oasisprotocol/oasis-core/go/common/quantity"
genesis "github.com/oasisprotocol/oasis-core/go/genesis/api"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
)

// AppendableStakingState is a staking genesis state that can be appended
// to an existing genesis document.
type AppendableStakingState struct {
State staking.Genesis
DebugTestEntity bool
}

// AppendTo appends the staking genesis state to an existing genesis document.
// Any pre-existing state will be overwritten.
func (st *AppendableStakingState) AppendTo(doc *genesis.Document) error {
if st.State.TokenSymbol == "" {
return fmt.Errorf("genesis/staking: token symbol not set")
}

if st.DebugTestEntity {
ent, _, err := entity.TestEntity()
if err != nil {
return fmt.Errorf("genesis/staking: failed to get test entity: %w", err)
}
entAddr := staking.NewAddress(ent.ID)

// Ok then, we hold the world ransom for One Hundred Billion Dollars.
var q quantity.Quantity
if err = q.FromBigInt(big.NewInt(100000000000)); err != nil {
return fmt.Errorf("genesis/staking: failed to allocate test entity stake: %w", err)
}

// Add the test entity's ledger entry.
if st.State.Ledger[entAddr] != nil {
return fmt.Errorf("genesis/staking: test entity already in ledger")
}
st.State.Ledger[entAddr] = &staking.Account{
General: staking.GeneralAccount{
Balance: q,
Nonce: 0,
},
Escrow: staking.EscrowAccount{
Active: staking.SharePool{
Balance: q,
TotalShares: *quantity.NewFromUint64(1),
},
},
}

// Add a self-delegation for the test entity.
if st.State.Delegations == nil {
st.State.Delegations = make(map[staking.Address]map[staking.Address]*staking.Delegation)
}
if st.State.Delegations[entAddr] == nil {
st.State.Delegations[entAddr] = make(map[staking.Address]*staking.Delegation)
}
if st.State.Delegations[entAddr][entAddr] != nil {
return fmt.Errorf("gensis/staking: test entity already has a self-delegation")
}
st.State.Delegations[entAddr][entAddr] = &staking.Delegation{
Shares: *quantity.NewFromUint64(1),
}

// Inflate the TotalSupply to account for the account's general and
// escrow balances.
_ = st.State.TotalSupply.Add(&q)
_ = st.State.TotalSupply.Add(&q)
}

// Set zero thresholds for all staking kinds, if none set.
if len(st.State.Parameters.Thresholds) == 0 {
sq := *quantity.NewFromUint64(0)
st.State.Parameters.Thresholds =
map[staking.ThresholdKind]quantity.Quantity{
staking.KindEntity: sq,
staking.KindNodeValidator: sq,
staking.KindNodeCompute: sq,
staking.KindNodeStorage: sq,
staking.KindNodeKeyManager: sq,
staking.KindRuntimeCompute: sq,
staking.KindRuntimeKeyManager: sq,
}
}

doc.Staking = st.State

return nil
}

func (st *AppendableStakingState) setDefaultFeeSplit() error {
if st.State.Parameters.FeeSplitWeightVote.IsZero() {
if err := st.State.Parameters.FeeSplitWeightVote.FromInt64(1); err != nil {
return fmt.Errorf("genesis/staking: couldn't set default fee split: %w", err)
}
}
return nil
}

// NewAppendableStakingState creates a new AppendableStakingState.
func NewAppendableStakingState() (*AppendableStakingState, error) {
st := &AppendableStakingState{
State: staking.Genesis{
Ledger: make(map[staking.Address]*staking.Account),
},
}
if err := st.setDefaultFeeSplit(); err != nil {
return nil, err
}

return st, nil
}

// NewAppendableStakingStateFromFile creates a new AppendableStakingState
// from a JSON document.
func NewAppendableStakingStateFromFile(path string) (*AppendableStakingState, error) {
st, err := NewAppendableStakingState()
if err != nil {
return nil, err
}

b, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("genesis/staking: failed to load staking genesis state: %w", err)
}
if err = json.Unmarshal(b, &st.State); err != nil {
return nil, fmt.Errorf("genesis/staking: failed to parse staking genesis state: %w", err)
}
if err := st.setDefaultFeeSplit(); err != nil {
return nil, err
}

return st, nil
}
Loading

0 comments on commit 0a8e82c

Please sign in to comment.