-
Notifications
You must be signed in to change notification settings - Fork 6
/
mastercoin.go
228 lines (176 loc) · 5.24 KB
/
mastercoin.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package mscd
import (
_ "errors"
"github.com/conformal/btcdb"
_"log"
"math"
_ "github.com/conformal/btcscript"
"github.com/conformal/btcutil"
_ "github.com/conformal/btcwire"
"github.com/mastercoin-MSC/mscutil"
"github.com/mastercoin-MSC/mscrpc"
_"fmt"
"os"
"os/user"
"path"
)
const (
Debug = true
blockChannelQueueLength = 50
// The height of the blocks until we reach the block which first included
// a Tx to the exodus address (i.e. mark of mastercoin's beginning)
ExodusBlockHeight = 249498
)
var MastercoinBlockChannel chan *btcutil.Block
func QueueMessageForBlockChannel(block *btcutil.Block) {
MastercoinBlockChannel <- block
}
type MastercoinServer struct {
// Main quit channel. This is used internally to determine whether
// certain process need to quit
quit chan bool
// Server shutdown channel.
shutdownChan chan bool
// Message parser
msgParser *MsgParser
// Rpc server
rpcServ mscrpc.RpcServer
btcdb btcdb.Db
db *mscutil.LDBDatabase
FundraiserHandler *FundraiserHandler
SimpleSendHandler *SimpleSendHandler
}
func NewMastercoinServer(btcdb btcdb.Db) *MastercoinServer {
mscutil.Logger.Println("Creating mastercoin server")
MastercoinBlockChannel = make(chan *btcutil.Block, blockChannelQueueLength)
// TODO: Rename this to replay database once we start reimporting
db, err := mscutil.NewLDBDatabase("db")
bDb, err := mscutil.NewLDBDatabase("bdb")
if err != nil {
panic("Could not create database")
}
s := &MastercoinServer{
quit: make(chan bool),
shutdownChan: make(chan bool),
btcdb: btcdb,
db: db,
rpcServ: mscrpc.NewJsonRpcServer(),
FundraiserHandler: &FundraiserHandler{db: bDb},
SimpleSendHandler: &SimpleSendHandler{db: bDb},
}
parser := NewMsgParser(s.btcdb, s)
s.msgParser = parser
return s
}
// Process block takes care of sorting out transactions with exodus output.
// At this point it doesn't matter whether the Txs are valid or not. Validation
// is done at the proper handlers.
func (s *MastercoinServer) ProcessBlock(block *btcutil.Block) error {
// Gather transactions from this block which had an exodus output
txPack := &mscutil.TxPack{
Txs: mscutil.GetExodusTransactions(block),
Time: block.MsgBlock().Header.Timestamp.Unix(),
Height: block.Height(),
}
// Update Exodus Vesting
//
// balance = ((1-(0.5 ** time_difference)) * 5631623576222 .round(8)
if len(txPack.Txs) > 0 {
serializedData, err := txPack.Serialize()
if err != nil {
return err
}
s.db.CreateTxPack(txPack.Height, serializedData)
mscutil.Logger.Println("Mastercoin data found at block with height:", txPack.Height)
// Queue the slice of transactions for further processing by the
// message parser.
_ = s.msgParser.ProcessTransactions(txPack)
}
return nil
}
// The inbound block handler
func (s *MastercoinServer) inboundBlockHandler() {
mscutil.Logger.Println("Inbound block handler started")
out:
for {
select {
case block := <-MastercoinBlockChannel:
// Process the current block, check for Msc Txs
err := s.ProcessBlock(block)
if err != nil {
mscutil.Logger.Println("Error processing block:", err.Error())
continue
}
case <-s.quit:
break out
}
}
// Drain the block channel of any remaining blocks.
clean:
for {
select {
case <-MastercoinBlockChannel:
// Continue
default:
break clean
}
}
}
func (s *MastercoinServer) Stop() {
// Close the channel and invoking the channel
close(s.quit)
s.db.Close()
mscutil.Logger.Println("Stopped successfull")
}
func GetPath(p string) string {
usr, _ := user.Current()
return path.Join(usr.HomeDir, ".mastercoin", p)
}
func (s *MastercoinServer) Start(playback bool) {
mscutil.Logger.Println("Started mastercoin server")
// Start the message parser before the block handler input handler
s.msgParser.Start()
// Start up the rpc server
go s.rpcServ.Start()
if playback{
mscutil.Logger.Println("Starting playback")
mscutil.Logger.Println("Removing database to start fresh")
path := GetPath("bdb")
err := os.RemoveAll(path)
if err != nil {
mscutil.Logger.Fatal(err)
}
iter := s.db.GetDb().NewIterator(nil)
for iter.Next() {
// Remember that the contents of the returned slice should not be modified, and
// only valid until the next call to Next.
value := iter.Value()
newPack := &mscutil.TxPack{}
newPack.Deserialize(value)
mscutil.Logger.Println("\n####### Processing block with height:", newPack.Height, "#############")
_ = s.msgParser.ProcessTransactions(newPack)
}
iter.Release()
// This is where we compare fundraiser consensus
// success := TestBalances(s.FundraiserHandler.db)
}else{
// Start the block inbound processing handler
go s.inboundBlockHandler()
}
}
func (s *MastercoinServer) WaitForShutdown() {
<-s.shutdownChan
}
// TODO: When Simple Sends are all synced up; check rounding balance here
func CalculateMscDevCoins(time int64) float64 {
timeDiff := float64(time - 1377993874) / 31556926
powr := math.Pow(0.5, timeDiff)
return (1 - powr ) * 56316.23576222
}
// Mastercoin main loop. Takes care of listening on the maistercoin block channel
// and calls the appropriate methods
func MastercoinMain(btcdb btcdb.Db) {
// Main server instance
server := NewMastercoinServer(btcdb)
server.Start(false)
}