Skip to content

Commit

Permalink
Base newsp (#119)
Browse files Browse the repository at this point in the history
* add Expenders

* add RegisterOrUpdateSminer_V2

* update p2p to v0.1.6

* add CertIdleSpace

* add ReplaceIdleSpace

* add QueryChallenge_V2

* add SubmitIdleProof

* add SubmitServiceProof

* update type

* update type

* update file and example

* update example

* update RetrieveFile
  • Loading branch information
AstaFrode authored Jul 18, 2023
1 parent f025aaf commit 068e4e8
Show file tree
Hide file tree
Showing 9 changed files with 691 additions and 190 deletions.
205 changes: 205 additions & 0 deletions chain/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,208 @@ func (c *Sdk) ReportProof(idlesigma, servicesigma string) (string, error) {
}
}
}

func (c *Sdk) QueryChallenge_V2() (pattern.ChallengeInfo_V2, error) {
defer func() {
if err := recover(); err != nil {
log.Println(utils.RecoverError(err))
}
}()
var data pattern.ChallengeInfo_V2

if !c.GetChainState() {
return data, pattern.ERR_RPC_CONNECTION
}

key, err := types.CreateStorageKey(c.metadata, pattern.AUDIT, pattern.ChALLENGESNAPSHOT)
if err != nil {
return data, errors.Wrap(err, "[CreateStorageKey]")
}

ok, err := c.api.RPC.State.GetStorageLatest(key, &data)
if err != nil {
return data, errors.Wrap(err, "[GetStorageLatest]")
}
if !ok {
return data, pattern.ERR_RPC_EMPTY_VALUE
}
return data, nil
}

func (c *Sdk) SubmitIdleProof(idleProve pattern.FileHash) (string, error) {
c.lock.Lock()
defer func() {
c.lock.Unlock()
if err := recover(); err != nil {
log.Println(utils.RecoverError(err))
}
}()

var (
txhash string
accountInfo types.AccountInfo
)

if !c.GetChainState() {
return txhash, pattern.ERR_RPC_CONNECTION
}

call, err := types.NewCall(c.metadata, pattern.TX_AUDIT_SUBMITIDLEPROOF, idleProve)
if err != nil {
return txhash, errors.Wrap(err, "[NewCall]")
}

key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey)
if err != nil {
return txhash, errors.Wrap(err, "[CreateStorageKey]")
}

ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo)
if err != nil {
return txhash, errors.Wrap(err, "[GetStorageLatest]")
}
if !ok {
return txhash, pattern.ERR_RPC_EMPTY_VALUE
}

o := types.SignatureOptions{
BlockHash: c.genesisHash,
Era: types.ExtrinsicEra{IsMortalEra: false},
GenesisHash: c.genesisHash,
Nonce: types.NewUCompactFromUInt(uint64(accountInfo.Nonce)),
SpecVersion: c.runtimeVersion.SpecVersion,
Tip: types.NewUCompactFromUInt(0),
TransactionVersion: c.runtimeVersion.TransactionVersion,
}

ext := types.NewExtrinsic(call)

// Sign the transaction
err = ext.Sign(c.keyring, o)
if err != nil {
return txhash, errors.Wrap(err, "[Sign]")
}

// Do the transfer and track the actual status
sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext)
if err != nil {
c.SetChainState(false)
return txhash, errors.Wrap(err, "[SubmitAndWatchExtrinsic]")
}
defer sub.Unsubscribe()

timeout := time.NewTimer(c.packingTime)
defer timeout.Stop()

for {
select {
case status := <-sub.Chan():
if status.IsInBlock {
events := event.EventRecords{}
txhash, _ = codec.EncodeToHex(status.AsInBlock)
h, err := c.api.RPC.State.GetStorageRaw(c.keyEvents, status.AsInBlock)
if err != nil {
return txhash, errors.Wrap(err, "[GetStorageRaw]")
}
err = types.EventRecordsRaw(*h).DecodeEventRecords(c.metadata, &events)
if err != nil || len(events.Audit_SubmitProof) > 0 {
return txhash, nil
}
return txhash, errors.New(pattern.ERR_Failed)
}
case err = <-sub.Err():
return txhash, errors.Wrap(err, "[sub]")
case <-timeout.C:
return txhash, pattern.ERR_RPC_TIMEOUT
}
}
}

func (c *Sdk) SubmitServiceProof(serviceProof []types.U8) (string, error) {
c.lock.Lock()
defer func() {
c.lock.Unlock()
if err := recover(); err != nil {
log.Println(utils.RecoverError(err))
}
}()

var (
txhash string
accountInfo types.AccountInfo
)

if !c.GetChainState() {
return txhash, pattern.ERR_RPC_CONNECTION
}

call, err := types.NewCall(c.metadata, pattern.TX_AUDIT_SUBMITSERVICEPROOF, serviceProof)
if err != nil {
return txhash, errors.Wrap(err, "[NewCall]")
}

key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey)
if err != nil {
return txhash, errors.Wrap(err, "[CreateStorageKey]")
}

ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo)
if err != nil {
return txhash, errors.Wrap(err, "[GetStorageLatest]")
}
if !ok {
return txhash, pattern.ERR_RPC_EMPTY_VALUE
}

o := types.SignatureOptions{
BlockHash: c.genesisHash,
Era: types.ExtrinsicEra{IsMortalEra: false},
GenesisHash: c.genesisHash,
Nonce: types.NewUCompactFromUInt(uint64(accountInfo.Nonce)),
SpecVersion: c.runtimeVersion.SpecVersion,
Tip: types.NewUCompactFromUInt(0),
TransactionVersion: c.runtimeVersion.TransactionVersion,
}

ext := types.NewExtrinsic(call)

// Sign the transaction
err = ext.Sign(c.keyring, o)
if err != nil {
return txhash, errors.Wrap(err, "[Sign]")
}

// Do the transfer and track the actual status
sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext)
if err != nil {
c.SetChainState(false)
return txhash, errors.Wrap(err, "[SubmitAndWatchExtrinsic]")
}
defer sub.Unsubscribe()

timeout := time.NewTimer(c.packingTime)
defer timeout.Stop()

for {
select {
case status := <-sub.Chan():
if status.IsInBlock {
events := event.EventRecords{}
txhash, _ = codec.EncodeToHex(status.AsInBlock)
h, err := c.api.RPC.State.GetStorageRaw(c.keyEvents, status.AsInBlock)
if err != nil {
return txhash, errors.Wrap(err, "[GetStorageRaw]")
}
err = types.EventRecordsRaw(*h).DecodeEventRecords(c.metadata, &events)
if err != nil || len(events.Audit_SubmitProof) > 0 {
return txhash, nil
}
return txhash, errors.New(pattern.ERR_Failed)
}
case err = <-sub.Err():
return txhash, errors.Wrap(err, "[sub]")
case <-timeout.C:
return txhash, pattern.ERR_RPC_TIMEOUT
}
}
}
Loading

0 comments on commit 068e4e8

Please sign in to comment.