88 "sync"
99 "time"
1010
11- "github.com/ellcrys/elld/util/activeobject"
12-
1311 "github.com/ellcrys/elld/crypto"
1412 "github.com/syndtr/goleveldb/leveldb"
1513
@@ -35,6 +33,11 @@ const (
3533 MaxRejectedBlocksCacheSize = 100
3634)
3735
36+ var (
37+ // GenesisBlockFileName is the name of the file that contains the genesis block
38+ GenesisBlockFileName = "genesis.json"
39+ )
40+
3841// Blockchain represents the Ellcrys blockchain. It provides
3942// functionalities for interacting with the underlying database
4043// and primitives.
@@ -76,9 +79,11 @@ type Blockchain struct {
7679 // events or broadcast events about its state
7780 eventEmitter * emitter.Emitter
7881
79- // activeObj provides active object pattern for handling
80- // function calls
81- activeObj * activeobject.ActiveObject
82+ // skipDecideBestChain skips the operation to decide which chain is the best
83+ // when a block is processed.
84+ // Note: Used to prevent re-organisation when creating
85+ // and assembling blocks into desired chains in integration test.
86+ skipDecideBestChain bool
8287
8388 // chl is a lock for chain events
8489 chl * sync.RWMutex
@@ -111,7 +116,6 @@ func New(txPool types.TxPool, cfg *config.EngineConfig, log logger.Logger) *Bloc
111116 bc .orphanBlocks = cache .NewCache (MaxOrphanBlocksCacheSize )
112117 bc .rejectedBlocks = cache .NewCache (MaxRejectedBlocksCacheSize )
113118 bc .eventEmitter = & emitter.Emitter {}
114- bc .activeObj = activeobject .NewActiveObject ()
115119 return bc
116120}
117121
@@ -176,7 +180,7 @@ func (b *Blockchain) Up() error {
176180 // been set, we attempt to load it from the
177181 // genesis.json file.
178182 if b .genesisBlock == nil {
179- b .genesisBlock , err = LoadBlockFromFile ("genesis.json" )
183+ b .genesisBlock , err = LoadBlockFromFile (GenesisBlockFileName )
180184 if err != nil {
181185 return err
182186 }
@@ -222,6 +226,9 @@ func (b *Blockchain) Up() error {
222226 b .log .Info ("Known branches have been loaded" , "NumBranches" , numChains )
223227 }
224228
229+ // Set the root chain and make it the initial main chain
230+ b .bestChain = b .getRootChain ()
231+
225232 // Using the best chain rule, we mush select the best chain
226233 // and set it as the current bestChain.
227234 err = b .decideBestChain ()
@@ -232,6 +239,19 @@ func (b *Blockchain) Up() error {
232239 return nil
233240}
234241
242+ // getRootChain finds the root chain of the tree.
243+ // This is usually the main chain and it has no branch.
244+ func (b * Blockchain ) getRootChain () * Chain {
245+ b .chl .RLock ()
246+ defer b .chl .RUnlock ()
247+ for _ , c := range b .chains {
248+ if ! c .HasParent () {
249+ return c
250+ }
251+ }
252+ return nil
253+ }
254+
235255// getBlockByHash finds and returns a block by hash only
236256// SetEventEmitter sets the event emitter
237257func (b * Blockchain ) SetEventEmitter (ee * emitter.Emitter ) {
0 commit comments