-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathaccount.go
152 lines (135 loc) · 3.42 KB
/
account.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
package chainload
import (
"context"
"math/big"
"math/rand"
"sync"
"github.com/gochain/gochain/v3/accounts"
"github.com/gochain/gochain/v3/accounts/keystore"
"github.com/gochain/gochain/v3/common"
"github.com/gochain/gochain/v3/core/types"
)
type AccountStore struct {
ks *keystore.KeyStore
chainID *big.Int
ksAccts []accounts.Account
pass string
acctsMu sync.RWMutex
nextIdx int
ksAcctsSet map[common.Address]struct{}
addrs []common.Address
pools map[int]map[common.Address]acctNonce
seeds map[common.Address]struct{}
}
type acctNonce struct {
*accounts.Account
nonce uint64
}
func NewAccountStore(ks *keystore.KeyStore, chainID *big.Int, pass string) *AccountStore {
return &AccountStore{
ks: ks,
ksAccts: ks.Accounts(),
chainID: chainID,
pass: pass,
ksAcctsSet: make(map[common.Address]struct{}),
pools: make(map[int]map[common.Address]acctNonce),
seeds: make(map[common.Address]struct{}),
}
}
func (a *AccountStore) SignTx(acct accounts.Account, tx *types.Transaction) (*types.Transaction, error) {
return a.ks.SignTx(acct, tx, a.chainID)
}
func (a *AccountStore) NextRecv(send common.Address, n int) []common.Address {
a.acctsMu.RLock()
defer a.acctsMu.RUnlock()
var addrs []common.Address
start := rand.Intn(len(a.addrs))
for i := 0; len(addrs) < n && i < len(a.addrs); i++ {
addr := a.addrs[(start+i)%len(a.addrs)]
if addr != send {
addrs = append(addrs, addr)
}
}
return addrs
}
func (a *AccountStore) Next(ctx context.Context, node int) (acct *accounts.Account, nonce uint64, err error) {
a.acctsMu.Lock()
defer a.acctsMu.Unlock()
if len(a.pools) > 0 && rand.Intn(2) == 0 {
pool := a.pools[node]
if pool != nil {
for addr, an := range pool {
delete(pool, addr)
return an.Account, an.nonce, nil
}
}
}
acct = a.nextAcct()
if acct == nil {
return
}
err = a.ks.Unlock(*acct, a.pass)
return
}
func (a *AccountStore) New(ctx context.Context) (*accounts.Account, error) {
acct, err := a.ks.NewAccount(a.pass)
if err != nil {
return nil, err
}
a.acctsMu.Lock()
a.addrs = append(a.addrs, acct.Address)
a.acctsMu.Unlock()
return &acct, a.ks.Unlock(acct, a.pass)
}
func (a *AccountStore) Return(acct *accounts.Account, node int, nonce uint64) {
a.acctsMu.Lock()
pool := a.pools[node]
if pool == nil {
pool = make(map[common.Address]acctNonce)
a.pools[node] = pool
}
pool[acct.Address] = acctNonce{Account: acct, nonce: nonce}
a.acctsMu.Unlock()
}
func (a *AccountStore) RandSeed() *common.Address {
a.acctsMu.RLock()
defer a.acctsMu.RUnlock()
for addr := range a.seeds {
return &addr
}
return nil
}
// NextSeed returns the next available account from the keystore.
func (a *AccountStore) NextSeed() (*accounts.Account, error) {
a.acctsMu.Lock()
acct := a.nextAcct()
if acct != nil {
a.seeds[acct.Address] = struct{}{}
}
a.acctsMu.Unlock()
if acct == nil {
return nil, nil
}
return acct, a.ks.Unlock(*acct, a.pass)
}
// nextAcct returns the next available account from the keystore, or nil if none
// are available.
func (a *AccountStore) nextAcct() *accounts.Account {
for {
if a.nextIdx >= len(a.ksAccts) {
return nil
}
i := a.nextIdx
a.nextIdx++
acct := a.ksAccts[i]
if acct == (accounts.Account{}) {
continue
}
if _, ok := a.ksAcctsSet[acct.Address]; ok {
continue
}
a.ksAcctsSet[acct.Address] = struct{}{}
a.addrs = append(a.addrs, acct.Address)
return &acct
}
}