-
Notifications
You must be signed in to change notification settings - Fork 199
/
miniNetwork.go
113 lines (93 loc) · 2.68 KB
/
miniNetwork.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
package integrationTests
import (
"encoding/hex"
"fmt"
"math/big"
"testing"
"github.com/multiversx/mx-chain-core-go/data/transaction"
)
// MiniNetwork is a mini network, useful for some integration tests
type MiniNetwork struct {
Round uint64
Nonce uint64
Nodes []*TestProcessorNode
ShardNode *TestProcessorNode
MetachainNode *TestProcessorNode
Users map[string]*TestWalletAccount
}
// NewMiniNetwork creates a MiniNetwork
func NewMiniNetwork() *MiniNetwork {
n := &MiniNetwork{}
nodes := CreateNodes(
1,
1,
1,
)
n.Nodes = nodes
n.ShardNode = nodes[0]
n.MetachainNode = nodes[1]
n.Users = make(map[string]*TestWalletAccount)
return n
}
// Stop stops the mini network
func (n *MiniNetwork) Stop() {
n.ShardNode.Close()
n.MetachainNode.Close()
}
// FundAccount funds an account
func (n *MiniNetwork) FundAccount(address []byte, value *big.Int) {
shard := n.MetachainNode.ShardCoordinator.ComputeId(address)
if shard == n.MetachainNode.ShardCoordinator.SelfId() {
MintAddress(n.MetachainNode.AccntState, address, value)
} else {
MintAddress(n.ShardNode.AccntState, address, value)
}
}
// AddUser adds a user (account) to the mini network
func (n *MiniNetwork) AddUser(balance *big.Int) *TestWalletAccount {
user := CreateTestWalletAccount(n.ShardNode.ShardCoordinator, 0)
n.Users[string(user.Address)] = user
n.FundAccount(user.Address, balance)
return user
}
// Start starts the mini network
func (n *MiniNetwork) Start() {
n.Round = 1
n.Nonce = 1
}
// Continue advances processing with a number of rounds
func (n *MiniNetwork) Continue(t *testing.T, numRounds int) {
idxProposers := []int{0, 1}
for i := int64(0); i < int64(numRounds); i++ {
n.Nonce, n.Round = ProposeAndSyncOneBlock(t, n.Nodes, idxProposers, n.Round, n.Nonce)
}
}
// SendTransaction sends a transaction
func (n *MiniNetwork) SendTransaction(
senderPubkey []byte,
receiverPubkey []byte,
value *big.Int,
data string,
additionalGasLimit uint64,
) (string, error) {
sender, ok := n.Users[string(senderPubkey)]
if !ok {
return "", fmt.Errorf("unknown sender: %s", hex.EncodeToString(senderPubkey))
}
tx := &transaction.Transaction{
Nonce: sender.Nonce,
Value: new(big.Int).Set(value),
SndAddr: sender.Address,
RcvAddr: receiverPubkey,
Data: []byte(data),
GasPrice: MinTxGasPrice,
GasLimit: MinTxGasLimit + uint64(len(data)) + additionalGasLimit,
ChainID: ChainID,
Version: MinTransactionVersion,
}
txBuff, _ := tx.GetDataForSigning(TestAddressPubkeyConverter, TestTxSignMarshalizer, TestTxSignHasher)
tx.Signature, _ = sender.SingleSigner.Sign(sender.SkTxSign, txBuff)
txHash, err := n.ShardNode.SendTransaction(tx)
sender.Nonce++
return txHash, err
}