Skip to content

Commit 91d6eb0

Browse files
weight twap contributions inversely with confidence interval; added testnet keys
1 parent f2d4343 commit 91d6eb0

File tree

7 files changed

+402
-40
lines changed

7 files changed

+402
-40
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ add_executable( test_publish pctest/test_publish.cpp )
9898
target_link_libraries( test_publish ${PC_DEP} )
9999
add_executable( test_qset pctest/test_qset.cpp )
100100
target_link_libraries( test_qset ${PC_DEP} )
101+
add_executable( test_twap pctest/test_twap.cpp )
102+
target_link_libraries( test_twap ${PC_DEP} )
101103
add_executable( slots_info pctest/slots_info.cpp )
102104
target_link_libraries( slots_info ${PC_DEP} )
103105
add_executable( leader_stats pctest/leader_stats.cpp )

pctest/test_twap.cpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
char heap_start[8192];
2+
#define PC_HEAP_START (heap_start)
3+
4+
#include <stdint.h>
5+
#include <stdlib.h>
6+
#include <stdio.h>
7+
#include <oracle/oracle.h>
8+
#include <oracle/upd_aggregate.h>
9+
#include <pc/mem_map.hpp>
10+
#include <math.h>
11+
#include <ctype.h>
12+
#include <iostream>
13+
14+
using namespace pc;
15+
16+
int usage()
17+
{
18+
std::cerr << "usage: test_twap <twap.csv>" << std::endl;
19+
return 1;
20+
}
21+
22+
class csv_parser
23+
{
24+
public:
25+
csv_parser( const char *buf, size_t len )
26+
: ptr_( buf ),
27+
eol_( buf ),
28+
end_( &buf[len] ),
29+
first_( false ) {
30+
}
31+
bool fetch_line() {
32+
for( ptr_ = eol_;;++ptr_ ) {
33+
if ( ptr_ == end_ ) return false;
34+
if ( *ptr_ != '\n' && *ptr_ != '\r' ) break;
35+
}
36+
for( eol_ = ptr_;ptr_ != end_ && *eol_ != '\n' && *eol_ != '\r';++eol_ );
37+
first_ = true;
38+
return true;
39+
}
40+
void fetch( int64_t& val ) {
41+
if ( ptr_ != eol_ && *ptr_ == ',' && !first_ ) {
42+
++ptr_;
43+
}
44+
first_ = false;
45+
const char *ptxt = ptr_;
46+
for( ; ptr_ != eol_ && *ptr_ != ','; ++ptr_ );
47+
char *etxt[1];
48+
etxt[0] = (char*)ptr_;
49+
val = strtol( ptxt, etxt, 10 );
50+
}
51+
private:
52+
const char *ptr_;
53+
const char *eol_;
54+
const char *end_;
55+
bool first_;
56+
};
57+
58+
int main( int argc,char** argv )
59+
{
60+
if ( argc < 2 ) {
61+
return usage();
62+
}
63+
64+
// read price file
65+
mem_map mf;
66+
std::string file = argv[1];
67+
mf.set_file(file );
68+
if ( !mf.init() ) {
69+
std::cerr << "test_qset: failed to read file[" << file << "]"
70+
<< std::endl;
71+
return 1;
72+
}
73+
pc_price_t px[1];
74+
__builtin_memset( px, 0, sizeof( pc_price_t ) );
75+
uint64_t slot = 1000;
76+
px->last_slot_ = slot;
77+
px->agg_.pub_slot_ = slot;
78+
px->num_ = 0;
79+
upd_aggregate( px, slot+1 );
80+
pc_qset_t *qs = qset_new( px->expo_ );
81+
82+
// skip first line
83+
csv_parser cp( mf.data(), mf.size( ));
84+
85+
// update twap for each update
86+
std::cout << "price,conf,expo,nslots,twap,twac" << std::endl;
87+
for( cp.fetch_line(); cp.fetch_line(); ) {
88+
int64_t nslots = 0, price = 0, conf = 0, expo = 0;
89+
cp.fetch( price );
90+
cp.fetch( conf );
91+
cp.fetch( expo );
92+
cp.fetch( nslots );
93+
px->expo_ = expo;
94+
px->agg_.price_ = price;
95+
px->agg_.conf_ = conf;
96+
upd_twap( px, nslots, qs );
97+
std::cout << price << ','
98+
<< conf << ','
99+
<< expo << ','
100+
<< nslots << ','
101+
<< px->twap_.val_ << ','
102+
<< px->twac_.val_
103+
<< std::endl;
104+
}
105+
106+
return 0;
107+
}

pctest/test_twap.txt

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
price,conf,expo,nslots,twap,twac
2+
100,10,0,5000,100,10
3+
147,48,0,1,108,16
4+
191,74,0,1,116,22
5+
235,99,0,1,124,27
6+
100,10,0,1,114,20
7+
147,48,0,1,117,22
8+
191,13,0,1,133,20
9+
235,22,0,1,145,20
10+
100,10,0,1,136,18
11+
147,15,0,1,137,18
12+
191,74,0,1,138,19
13+
235,99,0,1,140,20
14+
235,99,0,1,142,22
15+
235,99,0,1,143,23
16+
235,99,0,1,145,24
17+
235,99,0,1,146,25
18+
235,99,0,1,148,27
19+
235,99,0,1,149,28
20+
235,99,0,1,150,29
21+
235,99,0,1,152,30
22+
235,99,0,1,153,31
23+
235,99,0,20,154,32
24+
235,99,0,1,155,33
25+
235,99,0,1,156,34
26+
235,99,0,1,158,35
27+
235,99,0,1,159,36
28+
235,99,0,1,160,37
29+
235,99,0,1,161,37
30+
235,99,0,1,162,38
31+
235,99,0,40,163,39
32+
235,99,0,1,164,40
33+
235,99,0,1,165,41
34+
235,99,0,1,165,41
35+
235,99,0,1,166,42
36+
235,99,0,1,167,43
37+
235,99,0,1,168,43
38+
235,99,0,1,169,44
39+
235,99,0,1,170,45
40+
235,15,0,1,174,43
41+
235,99,0,1,175,43
42+
235,20,0,1,178,42
43+
235,99,0,1,179,43
44+
235,50,0,1,180,43
45+
235,99,0,1,180,43
46+
235,99,0,1,181,44
47+
235,99,0,1,182,44
48+
235,99,0,1,182,45
49+
235,33,0,1,184,45
50+
235,33,0,1,185,44
51+
235,33,0,1,186,44
52+
235,33,0,1,188,44
53+
235,33,0,1,189,43
54+
235,99,0,1,189,44
55+
235,99,0,1,190,44
56+
235,99,0,1,190,45
57+
235,99,0,1,190,45
58+
235,99,0,1,191,46
59+
235,99,0,1,191,46
60+
235,99,0,1,191,46
61+
235,99,0,1,192,47
62+
235,99,0,1,192,47
63+
235,99,0,1,192,48
64+
235,99,0,1,193,48
65+
235,99,0,1,193,48
66+
235,99,0,1,193,49
67+
235,99,0,1,194,49
68+
235,99,0,1,194,50
69+
235,99,0,1,194,50
70+
235,99,0,1,195,50
71+
235,99,0,1,195,51
72+
235,99,0,1,195,51
73+
235,99,0,1,195,51
74+
235,99,0,1,196,52
75+
235,99,0,1,196,52
76+
235,99,0,1,196,52
77+
235,99,0,1,197,53
78+
235,99,0,1,197,53
79+
235,99,0,1,197,53
80+
235,99,0,1,197,54
81+
235,99,0,1,198,54
82+
235,99,0,1,198,54
83+
235,99,0,1,198,55
84+
235,99,0,1,198,55
85+
235,99,0,1,199,55
86+
235,99,0,1,199,55
87+
235,99,0,1,199,56
88+
235,99,0,1,199,56
89+
235,99,0,1,200,56
90+
235,99,0,1,200,57
91+
235,99,0,1,200,57
92+
235,99,0,1,200,57
93+
235,99,0,1,200,57
94+
235,99,0,1,201,58
95+
235,99,0,1,201,58
96+
235,99,0,1,201,58
97+
235,99,0,1,201,58
98+
235,99,0,1,202,59
99+
235,99,0,1,202,59
100+
235,99,0,1,202,59
101+
235,99,0,1,202,59
102+
180,5,0,1,200,53
103+
180,5,0,1,198,49
104+
180,5,0,1,196,45
105+
180,5,0,1,195,42
106+
180,5,0,1,194,39
107+
180,5,0,1,193,36
108+
180,5,0,1,192,34
109+
180,5,0,1,191,32
110+
180,5,0,1,190,31
111+
180,5,0,1,190,29
112+
180,5,0,1,189,28
113+
180,5,0,1,189,27
114+
180,5,0,1,188,26
115+
180,5,0,1,188,25
116+
180,5,0,1,188,24
117+
180,5,0,1,187,23
118+
180,5,0,1,187,22
119+
180,5,0,1,187,22
120+
180,5,0,1,186,21
121+
180,5,0,1,186,21

pctest/test_twap_1.csv

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
price,conf,expo,nslots
2+
100,10,0,5000
3+
147,48,0,1
4+
191,74,0,1
5+
235,99,0,1
6+
100,10,0,1
7+
147,48,0,1
8+
191,13,0,1
9+
235,22,0,1
10+
100,10,0,1
11+
147,15,0,1
12+
191,74,0,1
13+
235,99,0,1
14+
235,99,0,1
15+
235,99,0,1
16+
235,99,0,1
17+
235,99,0,1
18+
235,99,0,1
19+
235,99,0,1
20+
235,99,0,1
21+
235,99,0,1
22+
235,99,0,1
23+
235,99,0,20
24+
235,99,0,1
25+
235,99,0,1
26+
235,99,0,1
27+
235,99,0,1
28+
235,99,0,1
29+
235,99,0,1
30+
235,99,0,1
31+
235,99,0,40
32+
235,99,0,1
33+
235,99,0,1
34+
235,99,0,1
35+
235,99,0,1
36+
235,99,0,1
37+
235,99,0,1
38+
235,99,0,1
39+
235,99,0,1
40+
235,15,0,1
41+
235,99,0,1
42+
235,20,0,1
43+
235,99,0,1
44+
235,50,0,1
45+
235,99,0,1
46+
235,99,0,1
47+
235,99,0,1
48+
235,99,0,1
49+
235,33,0,1
50+
235,33,0,1
51+
235,33,0,1
52+
235,33,0,1
53+
235,33,0,1
54+
235,99,0,1
55+
235,99,0,1
56+
235,99,0,1
57+
235,99,0,1
58+
235,99,0,1
59+
235,99,0,1
60+
235,99,0,1
61+
235,99,0,1
62+
235,99,0,1
63+
235,99,0,1
64+
235,99,0,1
65+
235,99,0,1
66+
235,99,0,1
67+
235,99,0,1
68+
235,99,0,1
69+
235,99,0,1
70+
235,99,0,1
71+
235,99,0,1
72+
235,99,0,1
73+
235,99,0,1
74+
235,99,0,1
75+
235,99,0,1
76+
235,99,0,1
77+
235,99,0,1
78+
235,99,0,1
79+
235,99,0,1
80+
235,99,0,1
81+
235,99,0,1
82+
235,99,0,1
83+
235,99,0,1
84+
235,99,0,1
85+
235,99,0,1
86+
235,99,0,1
87+
235,99,0,1
88+
235,99,0,1
89+
235,99,0,1
90+
235,99,0,1
91+
235,99,0,1
92+
235,99,0,1
93+
235,99,0,1
94+
235,99,0,1
95+
235,99,0,1
96+
235,99,0,1
97+
235,99,0,1
98+
235,99,0,1
99+
235,99,0,1
100+
235,99,0,1
101+
235,99,0,1
102+
180,5,0,1
103+
180,5,0,1
104+
180,5,0,1
105+
180,5,0,1
106+
180,5,0,1
107+
180,5,0,1
108+
180,5,0,1
109+
180,5,0,1
110+
180,5,0,1
111+
180,5,0,1
112+
180,5,0,1
113+
180,5,0,1
114+
180,5,0,1
115+
180,5,0,1
116+
180,5,0,1
117+
180,5,0,1
118+
180,5,0,1
119+
180,5,0,1
120+
180,5,0,1
121+
180,5,0,1

program/src/oracle/oracle.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ typedef struct pc_qset
170170
uint64_t decay_[1+PC_MAX_SEND_LATENCY];
171171
uint64_t fact_[PC_FACTOR_SIZE];
172172
uint32_t num_;
173+
int32_t expo_;
173174
} pc_qset_t;
174175

175176
// command enumeration

0 commit comments

Comments
 (0)