Browse Source

test

release/v0.1
jl777 9 years ago
parent
commit
2d41fc2edc
  1. 112
      iguana/iguana_ramchain.c
  2. 118
      iguana/iguana_scripts.c

112
iguana/iguana_ramchain.c

@ -374,74 +374,6 @@ uint32_t iguana_ramchain_addpkhash(struct iguana_info *coin,RAMCHAIN_FUNC,uint8_
return(pkind);
}
uint32_t iguana_ramchain_scriptencode(struct iguana_info *coin,uint8_t *Kspace,uint32_t *offsetp,int32_t type,uint8_t *script,int32_t scriptlen,uint32_t *pubkeyoffsetp)
{
uint32_t uoffset,offset = *offsetp,pubkeyoffset = *pubkeyoffsetp; int32_t plen;
if ( type == IGUANA_SCRIPT_76AC )
{
plen = bitcoin_pubkeylen(script+1);
/*if ( plen <= 0 )
{
char buf[1025];
buf[0] = 0;
for (i=0; i<33; i++)
sprintf(buf+strlen(buf),"%02x",script[1+i]);
printf("%s pubkey -> pubkeyoffset.%d offset.%d plen.%d\n",buf,pubkeyoffset,offset,plen);
}*/
if ( plen > 0 )
{
if ( pubkeyoffset == 0 )
{
if ( offset == 0 )
offset = 1;
*pubkeyoffsetp = pubkeyoffset = offset;
memcpy(&Kspace[pubkeyoffset],script+1,plen);
offset += plen;
*offsetp = offset;
return(0);
}
if ( memcmp(script+1,&Kspace[pubkeyoffset],plen) != 0 )
{
/*for (i=-1; i<=plen; i++)
printf("%02x",script[1+i]);
printf(" script arg\n");
for (i=0; i<plen; i++)
printf("%02x",Kspace[pubkeyoffset+i]);
printf(" Kspace[%d] len.%d pubkeyoffset.%d\n",offset,plen,pubkeyoffset);
printf("iguana_ramchain_scriptencode: mismatched pubkey?\n");*/
//getchar();
}
}
}
uoffset = offset;
offset += iguana_rwvarint32(1,&Kspace[offset],(void *)&scriptlen);
memcpy(&Kspace[offset],script,scriptlen);
(*offsetp) = (offset + scriptlen);
return(uoffset);
}
uint8_t *iguana_ramchain_scriptdecode(int32_t *metalenp,int32_t *scriptlenp,uint8_t *Kspace,int32_t type,uint8_t _script[IGUANA_MAXSCRIPTSIZE],uint32_t uoffset,uint32_t pubkeyoffset)
{
uint32_t plen,len = 0;
*metalenp = *scriptlenp = 0;
if ( type == IGUANA_SCRIPT_76AC && pubkeyoffset != 0 )
{
plen = bitcoin_pubkeylen(&Kspace[pubkeyoffset]);
_script[0] = 0x76;
memcpy(&_script[1],&Kspace[pubkeyoffset],plen);
_script[plen + 1] = 0xac;
*scriptlenp = plen + 2;
//printf("76AC special case\n");
return(_script);
}
if ( uoffset != 0 )
{
uoffset += iguana_rwvarint32(0,&Kspace[uoffset],(void *)scriptlenp);
*metalenp = len + *scriptlenp;
return(&Kspace[uoffset]);
} else return(0);
}
uint32_t iguana_ramchain_addunspent20(struct iguana_info *coin,struct iguana_peer *addr,RAMCHAIN_FUNC,uint64_t value,uint8_t *script,int32_t scriptlen,bits256 txid,int32_t vout,int8_t type,struct iguana_bundle *bp,uint8_t rmd160[20])
{
uint32_t unspentind; struct iguana_unspent20 *u; struct vin_info V;
@ -491,17 +423,27 @@ uint32_t iguana_ramchain_addunspent20(struct iguana_info *coin,struct iguana_pee
{
u->value = value;
u->type = type;
memcpy(u->rmd160,rmd160,sizeof(u->rmd160));
//fprintf(stderr,"type.%d scriptlen.%d bp.%p\n",type,scriptlen,bp);
if ( (u->scriptlen= scriptlen) > 0 && script != 0 && addr != 0 && addr->voutsfp != 0 )
u->scriptlen = scriptlen;
if ( scriptlen > 0 && script != 0 )
{
V.spendlen = iguana_scriptgen(coin,&V.M,&V.N,V.coinaddr,V.spendscript,0,u->rmd160,type,(const struct vin_info *)&V,vout);
if ( (V.spendlen != scriptlen || memcmp(V.spendscript,script,scriptlen) != 0) && addr != 0 && addr->voutsfp != 0 )
{
u->ipbits = (uint32_t)addr->ipbits;
u->scriptpos = (uint32_t)ftell(addr->voutsfp);
if ( fwrite(script,1,scriptlen,addr->voutsfp) != scriptlen )
printf("error writing scriptlen.%d\n",scriptlen);
else addr->dirty[0]++;
}
else
{
u->scriptpos = 0;
u->ipbits = 0;
}
} else u->scriptpos = 0;
u->txidind = ramchain->H.txidind;
memcpy(u->rmd160,rmd160,sizeof(u->rmd160));
}
return(unspentind);
}
@ -546,36 +488,6 @@ uint32_t iguana_ramchain_addunspent(struct iguana_info *coin,RAMCHAIN_FUNC,uint6
u->vout = vout, u->hdrsi = hdrsi;
u->txidind = ramchain->H.txidind, u->pkind = pkind;
u->prevunspentind = A[pkind].lastind;
/*origoffset = ramchain->H.scriptoffset;
if ( type != IGUANA_SCRIPT_STRANGE && type != IGUANA_SCRIPT_DATA && type != IGUANA_SCRIPT_OPRETURN && scriptlen > 0 && script != 0 )
{
if ( Kspace != 0 && ramchain->H.scriptoffset+scriptlen+3 <= ramchain->H.data->scriptspace-ramchain->H.stacksize )
{
if ( (u->scriptoffset= iguana_ramchain_scriptencode(coin,Kspace,&ramchain->H.scriptoffset,type,script,scriptlen,&pubkeyoffset)) > 0 || type == IGUANA_SCRIPT_76AC )
{
fprintf(stderr,"new offset.%d from scriptlen.%d pubkeyoffset.%d\n",ramchain->H.scriptoffset,scriptlen,pubkeyoffset);
}
//printf("[%d] u%d offset.%u len.%d\n",hdrsi,unspentind,u->scriptoffset,scriptlen);
} else printf("[%d] u%d Kspace.%p scriptspace overflow! %d + %d vs space.%d - stack.%d\n",hdrsi,unspentind,Kspace,ramchain->H.scriptoffset,scriptlen,ramchain->H.data->scriptspace,ramchain->H.stacksize);
checkscript = iguana_ramchain_scriptdecode(&metalen,&checklen,Kspace,u->type,_script,u->scriptoffset,P[pkind].pubkeyoffset < ramchain->H.scriptoffset ? P[pkind].pubkeyoffset : 0);
if ( checklen != scriptlen || (script != 0 && checkscript != 0 && memcmp(checkscript,script,scriptlen) != 0) )
{
//printf("create script mismatch len.%d vs %d or cmp error.%d\n",scriptlen,checklen,(script!=0&&checkscript!=0)?memcmp(checkscript,script,scriptlen):0);
type = IGUANA_SCRIPT_STRANGE;
} //else printf("RO spendscript match.%d\n",scriptlen);
}
if ( type == IGUANA_SCRIPT_DATA || type == IGUANA_SCRIPT_OPRETURN || type == IGUANA_SCRIPT_STRANGE )
{
if ( script != 0 && scriptlen > 0 )
{
u->scriptoffset = origoffset;
origoffset += iguana_rwvarint32(1,&Kspace[origoffset],(void *)&scriptlen);
memcpy(&Kspace[origoffset],script,scriptlen);
ramchain->H.scriptoffset = origoffset + scriptlen;
}
}
else if ( type == IGUANA_SCRIPT_76AC && pubkeyoffset != 0 && P[pkind].pubkeyoffset == 0 )
P[pkind].pubkeyoffset = pubkeyoffset;*/
u->ipbits = ipbits;
u->scriptlen = scriptlen;
u->scriptpos = fpos;

118
iguana/iguana_scripts.c

@ -452,6 +452,7 @@ int32_t iguana_scriptgen(struct iguana_info *coin,int32_t *Mp,int32_t *nump,char
{
uint8_t addrtype; char rmd160str[41],pubkeystr[256]; int32_t plen,i,m,n,flag = 0,scriptlen = 0;
m = n = 1;
if ( asmstr != 0 )
asmstr[0] = 0;
if ( type == IGUANA_SCRIPT_76A988AC || type == IGUANA_SCRIPT_76AC || type == IGUANA_SCRIPT_P2SH )
{
@ -464,6 +465,7 @@ int32_t iguana_scriptgen(struct iguana_info *coin,int32_t *Mp,int32_t *nump,char
switch ( type )
{
case IGUANA_SCRIPT_NULL:
if ( asmstr != 0 )
strcpy(asmstr,txi == 0 ? "coinbase " : "PoSbase ");
flag++;
coinaddr[0] = 0;
@ -472,19 +474,23 @@ int32_t iguana_scriptgen(struct iguana_info *coin,int32_t *Mp,int32_t *nump,char
if ( (plen= bitcoin_pubkeylen(vp->signers[0].pubkey)) < 0 )
return(0);
init_hexbytes_noT(pubkeystr,(uint8_t *)vp->signers[0].pubkey,plen);
if ( asmstr != 0 )
sprintf(asmstr,"OP_DUP %s OP_CHECKSIG // %s",pubkeystr,coinaddr);
scriptlen = bitcoin_pubkeyspend(script,0,(uint8_t *)vp->signers[0].pubkey);
//printf("[%02x] scriptlen.%d (%s)\n",vp->signers[0].pubkey[0],scriptlen,asmstr);
break;
case IGUANA_SCRIPT_76A988AC:
if ( asmstr != 0 )
sprintf(asmstr,"OP_DUP OP_HASH160 %s OP_EQUALVERIFY OP_CHECKSIG // %s",rmd160str,coinaddr);
scriptlen = bitcoin_standardspend(script,0,rmd160);
break;
case IGUANA_SCRIPT_P2SH:
if ( asmstr != 0 )
sprintf(asmstr,"OP_HASH160 %s OP_EQUAL // %s",rmd160str,coinaddr);
scriptlen = bitcoin_p2shspend(script,0,rmd160);
break;
case IGUANA_SCRIPT_OPRETURN:
if ( asmstr != 0 )
strcpy(asmstr,"OP_RETURN ");
flag++;
break;
@ -495,10 +501,12 @@ int32_t iguana_scriptgen(struct iguana_info *coin,int32_t *Mp,int32_t *nump,char
case IGUANA_SCRIPT_1of2: m = 1, n = 2; break;
case IGUANA_SCRIPT_MSIG: m = vp->M, n = vp->N; break;
case IGUANA_SCRIPT_DATA:
if ( asmstr != 0 )
strcpy(asmstr,"DATA ONLY");
flag++;
break;
case IGUANA_SCRIPT_STRANGE:
if ( asmstr != 0 )
strcpy(asmstr,"STRANGE SCRIPT ");
flag++;
break;
@ -507,21 +515,28 @@ int32_t iguana_scriptgen(struct iguana_info *coin,int32_t *Mp,int32_t *nump,char
if ( n > 1 )
{
scriptlen = bitcoin_MofNspendscript(rmd160,script,0,vp);
if ( asmstr != 0 )
sprintf(asmstr,"%d ",m);
for (i=0; i<n; i++)
{
if ( (plen= bitcoin_pubkeylen(vp->signers[i].pubkey)) > 0 )
{
init_hexbytes_noT(asmstr + strlen(asmstr),(uint8_t *)vp->signers[i].pubkey,plen);
if ( asmstr != 0 )
strcat(asmstr," ");
} else strcat(asmstr,"NOPUBKEY ");
}
else if ( asmstr != 0 )
strcat(asmstr,"NOPUBKEY ");
}
if ( asmstr != 0 )
{
sprintf(asmstr + strlen(asmstr),"%d // M.%d of N.%d [",n,m,n);
for (i=0; i<n; i++)
sprintf(asmstr + strlen(asmstr),"%s%s",vp->signers[i].coinaddr,i<n-1?" ":"");
strcat(asmstr,"]\n");
}
if ( flag != 0 && vp->spendlen > 0 )
}
if ( flag != 0 && asmstr != 0 && vp->spendlen > 0 )
init_hexbytes_noT(asmstr + strlen(asmstr),(uint8_t *)vp->spendscript,vp->spendlen);
*Mp = m, *nump = n;
return(scriptlen);
@ -1069,7 +1084,6 @@ int32_t iguana_metascript(struct iguana_info *coin,RAMCHAIN_FUNC,struct iguana_s
return(metalen);
}
int32_t iguana_scriptspaceraw(struct iguana_info *coin,int32_t *scriptspacep,int32_t *sigspacep,int32_t *pubkeyspacep,struct iguana_msgtx *txarray,int32_t txn_count)
{
uint32_t i,j,sigspace,suffixlen,scriptspace,pubkeyspace,p2shspace,p2shsize,sigsize,pubkeysize,type,scriptlen; //struct iguana_spend256 *s; struct iguana_unspent20 *u;
@ -1163,4 +1177,102 @@ int32_t iguana_ramchain_scriptspace(struct iguana_info *coin,int32_t *sigspacep,
return(scriptspace + p2shspace);
}
uint32_t iguana_ramchain_scriptencode(struct iguana_info *coin,uint8_t *Kspace,uint32_t *offsetp,int32_t type,uint8_t *script,int32_t scriptlen,uint32_t *pubkeyoffsetp)
{
uint32_t uoffset,offset = *offsetp,pubkeyoffset = *pubkeyoffsetp; int32_t plen;
if ( type == IGUANA_SCRIPT_76AC )
{
plen = bitcoin_pubkeylen(script+1);
/*if ( plen <= 0 )
{
char buf[1025];
buf[0] = 0;
for (i=0; i<33; i++)
sprintf(buf+strlen(buf),"%02x",script[1+i]);
printf("%s pubkey -> pubkeyoffset.%d offset.%d plen.%d\n",buf,pubkeyoffset,offset,plen);
}*/
if ( plen > 0 )
{
if ( pubkeyoffset == 0 )
{
if ( offset == 0 )
offset = 1;
*pubkeyoffsetp = pubkeyoffset = offset;
memcpy(&Kspace[pubkeyoffset],script+1,plen);
offset += plen;
*offsetp = offset;
return(0);
}
if ( memcmp(script+1,&Kspace[pubkeyoffset],plen) != 0 )
{
/*for (i=-1; i<=plen; i++)
printf("%02x",script[1+i]);
printf(" script arg\n");
for (i=0; i<plen; i++)
printf("%02x",Kspace[pubkeyoffset+i]);
printf(" Kspace[%d] len.%d pubkeyoffset.%d\n",offset,plen,pubkeyoffset);
printf("iguana_ramchain_scriptencode: mismatched pubkey?\n");*/
//getchar();
}
}
}
uoffset = offset;
offset += iguana_rwvarint32(1,&Kspace[offset],(void *)&scriptlen);
memcpy(&Kspace[offset],script,scriptlen);
(*offsetp) = (offset + scriptlen);
return(uoffset);
}
uint8_t *iguana_ramchain_scriptdecode(int32_t *metalenp,int32_t *scriptlenp,uint8_t *Kspace,int32_t type,uint8_t _script[IGUANA_MAXSCRIPTSIZE],uint32_t uoffset,uint32_t pubkeyoffset)
{
uint32_t plen,len = 0;
*metalenp = *scriptlenp = 0;
if ( type == IGUANA_SCRIPT_76AC && pubkeyoffset != 0 )
{
plen = bitcoin_pubkeylen(&Kspace[pubkeyoffset]);
_script[0] = 0x76;
memcpy(&_script[1],&Kspace[pubkeyoffset],plen);
_script[plen + 1] = 0xac;
*scriptlenp = plen + 2;
//printf("76AC special case\n");
return(_script);
}
if ( uoffset != 0 )
{
uoffset += iguana_rwvarint32(0,&Kspace[uoffset],(void *)scriptlenp);
*metalenp = len + *scriptlenp;
return(&Kspace[uoffset]);
} else return(0);
}
/*origoffset = ramchain->H.scriptoffset;
if ( type != IGUANA_SCRIPT_STRANGE && type != IGUANA_SCRIPT_DATA && type != IGUANA_SCRIPT_OPRETURN && scriptlen > 0 && script != 0 )
{
if ( Kspace != 0 && ramchain->H.scriptoffset+scriptlen+3 <= ramchain->H.data->scriptspace-ramchain->H.stacksize )
{
if ( (u->scriptoffset= iguana_ramchain_scriptencode(coin,Kspace,&ramchain->H.scriptoffset,type,script,scriptlen,&pubkeyoffset)) > 0 || type == IGUANA_SCRIPT_76AC )
{
fprintf(stderr,"new offset.%d from scriptlen.%d pubkeyoffset.%d\n",ramchain->H.scriptoffset,scriptlen,pubkeyoffset);
}
//printf("[%d] u%d offset.%u len.%d\n",hdrsi,unspentind,u->scriptoffset,scriptlen);
} else printf("[%d] u%d Kspace.%p scriptspace overflow! %d + %d vs space.%d - stack.%d\n",hdrsi,unspentind,Kspace,ramchain->H.scriptoffset,scriptlen,ramchain->H.data->scriptspace,ramchain->H.stacksize);
checkscript = iguana_ramchain_scriptdecode(&metalen,&checklen,Kspace,u->type,_script,u->scriptoffset,P[pkind].pubkeyoffset < ramchain->H.scriptoffset ? P[pkind].pubkeyoffset : 0);
if ( checklen != scriptlen || (script != 0 && checkscript != 0 && memcmp(checkscript,script,scriptlen) != 0) )
{
//printf("create script mismatch len.%d vs %d or cmp error.%d\n",scriptlen,checklen,(script!=0&&checkscript!=0)?memcmp(checkscript,script,scriptlen):0);
type = IGUANA_SCRIPT_STRANGE;
} //else printf("RO spendscript match.%d\n",scriptlen);
}
if ( type == IGUANA_SCRIPT_DATA || type == IGUANA_SCRIPT_OPRETURN || type == IGUANA_SCRIPT_STRANGE )
{
if ( script != 0 && scriptlen > 0 )
{
u->scriptoffset = origoffset;
origoffset += iguana_rwvarint32(1,&Kspace[origoffset],(void *)&scriptlen);
memcpy(&Kspace[origoffset],script,scriptlen);
ramchain->H.scriptoffset = origoffset + scriptlen;
}
}
else if ( type == IGUANA_SCRIPT_76AC && pubkeyoffset != 0 && P[pkind].pubkeyoffset == 0 )
P[pkind].pubkeyoffset = pubkeyoffset;*/
#endif

Loading…
Cancel
Save