forked from mosaicnetworks/babble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inmem_dummy_test.go
158 lines (121 loc) · 4.01 KB
/
inmem_dummy_test.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
package dummy
import (
"fmt"
"os"
"reflect"
"testing"
"time"
"github.com/mosaicnetworks/babble/src/babble"
"github.com/mosaicnetworks/babble/src/common"
"github.com/mosaicnetworks/babble/src/config"
bcrypto "github.com/mosaicnetworks/babble/src/crypto"
"github.com/mosaicnetworks/babble/src/hashgraph"
"github.com/mosaicnetworks/babble/src/node/state"
"github.com/mosaicnetworks/babble/src/peers"
)
func TestInmemDummyAppSide(t *testing.T) {
logger := common.NewTestEntry(t, common.TestLogLevel)
dummy := NewInmemDummyClient(logger)
tx := []byte("the test transaction")
go func() {
select {
case st := <-dummy.SubmitCh():
// Verify the command
if !reflect.DeepEqual(st, tx) {
t.Fatalf("tx mismatch: %#v %#v", tx, st)
}
case <-time.After(200 * time.Millisecond):
t.Fatalf("timeout")
}
}()
dummy.SubmitTx(tx)
}
func TestInmemDummyServerSide(t *testing.T) {
logger := common.NewTestEntry(t, common.TestLogLevel)
dummy := NewInmemDummyClient(logger)
//create a few blocks
blocks := [5]*hashgraph.Block{}
for i := 0; i < 5; i++ {
blocks[i] = hashgraph.NewBlock(i, i+1,
[]byte{},
[]*peers.Peer{},
[][]byte{
[]byte(fmt.Sprintf("block %d transaction", i)),
},
[]hashgraph.InternalTransaction{
hashgraph.NewInternalTransaction(hashgraph.PEER_ADD, *peers.NewPeer("node0", "paris", "")),
hashgraph.NewInternalTransaction(hashgraph.PEER_REMOVE, *peers.NewPeer("node1", "london", "")),
},
0,
)
}
//commit first block and check that the client's statehash is correct
commitResponse, err := dummy.CommitBlock(*blocks[0])
if err != nil {
t.Fatalf("Fatal Error: %v", err)
}
expectedStateHash := []byte{}
for _, t := range blocks[0].Transactions() {
tHash := bcrypto.SHA256(t)
expectedStateHash = bcrypto.SimpleHashFromTwoHashes(expectedStateHash, tHash)
}
if !reflect.DeepEqual(commitResponse.StateHash, expectedStateHash) {
t.Fatalf("StateHash should be %v, not %v", expectedStateHash, commitResponse.StateHash)
}
snapshot, err := dummy.GetSnapshot(blocks[0].Index())
if err != nil {
t.Fatalf("Fatal Error: %v", err)
}
if !reflect.DeepEqual(snapshot, expectedStateHash) {
t.Fatalf("Snapshot should be %v, not %v", expectedStateHash, snapshot)
}
//commit a few more blocks, then attempt to restore back to block 0 state
for i := 1; i < 5; i++ {
_, err := dummy.CommitBlock(*blocks[i])
if err != nil {
t.Fatalf("Fatal Error: %v", err)
}
}
err = dummy.Restore(snapshot)
if err != nil {
t.Fatalf("Error restoring snapshot: %v", err)
}
if !reflect.DeepEqual(dummy.state.stateHash, expectedStateHash) {
t.Fatalf("Restore StateHash should be %v, not %v", expectedStateHash, dummy.state.stateHash)
}
err = dummy.OnStateChanged(state.Babbling)
if err != nil {
t.Fatalf("Error in OnStateChanged: %v", err)
}
if !reflect.DeepEqual(dummy.state.babbleState, state.Babbling) {
t.Fatalf("Babble State should be Babbling, not %v", dummy.state.babbleState.String())
}
}
func ExampleInmemDummyClient() {
// Start from default Babble configuration.
babbleConfig := config.NewDefaultConfig()
// Create dummy InmemProxy
dummy := NewInmemDummyClient(babbleConfig.Logger())
// Set the proxy in the Babble configuration.
babbleConfig.Proxy = dummy
// Instantiate Babble.
babble := babble.NewBabble(babbleConfig)
// Read in the configuration and initialise the node accordingly.
if err := babble.Init(); err != nil {
babbleConfig.Logger().Error("Cannot initialize babble:", err)
os.Exit(1)
}
// The application can submit transactions to Babble using the proxy's
// SubmitTx. Babble will broadcast the transactions to other nodes, run them
// through the consensus algorithm, and eventually call the callback methods
// implemented in the handler.
go func() {
dummy.SubmitTx([]byte("the test transaction"))
}()
// Run the node aynchronously.
babble.Run()
// Babble reacts to SIGINT (Ctrl + c) and SIGTERM by calling the leave
// method to politely leave a Babble network, but it can also be called
// manually.
defer babble.Node.Leave()
}