Skip to content

Commit

Permalink
improve changeSet topo verification (deroproject#137)
Browse files Browse the repository at this point in the history
* missing variable assignation

* add comments, better verification
  • Loading branch information
Slixe committed Sep 10, 2023
1 parent ea277eb commit bfc5bc3
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 94 deletions.
78 changes: 44 additions & 34 deletions cmd/dero-wallet-cli/easymenu_post_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,26 @@

package main

import "io"
import "os"
import "time"
import "fmt"
import "errors"
import "runtime"
import "strings"

import "path/filepath"
import "encoding/json"

import "github.com/chzyer/readline"

import "github.com/deroproject/derohe/rpc"
import "github.com/deroproject/derohe/globals"
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"time"

"github.com/chzyer/readline"
"github.com/deroproject/derohe/cryptography/crypto"
"github.com/deroproject/derohe/globals"
"github.com/deroproject/derohe/rpc"
"github.com/deroproject/derohe/transaction"
)

//import "github.com/deroproject/derohe/address"

import "github.com/deroproject/derohe/cryptography/crypto"
import "github.com/deroproject/derohe/transaction"

// handle menu if a wallet is currently opened
func display_easymenu_post_open_command(l *readline.Instance) {
w := l.Stderr()
Expand Down Expand Up @@ -126,12 +125,10 @@ func handle_easymenu_post_open_command(l *readline.Instance, line string) (proce

if !wallet.IsRegistered() {

fmt.Fprintf(l.Stderr(), "Wallet address : "+color_green+"%s"+color_white+" is going to be registered.This is a pre-condition for using the online chain.It will take few seconds to register.\n", wallet.GetAddress())

// at this point we must send the registration transaction
fmt.Fprintf(l.Stderr(), "Wallet address : "+color_green+"%s"+color_white+" is going to be registered.Pls wait till the account is registered.\n", wallet.GetAddress())

fmt.Fprintf(l.Stderr(), "This will take a couple of minutes.Please wait....\n")
fmt.Fprintf(l.Stderr(), "Wallet address : "+color_green+"%s"+color_white+" is going to be registered. Please wait till the account is registered.", wallet.GetAddress())
fmt.Fprintf(l.Stderr(), "This is a pre-condition POW for using the online chain.")
fmt.Fprintf(l.Stderr(), "This will take a couple of minutes. Please wait....\n")

var reg_tx *transaction.Transaction

Expand Down Expand Up @@ -179,21 +176,34 @@ func handle_easymenu_post_open_command(l *readline.Instance, line string) (proce
break
}

scid, err := ReadSCID(l)
if err != nil {
logger.Error(err, "error reading SCID")
break
}

a, err := ReadAddress(l, wallet)
if err != nil {
logger.Error(err, "error reading address")
break
}

var amount_to_transfer uint64
// Request SCID from integrated address or from input
var scid crypto.Hash
if a.Arguments != nil && a.Arguments.HasValue(rpc.RPC_ASSET, rpc.DataHash) {
scid = a.Arguments.Value(rpc.RPC_ASSET, rpc.DataHash).(crypto.Hash)
logger.Info("Address has a integrated SCID", "scid", scid)
} else {
scid, err = ReadSCID(l)
if err != nil {
logger.Error(err, "error reading SCID")
break
}
}

amount_str := read_line_with_prompt(l, fmt.Sprintf("Enter token amount to transfer in SCID (max TODO): "))
var amount_to_transfer uint64
max_balance, _ := wallet.Get_Balance_scid(scid)
max_str := fmt.Sprintf("%d", max_balance)
if scid.IsZero() {
max_str = globals.FormatMoney(max_balance)
} // TODO else digits based on token standard

amount_str := read_line_with_prompt(l, fmt.Sprintf("Enter token amount to transfer (max %s): ", max_str))
// TODO digits based on token standard
if amount_str == "" {
amount_str = ".00001"
}
Expand All @@ -204,7 +214,7 @@ func handle_easymenu_post_open_command(l *readline.Instance, line string) (proce
}

if ConfirmYesNoDefaultNo(l, "Confirm Transaction (y/N)") {
tx, err := wallet.TransferPayload0([]rpc.Transfer{rpc.Transfer{SCID: scid, Amount: amount_to_transfer, Destination: a.String()}}, 0, false, rpc.Arguments{}, 0, false) // empty SCDATA
tx, err := wallet.TransferPayload0([]rpc.Transfer{{SCID: scid, Amount: amount_to_transfer, Destination: a.String()}}, 0, false, rpc.Arguments{}, 0, false) // empty SCDATA

if err != nil {
logger.Error(err, "Error while building Transaction")
Expand Down Expand Up @@ -270,7 +280,7 @@ func handle_easymenu_post_open_command(l *readline.Instance, line string) (proce

if a.Arguments.Has(rpc.RPC_COMMENT, rpc.DataString) { // but only it is present
logger.Info("Integrated Message", "comment", a.Arguments.Value(rpc.RPC_COMMENT, rpc.DataString))
arguments = append(arguments, rpc.Argument{rpc.RPC_COMMENT, rpc.DataString, a.Arguments.Value(rpc.RPC_COMMENT, rpc.DataString)})
arguments = append(arguments, rpc.Argument{Name: rpc.RPC_COMMENT, DataType: rpc.DataString, Value: a.Arguments.Value(rpc.RPC_COMMENT, rpc.DataString)})
}
}

Expand Down Expand Up @@ -369,7 +379,7 @@ func handle_easymenu_post_open_command(l *readline.Instance, line string) (proce

//src_port := uint64(0xffffffffffffffff)

tx, err := wallet.TransferPayload0([]rpc.Transfer{rpc.Transfer{Amount: amount_to_transfer, Destination: a.String(), Payload_RPC: arguments}}, 0, false, rpc.Arguments{}, 0, false) // empty SCDATA
tx, err := wallet.TransferPayload0([]rpc.Transfer{{Amount: amount_to_transfer, Destination: a.String(), Payload_RPC: arguments}}, 0, false, rpc.Arguments{}, 0, false) // empty SCDATA

if err != nil {
logger.Error(err, "Error while building Transaction")
Expand Down
27 changes: 23 additions & 4 deletions cmd/dero-wallet-cli/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,24 @@ func handle_prompt_command(l *readline.Instance, line string) {
fallthrough
case "balance": // give user his balance
balance_unlocked, locked_balance := wallet.Get_Balance_Rescan()
fmt.Fprintf(l.Stderr(), "DERO Balance : "+color_green+"%s"+color_white+"\n", globals.FormatMoney(locked_balance+balance_unlocked))
fmt.Fprintf(l.Stderr(), "DERO Balance: "+color_green+"%s"+color_white+"\n", globals.FormatMoney(locked_balance+balance_unlocked))

line_parts := line_parts[1:] // remove first part

switch len(line_parts) {
case 0:
//logger.Error(err,"not implemented")
addr := wallet.GetAddress().String()
for scid := range wallet.GetAccount().EntriesNative {
if !scid.IsZero() {
balance, _, err := wallet.GetDecryptedBalanceAtTopoHeight(scid, -1, addr)
if err != nil {
logger.Error(err, "error during Sc balance", "scid", scid.String())
} else {
// TODO digits token standard
fmt.Fprintf(l.Stderr(), "SCID %s Balance: "+color_green+"%d"+color_white+"\n\n", scid, balance)
}
}
}
break

case 1: // scid balance
Expand All @@ -110,7 +121,7 @@ func handle_prompt_command(l *readline.Instance, line string) {
if err != nil {
logger.Error(err, "error during Sc balance", "scid", scid.String())
} else {
fmt.Fprintf(l.Stderr(), "SCID %s Balance : "+color_green+"%s"+color_white+"\n\n", line_parts[0], globals.FormatMoney(balance))
fmt.Fprintf(l.Stderr(), "SCID %s Balance: "+color_green+"%s"+color_white+"\n\n", line_parts[0], globals.FormatMoney(balance))
}

case 2: // scid balance at topoheight
Expand Down Expand Up @@ -305,6 +316,15 @@ func handle_prompt_command(l *readline.Instance, line string) {
logger.Info("Menu mode enabled")
case "i8", "integrated_address": // user wants a random integrated address 8 bytes
a := wallet.GetRandomIAddress8()
if ConfirmYesNoDefaultNo(l, "Do you want to set a specific SCID ? (y/N)") {
scid, err := ReadSCID(l)
if err != nil {
logger.Error(err, "Error reading SCID")
break
}
a.Arguments = append(a.Arguments, rpc.Argument{Name: rpc.RPC_ASSET, DataType: rpc.DataHash, Value: scid})
}

fmt.Fprintf(l.Stderr(), "Wallet integrated address : "+color_green+"%s"+color_white+"\n", a.String())
fmt.Fprintf(l.Stderr(), "Embedded Arguments : "+color_green+"%s"+color_white+"\n", a.Arguments)

Expand Down Expand Up @@ -573,7 +593,6 @@ func ReadSCID(l *readline.Instance) (a crypto.Hash, err error) {
l.SetPrompt(fmt.Sprintf("%sEnter SCID: ", color))
} else {
l.SetPrompt(fmt.Sprintf("%sEnter SCID: ", color))

}

l.Refresh()
Expand Down
16 changes: 12 additions & 4 deletions p2p/rpc_changeset.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,29 @@ const max_request_topoheights = 50
func (c *Connection) ChangeSet(request ChangeList, response *Changes) (err error) {
defer handle_connection_panic(c)
if len(request.TopoHeights) < 1 || len(request.TopoHeights) > max_request_topoheights { // we are expecting 1 block or 1 tx
c.logger.V(1).Info("malformed object request received, banning peer", "request", request)
c.logger.V(1).Info("malformed object request received, banning peer", "request", request)
c.exit()
return nil
}

c.update(&request.Common) // update common information

previous_topo := int64(49) // used to verify the topo heights are in order
previous_topo := request.TopoHeights[0] // used to verify the topo heights are in order
// first requested topo can't be higher than chain AND can't be lower than 10 (because of connection.TopoHeight-50-max_request_topoheights < 10)
if previous_topo > chain.Load_TOPO_HEIGHT() || previous_topo < 10 {
c.logger.V(1).Info("malformed object request received, banning peer", "request", request)
c.exit()
return fmt.Errorf("invalid topo height for change set request (chain topo = %d, first topo requested = %d)", chain.Load_TOPO_HEIGHT(), previous_topo)
}

for _, topo := range request.TopoHeights {
// 50 is the minimum
// Check for well formed requested
if topo <= previous_topo || previous_topo+1 != topo {
c.logger.V(1).Info("malformed object request received, banning peer", "request", request)
c.exit()
return fmt.Errorf("invalid topo height for change set request")
return fmt.Errorf("invalid topo height for change set request (current = %d, previous = %d)", topo, previous_topo)
}
previous_topo = topo

var cbl Complete_Block

Expand Down
25 changes: 14 additions & 11 deletions rpc/rpc.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package rpc

import "fmt"
import "time"
import "sort"
import "encoding/json"

import "github.com/fxamacker/cbor/v2"
import "github.com/deroproject/derohe/cryptography/crypto"
import (
"encoding/json"
"fmt"
"sort"
"time"

"github.com/deroproject/derohe/cryptography/crypto"
"github.com/fxamacker/cbor/v2"
)

// this package defines interfaces and necessary glue code Digital Network, it exposes and provides encrypted RPC calls over DERO chain

Expand Down Expand Up @@ -360,10 +362,11 @@ func (args *Arguments) Sort() {

const RPC_DESTINATION_PORT = "D" // mandatory,uint64, used for ID of type uint64
const RPC_SOURCE_PORT = "S" // mandatory,uint64, used for ID
const RPC_VALUE_TRANSFER = "V" //uint64, this is representation and is only readable, value is never transferred
const RPC_COMMENT = "C" //optional,string, used for display MSG to user
const RPC_EXPIRY = "E" //optional,time used for Expiry for this service call
const RPC_REPLYBACK_ADDRESS = "R" //this is mandatory this is an address,otherwise how will otherside respond
const RPC_VALUE_TRANSFER = "V" // uint64, this is representation and is only readable, value is never transferred
const RPC_COMMENT = "C" // optional,string, used for display MSG to user
const RPC_EXPIRY = "E" // optional,time used for Expiry for this service call
const RPC_REPLYBACK_ADDRESS = "R" // this is mandatory this is an address,otherwise how will otherside respond
const RPC_ASSET = "A" // this is optional, a SCID to inform which asset we want to receive, by default DERO
//RPC will include own address so as the other enc can respond

const RPC_NEEDS_REPLYBACK_ADDRESS = "N" //optional, uint64
Expand Down
54 changes: 27 additions & 27 deletions walletapi/daemon_communication.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,38 @@ package walletapi
*/
//import "io"
//import "os"
import "fmt"
import "time"
import "sync"
import "bytes"
import "math/big"
import (
"bytes"
"context"
"encoding/hex"
"fmt"
"math/big"
"runtime/debug"
"strings"
"sync"
"time"

"github.com/creachadair/jrpc2"
"github.com/deroproject/derohe/block"
"github.com/deroproject/derohe/config"
"github.com/deroproject/derohe/cryptography/bn256"
"github.com/deroproject/derohe/cryptography/crypto"
"github.com/deroproject/derohe/errormsg"
"github.com/deroproject/derohe/globals"
"github.com/deroproject/derohe/rpc"
"github.com/deroproject/derohe/transaction"
)

//import "bufio"
import "strings"
import "context"

//import "runtime"
//import "compress/gzip"
import "encoding/hex"

import "runtime/debug"

//import "github.com/vmihailenco/msgpack"

//import "github.com/gorilla/websocket"
//import "github.com/mafredri/cdp/rpcc"

import "github.com/deroproject/derohe/rpc"
import "github.com/deroproject/derohe/block"
import "github.com/deroproject/derohe/config"
import "github.com/deroproject/derohe/globals"
import "github.com/deroproject/derohe/cryptography/crypto"
import "github.com/deroproject/derohe/errormsg"
import "github.com/deroproject/derohe/transaction"
import "github.com/deroproject/derohe/cryptography/bn256"

import "github.com/creachadair/jrpc2"

// this global variable should be within wallet structure
var Connected bool = false

Expand Down Expand Up @@ -198,16 +198,15 @@ func (w *Wallet_Memory) sync_loop() {
continue
}

var zerohash crypto.Hash
if len(w.account.EntriesNative) == 0 {
if err := w.Sync_Wallet_Memory_With_Daemon(); err != nil {
logger.Error(err, "wallet syncing err")
}
} else {
for k := range w.account.EntriesNative {
err := w.Sync_Wallet_Memory_With_Daemon_internal(k)
if k == zerohash && err != nil {
logger.Error(err, "wallet syncing err")
if err != nil {
globals.Logger.V(3).Error(err, "Error while syncing SCID", "scid", k)
}
}
}
Expand Down Expand Up @@ -259,18 +258,19 @@ func (w *Wallet_Memory) Sync_Wallet_Memory_With_Daemon_internal(scid crypto.Hash

if _, _, _, e, err := w.GetEncryptedBalanceAtTopoHeight(scid, -1, w.GetAddress().String()); err == nil {

//fmt.Printf("data '%s' previous '%s' scid %s\n",w.account.Balance_Result[scid].Data , previous,scid)
//fmt.Printf("data '%s' previous '%s' scid %s\n", w.account.Balance_Result[scid].Data, previous, scid)
if w.getEncryptedBalanceresult(scid).Data != previous {
b := w.DecodeEncryptedBalanceNow(e) // try to decode balance

if scid.IsZero() {
w.account.Balance_Mature = b
}
w.Lock()
w.account.Balance[scid] = b
w.Unlock()
w.SyncHistory(scid) // also update statement
} else {

}

w.save_if_disk() // save wallet
} else {
return err
Expand Down
Loading

0 comments on commit bfc5bc3

Please sign in to comment.