-
Notifications
You must be signed in to change notification settings - Fork 2
/
oracle.go
116 lines (92 loc) · 2.91 KB
/
oracle.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
114
115
116
package oracle
import (
"os"
"time"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/log"
"github.com/unification-com/wrkoracle/client/mainchain"
"github.com/unification-com/wrkoracle/client/wrkchains"
"github.com/unification-com/wrkoracle/types"
)
// WrkOracle is an object to hold Oracle settings, and the WRKChain and Mainchain clients
type WrkOracle struct {
frequency uint64
log log.Logger
wrkChain *wrkchains.WrkChain
mainchainClient *mainchain.MainchainClient
}
// NewWrkOracle returns an initialised WrkOracle object
func NewWrkOracle(cliCxt context.CLIContext, kb keys.Keybase, cdc *codec.Codec) (WrkOracle, error) {
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
mc := mainchain.NewMainchainClient(cliCxt, kb, cdc, logger)
err := mc.SetWrkchainMetaData()
if err != nil {
return WrkOracle{}, err
}
wrkChain, err := wrkchains.NewWrkChain(mc.GetWrkchainMeta(), logger)
if err != nil {
return WrkOracle{}, err
}
return WrkOracle{
frequency: viper.GetUint64(types.FlagFrequency),
log: logger.With("pkg", "oracle"),
mainchainClient: mc,
wrkChain: wrkChain,
}, nil
}
// Run runs the WRKOracle in automated mode
func (wo WrkOracle) Run() error {
return wo.runOracle()
}
func (wo WrkOracle) runOracle() error {
wo.log.Info("Start running WRKOracle")
errors := make(chan error)
for {
go func() {
timeNow := time.Now().Local()
wo.log.Info("start poll", "time", timeNow)
dueAt := time.Now().Local().Add(time.Duration(wo.frequency) * time.Second)
err := wo.poll()
if err != nil {
errors <- err
return
}
wo.log.Info("end poll. Next poll due:", "due", dueAt)
wo.log.Info("-----------------------------------")
}()
select {
case err := <-errors:
wo.log.Error(err.Error())
return err
case <-time.After(time.Duration(wo.frequency) * time.Second):
}
}
}
func (wo WrkOracle) poll() error {
wo.log.Info("polling WRKChain for latest block")
header, err := wo.wrkChain.GetLatestBlock()
if err != nil {
wo.log.Error(err.Error())
return err
}
wo.mainchainClient.SetRecordFees()
wo.log.Info("recording latest WRKChain block")
return wo.mainchainClient.BroadcastToMainchain(header)
}
// RecordSingleBlock is used to record a single WRKChain block header to Mainchain
func (wo WrkOracle) RecordSingleBlock(height uint64) error {
return wo.recordBlock(height)
}
func (wo WrkOracle) recordBlock(height uint64) error {
wo.log.Info("getting requested WRKChain block header and recording", "moniker", wo.mainchainClient.GetWrkchainMeta().Moniker, "height", height)
header, err := wo.wrkChain.GetWrkChainBlock(height)
if err != nil {
wo.log.Error(err.Error())
return err
}
wo.mainchainClient.SetRecordFees()
return wo.mainchainClient.BroadcastToMainchain(header)
}