forked from kaspanet/kaspad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
templatemanager.go
41 lines (36 loc) · 1.05 KB
/
templatemanager.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
package templatemanager
import (
"sync"
"github.com/romxxxx/nexepad/app/appmessage"
"github.com/romxxxx/nexepad/domain/consensus/model/externalapi"
"github.com/romxxxx/nexepad/domain/consensus/utils/pow"
)
var currentTemplate *externalapi.DomainBlock
var currentState *pow.State
var isSynced bool
var lock = &sync.Mutex{}
// Get returns the template to work on
func Get() (*externalapi.DomainBlock, *pow.State, bool) {
lock.Lock()
defer lock.Unlock()
// Shallow copy the block so when the user replaces the header it won't affect the template here.
if currentTemplate == nil {
return nil, nil, false
}
block := *currentTemplate
state := *currentState
return &block, &state, isSynced
}
// Set sets the current template to work on
func Set(template *appmessage.GetBlockTemplateResponseMessage) error {
block, err := appmessage.RPCBlockToDomainBlock(template.Block)
if err != nil {
return err
}
lock.Lock()
defer lock.Unlock()
currentTemplate = block
currentState = pow.NewState(block.Header.ToMutable())
isSynced = template.IsSynced
return nil
}