diff --git a/go.mod b/go.mod index 9b55051b7..37b991243 100644 --- a/go.mod +++ b/go.mod @@ -78,10 +78,12 @@ require ( require ( github.com/georgysavva/scany v1.2.1 + github.com/jackc/pgtype v1.8.1 github.com/jmoiron/sqlx v1.3.5 github.com/lib/pq v1.10.7 github.com/multiformats/go-multihash v0.0.15 github.com/pganalyze/pg_query_go/v2 v2.2.0 + github.com/shopspring/decimal v1.2.0 github.com/thoas/go-funk v0.9.3 ) @@ -123,7 +125,6 @@ require ( github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgproto3/v2 v2.1.1 // indirect github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect - github.com/jackc/pgtype v1.8.1 // indirect github.com/jackc/puddle v1.1.3 // indirect github.com/jbenet/goprocess v0.1.4 // indirect github.com/klauspost/compress v1.15.15 // indirect diff --git a/statediff/indexer/database/sql/writer.go b/statediff/indexer/database/sql/writer.go index 91adffbfb..3e281943b 100644 --- a/statediff/indexer/database/sql/writer.go +++ b/statediff/indexer/database/sql/writer.go @@ -20,10 +20,13 @@ import ( "fmt" "strconv" - "github.com/ethereum/go-ethereum/statediff/indexer/database/metrics" - "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/statediff/indexer/database/metrics" "github.com/ethereum/go-ethereum/statediff/indexer/models" + + "github.com/jackc/pgtype" + shopspring "github.com/jackc/pgtype/ext/shopspring-numeric" + "github.com/shopspring/decimal" ) var ( @@ -88,7 +91,7 @@ func (w *Writer) upsertTransactionCID(tx Tx, transaction models.TxModel) error { return insertError{"eth.transaction_cids", err, "COPY", transaction} } - value, err := strconv.ParseFloat(transaction.Value, 64) + value, err := toNumeric(transaction.Value) if err != nil { return insertError{"eth.transaction_cids", err, "COPY", transaction} } @@ -234,7 +237,7 @@ func (w *Writer) upsertStateAccount(tx Tx, stateAccount models.StateAccountModel if err != nil { return insertError{"eth.state_accounts", err, "COPY", stateAccount} } - balance, err := strconv.ParseFloat(stateAccount.Balance, 64) + balance, err := toNumeric(stateAccount.Balance) if err != nil { return insertError{"eth.state_accounts", err, "COPY", stateAccount} } @@ -304,6 +307,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 diff --git a/statediff/indexer/mocks/test_data.go b/statediff/indexer/mocks/test_data.go index 89d077aa7..ed49c15c4 100644 --- a/statediff/indexer/mocks/test_data.go +++ b/statediff/indexer/mocks/test_data.go @@ -187,9 +187,10 @@ var ( AccountCodeHash = common.HexToHash("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470") AccountLeafKey = test_helpers.Account2LeafKey RemovedLeafKey = test_helpers.Account1LeafKey + Balance, _ = new(big.Int).SetString("106387458790507306766", 10) Account, _ = rlp.EncodeToBytes(&types.StateAccount{ Nonce: nonce0, - Balance: big.NewInt(1000), + Balance: Balance, CodeHash: AccountCodeHash.Bytes(), Root: common.HexToHash(AccountRoot), }) diff --git a/statediff/indexer/test/test.go b/statediff/indexer/test/test.go index dedcd3655..614bf0b61 100644 --- a/statediff/indexer/test/test.go +++ b/statediff/indexer/test/test.go @@ -439,7 +439,7 @@ func TestPublishAndIndexStateIPLDs(t *testing.T, db sql.Database) { BlockNumber: mocks.BlockNumber.String(), HeaderID: account.HeaderID, StatePath: stateNode.Path, - Balance: "1000", + Balance: mocks.Balance.String(), CodeHash: mocks.AccountCodeHash.Bytes(), StorageRoot: mocks.AccountRoot, Nonce: 0,