-
Notifications
You must be signed in to change notification settings - Fork 1
/
genesis.go
67 lines (59 loc) · 2.08 KB
/
genesis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package indexer
import (
"math/big"
"github.com/Taraxa-project/taraxa-indexer/internal/chain"
"github.com/Taraxa-project/taraxa-indexer/internal/common"
"github.com/Taraxa-project/taraxa-indexer/internal/storage"
"github.com/Taraxa-project/taraxa-indexer/models"
log "github.com/sirupsen/logrus"
)
type Genesis struct {
storage storage.Storage
genesis chain.GenesisObject
bc blockContext
hash string
}
func MakeGenesis(s storage.Storage, c *chain.WsClient, gen_obj chain.GenesisObject, genesisHash storage.GenesisHash) (*Genesis, error) {
var genesis Genesis
var err error
genesis.storage = s
genesis.genesis = gen_obj
genesis.hash = string(genesisHash)
genesis.bc = *MakeBlockContext(s, c, &common.Config{Chain: gen_obj.ToChainConfig()})
return &genesis, err
}
func (g *Genesis) makeInitBalanceTrx(addr, value string) (trx models.Transaction) {
trx.Hash = "GENESIS_" + addr
trx.From = "GENESIS"
trx.To = addr
trx.Value = value
trx.BlockNumber = 0
trx.Timestamp = common.ParseUInt(g.genesis.DagGenesisBlock.Timestamp)
trx.Status = true
return
}
func (g *Genesis) process() {
genesisSupply := big.NewInt(0)
accounts := &storage.Balances{Accounts: g.storage.GetAccounts()}
for addr, value := range g.genesis.InitialBalances {
trx := g.makeInitBalanceTrx(addr, value)
g.bc.SaveTransaction(trx)
value := common.ParseStringToBigInt(trx.Value)
genesisSupply.Add(genesisSupply, value)
accounts.AddToBalance(trx.To, value)
}
for _, validator := range g.genesis.Dpos.InitialValidators {
for addr, value := range validator.Delegations {
delegation := common.ParseStringToBigInt(value)
accounts.AddToBalance(addr, big.NewInt(0).Neg(delegation))
accounts.AddToBalance(common.DposContractAddress, delegation)
}
}
log.WithField("count", len(g.genesis.InitialBalances)).Info("Genesis: Init balance transactions parsed")
// Genesis transactions isn't real transactions, so don't count it here
g.bc.Batch.SaveAccounts(accounts)
g.bc.finalized.TrxCount = 0
g.bc.Batch.SetGenesisHash(storage.GenesisHash(g.hash))
g.bc.Batch.SetTotalSupply(genesisSupply)
g.bc.commit()
}