-
Notifications
You must be signed in to change notification settings - Fork 25
/
log.go
88 lines (78 loc) · 2.2 KB
/
log.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
package substrate
import (
"encoding/json"
"fmt"
"github.com/itering/scale.go/types"
"github.com/itering/scale.go/types/scaleBytes"
"github.com/itering/substrate-api-rpc/storage"
"github.com/itering/substrate-api-rpc/util"
)
const (
CidAura = 0x61727561
CidBabe = 0x45424142
)
type PreRuntime struct {
Data string `json:"data"`
Engine int64 `json:"engine"`
}
func (p *PreRuntime) isAura() bool {
return CidAura == p.Engine
}
func (p *PreRuntime) isBabe() bool {
return CidBabe == p.Engine
}
func (p *PreRuntime) getAuraAuthor(sessionValidators []string) string {
if rawAuraPreDigestValue, err := storage.Decode(p.Data, "RawAuraPreDigest", nil); err == nil {
modn := rawAuraPreDigestValue.ToRawAuraPreDigest().SlotNumber % int64(len(sessionValidators))
return sessionValidators[modn]
}
return ""
}
func (p *PreRuntime) getBabeAuthor(sessionValidators []string) string {
if rawBabePreDigestValue, err := storage.Decode(p.Data, "RawBabePreDigest", nil); err == nil {
digest := rawBabePreDigestValue.ToRawBabePreDigest()
if digest != nil {
if digest.Primary != nil {
return sessionValidators[digest.Primary.AuthorityIndex]
} else if digest.Secondary != nil {
return sessionValidators[digest.Secondary.AuthorityIndex]
} else {
return sessionValidators[digest.VRF.AuthorityIndex]
}
}
}
return ""
}
func ExtractAuthor(data []byte, sessionValidators []string) string {
var p PreRuntime
if len(sessionValidators) == 0 {
return ""
}
if err := json.Unmarshal(data, &p); err != nil {
return ""
}
if p.isAura() {
return p.getAuraAuthor(sessionValidators)
} else if p.isBabe() {
return p.getBabeAuthor(sessionValidators)
} else {
return ""
}
}
// LogDigest decode
func DecodeLogDigest(rawList []string) (r []storage.DecoderLog, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Recovering from panic in DecodeLogDigest error is: %v \n", r)
}
}()
for _, logRaw := range rawList {
m := types.ScaleDecoder{}
m.Init(scaleBytes.ScaleBytes{Data: util.HexToBytes(logRaw)}, nil)
rb := m.ProcessAndUpdateData("LogDigest").(map[string]interface{})
var log storage.DecoderLog
util.UnmarshalToAnything(&log, rb)
r = append(r, log)
}
return
}