forked from dymensionxyz/dymint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestutil.go
160 lines (139 loc) · 5.28 KB
/
testutil.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package block
import (
"context"
"crypto/rand"
"encoding/hex"
"time"
"github.com/dymensionxyz/dymint/p2p"
"github.com/dymensionxyz/dymint/settlement"
"github.com/dymensionxyz/dymint/testutil"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/libs/pubsub"
"github.com/tendermint/tendermint/proxy"
"github.com/dymensionxyz/dymint/config"
"github.com/dymensionxyz/dymint/da"
localda "github.com/dymensionxyz/dymint/da/local"
mempoolv1 "github.com/dymensionxyz/dymint/mempool/v1"
nodemempool "github.com/dymensionxyz/dymint/node/mempool"
slregistry "github.com/dymensionxyz/dymint/settlement/registry"
"github.com/dymensionxyz/dymint/store"
tmcfg "github.com/tendermint/tendermint/config"
)
const (
defaultBatchSize = 5
batchLimitBytes = 2000
)
/* -------------------------------------------------------------------------- */
/* utils */
/* -------------------------------------------------------------------------- */
func getManagerWithProposerKey(conf config.BlockManagerConfig, proposerKey crypto.PrivKey, settlementlc settlement.LayerI, dalc da.DataAvailabilityLayerClient, genesisHeight int64, storeInitialHeight int64, storeLastBlockHeight int64, proxyAppConns proxy.AppConns, mockStore store.Store) (*Manager, error) {
genesis := testutil.GenerateGenesis(genesisHeight)
// Change the LastBlockHeight to avoid calling InitChainSync within the manager
// And updating the state according to the genesis.
state := testutil.GenerateState(storeInitialHeight, storeLastBlockHeight)
var managerStore store.Store
if mockStore == nil {
managerStore = store.New(store.NewDefaultInMemoryKVStore())
} else {
managerStore = mockStore
}
if _, err := managerStore.UpdateState(state, nil); err != nil {
return nil, err
}
logger := log.TestingLogger()
pubsubServer := pubsub.NewServer()
err := pubsubServer.Start()
if err != nil {
return nil, err
}
// Init the settlement layer mock
if settlementlc == nil {
settlementlc = slregistry.GetClient(slregistry.Local)
}
proposerPubKey := proposerKey.GetPublic()
pubKeybytes, err := proposerPubKey.Raw()
if err != nil {
return nil, err
}
err = initSettlementLayerMock(settlementlc, hex.EncodeToString(pubKeybytes), pubsubServer, logger)
if err != nil {
return nil, err
}
if dalc == nil {
dalc = &localda.DataAvailabilityLayerClient{}
}
initDALCMock(dalc, pubsubServer, logger)
var proxyApp proxy.AppConns
if proxyAppConns == nil {
proxyApp = testutil.GetABCIProxyAppMock(logger.With("module", "proxy"))
if err := proxyApp.Start(); err != nil {
return nil, err
}
} else {
proxyApp = proxyAppConns
}
mp := mempoolv1.NewTxMempool(logger, tmcfg.DefaultMempoolConfig(), proxyApp.Mempool(), 0)
mpIDs := nodemempool.NewMempoolIDs()
// Init p2p client and validator
p2pKey, _, _ := crypto.GenerateEd25519Key(rand.Reader)
p2pClient, err := p2p.NewClient(config.P2PConfig{
GossipCacheSize: 50,
BoostrapTime: 30 * time.Second,
}, p2pKey, "TestChain", logger)
if err != nil {
return nil, err
}
p2pValidator := p2p.NewValidator(logger, pubsubServer)
p2pClient.SetTxValidator(p2pValidator.TxValidator(mp, mpIDs))
p2pClient.SetBlockValidator(p2pValidator.BlockValidator())
if err = p2pClient.Start(context.Background()); err != nil {
return nil, err
}
manager, err := NewManager(proposerKey, conf, genesis, managerStore, mp, proxyApp, dalc, settlementlc, nil,
pubsubServer, p2pClient, logger)
if err != nil {
return nil, err
}
return manager, nil
}
func getManager(conf config.BlockManagerConfig, settlementlc settlement.LayerI, dalc da.DataAvailabilityLayerClient, genesisHeight int64, storeInitialHeight int64, storeLastBlockHeight int64, proxyAppConns proxy.AppConns, mockStore store.Store) (*Manager, error) {
proposerKey, _, err := crypto.GenerateEd25519Key(rand.Reader)
if err != nil {
return nil, err
}
return getManagerWithProposerKey(conf, proposerKey, settlementlc, dalc, genesisHeight, storeInitialHeight, storeLastBlockHeight, proxyAppConns, mockStore)
}
// TODO(omritoptix): Possible move out to a generic testutil
func getMockDALC(logger log.Logger) da.DataAvailabilityLayerClient {
dalc := &localda.DataAvailabilityLayerClient{}
initDALCMock(dalc, pubsub.NewServer(), logger)
return dalc
}
// TODO(omritoptix): Possible move out to a generic testutil
func initDALCMock(dalc da.DataAvailabilityLayerClient, pubsubServer *pubsub.Server, logger log.Logger) {
_ = dalc.Init(nil, pubsubServer, store.NewDefaultInMemoryKVStore(), logger)
_ = dalc.Start()
}
// TODO(omritoptix): Possible move out to a generic testutil
func initSettlementLayerMock(settlementlc settlement.LayerI, proposer string, pubsubServer *pubsub.Server, logger log.Logger) error {
err := settlementlc.Init(settlement.Config{ProposerPubKey: proposer}, pubsubServer, logger)
if err != nil {
return err
}
err = settlementlc.Start()
if err != nil {
return err
}
return nil
}
func getManagerConfig() config.BlockManagerConfig {
return config.BlockManagerConfig{
BlockTime: 100 * time.Millisecond,
BlockBatchSize: defaultBatchSize,
BlockBatchMaxSizeBytes: 1000,
BatchSubmitMaxTime: 30 * time.Minute,
NamespaceID: "0102030405060708",
GossipedBlocksCacheSize: 50,
}
}