Skip to content

Commit

Permalink
core/rawdb, eth: fix lost sign bit, shorten API output fields
Browse files Browse the repository at this point in the history
  • Loading branch information
karalabe committed May 3, 2022
1 parent 29025ab commit bd13c32
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
24 changes: 20 additions & 4 deletions core/rawdb/accessors_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,33 @@ func WriteTransitionStatus(db ethdb.KeyValueWriter, data []byte) {
// specific block. If unavailable for the specific block (non full synced node),
// nil will be returned.
func ReadIssuance(db ethdb.KeyValueReader, number uint64, hash common.Hash) *big.Int {
data, _ := db.Get(issuanceKey(number, hash))
if len(data) == 0 {
blob, _ := db.Get(issuanceKey(number, hash))
if len(blob) < 2 {
return nil
}
return new(big.Int).SetBytes(data)
// Since negative big ints can't be encoded to bytes directly, use a dirty
// hack to store the negativift flag in the first byte (0 == positive,
// 1 == negative)
issuance := new(big.Int).SetBytes(blob[1:])
if blob[0] == 1 {
issuance.Neg(issuance)
}
return issuance
}

// WriteIssuance stores the amount of Ether (in wei) issued (or burnt) in a
// specific block.
func WriteIssuance(db ethdb.KeyValueWriter, number uint64, hash common.Hash, issuance *big.Int) {
if err := db.Put(issuanceKey(number, hash), issuance.Bytes()); err != nil {
// Since negative big ints can't be encoded to bytes directly, use a dirty
// hack to store the negativift flag in the first byte (0 == positive,
// 1 == negative)
blob := []byte{0}
if issuance.Sign() < 0 {
blob[0] = 1
}
blob = append(blob, issuance.Bytes()...)

if err := db.Put(issuanceKey(number, hash), blob); err != nil {
log.Crit("Failed to store block issuance", "err", err)
}
}
15 changes: 7 additions & 8 deletions eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,14 @@ func (api *PublicEthereumAPI) Issuance(ctx context.Context, from uint64) (*rpc.S

// Define an internal type for issuance notifications
type issuanceNotification struct {
Number uint64 `json:"blockNumber"`
Hash common.Hash `json:"blockHash"`
Issuance *big.Int `json:"totalIssuance"`
Subsidy *big.Int `json:"subsidyMining"`
Uncles *big.Int `json:"subsidyUncles"`
Burn *big.Int `json:"burnProtocol"`
Destruct *big.Int `json:"burnDestruct"`
Number uint64 `json:"block"`
Hash common.Hash `json:"hash"`
Issuance *big.Int `json:"issuance"`
Subsidy *big.Int `json:"subsidy"`
Uncles *big.Int `json:"uncles"`
Burn *big.Int `json:"burn"`
Destruct *big.Int `json:"destruct"`
}

// Define a method to convert a block into an issuance notification
service := func(block *types.Block) {
// Retrieve the state-crawled issuance - if available
Expand Down

0 comments on commit bd13c32

Please sign in to comment.