DERO-HE STARGATE Testnet Release38

This commit is contained in:
Captain 2022-01-06 04:11:51 +00:00
parent 063b692a43
commit 9f67daeb86
No known key found for this signature in database
GPG Key ID: 18CDB3ED5E85D2D4
18 changed files with 127 additions and 62 deletions

View File

@ -39,6 +39,7 @@ var hasherPool = sync.Pool{
type MiniBlock struct {
// below 3 fields are serialized into single byte
Version uint8 // 1 byte // lower 5 bits (0,1,2,3,4)
HighDiff bool // bit 4 // triggers high diff
Final bool // bit 5
PastCount uint8 // previous count // bits 6,7
@ -76,6 +77,10 @@ func (mbl MiniBlock) String() string {
fmt.Fprintf(r, " Final ")
}
if mbl.HighDiff {
fmt.Fprintf(r, " HighDiff ")
}
if mbl.PastCount == 1 {
fmt.Fprintf(r, " Past [%08x]", mbl.Past[0])
} else {
@ -108,7 +113,7 @@ func (mbl *MiniBlock) GetPoWHash() (hash crypto.Hash) {
}
func (mbl *MiniBlock) SanityCheck() error {
if mbl.Version >= 31 {
if mbl.Version >= 16 {
return fmt.Errorf("version not supported")
}
if mbl.PastCount > 2 {
@ -133,11 +138,15 @@ func (mbl *MiniBlock) Serialize() (result []byte) {
}
var b bytes.Buffer
if mbl.Final {
b.WriteByte(mbl.Version | mbl.PastCount<<6 | 0x20)
} else {
b.WriteByte(mbl.Version | mbl.PastCount<<6)
versionbyte := mbl.Version | mbl.PastCount<<6
if mbl.HighDiff {
versionbyte |= 0x10
}
if mbl.Final {
versionbyte |= 0x20
}
b.WriteByte(versionbyte)
binary.Write(&b, binary.BigEndian, mbl.Timestamp)
@ -164,11 +173,14 @@ func (mbl *MiniBlock) Deserialize(buf []byte) (err error) {
return fmt.Errorf("Expected %d bytes. Actual %d", MINIBLOCK_SIZE, len(buf))
}
if mbl.Version = buf[0] & 0x1f; mbl.Version != 1 {
if mbl.Version = buf[0] & 0xf; mbl.Version != 1 {
return fmt.Errorf("unknown version '%d'", mbl.Version)
}
mbl.PastCount = buf[0] >> 6
if buf[0]&0x10 > 0 {
mbl.HighDiff = true
}
if buf[0]&0x20 > 0 {
mbl.Final = true
}

View File

@ -169,6 +169,10 @@ func (chain *Blockchain) VerifyMiniblockPoW(bl *block.Block, mbl block.MiniBlock
logger.Panicf("Difficuly mismatch between big and uint64 diff ")
}*/
if mbl.HighDiff {
block_difficulty.Mul(block_difficulty, new(big.Int).SetUint64(config.MINIBLOCK_HIGHDIFF))
}
if CheckPowHashBig(PoW, block_difficulty) == true {
if chain.cache_enabled {
chain.cache_IsMiniblockPowValid.Add(fmt.Sprintf("%s", cachekey), true) // set in cache

View File

@ -103,7 +103,7 @@ func (pool *Mempool) HouseKeeping(height uint64) {
pool.txs.Range(func(k, value interface{}) bool {
txhash := k.(crypto.Hash)
v := value.(*mempool_object)
if height >= (v.Tx.Height + 10) { // if we have moved 10 heights, chances of reorg are almost nil
if height >= (v.Tx.Height) { // if we have moved 10 heights, chances of reorg are almost nil
delete_list = append(delete_list, txhash)
}
return true

View File

@ -332,8 +332,8 @@ func (chain *Blockchain) Create_new_miner_block(miner_address rpc.Address) (cbl
}
if mbls := chain.MiniBlocks.GetAllMiniBlocks(key); len(mbls) > 0 {
if uint64(len(mbls)) > config.BLOCK_TIME-1 {
mbls = mbls[:config.BLOCK_TIME-1]
if uint64(len(mbls)) > config.BLOCK_TIME-config.MINIBLOCK_HIGHDIFF {
mbls = mbls[:config.BLOCK_TIME-config.MINIBLOCK_HIGHDIFF]
}
bl.MiniBlocks = mbls
}
@ -360,11 +360,12 @@ func ConvertBlockToMiniblock(bl block.Block, miniblock_miner_address rpc.Address
mbl.Past[i] = binary.BigEndian.Uint32(bl.Tips[i][:])
}
if uint64(len(bl.MiniBlocks)) != config.BLOCK_TIME-1 {
if uint64(len(bl.MiniBlocks)) < config.BLOCK_TIME-config.MINIBLOCK_HIGHDIFF {
miner_address_hashed_key := graviton.Sum(miniblock_miner_address.Compressed())
copy(mbl.KeyHash[:], miner_address_hashed_key[:])
} else {
mbl.Final = true
mbl.HighDiff = true
block_header_hash := sha3.Sum256(bl.Serialize()) // note here this block is not present
for i := range mbl.KeyHash {
mbl.KeyHash[i] = block_header_hash[i]
@ -477,14 +478,14 @@ func (chain *Blockchain) Accept_new_block(tstamp uint64, miniblock_blob []byte)
}
// lets try to check pow to detect whether the miner is cheating
if !chain.VerifyMiniblockPoW(&bl, mbl) {
logger.V(1).Error(err, "Error ErrInvalidPoW ")
if !chain.simulator && !chain.VerifyMiniblockPoW(&bl, mbl) {
logger.V(1).Error(err, "Error ErrInvalidPoW")
err = errormsg.ErrInvalidPoW
return
}
if !mbl.Final {
var miner_hash crypto.Hash
copy(miner_hash[:], mbl.KeyHash[:])
if !chain.IsAddressHashValid(true, miner_hash) {

View File

@ -23,17 +23,19 @@ import "fmt"
import "encoding/binary"
import "github.com/deroproject/derohe/block"
import "github.com/deroproject/derohe/config"
import "github.com/deroproject/derohe/cryptography/crypto"
import "golang.org/x/crypto/sha3"
const miniblock_genesis_distance = 0
const miniblock_normal_distance = 2
// last miniblock must be extra checked for corruption/attacks
func (chain *Blockchain) Verify_MiniBlocks_HashCheck(cbl *block.Complete_Block) (err error) {
last_mini_block := cbl.Bl.MiniBlocks[len(cbl.Bl.MiniBlocks)-1]
if !last_mini_block.HighDiff {
return fmt.Errorf("corrupted block")
}
if !last_mini_block.Final {
return fmt.Errorf("corrupted block")
}
@ -70,11 +72,16 @@ func Verify_MiniBlocks(bl block.Block) (err error) {
final_count++
}
}
if final_count != 1 {
if final_count < 1 {
err = fmt.Errorf("No final miniblock")
return
}
if uint64(len(bl.MiniBlocks)) != (config.BLOCK_TIME - config.MINIBLOCK_HIGHDIFF + 1) {
err = fmt.Errorf("incorrect number of miniblocks expected %d actual %d", config.BLOCK_TIME-config.MINIBLOCK_HIGHDIFF+1, len(bl.MiniBlocks))
return
}
// check whether the genesis blocks are all equal
for _, mbl := range bl.MiniBlocks {
@ -90,7 +97,9 @@ func Verify_MiniBlocks(bl block.Block) (err error) {
if binary.BigEndian.Uint32(bl.Tips[0][:]) != mbl.Past[0] {
return fmt.Errorf("MiniBlock has invalid tip")
}
} else if len(bl.Tips) == 2 {
} else {
panic("we only support 1 tips")
} /*else if len(bl.Tips) == 2 {
if binary.BigEndian.Uint32(bl.Tips[0][:]) != mbl.Past[0] {
return fmt.Errorf("MiniBlock has invalid tip")
}
@ -101,8 +110,8 @@ func Verify_MiniBlocks(bl block.Block) (err error) {
return fmt.Errorf("MiniBlock refers to same tip twice")
}
} else {
panic("we only support 2 tips")
}
panic("we only support 1 tips")
}*/
}
return nil

View File

@ -226,9 +226,6 @@ func (chain *Blockchain) execute_sc_function(w_sc_tree *Tree_Wrapper, data_tree
}
if err == nil && result.Type == dvm.Uint64 && result.ValueUint64 == 0 { // confirm the changes
for k, v := range tx_store.Keys {
chain.StoreSCValue(data_tree, scid, k.MarshalBinaryPanic(), v.MarshalBinaryPanic())
}
for k, v := range tx_store.RawKeys {
chain.StoreSCValue(data_tree, scid, []byte(k), v)
}

View File

@ -468,10 +468,16 @@ func (chain *Blockchain) process_transaction_sc(cache map[crypto.Hash]*graviton.
if _, ok := globals.Arguments["--debug"]; ok && globals.Arguments["--debug"] != nil && chain.simulator {
logger.V(1).Info("Writing", "txid", txhash, "scid", scid, "key", fmt.Sprintf("%x", k), "value", fmt.Sprintf("%x", v))
}
if len(v) == 0 {
if err = w_sc_data_tree.tree.Delete([]byte(k)); err != nil {
return
}
} else {
if err = w_sc_data_tree.tree.Put([]byte(k), v); err != nil {
return
}
}
}
for k, v := range w_sc_tree.entries {
if err = w_sc_tree.tree.Put([]byte(k), v); err != nil {

View File

@ -436,7 +436,7 @@ func getwork(wallet_address string) {
our_height = int64(job.Height)
Difficulty = job.Difficultyuint64
//fmt.Printf("recv: %s", result)
//fmt.Printf("recv: %+v diff %d\n", result, Difficulty)
goto wait_for_another_job
}

View File

@ -31,6 +31,7 @@ import "crypto/x509"
import "encoding/pem"
import "github.com/deroproject/derohe/globals"
import "github.com/deroproject/derohe/config"
import "github.com/deroproject/derohe/rpc"
import "github.com/deroproject/graviton"
import "github.com/go-logr/logr"
@ -93,6 +94,9 @@ func SendJob() {
params.Height = bl.Height
params.Prev_Hash = prev_hash
if mbl.HighDiff {
diff.Mul(diff, new(big.Int).SetUint64(config.MINIBLOCK_HIGHDIFF))
}
params.Difficultyuint64 = diff.Uint64()
params.Difficulty = diff.String()
client_list_mutex.Lock()

View File

@ -24,7 +24,7 @@
<td>Block size [kB]:</td><td>{{.block.Size}}</td>
</tr>
<tr>
<td>nonce:</td><td>{{.block.Nonce}}</td>
<td>Miniblocks:</td><td>{{len .block.Block.MiniBlocks}}</td>
<td>Total fees:</td><td>{{.block.Fees}}</td>
<td>No of txs:</td><td>{{.block.Tx_Count}}</td>
</tr>

View File

@ -100,11 +100,10 @@ func Mine_block_single(chain *blockchain.Blockchain, miner_address rpc.Address)
// return fmt.Errorf("this function can only run in simulator mode")
//}
for i := uint64(0); i < config.BLOCK_TIME; i++ {
for i := uint64(0); i < config.BLOCK_TIME-config.MINIBLOCK_HIGHDIFF; i++ {
bl, mbl, _, _, err := chain.Create_new_block_template_mining(miner_address)
if err != nil {
logger.Error(err, "err while request block template")
return err
}
if _, blid, _, err = chain.Accept_new_block(bl.Timestamp, mbl.Serialize()); blid.IsZero() || err != nil {

View File

@ -30,6 +30,7 @@ import "github.com/deroproject/derohe/cryptography/crypto"
// this is in millisecs
const BLOCK_TIME = uint64(18)
const BLOCK_TIME_MILLISECS = BLOCK_TIME * 1000
const MINIBLOCK_HIGHDIFF = 9
// note we are keeping the tree name small for disk savings, since they will be stored n times (atleast or archival nodes)
// this is used by graviton
@ -105,7 +106,7 @@ var Mainnet = CHAIN_CONFIG{Name: "mainnet",
}
var Testnet = CHAIN_CONFIG{Name: "testnet", // testnet will always have last 3 bytes 0
Network_ID: uuid.FromBytesOrNil([]byte{0x59, 0xd7, 0xf7, 0xe9, 0xdd, 0x48, 0xd5, 0xfd, 0x13, 0x0a, 0xf6, 0xe0, 0x76, 0x00, 0x00, 0x00}),
Network_ID: uuid.FromBytesOrNil([]byte{0x59, 0xd7, 0xf7, 0xe9, 0xdd, 0x48, 0xd5, 0xfd, 0x13, 0x0a, 0xf6, 0xe0, 0x77, 0x00, 0x00, 0x00}),
GETWORK_Default_Port: 10100,
P2P_Default_Port: 40401,
RPC_Default_Port: 40402,

View File

@ -20,4 +20,4 @@ import "github.com/blang/semver/v4"
// right now it has to be manually changed
// do we need to include git commitsha??
var Version = semver.MustParse("3.4.98-1.DEROHE.STARGATE+25112021")
var Version = semver.MustParse("3.4.99-0.DEROHE.STARGATE+03012022")

View File

@ -54,6 +54,7 @@ func init() {
func_table["load"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), Cost: 1, Ptr: dvm_load}}
func_table["exists"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), Cost: 1, Ptr: dvm_exists}}
func_table["store"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), Cost: 1, Ptr: dvm_store}}
func_table["delete"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), Cost: 1, Ptr: dvm_delete}}
func_table["random"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), Cost: 1, Ptr: dvm_random}}
func_table["scid"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), Cost: 1, Ptr: dvm_scid}}
func_table["blid"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), Cost: 1, Ptr: dvm_blid}}
@ -137,6 +138,10 @@ func (dvm *DVM_Interpreter) Store(key Variable, value Variable) {
dvm.State.Store.Store(DataKey{SCID: dvm.State.Chain_inputs.SCID, Key: key}, value)
}
func (dvm *DVM_Interpreter) Delete(key Variable) {
dvm.State.Store.Delete(DataKey{SCID: dvm.State.Chain_inputs.SCID, Key: key})
}
func dvm_version(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result interface{}) {
if len(expr.Args) != 1 { // expression without limit
panic("version expects 1 parameters")
@ -209,6 +214,26 @@ func dvm_store(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result i
return true, nil
}
func dvm_delete(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result interface{}) {
if len(expr.Args) != 1 {
panic("Delete function expects 2 variables as parameter")
}
key_eval := dvm.eval(expr.Args[0])
var key Variable
switch k := key_eval.(type) {
case uint64:
key = Variable{Type: Uint64, ValueUint64: k}
case string:
key = Variable{Type: String, ValueString: k}
default:
panic("This variable cannot be deleted")
}
dvm.Delete(key)
return true, nil
}
func dvm_random(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result interface{}) {
if len(expr.Args) >= 2 {
panic("RANDOM function expects 0 or 1 number as parameter")
@ -289,7 +314,7 @@ func dvm_update_sc_code(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool,
code_eval := dvm.eval(expr.Args[0])
switch k := code_eval.(type) {
case string:
dvm.State.Store.Keys[DataKey{Key: Variable{Type: String, ValueString: "C"}}] = Variable{Type: String, ValueString: k} // TODO verify code authenticity how
dvm.State.Store.Store(DataKey{Key: Variable{Type: String, ValueString: "C"}}, Variable{Type: String, ValueString: k}) // TODO verify code authenticity how
return true, uint64(1)
default:
return true, uint64(0)

View File

@ -35,8 +35,6 @@ type DataAtom struct {
Value Variable // current value if any
}
type Object map[Variable]Variable
type TransferInternal struct {
Asset crypto.Hash `cbor:"Asset,omitempty" json:"Asset,omitempty"` // transfer this asset
SCID string `cbor:"A,omitempty" json:"A,omitempty"` // transfer to this SCID
@ -63,24 +61,14 @@ type TX_Storage struct {
DiskLoaderRaw func([]byte) ([]byte, bool)
SCID crypto.Hash
BalanceAtStart uint64 // at runtime this will be fed balance
Keys map[DataKey]Variable // this keeps the in-transit DB updates, just in case we have to discard instantly
RawKeys map[string][]byte
RawKeys map[string][]byte // this keeps the in-transit DB updates, just in case we have to discard instantly
Transfers map[crypto.Hash]SC_Transfers // all transfers ( internal/external )
}
var DVM_STORAGE_BACKEND DVM_Storage_Loader // this variable can be hijacked at runtime to offer different stores such as RAM/file/DB etc
type DVM_Storage_Loader interface {
Load(DataKey, *uint64) Variable
Store(DataKey, Variable)
RawLoad([]byte) ([]byte, bool)
RawStore([]byte, []byte)
}
// initialize tx store
func Initialize_TX_store() (tx_store *TX_Storage) {
tx_store = &TX_Storage{Keys: map[DataKey]Variable{}, RawKeys: map[string][]byte{}, Transfers: map[crypto.Hash]SC_Transfers{}}
tx_store = &TX_Storage{RawKeys: map[string][]byte{}, Transfers: map[crypto.Hash]SC_Transfers{}}
return
}
@ -99,15 +87,24 @@ func (tx_store *TX_Storage) RawStore(key []byte, value []byte) {
return
}
func (tx_store *TX_Storage) Delete(dkey DataKey) {
tx_store.RawStore(dkey.MarshalBinaryPanic(), []byte{})
return
}
// this will load the variable, and if the key is found
func (tx_store *TX_Storage) Load(dkey DataKey, found_value *uint64) (value Variable) {
//fmt.Printf("Loading %+v \n", dkey)
*found_value = 0
if result, ok := tx_store.Keys[dkey]; ok { // if it was modified in current TX, use it
if result, ok := tx_store.RawKeys[string(dkey.MarshalBinaryPanic())]; ok { // if it was modified in current TX, use it
*found_value = 1
return result
if err := value.UnmarshalBinary(result); err != nil {
panic(err)
}
return value
}
if tx_store.DiskLoader == nil {
@ -123,7 +120,7 @@ func (tx_store *TX_Storage) Load(dkey DataKey, found_value *uint64) (value Varia
func (tx_store *TX_Storage) Store(dkey DataKey, v Variable) {
//fmt.Printf("Storing request %+v : %+v\n", dkey, v)
tx_store.Keys[dkey] = v
tx_store.RawKeys[string(dkey.MarshalBinaryPanic())] = v.MarshalBinaryPanic()
}
// store variable

View File

@ -212,7 +212,7 @@ try_again:
var orequest ObjectList
var oresponse Objects
fmt.Printf("inserting blocks %d %x\n", (int64(i) + response.Start_topoheight), response.Block_list[i][:])
//fmt.Printf("inserting blocks %d %x\n", (int64(i) + response.Start_topoheight), response.Block_list[i][:])
orequest.Block_list = append(orequest.Block_list, response.Block_list[i])
fill_common(&orequest.Common)
if err := connection.Client.Call("Peer.GetObject", orequest, &oresponse); err != nil {

View File

@ -392,9 +392,18 @@ func broadcast_Block_Coded(cbl *block.Complete_Block, PeerID uint64, first_seen
count := 0
unique_map := UniqueConnections()
var connections []*Connection
for _, v := range unique_map {
connections = append(connections, v)
}
sort.SliceStable(connections, func(i, j int) bool {
return connections[i].Latency < connections[j].Latency
})
for { // we must send all blocks atleast once, once we are done, break ut
old_count := count
for _, v := range unique_map {
for _, v := range connections {
select {
case <-Exit_Event:
return

View File

@ -214,18 +214,19 @@ func (w *Wallet_Memory) TransferPayload0(transfers []rpc.Transfer, ringsize uint
topoheight := int64(-1)
var block_hash crypto.Hash
{ // if wallet has not been recently used, increase probability of user's tx being successfully mined
var zeroscid crypto.Hash
if w.getEncryptedBalanceresult(zeroscid).Topoheight+2 <= daemon_topoheight {
topoheight = daemon_topoheight - 2
_, noncetopo, block_hash, self_e, err := w.GetEncryptedBalanceAtTopoHeight(zeroscid, -1, w.GetAddress().String())
if err != nil {
return
}
if w.getEncryptedBalanceresult(zeroscid).Topoheight+3 <= daemon_topoheight {
// TODO, we should check nonce for base token and other tokens at the same time
// right now, we are probably using a bit of luck here
if daemon_topoheight >= int64(noncetopo)+3 { // if wallet has not been recently used, increase probability of user's tx being successfully mined
topoheight = daemon_topoheight - 3
}
}
_, _, block_hash, self_e, _ := w.GetEncryptedBalanceAtTopoHeight(transfers[0].SCID, topoheight, w.GetAddress().String())
_, _, block_hash, self_e, _ = w.GetEncryptedBalanceAtTopoHeight(transfers[0].SCID, topoheight, w.GetAddress().String())
if err != nil {
fmt.Printf("self unregistered err %s\n", err)
return