forked from perlin-network/wavelet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontract_test.go
67 lines (52 loc) · 1.89 KB
/
contract_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
package wavelet
import (
"github.com/perlin-network/noise/skademlia"
"github.com/perlin-network/wavelet/avl"
"github.com/perlin-network/wavelet/store"
"github.com/perlin-network/wavelet/sys"
"github.com/stretchr/testify/assert"
"io/ioutil"
"testing"
)
func BenchmarkContractInGraph(b *testing.B) {
const InitialBalance = 100000000
stateStore := store.NewInmem()
state := avl.New(stateStore)
viewID := uint64(0)
state.SetViewID(viewID)
keys, err := skademlia.NewKeys(1, 1)
assert.NoError(b, err)
initialRoot := AttachSenderToTransaction(keys, NewTransaction(keys, sys.TagNop, nil))
graph := NewGraph(WithRoot(initialRoot))
WriteAccountBalance(state, keys.PublicKey(), 1000000000000)
round := NewRound(viewID, state.Checksum(), 0, Transaction{}, initialRoot)
accountState := NewAccounts(stateStore)
assert.NoError(b, accountState.Commit(state))
code, err := ioutil.ReadFile("testdata/transfer_back.wasm")
assert.NoError(b, err)
tx := AttachSenderToTransaction(keys, NewTransaction(keys, sys.TagContract, buildContractSpawnPayload(100000, 0, code).Marshal()))
err = ApplyTransaction(&round, state, &tx, nil)
assert.NoError(b, err)
contractID := AccountID(tx.ID)
var criticalCount int
for criticalCount < b.N {
tx := AttachSenderToTransaction(
keys,
NewTransaction(
keys,
sys.TagTransfer,
buildTransferWithInvocationPayload(contractID, 200, 500000, []byte("on_money_received"), nil, 0).Marshal(),
), graph.FindEligibleParents()...)
assert.NoError(b, graph.AddTransaction(tx))
if tx.IsCritical(4) {
results, err := collapseTransactions(graph, accountState, viewID+1, &round, round.End, tx, false)
assert.NoError(b, err)
err = accountState.Commit(results.snapshot)
assert.NoError(b, err)
state = results.snapshot
round = NewRound(viewID+1, state.Checksum(), uint64(results.appliedCount), round.End, tx)
viewID += 1
criticalCount += 1
}
}
}