Skip to content

Commit

Permalink
Saves validator to right address
Browse files Browse the repository at this point in the history
  • Loading branch information
Elod23 committed Nov 27, 2023
1 parent b56d670 commit 1e0efe5
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions internal/indexer/logs.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package indexer

import (
"math/big"
"strings"

"github.com/Taraxa-project/taraxa-indexer/internal/chain"
"github.com/Taraxa-project/taraxa-indexer/internal/storage"
"github.com/Taraxa-project/taraxa-indexer/models"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
)

func (bc *blockContext) processTransactionLogs(tx chain.Transaction) (err error) {
Expand Down Expand Up @@ -34,18 +37,36 @@ func (bc *blockContext) handleValidatorRegistrations(logs []models.EventLog) (er
if strings.Compare(log.Topics[0], registerValidatorTopic) != 0 && strings.Compare(log.Address, "0x00000000000000000000000000000000000000fe") != 0 {
continue
}
addressStats := bc.addressStats[log.Address]
address, err := decodePaddedAddress(log.Topics[1])
if err != nil {
return err
}
addressStats := bc.addressStats[address.Hex()]
if addressStats == nil {
addressStats = &storage.AddressStats{
Address: log.Address,
Address: address.Hex(),
StatsResponse: models.StatsResponse{
ValidatorRegisteredBlock: &bc.block.Number,
},
}
} else {
addressStats.ValidatorRegisteredBlock = &bc.block.Number
}
bc.addressStats[log.Address] = addressStats
bc.addressStats[address.Hex()] = addressStats
}
return nil
}

func decodePaddedAddress(hexStr string) (common.Address, error) {
// Decode the hex string to bytes.
bytes, err := hexutil.Decode(hexStr)
if err != nil {
return common.Address{}, err
}

// Convert bytes to big.Int.
bigInt := new(big.Int).SetBytes(bytes)
// convert to uint64
address := common.BigToAddress(bigInt)
return address, nil
}

0 comments on commit 1e0efe5

Please sign in to comment.