Use supported Numeric type.

This commit is contained in:
Thomas E Lackey 2023-04-05 15:29:16 -05:00 committed by i-norden
parent fbbf09a98d
commit a260c14954
3 changed files with 29 additions and 9 deletions

7
go.mod
View File

@ -139,9 +139,10 @@ require (
github.com/prometheus/procfs v0.9.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/stretchr/objx v0.3.0 // indirect
github.com/tklauser/numcpus v0.2.2 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/mod v0.6.0 // indirect
golang.org/x/net v0.4.0 // indirect

2
go.sum
View File

@ -712,6 +712,8 @@ github.com/shopspring/decimal v0.0.0-20200227202807-02e2044944cc/go.mod h1:DKyhr
github.com/shopspring/decimal v0.0.0-20200419222939-1884f454f8ea/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=

View File

@ -20,7 +20,10 @@ import (
"fmt"
"strconv"
"github.com/jackc/pgtype"
shopspring "github.com/jackc/pgtype/ext/shopspring-numeric"
"github.com/lib/pq"
"github.com/shopspring/decimal"
"github.com/ethereum/go-ethereum/statediff/indexer/database/metrics"
"github.com/ethereum/go-ethereum/statediff/indexer/models"
@ -96,13 +99,17 @@ INSERT INTO eth.transaction_cids (block_number, header_id, tx_hash, cid, dst, sr
ON CONFLICT (tx_hash, header_id, block_number) DO NOTHING
*/
func (w *Writer) upsertTransactionCID(tx Tx, transaction models.TxModel) error {
val := transaction.Value
if val == "" {
val = "0"
}
if w.useCopyForTx(tx) {
blockNum, err := strconv.ParseInt(transaction.BlockNumber, 10, 64)
if err != nil {
return insertError{"eth.transaction_cids", err, "COPY", transaction}
}
value, err := strconv.ParseFloat(transaction.Value, 64)
value, err := toNumeric(val)
if err != nil {
return insertError{"eth.transaction_cids", err, "COPY", transaction}
}
@ -216,9 +223,9 @@ INSERT INTO eth.state_cids (block_number, header_id, state_leaf_key, cid, remove
ON CONFLICT (header_id, state_leaf_key, block_number) DO NOTHING
*/
func (w *Writer) upsertStateCID(tx Tx, stateNode models.StateNodeModel) error {
balance := stateNode.Balance
bal := stateNode.Balance
if stateNode.Removed {
balance = "0"
bal = "0"
}
if w.useCopyForTx(tx) {
@ -226,14 +233,15 @@ func (w *Writer) upsertStateCID(tx Tx, stateNode models.StateNodeModel) error {
if err != nil {
return insertError{"eth.state_cids", err, "COPY", stateNode}
}
balInt, err := strconv.ParseUint(balance, 10, 64)
balance, err := toNumeric(bal)
if err != nil {
return insertError{"eth.state_cids", err, "COPY", stateNode}
}
_, err = tx.CopyFrom(w.db.Context(), w.db.StateTableName(), w.db.StateColumnNames(),
toRows(toRow(blockNum, stateNode.HeaderID, stateNode.StateKey, stateNode.CID,
true, balInt, stateNode.Nonce, stateNode.CodeHash, stateNode.StorageRoot, stateNode.Removed)))
true, balance, stateNode.Nonce, stateNode.CodeHash, stateNode.StorageRoot, stateNode.Removed)))
if err != nil {
return insertError{"eth.state_cids", err, "COPY", stateNode}
}
@ -244,7 +252,7 @@ func (w *Writer) upsertStateCID(tx Tx, stateNode models.StateNodeModel) error {
stateNode.StateKey,
stateNode.CID,
true,
balance,
bal,
stateNode.Nonce,
stateNode.CodeHash,
stateNode.StorageRoot,
@ -308,6 +316,15 @@ func toRow(args ...interface{}) []interface{} {
return row
}
func toNumeric(value string) (*shopspring.Numeric, error) {
decimalValue, err := decimal.NewFromString(value)
if nil != err {
return nil, err
}
return &shopspring.Numeric{Decimal: decimalValue, Status: pgtype.Present}, nil
}
// combine row (or rows) into a slice of rows for CopyFrom
func toRows(rows ...[]interface{}) [][]interface{} {
return rows