This repository has been archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
account.go
77 lines (67 loc) · 1.82 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
package models
import (
"encoding/binary"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
bh "github.com/timshannon/badgerhold/v4"
)
var (
AccountLeafName = GetTypeName(AccountLeaf{})
AccountLeafPrefix = GetBadgerHoldPrefix(AccountLeaf{})
errInvalidAccountLeafIndexType = fmt.Errorf("invalid models.AccountLeaf index type")
)
type AccountLeaf struct {
PubKeyID uint32
PublicKey PublicKey `badgerhold:"index"`
}
type AccountNode struct {
MerklePath MerklePath
DataHash common.Hash
}
// nolint:gocritic
// Type implements badgerhold.Storer
func (a AccountLeaf) Type() string {
return string(AccountLeafName)
}
// nolint:gocritic
// Indexes implements badgerhold.Storer
// We're adding a lot of Accounts with ZeroPublicKey to the database as a result of the way C2T processing is currently implemented.
// See the usages of ZeroPublicKey for more context.
func (a AccountLeaf) Indexes() map[string]bh.Index {
return map[string]bh.Index{
"PublicKey": {
IndexFunc: func(_ string, value interface{}) ([]byte, error) {
v, err := interfaceToAccountLeaf(value)
if err != nil {
return nil, err
}
if v.PublicKey == ZeroPublicKey {
return nil, nil
}
return v.PublicKey.Bytes(), nil
},
},
}
}
func (a *AccountLeaf) Bytes() []byte {
b := make([]byte, 132)
binary.BigEndian.PutUint32(b[0:4], a.PubKeyID)
copy(b[4:132], a.PublicKey.Bytes())
return b
}
func (a *AccountLeaf) SetBytes(data []byte) error {
a.PubKeyID = binary.BigEndian.Uint32(data[0:4])
return a.PublicKey.SetBytes(data[4:132])
}
func interfaceToAccountLeaf(value interface{}) (*AccountLeaf, error) {
p, ok := value.(*AccountLeaf)
if ok {
return p, nil
}
v, ok := value.(AccountLeaf)
if ok {
return &v, nil
}
return nil, errors.WithStack(errInvalidAccountLeafIndexType)
}