-
Notifications
You must be signed in to change notification settings - Fork 199
/
esdtSuppliesProcessor.go
82 lines (66 loc) · 2.25 KB
/
esdtSuppliesProcessor.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
//go:generate protoc -I=proto -I=$GOPATH/src -I=$GOPATH/src/github.com/ElrondNetwork/protobuf/protobuf --gogoslick_out=. supplyESDT.proto
package esdtSupply
import (
"sync"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go-core/data"
"github.com/ElrondNetwork/elrond-go-core/marshal"
logger "github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/storage"
)
var log = logger.GetOrCreate("dblookupext/esdtSupply")
type suppliesProcessor struct {
logsProc *logsProcessor
logsGet *logsGetter
mutex sync.Mutex
}
// NewSuppliesProcessor will create a new instance of the supplies processor
func NewSuppliesProcessor(
marshalizer marshal.Marshalizer,
suppliesStorer storage.Storer,
logsStorer storage.Storer,
) (*suppliesProcessor, error) {
if check.IfNil(marshalizer) {
return nil, core.ErrNilMarshalizer
}
if check.IfNil(suppliesStorer) {
return nil, core.ErrNilStore
}
if check.IfNil(logsStorer) {
return nil, core.ErrNilStore
}
logsGet := newLogsGetter(marshalizer, logsStorer)
logsProc := newLogsProcessor(marshalizer, suppliesStorer)
return &suppliesProcessor{
logsProc: logsProc,
logsGet: logsGet,
}, nil
}
// ProcessLogs will process the provided logs
func (sp *suppliesProcessor) ProcessLogs(blockNonce uint64, logs map[string]data.LogHandler) error {
sp.mutex.Lock()
defer sp.mutex.Unlock()
return sp.logsProc.processLogs(blockNonce, logs, false)
}
// RevertChanges will revert supplies changes based on the provided block body
func (sp *suppliesProcessor) RevertChanges(header data.HeaderHandler, body data.BodyHandler) error {
if check.IfNil(header) || check.IfNil(body) {
return nil
}
sp.mutex.Lock()
defer sp.mutex.Unlock()
logsFromDB, err := sp.logsGet.getLogsBasedOnBody(body)
if err != nil {
return err
}
return sp.logsProc.processLogs(header.GetNonce(), logsFromDB, true)
}
// GetESDTSupply will return the supply from the storage for the given token
func (sp *suppliesProcessor) GetESDTSupply(token string) (string, error) {
return sp.logsProc.getESDTSupply(token)
}
// IsInterfaceNil returns true if there is no value under the interface
func (sp *suppliesProcessor) IsInterfaceNil() bool {
return sp == nil
}