-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd: refactors all files inside cmd/
Everything is refactored. While reviewing code to do the TODOs mentioned in #79, I noticed some things to clean up. This refactor is needed for the goals mentioned in #79 as it separates 1. Bridge node and CSN (Compact State Node) 2. Chain resume functionality from both bridgenode and csn (placed in chain.go and chainio.go in both bridgenode/ and csn/ Changed things 1. All functionality for store and restore to disk is now placed in chain.go and chainio.go in both bridgenode/ and csn/ 2. Various bugs involving offsetfile.go was fixed. To add new blocks from blk*.dat files, all the user has to do is delete the offsetdata directory and re-run genproofs to rebuild the offset index. Both genproofs and ibdsim will resume where they left off and sync the new blocks. 3. main.go file in directory cmd/ is the main file now. 4. Separated all ttl related things to its own directory. 5. Removed all names related or implying a simulator. 6. Various comment cleanups
- Loading branch information
1 parent
2a4ab57
commit 19ca2a3
Showing
21 changed files
with
1,123 additions
and
950 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package bridge | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/mit-dci/utreexo/cmd/util" | ||
"github.com/mit-dci/utreexo/utreexo" | ||
) | ||
|
||
// initBridgeNodeState attempts to load and initialize the chain state from the disk. | ||
// If a chain state is not present, chain is initialized to the genesis | ||
// returns forest, height, lastIndexOffsetHeight, pOffset and error | ||
func initBridgeNodeState(isTestnet bool, offsetFinished chan bool) ( | ||
*utreexo.Forest, int32, int32, int32, error) { | ||
|
||
var offsetInitialized, forestInitialized bool | ||
|
||
// bool to check if the offsetfile is present | ||
offsetInitialized = util.HasAccess(util.OffsetFilePath) | ||
|
||
var lastIndexOffsetHeight int32 | ||
|
||
// Default behavior is that the user should delete all offsetdata | ||
// if they have new blk*.dat files to sync | ||
// User needs to re-index blk*.dat files when added new files to sync | ||
if offsetInitialized { | ||
var err error | ||
lastIndexOffsetHeight, err = restoreLastIndexOffsetHeight(offsetFinished) | ||
if err != nil { | ||
return nil, 0, 0, 0, err | ||
} | ||
} else { | ||
var err error | ||
fmt.Println("Offsetfile not present. Indexing offset for blocks blk*.dat files...") | ||
lastIndexOffsetHeight, err = createOffsetData(isTestnet, offsetFinished) | ||
if err != nil { | ||
return nil, 0, 0, 0, err | ||
} | ||
} | ||
|
||
// bool to check if the forestdata is present | ||
forestInitialized = util.HasAccess(util.ForestFilePath) | ||
|
||
var forest *utreexo.Forest | ||
var height, pOffset int32 | ||
|
||
if forestInitialized { | ||
var err error | ||
fmt.Println("Has access to forestdata, resuming") | ||
forest, err = restoreForest() | ||
if err != nil { | ||
return nil, 0, 0, 0, err | ||
} | ||
height, err = restoreHeight() | ||
if err != nil { | ||
return nil, 0, 0, 0, err | ||
} | ||
pOffset, err = restoreLastProofFileOffset() | ||
if err != nil { | ||
return nil, 0, 0, 0, err | ||
} | ||
} else { | ||
var err error | ||
fmt.Println("Creating new forestdata") | ||
forest, err = createForest() | ||
if err != nil { | ||
return nil, 0, 0, 0, err | ||
} | ||
} | ||
|
||
return forest, height, lastIndexOffsetHeight, pOffset, nil | ||
} | ||
|
||
// saveBridgeNodeData saves the state of the bridgenode so that when the | ||
// user restarts, they'll be able to resume. | ||
// Saves height, forest fields, and pOffset | ||
func saveBridgeNodeData( | ||
forest *utreexo.Forest, pOffset int32, height int32) error { | ||
|
||
/* | ||
** Open files | ||
*/ | ||
lastPOffsetFile, err := os.OpenFile( | ||
util.LastPOffsetFilePath, os.O_CREATE|os.O_RDWR, 0600) | ||
if err != nil { | ||
return err | ||
} | ||
heightFile, err := os.OpenFile( | ||
util.ForestLastSyncedBlockHeightFilePath, | ||
os.O_CREATE|os.O_RDWR, 0600) | ||
if err != nil { | ||
return err | ||
} | ||
miscForestFile, err := os.OpenFile( | ||
util.MiscForestFilePath, os.O_CREATE|os.O_RDWR, 0600) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
/* | ||
** Write to files | ||
*/ | ||
_, err = heightFile.WriteAt(util.I32tB(height), 0) | ||
if err != nil { | ||
return err | ||
} | ||
// write other misc forest data | ||
err = forest.WriteForest(miscForestFile) | ||
if err != nil { | ||
return err | ||
} | ||
// write pOffset | ||
_, err = lastPOffsetFile.WriteAt( | ||
util.I32tB(pOffset), 0) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package bridge | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/mit-dci/utreexo/cmd/util" | ||
"github.com/mit-dci/utreexo/utreexo" | ||
) | ||
|
||
// createOffsetData restores the offsetfile needed to index the | ||
// blocks in the raw blk*.dat files. | ||
func createOffsetData( | ||
isTestnet bool, offsetFinished chan bool) (int32, error) { | ||
|
||
var lastIndexOffsetHeight int32 | ||
|
||
// Set the Block Header hash | ||
// buildOffsetFile matches the header hash to organize | ||
var tip util.Hash | ||
if isTestnet == true { | ||
tip = util.TestNet3GenHash | ||
} else { | ||
tip = util.MainnetGenHash | ||
} | ||
|
||
var err error | ||
lastIndexOffsetHeight, err = buildOffsetFile(tip, offsetFinished) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return lastIndexOffsetHeight, nil | ||
} | ||
|
||
// restoreLastProofFileOffset restores POffset from util.LastPOffsetFilePath | ||
func restoreLastProofFileOffset() (int32, error) { | ||
|
||
// Gives the location of where a particular block height's proofs are | ||
// Basically an index | ||
var pOffset int32 | ||
|
||
if util.HasAccess(util.LastPOffsetFilePath) { | ||
f, err := os.OpenFile( | ||
util.LastPOffsetFilePath, | ||
os.O_CREATE|os.O_RDWR, 0600) | ||
if err != nil { | ||
return 0, err | ||
} | ||
var pOffsetByte [4]byte | ||
_, err = f.ReadAt(pOffsetByte[:], 0) | ||
if err != nil { | ||
return 0, err | ||
} | ||
pOffset = util.BtI32(pOffsetByte[:]) | ||
|
||
} | ||
return pOffset, nil | ||
} | ||
|
||
// createForest initializes forest | ||
func createForest() (*utreexo.Forest, error) { | ||
|
||
// Where the forestfile exists | ||
forestFile, err := os.OpenFile( | ||
util.ForestFilePath, os.O_CREATE|os.O_RDWR, 0600) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Restores all the forest data | ||
forest := utreexo.NewForest(forestFile) | ||
|
||
return forest, nil | ||
} | ||
|
||
// restoreForest restores forest fields based off the existing forestdata | ||
// on disk. | ||
func restoreForest() (*utreexo.Forest, error) { | ||
|
||
var forest *utreexo.Forest | ||
|
||
// Where the forestfile exists | ||
forestFile, err := os.OpenFile( | ||
util.ForestFilePath, os.O_RDWR, 0400) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Where the misc forest data exists | ||
miscForestFile, err := os.OpenFile( | ||
util.MiscForestFilePath, os.O_RDONLY, 0400) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
forest, err = utreexo.RestoreForest(miscForestFile, forestFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return forest, nil | ||
} | ||
|
||
// restoreHeight restores height from util.ForestLastSyncedBlockHeightFilePath | ||
func restoreHeight() (int32, error) { | ||
|
||
var height int32 | ||
|
||
// if there is a heightfile, get the height from that | ||
// heightFile saves the last block that was written to ttldb | ||
if util.HasAccess(util.ForestLastSyncedBlockHeightFilePath) { | ||
heightFile, err := os.OpenFile( | ||
util.ForestLastSyncedBlockHeightFilePath, | ||
os.O_RDONLY, 0400) | ||
if err != nil { | ||
return 0, err | ||
} | ||
var t [4]byte | ||
_, err = heightFile.Read(t[:]) | ||
if err != nil { | ||
return 0, err | ||
} | ||
height = util.BtI32(t[:]) | ||
} | ||
return height, nil | ||
} | ||
|
||
// restoreLastIndexOffsetHeight restores the lastIndexOffsetHeight | ||
func restoreLastIndexOffsetHeight(offsetFinished chan bool) (int32, error) { | ||
|
||
var lastIndexOffsetHeight int32 | ||
|
||
// grab the last block height from currentoffsetheight | ||
// currentoffsetheight saves the last height from the offsetfile | ||
var lastIndexOffsetHeightByte [4]byte | ||
|
||
f, err := os.OpenFile( | ||
util.LastIndexOffsetHeightFilePath, os.O_CREATE|os.O_RDWR, 0600) | ||
if err != nil { | ||
return 0, err | ||
} | ||
_, err = f.Read(lastIndexOffsetHeightByte[:]) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
f.Read(lastIndexOffsetHeightByte[:]) | ||
lastIndexOffsetHeight = util.BtI32(lastIndexOffsetHeightByte[:]) | ||
|
||
// if there is a offset file, we should pass true to offsetFinished | ||
// to let stopParse() know that it shouldn't delete offsetfile | ||
offsetFinished <- true | ||
|
||
return lastIndexOffsetHeight, nil | ||
} |
Oops, something went wrong.