Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package currency
import (
"context"
"fmt"
"testing"
"time"
"github.com/spikeekips/mitum/base"
"github.com/spikeekips/mitum/base/ballot"
"github.com/spikeekips/mitum/base/block"
"github.com/spikeekips/mitum/base/key"
"github.com/spikeekips/mitum/base/operation"
"github.com/spikeekips/mitum/base/seal"
"github.com/spikeekips/mitum/base/state"
"github.com/spikeekips/mitum/base/tree"
"github.com/spikeekips/mitum/isaac"
"github.com/spikeekips/mitum/storage"
"github.com/spikeekips/mitum/util"
"github.com/spikeekips/mitum/util/encoder"
"github.com/spikeekips/mitum/util/localtime"
"github.com/spikeekips/mitum/util/valuehash"
"github.com/stretchr/testify/suite"
)
// BLOCK remove
type testRemoveMe struct {
suite.Suite
isaac.StorageSupportTest
encs *encoder.Encoders
enc encoder.Encoder
local *isaac.Localstate
storage storage.Storage
senderPriv key.Privatekey
sender Address
}
func (t *testRemoveMe) SetupSuite() {
t.StorageSupportTest.SetupSuite()
_ = t.Encs.AddHinter(key.BTCPrivatekeyHinter)
_ = t.Encs.AddHinter(key.BTCPublickeyHinter)
_ = t.Encs.AddHinter(valuehash.SHA256{})
_ = t.Encs.AddHinter(valuehash.Bytes{})
_ = t.Encs.AddHinter(base.StringAddress(""))
_ = t.Encs.AddHinter(ballot.INITBallotV0{})
_ = t.Encs.AddHinter(ballot.INITBallotFactV0{})
_ = t.Encs.AddHinter(ballot.ProposalV0{})
_ = t.Encs.AddHinter(ballot.ProposalFactV0{})
_ = t.Encs.AddHinter(ballot.SIGNBallotV0{})
_ = t.Encs.AddHinter(ballot.SIGNBallotFactV0{})
_ = t.Encs.AddHinter(ballot.ACCEPTBallotV0{})
_ = t.Encs.AddHinter(ballot.ACCEPTBallotFactV0{})
_ = t.Encs.AddHinter(base.VoteproofV0{})
_ = t.Encs.AddHinter(base.BaseNodeV0{})
_ = t.Encs.AddHinter(block.BlockV0{})
_ = t.Encs.AddHinter(block.ManifestV0{})
_ = t.Encs.AddHinter(block.ConsensusInfoV0{})
_ = t.Encs.AddHinter(block.SuffrageInfoV0{})
_ = t.Encs.AddHinter(operation.BaseFactSign{})
_ = t.Encs.AddHinter(operation.BaseSeal{})
_ = t.Encs.AddHinter(operation.KVOperationFact{})
_ = t.Encs.AddHinter(operation.KVOperation{})
_ = t.Encs.AddHinter(isaac.KVOperation{})
_ = t.Encs.AddHinter(isaac.LongKVOperation{})
_ = t.Encs.AddHinter(tree.AVLTree{})
_ = t.Encs.AddHinter(operation.OperationAVLNode{})
_ = t.Encs.AddHinter(operation.OperationAVLNodeMutable{})
_ = t.Encs.AddHinter(state.StateV0{})
_ = t.Encs.AddHinter(state.StateV0AVLNode{})
_ = t.Encs.AddHinter(state.StateV0AVLNodeMutable{})
_ = t.Encs.AddHinter(state.BytesValue{})
_ = t.Encs.AddHinter(state.DurationValue{})
_ = t.Encs.AddHinter(state.HintedValue{})
_ = t.Encs.AddHinter(state.NumberValue{})
_ = t.Encs.AddHinter(state.SliceValue{})
_ = t.Encs.AddHinter(state.StringValue{})
t.Encs.AddHinter(Key{})
t.Encs.AddHinter(Keys{})
t.Encs.AddHinter(Address(""))
t.Encs.AddHinter(GenesisAccount{})
t.Encs.AddHinter(GenesisAccountFact{})
t.Encs.AddHinter(CreateAccountFact{})
t.Encs.AddHinter(CreateAccount{})
t.Encs.AddHinter(TransferFact{})
t.Encs.AddHinter(Transfer{})
t.senderPriv = key.MustNewBTCPrivatekey()
}
func (t *testRemoveMe) SetupTest() {
t.storage = t.Storage(t.Encs, t.JSONEnc)
t.local = t.localstate()
}
func (t *testRemoveMe) suffrage(proposerState *isaac.Localstate, states ...*isaac.Localstate) base.Suffrage {
nodes := make([]base.Address, len(states))
for i, s := range states {
nodes[i] = s.Node().Address()
}
sf := base.NewFixedSuffrage(proposerState.Node().Address(), nodes)
if err := sf.Initialize(); err != nil {
panic(err)
}
return sf
}
func (t *testRemoveMe) newVoteproof(stage base.Stage, fact base.Fact, states ...*isaac.Localstate) (base.VoteproofV0, error) {
factHash := fact.Hash()
var votes []base.VoteproofNodeFact
for _, state := range states {
factSignature, err := state.Node().Privatekey().Sign(
util.ConcatBytesSlice(
factHash.Bytes(),
state.Policy().NetworkID(),
),
)
if err != nil {
return base.VoteproofV0{}, err
}
votes = append(votes, base.NewVoteproofNodeFact(
state.Node().Address(),
valuehash.RandomSHA256(),
factHash,
factSignature,
state.Node().Publickey(),
))
}
var height base.Height
var round base.Round
switch f := fact.(type) {
case ballot.ACCEPTBallotFactV0:
height = f.Height()
round = f.Round()
case ballot.INITBallotFactV0:
height = f.Height()
round = f.Round()
}
vp := base.NewTestVoteproofV0(
height,
round,
t.suffrage(states[0], states...).Nodes(),
states[0].Policy().ThresholdRatio(),
base.VoteResultMajority,
false,
stage,
fact,
[]base.Fact{fact},
votes,
localtime.Now(),
)
return vp, nil
}
func (t *testRemoveMe) newINITBallot(local *isaac.Localstate, voteproof base.Voteproof) ballot.INITBallotV0 {
var ib ballot.INITBallotV0
if b, err := isaac.NewINITBallotV0Round0(local.Storage(), local.Node().Address()); err != nil {
panic(err)
} else {
ib = b
}
_ = ib.Sign(local.Node().Privatekey(), local.Policy().NetworkID())
return ib
}
func (t *testRemoveMe) localstate() *isaac.Localstate {
n := isaac.RandomLocalNode(util.UUID().String(), nil)
local, err := isaac.NewLocalstate(t.storage, n, isaac.TestNetworkID)
if err != nil {
panic(err)
} else if err := local.Initialize(); err != nil {
panic(err)
}
ks := []Key{NewKey(t.senderPriv.Publickey(), 100)}
keys, _ := NewKeys(ks, 100)
t.sender, _ = NewAddressFromKeys(ks)
amount, _ := NewAmountFromString("99999999999999999999999999")
ga, err := NewGenesisAccount(t.senderPriv, keys, amount, isaac.TestNetworkID)
t.NoError(err)
t.NoError(ga.IsValid(isaac.TestNetworkID))
genesis, err := isaac.NewGenesisBlockV0Generator(local, []operation.Operation{ga})
if err != nil {
panic(err)
} else if _, err := genesis.Generate(); err != nil {
panic(err)
}
return local
}
func (t *testRemoveMe) newOperation(sender base.Address, amount Amount, keys Keys, pks []key.Privatekey) CreateAccount {
token := util.UUID().Bytes()
fact := NewCreateAccountFact(token, sender, keys, amount)
var fs []operation.FactSign
for _, pk := range pks {
sig, err := operation.NewFactSignature(pk, fact, nil)
t.NoError(err)
fs = append(fs, operation.NewBaseFactSign(pk.Publickey(), sig))
}
ca, err := NewCreateAccount(fact, fs, "")
t.NoError(err)
t.NoError(ca.IsValid(nil))
return ca
}
func (t *testRemoveMe) newOperations(n int) []operation.Operation {
ops := make([]operation.Operation, n)
for i := 0; i < n; i++ {
keys, _ := NewKeys([]Key{NewKey(key.MustNewBTCPrivatekey().Publickey(), 100)}, 100)
ca := t.newOperation(
t.sender,
NewAmount(1),
keys,
[]key.Privatekey{t.senderPriv},
)
ops[i] = ca
}
return ops
}
func (t *testRemoveMe) commit(ops []operation.Operation) {
max := uint(len(ops))
t.local.Policy().SetMaxOperationsInSeal(max)
t.local.Policy().SetMaxOperationsInProposal(max)
sl, err := operation.NewBaseSeal(
t.senderPriv,
ops,
isaac.TestNetworkID,
)
t.NoError(err)
t.NoError(t.storage.NewSeals([]seal.Seal{sl}))
dp := isaac.NewDefaultProposalProcessor(t.local, t.suffrage(t.local))
dp.SetLogger(log) // BLOCK remove
pm := isaac.NewProposalMaker(t.local)
ib := t.newINITBallot(t.local, nil)
initFact := ib.INITBallotFactV0
ivp, err := t.newVoteproof(base.StageINIT, initFact, t.local)
proposal, err := pm.Proposal(ivp.Round())
t.NoError(err)
_ = t.local.Storage().NewProposal(proposal)
blk, err := dp.ProcessINIT(proposal.Hash(), ivp)
t.NoError(err)
acceptFact := ballot.NewACCEPTBallotV0(
nil,
ivp.Height(),
ivp.Round(),
proposal.Hash(),
blk.Hash(),
nil,
).Fact()
avp, err := t.newVoteproof(base.StageACCEPT, acceptFact, t.local)
t.NoError(err)
bs, err := dp.ProcessACCEPT(proposal.Hash(), avp)
t.NoError(err)
s := time.Now()
t.NoError(bs.Commit(context.Background()))
fmt.Println(">", time.Since(s).String())
ublk, found, err := t.local.Storage().Block(blk.Hash())
t.NoError(err)
t.True(found)
for _, op := range ops {
n, err := ublk.Operations().Get([]byte(op.Fact().Hash().String()))
t.NoError(err)
t.NotNil(n)
}
}
func (t *testRemoveMe) Test10() {
ops := t.newOperations(5000)
t.commit(ops)
}
func TestRemoveMe(t *testing.T) {
handler := new(testRemoveMe)
handler.DBType = "mongodb"
suite.Run(t, handler)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment