]>
Commit | Line | Data |
---|---|---|
3ec03ada | 1 | /****************************************************************************** |
a20f07e7 | 2 | * Copyright © 2014-2018 The SuperNET Developers. * |
3ec03ada | 3 | * * |
4 | * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * | |
5 | * the top-level directory of this distribution for the individual copyright * | |
6 | * holder information and the developer policies on copyright and licensing. * | |
7 | * * | |
8 | * Unless otherwise agreed in a custom licensing agreement, no part of the * | |
9 | * SuperNET software, including this file may be copied, modified, propagated * | |
10 | * or distributed except according to the terms contained in the LICENSE file * | |
11 | * * | |
12 | * Removal or modification of this copyright notice is prohibited. * | |
13 | * * | |
14 | ******************************************************************************/ | |
15 | ||
16 | #ifndef H_KOMODOEVENTS_H | |
17 | #define H_KOMODOEVENTS_H | |
5416af1d | 18 | #include "komodo_defs.h" |
3ec03ada | 19 | |
ab918767 | 20 | struct komodo_event *komodo_eventadd(struct komodo_state *sp,int32_t height,char *symbol,uint8_t type,uint8_t *data,uint16_t datalen) |
3ec03ada | 21 | { |
36ec445a | 22 | struct komodo_event *ep=0; uint16_t len = (uint16_t)(sizeof(*ep) + datalen); |
a20f07e7 | 23 | if ( sp != 0 && ASSETCHAINS_SYMBOL[0] != 0 ) |
36ec445a | 24 | { |
25 | portable_mutex_lock(&komodo_mutex); | |
26 | ep = (struct komodo_event *)calloc(1,len); | |
27 | ep->len = len; | |
28 | ep->height = height; | |
29 | ep->type = type; | |
30 | strcpy(ep->symbol,symbol); | |
31 | if ( datalen != 0 ) | |
32 | memcpy(ep->space,data,datalen); | |
33 | sp->Komodo_events = (struct komodo_event **)realloc(sp->Komodo_events,(1 + sp->Komodo_numevents) * sizeof(*sp->Komodo_events)); | |
34 | sp->Komodo_events[sp->Komodo_numevents++] = ep; | |
35 | portable_mutex_unlock(&komodo_mutex); | |
36 | } | |
3ec03ada | 37 | return(ep); |
38 | } | |
39 | ||
26f6fa01 | 40 | void komodo_eventadd_notarized(struct komodo_state *sp,char *symbol,int32_t height,char *dest,uint256 notarized_hash,uint256 notarized_desttxid,int32_t notarizedheight,uint256 MoM,int32_t MoMdepth) |
3ec03ada | 41 | { |
3e9fcad0 | 42 | static uint32_t counter; int32_t verified=0; char *coin; struct komodo_event_notarized N; |
daf214b1 | 43 | coin = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; |
4d068367 | 44 | if ( IS_KOMODO_NOTARY != 0 && (verified= komodo_verifynotarization(symbol,dest,height,notarizedheight,notarized_hash,notarized_desttxid)) < 0 ) |
17914f3e | 45 | { |
4d068367 | 46 | if ( counter++ < 100 ) |
c452b1d7 | 47 | printf("[%s] error validating notarization ht.%d notarized_height.%d, if on a pruned %s node this can be ignored\n",ASSETCHAINS_SYMBOL,height,notarizedheight,dest); |
17914f3e | 48 | } |
3e9fcad0 | 49 | else if ( strcmp(symbol,coin) == 0 ) |
13b64fd6 | 50 | { |
4d068367 | 51 | if ( 0 && IS_KOMODO_NOTARY != 0 && verified != 0 ) |
29da16c8 | 52 | fprintf(stderr,"validated [%s] ht.%d notarized %d\n",coin,height,notarizedheight); |
13b64fd6 | 53 | memset(&N,0,sizeof(N)); |
54 | N.blockhash = notarized_hash; | |
55 | N.desttxid = notarized_desttxid; | |
56 | N.notarizedheight = notarizedheight; | |
26f6fa01 | 57 | N.MoM = MoM; |
58 | N.MoMdepth = MoMdepth; | |
a20f07e7 | 59 | strncpy(N.dest,dest,sizeof(N.dest)-1); |
ed937645 | 60 | komodo_eventadd(sp,height,symbol,KOMODO_EVENT_NOTARIZED,(uint8_t *)&N,sizeof(N)); |
13b64fd6 | 61 | if ( sp != 0 ) |
26f6fa01 | 62 | komodo_notarized_update(sp,height,notarizedheight,notarized_hash,notarized_desttxid,MoM,MoMdepth); |
13b64fd6 | 63 | } |
3ec03ada | 64 | } |
65 | ||
c75c18fc | 66 | void komodo_eventadd_pubkeys(struct komodo_state *sp,char *symbol,int32_t height,uint8_t num,uint8_t pubkeys[64][33]) |
3ec03ada | 67 | { |
68 | struct komodo_event_pubkeys P; | |
3f8696e5 | 69 | //printf("eventadd pubkeys ht.%d\n",height); |
3ec03ada | 70 | memset(&P,0,sizeof(P)); |
71 | P.num = num; | |
72 | memcpy(P.pubkeys,pubkeys,33 * num); | |
ab918767 | 73 | komodo_eventadd(sp,height,symbol,KOMODO_EVENT_RATIFY,(uint8_t *)&P,(int32_t)(sizeof(P.num) + 33 * num)); |
c75c18fc | 74 | if ( sp != 0 ) |
75 | komodo_notarysinit(height,pubkeys,num); | |
3ec03ada | 76 | } |
77 | ||
c75c18fc | 78 | void komodo_eventadd_pricefeed(struct komodo_state *sp,char *symbol,int32_t height,uint32_t *prices,uint8_t num) |
3ec03ada | 79 | { |
80 | struct komodo_event_pricefeed F; | |
a20f07e7 | 81 | if ( num == sizeof(F.prices)/sizeof(*F.prices) ) |
82 | { | |
83 | memset(&F,0,sizeof(F)); | |
84 | F.num = num; | |
85 | memcpy(F.prices,prices,sizeof(*F.prices) * num); | |
86 | komodo_eventadd(sp,height,symbol,KOMODO_EVENT_PRICEFEED,(uint8_t *)&F,(int32_t)(sizeof(F.num) + sizeof(*F.prices) * num)); | |
87 | if ( sp != 0 ) | |
88 | komodo_pvals(height,prices,num); | |
862285c7 | 89 | } //else fprintf(stderr,"skip pricefeed[%d]\n",num); |
3ec03ada | 90 | } |
91 | ||
40d4047d | 92 | void komodo_eventadd_opreturn(struct komodo_state *sp,char *symbol,int32_t height,uint256 txid,uint64_t value,uint16_t vout,uint8_t *buf,uint16_t opretlen) |
3ec03ada | 93 | { |
a20f07e7 | 94 | struct komodo_event_opreturn O; uint8_t *opret; |
95 | if ( ASSETCHAINS_SYMBOL[0] != 0 ) | |
96 | { | |
97 | opret = (uint8_t *)calloc(1,sizeof(O) + opretlen + 16); | |
98 | O.txid = txid; | |
99 | O.value = value; | |
100 | O.vout = vout; | |
101 | memcpy(opret,&O,sizeof(O)); | |
102 | memcpy(&opret[sizeof(O)],buf,opretlen); | |
103 | O.oplen = (int32_t)(opretlen + sizeof(O)); | |
104 | komodo_eventadd(sp,height,symbol,KOMODO_EVENT_OPRETURN,opret,O.oplen); | |
105 | free(opret); | |
106 | if ( sp != 0 ) | |
107 | komodo_opreturn(height,value,buf,opretlen,txid,vout,symbol); | |
108 | } | |
c75c18fc | 109 | } |
110 | ||
ab918767 | 111 | void komodo_event_undo(struct komodo_state *sp,struct komodo_event *ep) |
c75c18fc | 112 | { |
113 | switch ( ep->type ) | |
114 | { | |
ab918767 | 115 | case KOMODO_EVENT_RATIFY: printf("rewind of ratify, needs to be coded.%d\n",ep->height); break; |
37fed603 | 116 | case KOMODO_EVENT_NOTARIZED: break; |
c75c18fc | 117 | case KOMODO_EVENT_KMDHEIGHT: |
ab918767 | 118 | if ( ep->height <= sp->SAVEDHEIGHT ) |
119 | sp->SAVEDHEIGHT = ep->height; | |
120 | break; | |
c75c18fc | 121 | case KOMODO_EVENT_PRICEFEED: |
ab918767 | 122 | // backtrack prices; |
123 | break; | |
c75c18fc | 124 | case KOMODO_EVENT_OPRETURN: |
ab918767 | 125 | // backtrack opreturns |
c75c18fc | 126 | break; |
127 | } | |
3ec03ada | 128 | } |
129 | ||
f3a1de3a | 130 | void komodo_event_rewind(struct komodo_state *sp,char *symbol,int32_t height) |
c75c18fc | 131 | { |
40d4047d | 132 | struct komodo_event *ep; |
f3a1de3a | 133 | if ( sp != 0 ) |
c75c18fc | 134 | { |
8ee93080 | 135 | if ( ASSETCHAINS_SYMBOL[0] == 0 && height <= KOMODO_LASTMINED && prevKOMODO_LASTMINED != 0 ) |
136 | { | |
137 | printf("undo KOMODO_LASTMINED %d <- %d\n",KOMODO_LASTMINED,prevKOMODO_LASTMINED); | |
138 | KOMODO_LASTMINED = prevKOMODO_LASTMINED; | |
139 | prevKOMODO_LASTMINED = 0; | |
140 | } | |
ab918767 | 141 | while ( sp->Komodo_events != 0 && sp->Komodo_numevents > 0 ) |
c75c18fc | 142 | { |
ab918767 | 143 | if ( (ep= sp->Komodo_events[sp->Komodo_numevents-1]) != 0 ) |
f3a1de3a | 144 | { |
145 | if ( ep->height < height ) | |
146 | break; | |
48a3cd18 | 147 | //printf("[%s] undo %s event.%c ht.%d for rewind.%d\n",ASSETCHAINS_SYMBOL,symbol,ep->type,ep->height,height); |
ab918767 | 148 | komodo_event_undo(sp,ep); |
149 | sp->Komodo_numevents--; | |
f3a1de3a | 150 | } |
c75c18fc | 151 | } |
152 | } | |
153 | } | |
154 | ||
e155dbe9 | 155 | void komodo_setkmdheight(struct komodo_state *sp,int32_t kmdheight,uint32_t timestamp) |
ab918767 | 156 | { |
36ec445a | 157 | if ( sp != 0 ) |
e155dbe9 | 158 | { |
36ec445a | 159 | if ( kmdheight > sp->SAVEDHEIGHT ) |
160 | { | |
161 | sp->SAVEDHEIGHT = kmdheight; | |
162 | sp->SAVEDTIMESTAMP = timestamp; | |
163 | } | |
164 | if ( kmdheight > sp->CURRENT_HEIGHT ) | |
165 | sp->CURRENT_HEIGHT = kmdheight; | |
e155dbe9 | 166 | } |
ab918767 | 167 | } |
168 | ||
e155dbe9 | 169 | void komodo_eventadd_kmdheight(struct komodo_state *sp,char *symbol,int32_t height,int32_t kmdheight,uint32_t timestamp) |
ab918767 | 170 | { |
e155dbe9 | 171 | uint32_t buf[2]; |
ab918767 | 172 | if ( kmdheight > 0 ) |
173 | { | |
e155dbe9 | 174 | buf[0] = (uint32_t)kmdheight; |
175 | buf[1] = timestamp; | |
176 | komodo_eventadd(sp,height,symbol,KOMODO_EVENT_KMDHEIGHT,(uint8_t *)buf,sizeof(buf)); | |
ab918767 | 177 | if ( sp != 0 ) |
e155dbe9 | 178 | komodo_setkmdheight(sp,kmdheight,timestamp); |
ab918767 | 179 | } |
180 | else | |
181 | { | |
a20f07e7 | 182 | //fprintf(stderr,"REWIND kmdheight.%d\n",kmdheight); |
ab918767 | 183 | kmdheight = -kmdheight; |
2b983c80 | 184 | komodo_eventadd(sp,height,symbol,KOMODO_EVENT_REWIND,(uint8_t *)&height,sizeof(height)); |
ab918767 | 185 | if ( sp != 0 ) |
186 | komodo_event_rewind(sp,symbol,height); | |
187 | } | |
188 | } | |
189 | ||
190 | ||
c75c18fc | 191 | /*void komodo_eventadd_deposit(int32_t actionflag,char *symbol,int32_t height,uint64_t komodoshis,char *fiat,uint64_t fiatoshis,uint8_t rmd160[20],bits256 kmdtxid,uint16_t kmdvout,uint64_t price) |
a20f07e7 | 192 | { |
193 | uint8_t opret[512]; uint16_t opretlen; | |
194 | komodo_eventadd_opreturn(symbol,height,KOMODO_OPRETURN_DEPOSIT,kmdtxid,komodoshis,kmdvout,opret,opretlen); | |
195 | } | |
196 | ||
197 | void komodo_eventadd_issued(int32_t actionflag,char *symbol,int32_t height,int32_t fiatheight,bits256 fiattxid,uint16_t fiatvout,bits256 kmdtxid,uint16_t kmdvout,uint64_t fiatoshis) | |
198 | { | |
199 | uint8_t opret[512]; uint16_t opretlen; | |
200 | komodo_eventadd_opreturn(symbol,height,KOMODO_OPRETURN_ISSUED,fiattxid,fiatoshis,fiatvout,opret,opretlen); | |
201 | } | |
202 | ||
203 | void komodo_eventadd_withdraw(int32_t actionflag,char *symbol,int32_t height,uint64_t komodoshis,char *fiat,uint64_t fiatoshis,uint8_t rmd160[20],bits256 fiattxid,int32_t fiatvout,uint64_t price) | |
204 | { | |
205 | uint8_t opret[512]; uint16_t opretlen; | |
206 | komodo_eventadd_opreturn(symbol,height,KOMODO_OPRETURN_WITHDRAW,fiattxid,fiatoshis,fiatvout,opret,opretlen); | |
207 | } | |
208 | ||
209 | void komodo_eventadd_redeemed(int32_t actionflag,char *symbol,int32_t height,bits256 kmdtxid,uint16_t kmdvout,int32_t fiatheight,bits256 fiattxid,uint16_t fiatvout,uint64_t komodoshis) | |
210 | { | |
211 | uint8_t opret[512]; uint16_t opretlen; | |
212 | komodo_eventadd_opreturn(symbol,height,KOMODO_OPRETURN_REDEEMED,kmdtxid,komodoshis,kmdvout,opret,opretlen); | |
213 | }*/ | |
3ec03ada | 214 | |
215 | // process events | |
a20f07e7 | 216 | // |
3501e4d2 | 217 | |
3ec03ada | 218 | #endif |