Skip to content

Commit 3acbbee

Browse files
committed
DERO-HE STARGATE Mainnet HardFork-2 Release48
1 parent a9ad008 commit 3acbbee

File tree

12 files changed

+116
-39
lines changed

12 files changed

+116
-39
lines changed

blockchain/blockchain.go

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ import "github.com/deroproject/graviton"
5858
// this structure must be update while mutex
5959
type Blockchain struct {
6060
Store storage // interface to storage layer
61-
Height int64 // chain height is always 1 more than block
6261
Top_ID crypto.Hash // id of the top block
6362
Pruned int64 // until where the chain has been pruned
6463
MiniBlocks *block.MiniBlocksCollection // used for consensus
@@ -210,6 +209,42 @@ func Blockchain_Start(params map[string]interface{}) (*Blockchain, error) {
210209
logger.Info("Chain Pruned till", "topoheight", chain.Pruned)
211210
}
212211

212+
// detect case if chain was corrupted earlier,so as it can be deleted and resynced
213+
if chain.Pruned < globals.Config.HF1_HEIGHT && globals.IsMainnet() && chain.Get_Height() >= globals.Config.HF1_HEIGHT+1 {
214+
toporecord, err := chain.Store.Topo_store.Read(globals.Config.HF1_HEIGHT + 1)
215+
if err != nil {
216+
panic(err)
217+
}
218+
var ss *graviton.Snapshot
219+
ss, err = chain.Store.Balance_store.LoadSnapshot(toporecord.State_Version)
220+
if err != nil {
221+
panic(err)
222+
}
223+
224+
var namehash_scid crypto.Hash
225+
namehash_scid[31] = 1
226+
sc_data_tree, err := ss.GetTree(string(namehash_scid[:]))
227+
if err != nil {
228+
panic(err)
229+
}
230+
231+
if code_bytes, err := sc_data_tree.Get(dvm.SC_Code_Key(namehash_scid)); err == nil {
232+
var v dvm.Variable
233+
if err = v.UnmarshalBinary(code_bytes); err != nil {
234+
panic("Unmarshal error")
235+
}
236+
if !strings.Contains(v.ValueString, "UpdateCode") {
237+
logger.Error(nil, "Chain corruption detected")
238+
logger.Error(nil, "You need to delete existing mainnet folder and resync chain again")
239+
os.Exit(-1)
240+
return nil, err
241+
}
242+
243+
} else {
244+
panic(err)
245+
}
246+
}
247+
213248
metrics.Version = config.Version.String()
214249
go metrics.Dump_metrics_data_directly(logger, globals.Arguments["--node-tag"]) // enable metrics if someone needs them
215250

@@ -294,15 +329,14 @@ func (chain *Blockchain) Initialise_Chain_From_DB() {
294329
// then downgrading to top-10 height
295330
// then reworking the chain to get the tip
296331
best_height := chain.Load_TOP_HEIGHT()
297-
chain.Height = best_height
298332

299333
chain.Tips = map[crypto.Hash]crypto.Hash{} // reset the map
300334
// reload top tip from disk
301335
top := chain.Get_Top_ID()
302336

303337
chain.Tips[top] = top // we only can load a single tip from db
304338

305-
logger.V(1).Info("Reloaded Chain from disk", "Tips", chain.Tips, "Height", chain.Height)
339+
logger.V(1).Info("Reloaded Chain from disk", "Tips", chain.Tips, "Height", best_height)
306340
}
307341

308342
// before shutdown , make sure p2p is confirmed stopped
@@ -807,9 +841,9 @@ func (chain *Blockchain) Add_Complete_Block(cbl *block.Complete_Block) (err erro
807841
if height > chain.Get_Height() || height == 0 { // exception for genesis block
808842
//atomic.StoreInt64(&chain.Height, height)
809843
height_changed = true
810-
block_logger.Info("Chain extended", "new height", chain.Height)
844+
block_logger.Info("Chain extended", "new height", height)
811845
} else {
812-
block_logger.Info("Chain extended but height is same", "new height", chain.Height)
846+
block_logger.Info("Chain extended but height is same", "new height", height)
813847
}
814848

815849
{
@@ -916,15 +950,24 @@ func (chain *Blockchain) Add_Complete_Block(cbl *block.Complete_Block) (err erro
916950
}
917951

918952
var meta dvm.SC_META_DATA // the meta contains metadata about SC
919-
if err := meta.UnmarshalBinary(meta_bytes); err != nil {
920-
panic(err)
921-
}
922953

923-
if meta.DataHash, err = v.Hash(); err != nil { // encode data tree hash
924-
panic(err)
954+
if bl_current.Height < uint64(globals.Config.HF2_HEIGHT) {
955+
if err := meta.UnmarshalBinary(meta_bytes); err != nil {
956+
panic(err)
957+
}
958+
if meta.DataHash, err = v.Hash(); err != nil { // encode data tree hash
959+
panic(err)
960+
}
961+
sc_meta.Put(dvm.SC_Meta_Key(scid), meta.MarshalBinary())
962+
} else {
963+
if err := meta.UnmarshalBinaryGood(meta_bytes); err != nil {
964+
panic(err)
965+
}
966+
if meta.DataHash, err = v.Hash(); err != nil { // encode data tree hash
967+
panic(err)
968+
}
969+
sc_meta.Put(dvm.SC_Meta_Key(scid), meta.MarshalBinaryGood())
925970
}
926-
927-
sc_meta.Put(dvm.SC_Meta_Key(scid), meta.MarshalBinary())
928971
data_trees = append(data_trees, v)
929972

930973
/*fmt.Printf("will commit tree name %x \n", v.GetName())
@@ -1039,7 +1082,7 @@ func (chain *Blockchain) Add_Complete_Block(cbl *block.Complete_Block) (err erro
10391082

10401083
// every 2000 block print a line
10411084
if chain.Get_Height()%2000 == 0 {
1042-
block_logger.Info(fmt.Sprintf("Chain Height %d", chain.Height))
1085+
block_logger.Info(fmt.Sprintf("Chain Height %d", chain.Get_Height()))
10431086
}
10441087

10451088
purge_count := chain.MiniBlocks.PurgeHeight(chain.Get_Stable_Height()) // purge all miniblocks upto this height

blockchain/hardcoded_contracts.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import _ "embed"
2222

2323
import "github.com/deroproject/graviton"
2424
import "github.com/deroproject/derohe/dvm"
25+
import "github.com/deroproject/derohe/globals"
2526
import "github.com/deroproject/derohe/cryptography/crypto"
2627

2728
//go:embed hardcoded_sc/nameservice.bas
@@ -34,35 +35,29 @@ var source_nameservice_updateable string
3435
func (chain *Blockchain) install_hardcoded_contracts(cache map[crypto.Hash]*graviton.Tree, ss *graviton.Snapshot, balance_tree *graviton.Tree, sc_tree *graviton.Tree, height uint64) (err error) {
3536

3637
if height == 0 {
37-
3838
if _, _, err = dvm.ParseSmartContract(source_nameservice); err != nil {
3939
logger.Error(err, "error Parsing hard coded sc")
4040
panic(err)
41-
return
4241
}
4342

4443
var name crypto.Hash
4544
name[31] = 1
4645
if err = chain.install_hardcoded_sc(cache, ss, balance_tree, sc_tree, source_nameservice, name); err != nil {
4746
panic(err)
48-
return
4947
}
50-
51-
return
5248
}
5349

54-
if height == 21480 { // update SC at specific height
50+
// it is updated at 0 height for testnets
51+
if height == uint64(globals.Config.HF1_HEIGHT) { // update SC at specific height
5552
if _, _, err = dvm.ParseSmartContract(source_nameservice_updateable); err != nil {
5653
logger.Error(err, "error Parsing hard coded sc")
5754
panic(err)
58-
return
5955
}
6056

6157
var name crypto.Hash
6258
name[31] = 1
6359
if err = chain.install_hardcoded_sc(cache, ss, balance_tree, sc_tree, source_nameservice_updateable, name); err != nil {
6460
panic(err)
65-
return
6661
}
6762
}
6863

blockchain/transaction_execute.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,6 @@ func (chain *Blockchain) process_transaction(changed map[crypto.Hash]*graviton.T
250250

251251
default:
252252
panic("unknown transaction, do not know how to process it")
253-
return 0
254253
}
255254
}
256255

@@ -277,7 +276,7 @@ func (chain *Blockchain) process_transaction_sc(cache map[crypto.Hash]*graviton.
277276

278277
defer func() {
279278
if r := recover(); r != nil {
280-
logger.V(1).Error(nil, "Recover while executing SC ", "txid", txhash, "error", r, "stack", fmt.Sprintf("%s", string(debug.Stack())))
279+
logger.V(2).Error(nil, "Recover while executing SC ", "txid", txhash, "error", r, "stack", fmt.Sprintf("%s", string(debug.Stack())))
281280

282281
}
283282
}()

cmd/dero-miner/miner.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,8 @@ func mineblock(tid int) {
446446
var diff big.Int
447447
var work [block.MINIBLOCK_SIZE]byte
448448

449+
time.Sleep(5*time.Second)
450+
449451
nonce_buf := work[block.MINIBLOCK_SIZE-5:] //since slices are linked, it modifies parent
450452
runtime.LockOSThread()
451453
threadaffinity()

cmd/derod/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ restart_loop:
678678
panic(err)
679679
}
680680
if r.BLOCK_ID != current_blid {
681-
fmt.Printf("corruption due to XYZ r %+v , current_blid %s current_blid_height\n", r, current_blid, height)
681+
fmt.Printf("corruption due to XYZ r %+v , current_blid %s current_blid_height %d\n", r, current_blid, height)
682682

683683
fix_commit_version, err := chain.ReadBlockSnapshotVersion(current_blid)
684684
if err != nil {

config/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ type CHAIN_CONFIG struct {
8585
RPC_Default_Port int
8686
Wallet_RPC_Default_Port int
8787

88+
HF1_HEIGHT int64 // first HF applied here
89+
HF2_HEIGHT int64 // second HF applie here
90+
8891
Dev_Address string // to which address the integrator rewatd will go, if user doesn't specify integrator address'
8992
Genesis_Tx string
9093
Genesis_Block_Hash crypto.Hash
@@ -96,6 +99,8 @@ var Mainnet = CHAIN_CONFIG{Name: "mainnet",
9699
RPC_Default_Port: 10102,
97100
Wallet_RPC_Default_Port: 10103,
98101
Dev_Address: "dero1qykyta6ntpd27nl0yq4xtzaf4ls6p5e9pqu0k2x4x3pqq5xavjsdxqgny8270",
102+
HF1_HEIGHT: 21480,
103+
HF2_HEIGHT: 29000,
99104

100105
Genesis_Tx: "" +
101106
"01" + // version
@@ -114,6 +119,8 @@ var Testnet = CHAIN_CONFIG{Name: "testnet", // testnet will always have last 3 b
114119
Wallet_RPC_Default_Port: 40403,
115120

116121
Dev_Address: "deto1qy0ehnqjpr0wxqnknyc66du2fsxyktppkr8m8e6jvplp954klfjz2qqdzcd8p",
122+
HF1_HEIGHT: 0, // on testnet apply at genesis
123+
HF2_HEIGHT: 0, // on testnet apply at genesis
117124

118125
Genesis_Tx: "" +
119126
"01" + // version

config/seed_nodes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ package config
2222
// only version 2
2323
var Mainnet_seed_nodes = []string{
2424
"185.132.176.174:11011",
25-
"190.2.135.218:11011",
25+
"45.82.66.54:8080",
2626
"185.107.69.12:11011",
2727
"89.38.97.110:11011",
2828
}

config/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ import "github.com/blang/semver/v4"
2020

2121
// right now it has to be manually changed
2222
// do we need to include git commitsha??
23-
var Version = semver.MustParse("3.4.130-0.DEROHE.STARGATE+26022022")
23+
var Version = semver.MustParse("3.4.133-48.DEROHE.STARGATE+26022022")

dvm/sc.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,23 @@ func (meta *SC_META_DATA) UnmarshalBinary(buf []byte) (err error) {
5656
return nil
5757
}
5858

59+
// serialize the structure
60+
func (meta SC_META_DATA) MarshalBinaryGood() (buf []byte) {
61+
buf = make([]byte, 0, 33)
62+
buf = append(buf, meta.Type)
63+
buf = append(buf, meta.DataHash[:]...)
64+
return
65+
}
66+
67+
func (meta *SC_META_DATA) UnmarshalBinaryGood(buf []byte) (err error) {
68+
if len(buf) != 1+32 {
69+
return fmt.Errorf("input buffer should be of 33 bytes in length")
70+
}
71+
meta.Type = buf[0]
72+
copy(meta.DataHash[:], buf[1:])
73+
return nil
74+
}
75+
5976
func SC_Meta_Key(scid crypto.Hash) []byte {
6077
return scid[:]
6178
}
@@ -277,15 +294,6 @@ func Execute_sc_function(w_sc_tree *Tree_Wrapper, data_tree *Tree_Wrapper, scid
277294

278295
// reads SC, balance
279296
func ReadSC(w_sc_tree *Tree_Wrapper, data_tree *Tree_Wrapper, scid crypto.Hash) (balance uint64, sc SmartContract, found bool) {
280-
meta_bytes, err := w_sc_tree.Get(SC_Meta_Key(scid))
281-
if err != nil {
282-
return
283-
}
284-
285-
var meta SC_META_DATA // the meta contains the link to the SC bytes
286-
if err := meta.UnmarshalBinary(meta_bytes); err != nil {
287-
return
288-
}
289297
var zerohash crypto.Hash
290298
balance, _ = LoadSCAssetValue(data_tree, scid, zerohash)
291299

dvm/sc_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package dvm
2+
3+
import "time"
4+
import "testing"
5+
import "math/rand"
6+
7+
func TestSC_META_DATA(t *testing.T) {
8+
rand.Seed(time.Now().UnixNano())
9+
for i := 0; i < 256; i++ {
10+
sctype := byte(i)
11+
var meta, meta2 SC_META_DATA
12+
for j := 0; j < 10000; j++ {
13+
meta.Type = sctype
14+
rand.Read(meta.DataHash[:])
15+
16+
ser := meta.MarshalBinaryGood()
17+
if err := meta2.UnmarshalBinaryGood(ser[:]); err != nil {
18+
t.Fatalf("marshallling unmarshalling failed")
19+
}
20+
if meta != meta2 {
21+
t.Fatalf("marshallling unmarshalling failed")
22+
}
23+
}
24+
}
25+
}

hardfork.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

p2p/rpc_handshake.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ func (connection *Connection) dispatch_test_handshake() {
5858
//scan our peer list and send peers which have been recently communicated
5959
request.PeerList = get_peer_list_specific(Address(connection))
6060

61-
ctx, _ := context.WithTimeout(context.Background(), 4*time.Second)
61+
ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
62+
defer cancel()
6263
if err := connection.Client.CallWithContext(ctx, "Peer.Handshake", request, &response); err != nil {
6364
connection.logger.V(4).Error(err, "cannot handshake")
6465
connection.exit()

0 commit comments

Comments
 (0)