Skip to content

Commit

Permalink
DERO-HE STARGATE Testnet Release29
Browse files Browse the repository at this point in the history
  • Loading branch information
CaptainDero committed Nov 24, 2021
1 parent 49651db commit 4501db4
Show file tree
Hide file tree
Showing 25 changed files with 224 additions and 111 deletions.
18 changes: 16 additions & 2 deletions block/miniblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package block

import "fmt"
import "time"
import "hash"
import "sync"
import "strings"
import "encoding/binary"

Expand All @@ -28,6 +30,10 @@ import "github.com/deroproject/derohe/pow"

const MINIBLOCK_SIZE = 68

var hasherPool = sync.Pool{
New: func() interface{} { return sha3.New256() },
}

// it should be exactly 68 bytes after serialization
// structure size 1 + 6 + 4 + 4 + 16 +32 + 5 bytes
type MiniBlock struct {
Expand Down Expand Up @@ -91,9 +97,17 @@ func (mbl *MiniBlock) GetMiniID() uint32 {
}

// this function gets the block identifier hash, this is only used to deduplicate mini blocks
func (mbl *MiniBlock) GetHash() (hash crypto.Hash) {
func (mbl *MiniBlock) GetHash() (result crypto.Hash) {
ser := mbl.Serialize()
return sha3.Sum256(ser[:])
sha := hasherPool.Get().(hash.Hash)
sha.Reset()
sha.Write(ser[:])
x := sha.Sum(nil)
copy(result[:], x[:])
hasherPool.Put(sha)
return result

// return sha3.Sum256(ser[:])
}

// Get PoW hash , this is very slow function
Expand Down
32 changes: 27 additions & 5 deletions block/miniblockdag.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (c *MiniBlocksCollection) GetAllTips() (mbls []MiniBlock) {

clone := map[uint32]MiniBlock{}

var clone_list []MiniBlock
clone_list := make([]MiniBlock, 0, 64)
for k, v := range c.Collection {
clone[k] = v
clone_list = append(clone_list, v)
Expand Down Expand Up @@ -273,6 +273,8 @@ func (c *MiniBlocksCollection) GetGenesisFromMiniBlock(mbl MiniBlock) (genesis [

// this works in all cases, but it may return truncated history,all returns must be checked for connectivity
func (c *MiniBlocksCollection) GetEntireMiniBlockHistory(mbl MiniBlock) (history []MiniBlock) {

history = make([]MiniBlock, 0, 128)
if mbl.Genesis {
history = append(history, mbl)
return
Expand Down Expand Up @@ -301,6 +303,7 @@ func (c *MiniBlocksCollection) GetEntireMiniBlockHistory(mbl MiniBlock) (history
// gets the genesis from the tips
// this function only works, if the miniblock has been expanded
func GetGenesisFromMiniBlock(mbl MiniBlock) (genesis []MiniBlock) {

if mbl.Genesis {
genesis = append(genesis, mbl)
return
Expand All @@ -325,20 +328,36 @@ func GetGenesisFromMiniBlock(mbl MiniBlock) (genesis []MiniBlock) {

// get entire history,its in sorted form
func GetEntireMiniBlockHistory(mbls ...MiniBlock) (history []MiniBlock) {
var queue []MiniBlock
queue := make([]MiniBlock, 0, 128)
queue = append(queue, mbls...)
history = make([]MiniBlock, 0, 128)
unique := make([]MiniBlock, 0, 128)

unique_map := map[crypto.Hash]MiniBlock{}

for len(queue) > 0 {
item := queue[0]
queue = queue[1:] // Dequeue

history = append(history, item) //mini blocks might be duplicated
if !item.Genesis {
queue = append(queue, item.PastMiniBlocks...)
if _, ok := unique_map[item.GetHash()]; !ok {
unique_map[item.GetHash()] = item
history = append(history, item) //mini blocks might be duplicated
if !item.Genesis {
queue = append(queue, item.PastMiniBlocks...)
}
}
}

for _, v := range unique_map {
unique = append(unique, v)
}

history = MiniBlocks_Unique(history)

if len(unique) != len(history) {
panic("result mismatch")
}

history = MiniBlocks_SortByTimeAsc(history) // sort on the basis of timestamps

return
Expand All @@ -347,6 +366,7 @@ func GetEntireMiniBlockHistory(mbls ...MiniBlock) (history []MiniBlock) {
// this sorts by distance, in descending order
// if distance is equal, then it sorts by its id which is collision free
func MiniBlocks_SortByDistanceDesc(mbls []MiniBlock) (sorted []MiniBlock) {
sorted = make([]MiniBlock, 0, len(mbls))
sorted = append(sorted, mbls...)
sort.SliceStable(sorted, func(i, j int) bool { // sort descending on the basis of Distance
if sorted[i].Distance == sorted[j].Distance {
Expand All @@ -360,6 +380,7 @@ func MiniBlocks_SortByDistanceDesc(mbls []MiniBlock) (sorted []MiniBlock) {
// this sorts by timestamp,ascending order
// if timestamp is equal, then it sorts by its id which is collision free
func MiniBlocks_SortByTimeAsc(mbls []MiniBlock) (sorted []MiniBlock) {
sorted = make([]MiniBlock, 0, len(mbls))
sorted = append(sorted, mbls...)
sort.SliceStable(sorted, func(i, j int) bool { // sort on the basis of timestamps
if sorted[i].Timestamp == sorted[j].Timestamp {
Expand All @@ -371,6 +392,7 @@ func MiniBlocks_SortByTimeAsc(mbls []MiniBlock) (sorted []MiniBlock) {
}

func MiniBlocks_Unique(mbls []MiniBlock) (unique []MiniBlock) {
unique = make([]MiniBlock, 0, len(mbls))
unique_map := map[crypto.Hash]MiniBlock{}
for _, mbl := range mbls {
unique_map[mbl.GetHash()] = mbl
Expand Down
26 changes: 21 additions & 5 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type Blockchain struct {
cache_Get_Difficulty_At_Tips *lru.Cache // used to cache some outputs
cache_BlockPast *lru.Cache // used to cache a blocks past
cache_BlockHeight *lru.Cache // used to cache a blocks past
cache_VersionMerkle *lru.Cache // used to cache a versions merkle root

integrator_address rpc.Address // integrator rewards will be given to this address

Expand Down Expand Up @@ -153,11 +154,15 @@ func Blockchain_Start(params map[string]interface{}) (*Blockchain, error) {
return nil, err
}

if chain.cache_BlockPast, err = lru.New(100 * 1024); err != nil { // temporary cache for a blocks past
if chain.cache_BlockPast, err = lru.New(1024); err != nil { // temporary cache for a blocks past
return nil, err
}

if chain.cache_BlockHeight, err = lru.New(100 * 1024); err != nil { // temporary cache for a blocks height
if chain.cache_BlockHeight, err = lru.New(10 * 1024); err != nil { // temporary cache for a blocks height
return nil, err
}

if chain.cache_VersionMerkle, err = lru.New(1024); err != nil { // temporary cache for a snapshot version
return nil, err
}

Expand Down Expand Up @@ -231,11 +236,11 @@ func Blockchain_Start(params map[string]interface{}) (*Blockchain, error) {

top_block_topo_index := chain.Load_TOPO_HEIGHT()

if top_block_topo_index < 10 {
if top_block_topo_index < 4 {
return
}

top_block_topo_index -= 10
top_block_topo_index -= 4

blid, err := chain.Load_Block_Topological_order_at_index(top_block_topo_index)
if err != nil {
Expand Down Expand Up @@ -689,7 +694,7 @@ func (chain *Blockchain) Add_Complete_Block(cbl *block.Complete_Block) (err erro
if tx.SCDATA.Has(rpc.SCACTION, rpc.DataUint64) {
if rpc.SC_INSTALL == rpc.SC_ACTION(tx.SCDATA.Value(rpc.SCACTION, rpc.DataUint64).(uint64)) {
txid := tx.GetHash()
if txid[31] < 0x80 { // last byte should be more than 0x80
if txid[0] < 0x80 || txid[31] < 0x80 { // last byte should be more than 0x80
block_logger.Error(fmt.Errorf("Invalid SCID"), "SCID installing tx must end with >0x80 byte", "txid", cbl.Txs[i].GetHash())
return errormsg.ErrTXDoubleSpend, false
}
Expand Down Expand Up @@ -1188,6 +1193,17 @@ func (chain *Blockchain) Add_TX_To_Pool(tx *transaction.Transaction) error {
return err
}

if tx.TransactionType == transaction.SC_TX {
if tx.SCDATA.Has(rpc.SCACTION, rpc.DataUint64) {
if rpc.SC_INSTALL == rpc.SC_ACTION(tx.SCDATA.Value(rpc.SCACTION, rpc.DataUint64).(uint64)) {
txid := tx.GetHash()
if txid[0] < 0x80 || txid[31] < 0x80 { // last byte should be more than 0x80
return fmt.Errorf("Invalid SCID ID,it must not start with 0x80")
}
}
}
}

if err := chain.Verify_Transaction_NonCoinbase_CheckNonce_Tips(hf_version, tx, chain.Get_TIPS()); err != nil { // transaction verification failed
logger.V(1).Error(err, "Incoming TX nonce verification failed", "txid", txhash, "stacktrace", globals.StackTrace(false))
return fmt.Errorf("Incoming TX %s nonce verification failed, err %s", txhash, err)
Expand Down
22 changes: 13 additions & 9 deletions blockchain/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package blockchain

import "fmt"
import "math/big"
import "crypto/rand"
import "path/filepath"

import "github.com/deroproject/derohe/globals"
Expand Down Expand Up @@ -55,22 +54,18 @@ func (s *storage) Initialize(params map[string]interface{}) (err error) {
}

func (s *storage) IsBalancesIntialized() bool {

var err error
var buf [64]byte
var balancehash, random_hash [32]byte

balance_ss, _ := s.Balance_store.LoadSnapshot(0) // load most recent snapshot
balancetree, _ := balance_ss.GetTree(config.BALANCE_TREE)

// avoid hardcoding any hash
if balancehash, err = balancetree.Hash(); err == nil {
if _, err = rand.Read(buf[:]); err == nil {
random_tree, _ := balance_ss.GetTree(string(buf[:]))
if random_hash, err = random_tree.Hash(); err == nil {
if random_hash == balancehash {
return false
}
random_tree, _ := balance_ss.GetTree(config.SC_META)
if random_hash, err = random_tree.Hash(); err == nil {
if random_hash == balancehash {
return false
}
}
}
Expand Down Expand Up @@ -300,6 +295,11 @@ func (chain *Blockchain) Load_Block_Topological_order_at_index(index_pos int64)
//load store hash from 2 tree
func (chain *Blockchain) Load_Merkle_Hash(version uint64) (hash crypto.Hash, err error) {

if hashi, ok := chain.cache_VersionMerkle.Get(version); ok {
hash = hashi.(crypto.Hash)
return
}

ss, err := chain.Store.Balance_store.LoadSnapshot(version)
if err != nil {
return
Expand All @@ -324,5 +324,9 @@ func (chain *Blockchain) Load_Merkle_Hash(version uint64) (hash crypto.Hash, err
for i := range balance_merkle_hash {
hash[i] = balance_merkle_hash[i] ^ meta_merkle_hash[i]
}

if chain.cache_enabled { //set in cache
chain.cache_VersionMerkle.Add(version, hash)
}
return hash, nil
}
2 changes: 1 addition & 1 deletion blockchain/transaction_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func clean_up_valid_cache() {
current_time := time.Now()
transaction_valid_cache.Range(func(k, value interface{}) bool {
first_seen := value.(time.Time)
if current_time.Sub(first_seen).Round(time.Second).Seconds() > 3600 {
if current_time.Sub(first_seen).Round(time.Second).Seconds() > 360 {
transaction_valid_cache.Delete(k)
}
return true
Expand Down
24 changes: 23 additions & 1 deletion cmd/derod/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import "bufio"
import "strings"
import "strconv"
import "runtime"
import "runtime/debug"
import "math/big"
import "os/signal"
import "io/ioutil"
Expand Down Expand Up @@ -89,9 +90,24 @@ var Exit_In_Progress = make(chan bool)

var logger logr.Logger

func dump(filename string) {
f, err := os.Create(filename)
if err != nil {
fmt.Printf("err creating file %s\n", err)
return
}

runtime.GC()
debug.WriteHeapDump(f.Fd())

err = f.Close()
if err != nil {
fmt.Printf("err closing file %s\n", err)
}
}

func main() {
var err error

globals.Arguments, err = docopt.Parse(command_line, nil, true, config.Version.String(), false)

if err != nil {
Expand Down Expand Up @@ -781,6 +797,12 @@ restart_loop:

case command == "gc":
runtime.GC()
case command == "heap":
if len(line_parts) == 1 {
fmt.Printf("heap needs a filename to write\n")
break
}
dump(line_parts[1])

case command == "ban":

Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,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, 0x70, 0x00, 0x00, 0x00}),
Network_ID: uuid.FromBytesOrNil([]byte{0x59, 0xd7, 0xf7, 0xe9, 0xdd, 0x48, 0xd5, 0xfd, 0x13, 0x0a, 0xf6, 0xe0, 0x71, 0x00, 0x00, 0x00}),
P2P_Default_Port: 40401,
RPC_Default_Port: 40402,
Wallet_RPC_Default_Port: 40403,
Expand Down
1 change: 1 addition & 0 deletions config/seed_nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ var Mainnet_seed_nodes = []string{
// some seed node for testnet
var Testnet_seed_nodes = []string{
"68.183.12.117:40401",
"167.99.145.53:40401",
}
2 changes: 1 addition & 1 deletion config/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.87-1.DEROHE.STARGATE+24112021")
var Version = semver.MustParse("3.4.89-1.DEROHE.STARGATE+22112021")
2 changes: 2 additions & 0 deletions vendor/github.com/creachadair/jrpc2/base.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions vendor/github.com/creachadair/jrpc2/channel/channel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions vendor/github.com/creachadair/jrpc2/channel/json.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4501db4

Please sign in to comment.