/
context.go
113 lines (92 loc) · 2.21 KB
/
context.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 types
import (
"bytes"
"github.com/blockservice/echoin/utils"
"math/rand"
"sort"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
)
type nonce int64
type Context struct {
id nonce
chain string
height int64
signers []common.Address
ethappState *state.StateDB
nonce uint64
time int64
}
func NewContext(chain string, height, time int64, ethappState *state.StateDB) Context {
return Context{
id: nonce(rand.Int63()),
chain: chain,
height: height,
time: time,
ethappState: ethappState,
}
}
func (c Context) ChainID() string {
return c.chain
}
func (c Context) BlockHeight() int64 {
return c.height
}
func (c Context) BlockTime() int64 {
return c.time
}
func (c Context) FormatBlockTime() string {
return utils.FormatUnixTime(c.time)
}
/*func (c Context) WithSigners(signers ...common.Address) Context {
return Context{
id: c.id,
chain: c.chain,
height: c.height,
signers: append(c.signers, signers...),
}
}*/
func (c Context) EthappState() *state.StateDB {
return c.ethappState
}
func (c *Context) WithSigners(signers ...common.Address) {
c.signers = append(c.signers, signers...)
}
func (c Context) HasSigner(signer common.Address) bool {
for _, s := range c.signers {
if s == signer {
return true
}
}
return false
}
func (c Context) GetSigners() []common.Address {
return c.signers
}
// Reset should clear out all permissions,
// but carry on knowledge that this is a child
func (c Context) Reset() Context {
return Context{
id: c.id,
chain: c.chain,
height: c.height,
}
}
func (c *Context) SetNonce(nonce uint64) {
c.nonce = nonce
}
func (c Context) GetNonce() uint64 {
return c.nonce
}
//////////////////////////////// Sort Interface
// USAGE sort.Sort(ByAll(<common.Address>))
// ByAll implements sort.Interface for []common.Address.
// It sorts be the Address
type ByAll []common.Address
// Verify the sort interface at compile time
var _ sort.Interface = ByAll{}
func (a ByAll) Len() int { return len(a) }
func (a ByAll) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByAll) Less(i, j int) bool {
return bytes.Compare(a[i].Bytes(), a[j].Bytes()) == -1
}