Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

EOS智能合约的多索引表table

西门幻雪雪vj
114 0 0
建多索引表是一种为了在RAM快速访问的方法,主要用来来缓存状态和数据。多索引表支持创建、读取、更新和删除(CRUD)业务,区块链不行(它只支持创建和读取)。
8 }* j$ Q2 ?, L4 H    多索引表提供了快速访问数据存储接口,是一种存储智能合同中使用的数据的实用的方法。在区块链记录交易信息,你应该使用多索引表存储应用程序数据。
5 `- e  Y$ j+ n7 e, |  l    使用多索引表,因为他们支持为使用的数据建立多个索引,主索引必须是uint64_t类型和唯一的,但其他的索引,可以有重复的,你可以使用多达16个,类型可以是uint64_t,uint128_t,uint256_t,doubleorlongdouble。# Q( n2 a4 l7 I9 \" `7 ~
    如果你想你需要使用一个字符串做索引,需要转换成一个整数型,将结果存储在随后索引的字段中。  h, I+ H  _% z* t' m, }% g! H
    1.创建一个结构; ?8 ]9 O' [# {9 J1 a) \
    创建一个可以存储在多索引表中的结构,并在要索引的字段上定义getter。+ V* r* M' f( j$ m! F8 A/ m' z8 X
    请记住,这些getter中必须有一个命名为primary_key(),如果没有这个,编译器eosiocpp将产生一个错误…"itcan’tfindthefieldtouseastheprimarykey"即它找不到任何一个字段被作为主键。
& t. h7 _- ~; C    如果你想要有一个以上的索引,(最多允许16个),然后为你想要索引的任何字段定义一个getter,这时这个名称就不那么重要了,因为你会把getter名称传递给typedef。( \3 X1 f3 e8 i% K# a8 y
    ///@abitable3 \# U' M0 ^4 N1 n
    structmystruct
- Q- f( g0 c/ P6 e6 P+ m    {
0 ^3 B0 E9 C+ u$ M$ x    uint64_tkey;
9 q/ }& g, {" p/ P' L, y, {; u    uint64_tsecondid;" `$ h5 T  M& {- B
    std::stringname;
1 z5 X& |3 p* m& J' k; [* y    std::stringaccount;
$ `" v% q) G6 r( H. J1 b' ^$ T    uint64_tprimary_key()const{returnkey;}//getterforprimarykey& ]8 v. h4 {4 [& e. q" e( N
    uint64_tby_id()const{returnsecondid;}//getterforadditionalkey
0 f# g( P+ x& S% q* F; }" L4 o5 D    };( |" U7 V' n. K# Z$ P
    这里还要注意两件事:
' e4 }5 n. K: d& ?; q# p+ X3 R    1.注释:
, h8 }4 w1 l9 z9 L0 b2 ?    ///@abitable* @0 K& Y: T* G: u
    编译器需要使用eosiocpp来识别要通过ABI公开该表并使其在智能合约之外可见。  R+ k! o" ^0 m( ^# ?
    2.结构名称少于12个字符,而且所有的字符都要小写字母。7 O7 s2 q  h3 _& b3 n" f: e
    2.多索引表和定义索引
! K& }3 C! D+ D- K* h5 L    定义多索引表将使用mystruct,告诉它要索引什么,以及如何获取正在索引的数据。主键将自动创建的,所以使用struct后,如果我想要一个只有一个主键的多索引表,我可以定义它为:3 [, ^) v8 p) _* {" L) u3 |% a- O/ {
    typedefeosio::multi_indexdatastore;8 X; z/ z7 ^0 k9 m/ P
    这定义了多个索引通过表名N(mystruct)和结构名mystruct。N(mystruct)会对结构名编译转换到uint64_t,使用uint64_t来标识属于多索引表的数据。
$ _$ I8 p1 F0 y" h    若要添加附加索引或辅助索引,则使用indexed_by模板作为参数,因此定义变为:0 B! P% Z( C- a7 j2 K6 Y# ~1 r) x
    typedefeosio::multi_index>>datastore;* e$ {' j4 x% w
    注意:
8 R; W3 H' E) q, s    indexed_by>
- P4 I- Y3 U' }0 S+ ?8 G    参数:$ R. t, Y$ ^( n  }  d) g& q
    字段的名称转换为整数,N(secondid)
. U9 \+ a% D& T, Y7 I+ S3 E    一个用户定义的密钥调用接口,const_mem_fun* k% W( `, @" Q! G
    来看看有三个索引的情况。
1 [$ x2 l+ H  v3 Y& D. v% J1 q$ W7 t    ///@abitable
7 m& X1 p4 G5 D2 j, Q  T2 W    structmystruct
3 h! ~  h; J( W  S    {* x" U. d1 H/ u3 s$ f( v
    uint64_tkey;
7 I9 H; ?3 \1 a& b    uint64_tsecondid;/ R7 [6 v+ R' t) W
    uint64_tanotherid;9 X% n2 i5 f, ?3 Y6 A2 o
    std::stringname;2 L0 t% ]0 t1 [8 C; G# z
    std::stringaccount;
  `: L5 g$ m( C* F6 y    uint64_tprimary_key()const{returnkey;}8 e. H& ^9 ]6 V8 B, r! X
    uint64_tby_id()const{returnsecondid;}9 |1 p6 v0 o8 `6 m# I% X- n
    uint64_tby_anotherid()const{returnanotherid;}
1 q3 L  q: w4 A0 U    };
2 G- N/ T. {& f7 }0 Q4 p* g! f, G    typedefeosio::multi_index>,indexed_by>>datastore;
% V; n# o+ e% p1 o" z5 U    更多的就不列了。
& u3 p8 F2 H3 N" j; o: X; f    这里要注意的一个重要事项是,结构名与表名的匹配,并且将出现在ABI文件中的名称遵循规则(12个字符,所有都是小写的字母)。如果它们没有遵循这个规则,则表不会通过ABI可见(当然可以通过编辑ABI文件来绕过这一点)。
9 ~6 O( F5 P5 j+ S1 D1 R4 \    3.创建定义类型的局部变量
5 T% o" I! Y* F5 P    //localinstancesofthemultiindexes
) S2 }; a5 V. w; S- b) @    pollstable_polls;: s1 ~. w1 j5 ?7 Y$ |3 |* F/ G$ `( p
    votes_votes;
3 x' S" c+ i" k4 J, F    现在我已经定义了一个带有两个索引的多索引表,我可以在我的智能合约中使用它。. I8 q& D  {0 a8 P9 Z+ g
    如下是一个智能合约使用两个索引的多索引表的例子。在这里你可以看到如何遍历表,如何在同一合约中使用两个表,我们未来将增加额外的教程,利用多索引表。  _1 }% Y2 I( ?! H) ?: @* W
    #include
0 s& j" c: G' H* f% h    usingnamespaceeosio;7 u0 L9 v3 v: _9 h* a" g
    classyouvote:publiccontract{
$ [1 |6 I7 Y' U% r- P    public:" w: {6 e9 }) T9 q2 b
    youvote(account_names):contract(s),_polls(s,s),_votes(s,s), R8 P4 {6 D0 l  m
    {}
; ^, `2 E! g: w( B! m4 a1 u( a    //publicmethodsexposedviatheABI4 z- @& h7 E1 `9 \$ c8 f5 R
    //onpollsTable6 a7 M: i' q' N1 Z; B# X5 [
    ///@abiaction
' ^0 y6 t4 O% [, C5 I) y3 L$ }    voidversion(). y$ c: I) B# [* m
    {  Z0 F, a! K, q! M0 r7 L% z2 r
    print("YouVoteversion0.01");
* L* K$ ^3 `- A& X# u, ?    };
$ B1 h5 A6 U9 _* U    ///@abiaction6 |/ s5 H1 ~6 ~+ m+ a
    voidaddpoll(account_names,std::stringpollName)
! l5 ]. ?$ k# I5 I$ R) n    {
% H4 c6 I9 C' c. ~    //require_auth(s);
# `# M- y1 a4 _2 B7 b( |, S+ ~, h    print("Addpoll",pollName);
7 p9 i" U3 J0 Q, h+ K5 U" S    //updatethetabletoincludeanewpoll0 |9 i, G5 n  n/ U9 N. \/ W
    _polls.emplace(get_self(),[&](auto&p)
  |( `4 R, s0 E4 m    {
) |* c* _8 ~: F3 R, ]( o6 t' t    p.key=_polls.available_primary_key();9 K6 p) e1 {; E, I) O* b' U
    p.pollId=_polls.available_primary_key();5 A9 Z  ~$ _, ]# V9 m- I
    p.pollName=pollName;. b$ N! @* c, ?+ a
    p.pollStatus=0;
7 m0 a" T/ D+ F    p.option="";: _/ ]$ Z+ ~  ]  `+ d& x* {2 L3 }
    p.count=0;
8 d* A0 L8 ~% M& O7 Y    });( {  v" D  }, [' _. J
    };
& |+ p% P2 N2 a: g+ C    ///@abiaction
! W2 G  J  i" k  M6 _    voidrmpoll(account_names,std::stringpollName)
0 d3 F/ F+ Y' R2 q- e    {
; M* P4 B8 g1 S, V& W    //require_auth(s);
0 n4 j5 L4 l1 G5 Z    print("Removepoll",pollName);
9 @& t2 \/ O( z; J* E    std::vectorkeysForDeletion;
+ ]8 [) ^+ ^  B1 W2 d6 q8 R    //finditemswhichareforthenamedpoll  Z- U1 S; E9 C# \$ h$ c
    for(auto&item:_polls)9 K9 y  n' |6 r! U1 h! J: a
    {, J+ Z0 e0 ~. k9 k
    if(item.pollName==pollName)
# d; h7 I* t1 H" T+ v    {
& L* h+ X; X& X- p    keysForDeletion.push_back(item.key);
8 W3 w5 Q6 R; ~. ~( J% k    }9 n# I6 u! a) F* F2 p/ a% b' I
    }3 }7 N0 [7 v% K3 v. s0 p
    //nowdeleteeachitemforthatpoll
/ b5 U1 U) }' W6 y+ Q# L! q    for(uint64_tkey:keysForDeletion)( R: \/ g2 B) U
    {* R) m  x) X- Q( @. [, J, C
    print("removefrom_polls",key);
+ D5 ?' ]; y& I. ~( }+ d    autoitr=_polls.find(key);2 W, i& L/ k* r1 T
    if(itr!=_polls.end())
; I, @, R0 b& `5 W% Q' _    {
; C6 i2 H; T' G/ v    _polls.erase(itr);, M2 |7 g2 Z0 s  S2 o- q3 ~( g* P/ ?) Z
    }2 s0 W, f: V5 R" s% E: A* t! \
    }
( D$ M, W+ d; H+ p! ?: L' m  C, i    //addremovevotes...don'tneedittheactionsarepermanentlystoredontheblockchain
+ i& |4 J! {. X; W    std::vectorkeysForDeletionFromVotes;
$ u2 m" Y: {* G2 b+ W; ?+ \    //finditemswhichareforthenamedpoll5 q5 B7 \2 Y2 T) \7 V
    for(auto&item:_votes)
4 H; }1 U0 [% ^' K+ E3 v    {5 l, z9 }+ ~6 {3 Y# w
    if(item.pollName==pollName)
" K/ h3 t+ Y* b2 v    {
# l. ]- X% y. l& k    keysForDeletionFromVotes.push_back(item.key);6 K5 J, a+ L# S( k! ?& q9 }5 `
    }
6 v: S. k. |3 a    }
, i' e& Q! c) q    //nowdeleteeachitemforthatpoll
  X5 X4 {5 K3 n; q; j9 X    for(uint64_tkey:keysForDeletionFromVotes)
6 s: g# i. q1 i    {
1 \; R+ D+ I. p- A/ q6 P    print("removefrom_votes",key);# v' A8 T" R7 ]3 O2 E. y
    autoitr=_votes.find(key);$ z# t0 H8 B- v7 P
    if(itr!=_votes.end())6 I( N  }. Z% U! o8 c$ x  L
    {
  m7 \4 w$ E1 G# ~    _votes.erase(itr);
+ y5 T# L$ ~, G) @    }
6 E* z8 [- b, E2 y; |' C4 J    }
; V* T8 U  C5 f1 o    };
  ~+ G3 O/ a# s7 Q5 U+ h  x    ///@abiaction
- U$ @. Y1 X$ p$ D/ X& x    voidstatus(std::stringpollName)
7 U' `5 @" ~" ~. m, `    {, }( ?8 l  R" k. f0 P* ^2 g% W
    print("Changepollstatus",pollName);1 T/ _- ^) p; f: u9 C
    std::vectorkeysForModify;
( m4 X( O- N3 t2 c    //finditemswhichareforthenamedpoll
9 T: S- v5 R3 ?, U* d2 h5 P  }* t    for(auto&item:_polls): N! D' L' y/ F+ B1 d1 S5 M
    {. G5 n6 F3 k1 p4 H4 L
    if(item.pollName==pollName)! y! G1 J* }( v9 m
    {
; n/ N2 `$ A! D3 m/ D. T+ z    keysForModify.push_back(item.key);" a7 j$ l8 ]! v$ `* T
    }) `, A+ O7 ?; s6 ~0 N
    }3 {7 \/ D. a2 x  D
    //nowgeteachitemandmodifythestatus
& b' K1 R' _6 [# }( a; f    for(uint64_tkey:keysForModify)
2 ]- C, C0 R8 d4 B  l    {
* i3 i" a/ [. h0 b3 i    print("modify_pollsstatus",key);' d& ^- W& O' R; A2 F% p$ {' ?
    autoitr=_polls.find(key);
8 L/ X$ Q9 m5 }8 F9 \. n    if(itr!=_polls.end())
7 o) p0 S% @& F  W    {
/ X& n. g$ j0 d) j" S/ Q4 C    _polls.modify(itr,get_self(),[&](auto&p)
! H  C1 F% G2 M* y- E8 b/ q7 g9 K$ o    {
7 f/ E, ?: Z6 h, X, a* m: x1 @7 Y    p.pollStatus=p.pollStatus+1;
. s1 ?+ B8 a2 f6 L1 E! O    });+ V: t9 e* Y- v4 U  \( @4 P
    }
" f+ `" ]# T& @7 m7 Z# _* \9 \6 {    }& O8 M& J! y. f$ M5 N& n
    };
6 {8 Q! ?& B" R6 R/ k1 `1 }3 j6 s    ///@abiaction9 I4 h: Q6 y/ ~1 c; l5 V2 n9 p
    voidstatusreset(std::stringpollName)9 {7 x# h$ z! m( ^
    {
" F7 B. h' t2 j# ?: V* M5 H    print("Resetpollstatus",pollName);
  S+ O3 v  G& k# x) J" J: A    std::vectorkeysForModify;$ U- z4 `: N( O4 S
    //findallpollitems
( ^! `5 N! v( n' j+ D9 ^    for(auto&item:_polls)
& b* ^5 _; S( i4 I# M+ F9 F4 \    {% o  n% y) X; x) z5 U' N/ {
    if(item.pollName==pollName)
' W% z/ ^% S3 ^/ o0 {, h& H2 c  d    {
) |& i; o8 q$ Z" u  y% p1 N    keysForModify.push_back(item.key);, I% L0 ^6 b* e# i
    }
2 K; x* p  l$ y    }
1 T9 V; }% H# {    //updatethestatusineachpollitem
- n# e7 W! a. N3 v    for(uint64_tkey:keysForModify), ~5 E9 Z8 D: a6 m* e
    {! W% T  x& r8 u; n9 C3 N: C; s- H# B. b
    print("modify_pollsstatus",key);4 H, j: Q1 z+ P  j. h
    autoitr=_polls.find(key);
" S* n6 P: |: f4 \    if(itr!=_polls.end())' `1 ^  Z9 q: z9 C
    {
# u# ^/ {% x$ I0 v    _polls.modify(itr,get_self(),[&](auto&p)
8 z7 `/ `9 p- l5 y6 O- K/ h    {/ ~9 P2 P; k2 [- V/ C* m. G& N
    p.pollStatus=0;& F4 J, G/ k( o: {# h4 G. B. K
    });
# m+ ?9 U6 G. }5 F9 W    }& ~) w) [; P; e* S( v+ |
    }
% E0 K, Y  \3 ]3 J" m+ ]    };0 C: y4 `* T* q8 r8 t
    ///@abiaction
6 I- Y; V( d: c1 Y    voidaddpollopt(std::stringpollName,std::stringoption)
8 s! O) z$ D  O6 {% A* {5 o    {' N; G) D" ]7 l. H
    print("Addpolloption",pollName,"option",option);* Y8 [- b' f- m
    //findthepollId,from_polls,usethistoupdatethe_pollswithanewoption
6 H0 N2 i  ]. z  _+ o) p2 O2 W' R    for(auto&item:_polls)
5 N$ l6 `. j, S+ s    {( J/ m. T5 w6 L1 t
    if(item.pollName==pollName)
" J  A4 Y4 m# W- q# c    {
- h7 v8 N+ x# @# f) ]5 l- E    //canonlyaddifthepollisnotstartedorfinished
8 j$ K" U7 i) f8 |* [    if(item.pollStatus==0)
- i& [# ~, o# Q+ u$ R+ y6 A    {9 t+ n; K$ z, s0 o* T! W
    _polls.emplace(get_self(),[&](auto&p)  V9 T9 X1 \9 s: X& F
    {: V) F9 ]' d5 u0 Z- N
    p.key=_polls.available_primary_key();
. `7 m1 y5 i0 X6 v& R+ y2 W  R+ d; s' o    p.pollId=item.pollId;
" C+ Y" U3 F" A# O% d5 ]    p.pollName=item.pollName;' J' I* z- s/ P! u+ I2 j5 |6 k* ?& ~
    p.pollStatus=0;3 T0 i7 W! a& e* x$ X. \6 H
    p.option=option;
" n* a! Y" }6 j/ _- m    p.count=0;' |4 G8 L. E; y7 M- s+ \9 d
    });
1 t2 O/ ~( g, I" D    }
6 d* Z0 t5 R4 }! a* e' a    else# A0 z$ Z' w( O* x. b! Y6 q+ o( }
    {
" Z: Q1 N) r  \' B& o( Y: Z    print("Cannotaddpolloption",pollName,"option",option,"Pollhasstartedorisfinished.");: G+ Y4 ^3 s$ O0 D
    }
$ J6 E# D3 o& e  N    break;//soyouonlyadditonce8 Y  ]: d/ K3 o! \7 _  y
    }
4 ]6 k% F  ~  G; ?' [" w) r9 k    }3 N+ F! T* J, o$ Q, D' G
    };9 ^" O9 B* l* Z
    ///@abiaction
. i- }2 i5 \! {- M    voidrmpollopt(std::stringpollName,std::stringoption)5 q! a- [8 h6 H, P: u
    {+ m' x' L+ i+ U3 J
    print("Removepolloption",pollName,"option",option);6 u* a$ c9 ~2 g. Y0 [: u
    std::vectorkeysForDeletion;6 E/ ~' @7 F: r& W- C: S: f
    //findandremovethenamedpoll4 U' R( I4 H7 r8 c
    for(auto&item:_polls)
' |+ @" M0 r. i5 F7 }( o/ v! H    {
: O, l% s& A8 u" |8 G    if(item.pollName==pollName)
2 E8 E: W: q, ^' c- d! Z4 `    {: J/ q6 W3 W' u! i  j' C, O
    keysForDeletion.push_back(item.key);
# b% f4 I0 j) T3 _9 r    }& s- U9 n1 o# m- x, k- {
    }6 m" h4 N4 p5 K# N6 ^
    for(uint64_tkey:keysForDeletion)
2 O' P3 T( ?9 G4 g6 J    {! b) ?+ ^% Y. _! o- p+ c
    print("removefrom_polls",key);
  h. P' x  o1 F0 w3 \) b- L    autoitr=_polls.find(key);
& T# H$ ^: I2 G4 u. S* N    if(itr!=_polls.end()). w7 c3 |# {- o. c, q, M# N; H4 u5 Z
    {
. X6 V, L2 p8 d* F8 `    if(itr->option==option)6 m! ^" x" u# d* K6 \. S) J8 T3 b
    {
9 c! \7 o2 L2 |5 A1 g1 H3 s    _polls.erase(itr);
0 G1 }* C3 a' o9 r$ S  L7 B    }
6 d2 s3 a, l3 z, z) f  t" F    }8 I1 L7 i; e$ j1 L. J
    }
! t: t/ ?, F! v2 ^+ I    };
& R$ B; a  V$ t6 V/ j( R    ///@abiaction
( |( O; z9 D/ x! N% ?    voidvote(std::stringpollName,std::stringoption,std::stringaccountName)
$ d2 A4 R7 P; u! A& V    {
% {0 Q7 H( E( t! @    print("votefor",option,"inpoll",pollName,"by",accountName);/ h  B* z3 e) C$ n
    //isthepollopen: d: N+ \0 S# F/ Q9 K" ]( y6 R0 [
    for(auto&item:_polls); a# Y% q1 d' {5 `# f
    {
) @8 U5 F- R2 L" O    if(item.pollName==pollName)
! T! V; d+ m  N4 B    {4 v7 q* M3 c) S% S9 @
    if(item.pollStatus!=1)
; K% F7 g# S" D# q    {
' r% E! ?# {/ Z3 A    print("Poll",pollName,"isnotopen");
, _0 {$ d2 [! X0 E    return;0 i( V6 |6 {9 r' n* Y2 K. c
    }
$ y+ j' I& v1 @) F( q    break;//onlyneedtocheckstatusonce
( g# t6 r) l0 ~) d/ n    }' v) ?! B3 }) d9 b
    }% J3 ~1 ~$ y. s5 F: l3 p% W
    //hasaccountnamealreadyvoted?& Q7 \' P# s  \8 x( y; j
    for(auto&vote:_votes)9 [8 t4 e/ {8 N- v  j) I3 H
    {. w3 {9 @5 T# y4 ?
    if(vote.pollName==pollName&&vote.account==accountName)) t' p4 @, [5 D* t- A, E
    {
" r& ~( D" T" t# f* W" t    print(accountName,"hasalreadyvotedinpoll",pollName);* r( y$ M4 m: E
    //eosio_assert(true,"AlreadyVoted");
& V( x: a$ b7 M5 g9 K3 Y5 v    return;
/ `; Q3 T  x0 T* E& f    }
1 P2 v& f8 _- n5 K" S4 a3 F& |    }
0 Z& K7 q) N& Y$ l    uint64_tpollId=99999;//getthepollIdforthe_votestable0 N: J3 N# f+ A( _) T3 |2 m
    //findthepollandtheoptionandincrementthecount; f0 _7 Q# c6 I, r) p/ `; S
    for(auto&item:_polls)
5 W& v# T% t2 b$ `7 J    {  [6 \* x+ F9 B/ \/ Z) {) w
    if(item.pollName==pollName&&item.option==option)8 m- `' z' i. ?* Y/ R% x
    {
/ k% ~2 b) _7 V9 }! g    pollId=item.pollId;//forrecordingvoteinthispoll4 g* s, O; A" v5 V) i; |
    _polls.modify(item,get_self(),[&](auto&p)+ H+ W$ X& _! `
    {
5 {' x$ y# W9 G# a3 W7 A" }4 I    p.count=p.count+1;) G3 m: ?* s) h, a3 e' A
    });; @& U, I% X/ b' }0 d1 a3 U' V
    }
) A- M6 ]2 d2 f2 _, e1 S; B) }    }
9 ?* @! w" ?+ d* |5 U    //recordthataccountNamehasvoted' t% M" q% J8 f0 z  I  d
    _votes.emplace(get_self(),[&](auto&pv)
4 X9 Q$ I) c" G7 y# \6 w0 A    {
6 G$ o# p5 M$ d3 m0 O" J8 u/ J    pv.key=_votes.available_primary_key();  l& s4 y$ X: h- T1 D
    pv.pollId=pollId;) L& {& z; k! i7 {' x' k8 ^
    pv.pollName=pollName;
5 m; @' @' D: k: |    pv.account=accountName;- f2 |4 \6 W  e1 u# K# ~
    });
3 F) U5 N3 R% C: b" D0 S0 P4 Z3 Y    };
% K+ g5 a: r, @: Y: ^% N# h2 ^* |    private:
7 M: X+ S* w( \    //createthemultiindextablestostorethedata
6 p) {, w; d( a: E% E  Q& A5 \    ///@abitable$ s1 @  N  \. {) `, ^: ~7 p
    structpoll
: d5 `. A& d& _6 K7 @    {
  r$ G/ f5 t- I; h    uint64_tkey;//primarykey) ^6 m0 ^9 S2 W! U
    uint64_tpollId;//secondkey,non-unique,thistablewillhaveduprowsforeachpollbecauseofoption
' ~% k/ K/ K$ ~, E$ l% q  Q3 w    std::stringpollName;//nameofpoll1 C: W& Y. _5 O2 S' Q
    uint8_tpollStatus=0;//stauswhere0=closed,1=open,2=finished4 P: t  x6 ?5 t* v5 p2 a
    std::stringoption;//theitemyoucanvotefor$ K9 W( }8 K/ Y6 c+ D2 v4 ?4 a
    uint32_tcount=0;//thenumberofvotesforeachitme--thistobepulledouttosepartetable.9 F- z+ a; Q; v8 L  G1 a, B+ w
    uint64_tprimary_key()const{returnkey;}/ E/ T. l2 H( b5 |
    uint64_tby_pollId()const{returnpollId;}& Z; r% e9 e/ V9 _/ i! W# g4 k
    };
7 B; o2 i$ X+ X    typedefeosio::multi_index>>pollstable;
0 Y! y, D/ `9 X/ U" ]2 M" `& r    ///@abitable
) |/ A; m+ y# r* Y9 A, y  t7 _  ?    structpollvotes
% E5 }3 `( J- j$ p, C: E    {
1 R+ h% V! ^; y, T& @    uint64_tkey;; b% J0 \( |( c9 @; N) o+ N
    uint64_tpollId;3 w( C6 Q* y  B- s
    std::stringpollName;//nameofpoll
/ Z7 u" z3 k+ @3 l* @8 ^! e) S    std::stringaccount;//thisaccounthasvoted,usethistomakesurenoonevotes>10 E: N( u! z! |
    uint64_tprimary_key()const{returnkey;}7 Z& N' u- m' f% |
    uint64_tby_pollId()const{returnpollId;}
9 e4 r& b( {, h4 c. U6 `    };. i( c- i; k+ a" W
    typedefeosio::multi_index>>votes;, _, P/ [% b: r+ J
    //localinstancesofthemultiindexes+ R  ?$ M4 y3 R! |" F$ g
    pollstable_polls;- h% {2 W" \4 h
    votes_votes;5 P8 d: u9 n+ s) B, {- q6 {" M0 V% Z
    };
4 g5 [0 ^) [7 o6 E$ g- M! Y/ o    EOSIO_ABI(youvote,(version)(addpoll)(rmpoll)(status)(statusreset)(addpollopt)(rmpollopt)(vote))+ y. m% u0 w$ X4 F! V
    注意EOSIO_ABI调用,它通过ABI公开函数,重要的是函数名与ABI函数名规则一定要匹配。
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

西门幻雪雪vj 初中生
  • 粉丝

    0

  • 关注

    0

  • 主题

    10