-
Notifications
You must be signed in to change notification settings - Fork 12
/
accounts_framework.go
96 lines (73 loc) · 2.54 KB
/
accounts_framework.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
package tests
import (
"testing"
"golang.org/x/crypto/blake2b"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/iota-core/pkg/core/account"
iotago "github.com/iotaledger/iota.go/v4"
)
type AccountsTestFramework struct {
Instance *account.Accounts
Committee *account.SeatedAccounts
test *testing.T
identitiesByAlias map[string]iotago.AccountID
}
func NewAccountsTestFramework(t *testing.T, instance *account.Accounts) *AccountsTestFramework {
t.Helper()
return &AccountsTestFramework{
Instance: instance,
Committee: instance.SeatedAccounts(),
test: t,
identitiesByAlias: make(map[string]iotago.AccountID),
}
}
func (f *AccountsTestFramework) Add(alias string) {
validatorID, exists := f.identitiesByAlias[alias]
if !exists {
f.test.Fatal(ierrors.Errorf("identity with alias '%s' does not exist", alias))
}
f.Committee.Set(account.SeatIndex(f.Committee.SeatCount()), validatorID)
}
func (f *AccountsTestFramework) Delete(alias string) bool {
validatorID, exists := f.identitiesByAlias[alias]
if !exists {
f.test.Fatal(ierrors.Errorf("identity with alias '%s' does not exist", alias))
}
return f.Committee.Delete(validatorID)
}
func (f *AccountsTestFramework) Get(alias string) (seat account.SeatIndex, exists bool) {
validatorID, exists := f.identitiesByAlias[alias]
if !exists {
f.test.Fatal(ierrors.Errorf("identity with alias '%s' does not exist", alias))
}
return f.Committee.GetSeat(validatorID)
}
func (f *AccountsTestFramework) Has(alias string) bool {
validatorID, exists := f.identitiesByAlias[alias]
if !exists {
f.test.Fatal(ierrors.Errorf("identity with alias '%s' does not exist", alias))
}
return f.Committee.HasAccount(validatorID)
}
func (f *AccountsTestFramework) CreateID(alias string) iotago.AccountID {
_, exists := f.identitiesByAlias[alias]
if exists {
f.test.Fatal(ierrors.Errorf("identity with alias '%s' already exists", alias))
}
hashedAlias := blake2b.Sum256([]byte(alias))
validatorID := iotago.AccountIDFromData(hashedAlias[:])
validatorID.RegisterAlias(alias)
if err := f.Instance.Set(validatorID, &account.Pool{}); err != nil { // we don't care about pools when doing PoA
f.test.Fatal(err)
}
f.Committee.Set(account.SeatIndex(f.Committee.SeatCount()), validatorID)
f.identitiesByAlias[alias] = validatorID
return validatorID
}
func (f *AccountsTestFramework) ID(alias string) iotago.AccountID {
id, exists := f.identitiesByAlias[alias]
if !exists {
f.test.Fatal(ierrors.Errorf("identity with alias '%s' does not exist", alias))
}
return id
}