Remove marshalling of ethereum tx to hex field

ethereum
Martin Boehm 2018-12-05 01:10:00 +01:00
parent 5e170b8dd8
commit 3e532e9130
3 changed files with 105 additions and 79 deletions

View File

@ -3,7 +3,6 @@ package eth
import (
"blockbook/bchain"
"encoding/hex"
"encoding/json"
"math/big"
"strconv"
@ -102,7 +101,7 @@ func ethNumber(n string) (int64, error) {
return 0, errors.Errorf("Not a number: '%v'", n)
}
func (p *EthereumParser) ethTxToTx(tx *rpcTransaction, receipt *rpcReceipt, blocktime int64, confirmations uint32, marshallHex bool) (*bchain.Tx, error) {
func (p *EthereumParser) ethTxToTx(tx *rpcTransaction, receipt *rpcReceipt, blocktime int64, confirmations uint32) (*bchain.Tx, error) {
txid := tx.Hash
var (
fa, ta []string
@ -118,18 +117,6 @@ func (p *EthereumParser) ethTxToTx(tx *rpcTransaction, receipt *rpcReceipt, bloc
Tx: tx,
Receipt: receipt,
}
var h string
if marshallHex {
// completeTransaction without BlockHash is marshalled and hex encoded to bchain.Tx.Hex
bh := tx.BlockHash
tx.BlockHash = ""
b, err := json.Marshal(ct)
if err != nil {
return nil, err
}
tx.BlockHash = bh
h = hex.EncodeToString(b)
}
vs, err := hexutil.DecodeBig(tx.Value)
if err != nil {
return nil, err
@ -137,7 +124,7 @@ func (p *EthereumParser) ethTxToTx(tx *rpcTransaction, receipt *rpcReceipt, bloc
return &bchain.Tx{
Blocktime: blocktime,
Confirmations: confirmations,
Hex: h,
// Hex
// LockTime
Time: blocktime,
Txid: txid,
@ -223,15 +210,11 @@ func hexEncodeBig(b []byte) string {
// PackTx packs transaction to byte array
func (p *EthereumParser) PackTx(tx *bchain.Tx, height uint32, blockTime int64) ([]byte, error) {
b, err := hex.DecodeString(tx.Hex)
if err != nil {
return nil, err
}
var r completeTransaction
var err error
var n uint64
err = json.Unmarshal(b, &r)
if err != nil {
return nil, err
r, ok := tx.CoinSpecificData.(completeTransaction)
if !ok {
return nil, errors.New("Missing CoinSpecificData")
}
pt := &ProtoCompleteTransaction{}
pt.Tx = &ProtoCompleteTransaction_TxType{}
@ -357,7 +340,7 @@ func (p *EthereumParser) UnpackTx(buf []byte) (*bchain.Tx, uint32, error) {
Logs: logs,
}
}
tx, err := p.ethTxToTx(&rt, rr, int64(pt.BlockTime), 0, true)
tx, err := p.ethTxToTx(&rt, rr, int64(pt.BlockTime), 0)
if err != nil {
return nil, 0, err
}
@ -405,19 +388,9 @@ func GetHeightFromTx(tx *bchain.Tx) (uint32, error) {
var bn string
csd, ok := tx.CoinSpecificData.(completeTransaction)
if !ok {
b, err := hex.DecodeString(tx.Hex)
if err != nil {
return 0, err
}
var ct completeTransaction
err = json.Unmarshal(b, &ct)
if err != nil {
return 0, err
}
bn = ct.Tx.BlockNumber
} else {
bn = csd.Tx.BlockNumber
return 0, errors.New("Missing CoinSpecificData")
}
bn = csd.Tx.BlockNumber
n, err := hexutil.DecodeUint64(bn)
if err != nil {
return 0, errors.Annotatef(err, "BlockNumber %v", bn)

View File

@ -4,6 +4,7 @@ package eth
import (
"blockbook/bchain"
"blockbook/tests/dbtestdata"
"encoding/hex"
"fmt"
"math/big"
@ -66,18 +67,51 @@ func TestEthParser_GetAddrDescFromAddress(t *testing.T) {
}
}
var (
testTx1, testTx2 bchain.Tx
testTxPacked1 = "08d38388021092f4c1d5051aa20108d001120509502f900018d5e1042a44a9059cbb00000000000000000000000008e93c026b6454b7437d097aabd550f98cb89ed300000000000000000000000000000000000000000000021e19e0c9bab24000003220a9cd088aba2131000da6f38a33c20169baee476218deea6b78720700b895b1013a144af4114f73d1c1c903ac9e0361b379d1291808a2421420cd153de35d469ba46127a0c8f18626b59a256a22a8010a02cb391201011a9e010a144af4114f73d1c1c903ac9e0361b379d1291808a2122000000000000000000000000000000000000000000000021e19e0c9bab24000001a20ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef1a2000000000000000000000000020cd153de35d469ba46127a0c8f18626b59a256a1a2000000000000000000000000008e93c026b6454b7437d097aabd550f98cb89ed3"
testTxPacked2 = "08889eaf0110fa83c3d5051a6908ece40212050430e234001888a40122081bc0159d530e60003220cd647151552b5132b2aef7c9be00dc6f73afc5901dde157aab131335baaa853b3a14555ee11fbddc0e49a9bab358a8941ad95ffdb48f42143e3a3d69dc66ba10737f531ed088954a9ec89d97480a22070a025208120101"
)
var testTx1, testTx2 bchain.Tx
func init() {
testTx1 = bchain.Tx{
Blocktime: 1521515026,
Hex: "7b227478223a7b226e6f6e6365223a2230786430222c226761735072696365223a223078393530326639303030222c22676173223a2230783133306435222c22746f223a22307834616634313134663733643163316339303361633965303336316233373964313239313830386132222c2276616c7565223a22307830222c22696e707574223a22307861393035396362623030303030303030303030303030303030303030303030303038653933633032366236343534623734333764303937616162643535306639386362383965643330303030303030303030303030303030303030303030303030303030303030303030303030303030303030303032316531396530633962616232343030303030222c2268617368223a22307861396364303838616261323133313030306461366633386133336332303136396261656534373632313864656561366237383732303730306238393562313031222c22626c6f636b4e756d626572223a223078343230316433222c2266726f6d223a22307832306364313533646533356434363962613436313237613063386631383632366235396132353661222c227472616e73616374696f6e496e646578223a22307830227d2c2272656365697074223a7b2267617355736564223a22307863623339222c22737461747573223a22307831222c226c6f6773223a5b7b2261646472657373223a22307834616634313134663733643163316339303361633965303336316233373964313239313830386132222c22746f70696373223a5b22307864646632353261643162653263383962363963326230363866633337386461613935326261376631363363346131313632386635356134646635323362336566222c22307830303030303030303030303030303030303030303030303032306364313533646533356434363962613436313237613063386631383632366235396132353661222c22307830303030303030303030303030303030303030303030303030386539336330323662363435346237343337643039376161626435353066393863623839656433225d2c2264617461223a22307830303030303030303030303030303030303030303030303030303030303030303030303030303030303030303032316531396530633962616232343030303030227d5d7d7d",
Time: 1521515026,
Blocktime: 1534858022,
Time: 1534858022,
Txid: "0xcd647151552b5132b2aef7c9be00dc6f73afc5901dde157aab131335baaa853b",
Vin: []bchain.Vin{
{
Addresses: []string{"0x3e3a3d69dc66ba10737f531ed088954a9ec89d97"},
},
},
Vout: []bchain.Vout{
{
ValueSat: *big.NewInt(1999622000000000000),
ScriptPubKey: bchain.ScriptPubKey{
Addresses: []string{"0x555ee11fbddc0e49a9bab358a8941ad95ffdb48f"},
},
},
},
CoinSpecificData: completeTransaction{
Tx: &rpcTransaction{
AccountNonce: "0xb26c",
GasPrice: "0x430e23400",
GasLimit: "0x5208",
To: "0x555ee11fbddc0e49a9bab358a8941ad95ffdb48f",
Value: "0x1bc0159d530e6000",
Payload: "0x",
Hash: "0xcd647151552b5132b2aef7c9be00dc6f73afc5901dde157aab131335baaa853b",
BlockNumber: "0x41eee8",
From: "0x3e3a3d69dc66ba10737f531ed088954a9ec89d97",
TransactionIndex: "0xa",
},
Receipt: &rpcReceipt{
GasUsed: "0x5208",
Status: "0x1",
Logs: []*rpcLog{},
},
},
}
testTx2 = bchain.Tx{
Blocktime: 1534858022,
Time: 1534858022,
Txid: "0xa9cd088aba2131000da6f38a33c20169baee476218deea6b78720700b895b101",
Vin: []bchain.Vin{
{
@ -92,23 +126,31 @@ func init() {
},
},
},
}
testTx2 = bchain.Tx{
Blocktime: 1521533434,
Hex: "7b227478223a7b226e6f6e6365223a22307862323663222c226761735072696365223a223078343330653233343030222c22676173223a22307835323038222c22746f223a22307835353565653131666264646330653439613962616233353861383934316164393566666462343866222c2276616c7565223a22307831626330313539643533306536303030222c22696e707574223a223078222c2268617368223a22307863643634373135313535326235313332623261656637633962653030646336663733616663353930316464653135376161623133313333356261616138353362222c22626c6f636b4e756d626572223a223078326263663038222c2266726f6d223a22307833653361336436396463363662613130373337663533316564303838393534613965633839643937222c227472616e73616374696f6e496e646578223a22307861227d2c2272656365697074223a7b2267617355736564223a22307835323038222c22737461747573223a22307831222c226c6f6773223a5b5d7d7d",
Time: 1521533434,
Txid: "0xcd647151552b5132b2aef7c9be00dc6f73afc5901dde157aab131335baaa853b",
Vin: []bchain.Vin{
{
Addresses: []string{"0x3e3a3d69dc66ba10737f531ed088954a9ec89d97"},
},
},
Vout: []bchain.Vout{
{
ValueSat: *big.NewInt(1999622000000000000),
ScriptPubKey: bchain.ScriptPubKey{
Addresses: []string{"0x555ee11fbddc0e49a9bab358a8941ad95ffdb48f"},
CoinSpecificData: completeTransaction{
Tx: &rpcTransaction{
AccountNonce: "0xd0",
GasPrice: "0x9502f9000",
GasLimit: "0x130d5",
To: "0x4af4114f73d1c1c903ac9e0361b379d1291808a2",
Value: "0x0",
Payload: "0xa9059cbb000000000000000000000000555ee11fbddc0e49a9bab358a8941ad95ffdb48f00000000000000000000000000000000000000000000021e19e0c9bab2400000",
Hash: "0xa9cd088aba2131000da6f38a33c20169baee476218deea6b78720700b895b101",
BlockNumber: "0x41eee8",
From: "0x20cd153de35d469ba46127a0c8f18626b59a256a",
TransactionIndex: "0x0"},
Receipt: &rpcReceipt{
GasUsed: "0xcb39",
Status: "0x1",
Logs: []*rpcLog{
&rpcLog{
Address: "0x4af4114f73d1c1c903ac9e0361b379d1291808a2",
Data: "0x00000000000000000000000000000000000000000000021e19e0c9bab2400000",
Topics: []string{
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0x00000000000000000000000020cd153de35d469ba46127a0c8f18626b59a256a",
"0x000000000000000000000000555ee11fbddc0e49a9bab358a8941ad95ffdb48f",
},
},
},
},
},
@ -132,19 +174,19 @@ func TestEthereumParser_PackTx(t *testing.T) {
name: "1",
args: args{
tx: &testTx1,
height: 4325843,
blockTime: 1521515026,
height: 4321000,
blockTime: 1534858022,
},
want: testTxPacked1,
want: dbtestdata.EthTx1Packed,
},
{
name: "2",
args: args{
tx: &testTx2,
height: 2871048,
blockTime: 1521533434,
height: 4321000,
blockTime: 1534858022,
},
want: testTxPacked2,
want: dbtestdata.EthTx2Packed,
},
}
p := NewEthereumParser(1)
@ -177,15 +219,15 @@ func TestEthereumParser_UnpackTx(t *testing.T) {
}{
{
name: "1",
args: args{hex: testTxPacked1},
args: args{hex: dbtestdata.EthTx1Packed},
want: &testTx1,
want1: 4325843,
want1: 4321000,
},
{
name: "2",
args: args{hex: testTxPacked2},
args: args{hex: dbtestdata.EthTx2Packed},
want: &testTx2,
want1: 2871048,
want1: 4321000,
},
}
p := NewEthereumParser(1)
@ -200,11 +242,22 @@ func TestEthereumParser_UnpackTx(t *testing.T) {
t.Errorf("EthereumParser.UnpackTx() error = %v, wantErr %v", err, tt.wantErr)
return
}
// CoinSpecificData are not set in want struct
got.CoinSpecificData = nil
// DeepEqual compares empty nil slices as not equal
if fmt.Sprint(got) != fmt.Sprint(tt.want) {
t.Errorf("EthereumParser.UnpackTx() got = %+v, want %+v", got, tt.want)
// DeepEqual has problems with pointers in completeTransaction
gs := got.CoinSpecificData.(completeTransaction)
ws := tt.want.CoinSpecificData.(completeTransaction)
gc := *got
wc := *tt.want
gc.CoinSpecificData = nil
wc.CoinSpecificData = nil
if fmt.Sprint(gc) != fmt.Sprint(wc) {
// if !reflect.DeepEqual(gc, wc) {
t.Errorf("EthereumParser.UnpackTx() gc got = %+v, want %+v", gc, wc)
}
if !reflect.DeepEqual(gs.Tx, ws.Tx) {
t.Errorf("EthereumParser.UnpackTx() gs.Tx got = %+v, want %+v", gs.Tx, ws.Tx)
}
if !reflect.DeepEqual(gs.Receipt, ws.Receipt) {
t.Errorf("EthereumParser.UnpackTx() gs.Receipt got = %+v, want %+v", gs.Receipt, ws.Receipt)
}
if got1 != tt.want1 {
t.Errorf("EthereumParser.UnpackTx() got1 = %v, want %v", got1, tt.want1)

View File

@ -468,7 +468,7 @@ func (b *EthereumRPC) GetBlock(hash string, height uint32) (*bchain.Block, error
}
btxs := make([]bchain.Tx, len(body.Transactions))
for i, tx := range body.Transactions {
btx, err := b.Parser.ethTxToTx(&tx, &rpcReceipt{Logs: logs[tx.Hash]}, bbh.Time, uint32(bbh.Confirmations), false)
btx, err := b.Parser.ethTxToTx(&tx, &rpcReceipt{Logs: logs[tx.Hash]}, bbh.Time, uint32(bbh.Confirmations))
if err != nil {
return nil, errors.Annotatef(err, "hash %v, height %v, txid %v", hash, height, tx.Hash)
}
@ -525,7 +525,7 @@ func (b *EthereumRPC) GetTransaction(txid string) (*bchain.Tx, error) {
var btx *bchain.Tx
if tx.BlockNumber == "" {
// mempool tx
btx, err = b.Parser.ethTxToTx(tx, nil, 0, 0, true)
btx, err = b.Parser.ethTxToTx(tx, nil, 0, 0)
if err != nil {
return nil, errors.Annotatef(err, "txid %v", txid)
}
@ -573,7 +573,7 @@ func (b *EthereumRPC) GetTransaction(txid string) (*bchain.Tx, error) {
if err != nil {
return nil, errors.Annotatef(err, "txid %v", txid)
}
btx, err = b.Parser.ethTxToTx(tx, &receipt, time, confirmations, true)
btx, err = b.Parser.ethTxToTx(tx, &receipt, time, confirmations)
if err != nil {
return nil, errors.Annotatef(err, "txid %v", txid)
}