Implement EthereumParser.UnpackTx using protobuf
parent
5b2aa276ee
commit
9aec4ec717
|
@ -4,6 +4,7 @@ import (
|
|||
"blockbook/bchain"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"math/big"
|
||||
"strconv"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
|
@ -15,17 +16,17 @@ import (
|
|||
)
|
||||
|
||||
type rpcTransaction struct {
|
||||
AccountNonce string `json:"nonce" gencodec:"required"`
|
||||
Price string `json:"gasPrice" gencodec:"required"`
|
||||
GasLimit string `json:"gas" gencodec:"required"`
|
||||
To string `json:"to" rlp:"nil"` // nil means contract creation
|
||||
Value string `json:"value" gencodec:"required"`
|
||||
Payload string `json:"input" gencodec:"required"`
|
||||
Hash ethcommon.Hash `json:"hash" rlp:"-"`
|
||||
BlockNumber string
|
||||
BlockHash ethcommon.Hash
|
||||
From string
|
||||
TransactionIndex string `json:"transactionIndex"`
|
||||
AccountNonce string `json:"nonce" gencodec:"required"`
|
||||
Price string `json:"gasPrice" gencodec:"required"`
|
||||
GasLimit string `json:"gas" gencodec:"required"`
|
||||
To string `json:"to" rlp:"nil"` // nil means contract creation
|
||||
Value string `json:"value" gencodec:"required"`
|
||||
Payload string `json:"input" gencodec:"required"`
|
||||
Hash ethcommon.Hash `json:"hash" rlp:"-"`
|
||||
BlockNumber string `json:"blockNumber"`
|
||||
BlockHash *ethcommon.Hash `json:"blockHash,omitempty"`
|
||||
From string `json:"from"`
|
||||
TransactionIndex string `json:"transactionIndex"`
|
||||
// Signature values
|
||||
V string `json:"v" gencodec:"required"`
|
||||
R string `json:"r" gencodec:"required"`
|
||||
|
@ -58,11 +59,14 @@ func ethTxToTx(tx *rpcTransaction, blocktime int64, confirmations uint32) (*bcha
|
|||
if len(tx.To) > 2 {
|
||||
ta = []string{tx.To[2:]}
|
||||
}
|
||||
// temporarily, the complete rpcTransaction is marshalled and hex encoded to bchain.Tx.Hex
|
||||
// temporarily, the complete rpcTransaction without BlockHash is marshalled and hex encoded to bchain.Tx.Hex
|
||||
bh := tx.BlockHash
|
||||
tx.BlockHash = nil
|
||||
b, err := json.Marshal(tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tx.BlockHash = bh
|
||||
h := hex.EncodeToString(b)
|
||||
return &bchain.Tx{
|
||||
Blocktime: blocktime,
|
||||
|
@ -154,6 +158,12 @@ func hexDecodeBig(s string) ([]byte, error) {
|
|||
return b.Bytes(), nil
|
||||
}
|
||||
|
||||
func hexEncodeBig(b []byte) string {
|
||||
var i big.Int
|
||||
i.SetBytes(b)
|
||||
return hexutil.EncodeBig(&i)
|
||||
}
|
||||
|
||||
func (p *EthereumParser) PackTx(tx *bchain.Tx, height uint32, blockTime int64) ([]byte, error) {
|
||||
b, err := hex.DecodeString(tx.Hex)
|
||||
if err != nil {
|
||||
|
@ -187,13 +197,13 @@ func (p *EthereumParser) PackTx(tx *bchain.Tx, height uint32, blockTime int64) (
|
|||
if pt.Price, err = hexDecodeBig(r.Price); err != nil {
|
||||
return nil, errors.Annotatef(err, "Price %v", r.Price)
|
||||
}
|
||||
if pt.R, err = hexDecode(r.R); err != nil {
|
||||
if pt.R, err = hexDecodeBig(r.R); err != nil {
|
||||
return nil, errors.Annotatef(err, "R %v", r.R)
|
||||
}
|
||||
if pt.S, err = hexDecode(r.S); err != nil {
|
||||
if pt.S, err = hexDecodeBig(r.S); err != nil {
|
||||
return nil, errors.Annotatef(err, "S %v", r.S)
|
||||
}
|
||||
if pt.V, err = hexDecode(r.V); err != nil {
|
||||
if pt.V, err = hexDecodeBig(r.V); err != nil {
|
||||
return nil, errors.Annotatef(err, "V %v", r.V)
|
||||
}
|
||||
if pt.To, err = hexDecode(r.To); err != nil {
|
||||
|
@ -210,7 +220,31 @@ func (p *EthereumParser) PackTx(tx *bchain.Tx, height uint32, blockTime int64) (
|
|||
}
|
||||
|
||||
func (p *EthereumParser) UnpackTx(buf []byte) (*bchain.Tx, uint32, error) {
|
||||
return nil, 0, errors.New("UnpackTx: not implemented")
|
||||
var pt ProtoTransaction
|
||||
err := proto.Unmarshal(buf, &pt)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
r := rpcTransaction{
|
||||
AccountNonce: hexutil.EncodeUint64(pt.AccountNonce),
|
||||
BlockNumber: hexutil.EncodeUint64(uint64(pt.BlockNumber)),
|
||||
From: hexutil.Encode(pt.From),
|
||||
GasLimit: hexutil.EncodeUint64(pt.GasLimit),
|
||||
Hash: ethcommon.BytesToHash(pt.Hash),
|
||||
Payload: hexutil.Encode(pt.Payload),
|
||||
Price: hexEncodeBig(pt.Price),
|
||||
R: hexEncodeBig(pt.R),
|
||||
S: hexEncodeBig(pt.S),
|
||||
V: hexEncodeBig(pt.V),
|
||||
To: hexutil.Encode(pt.To),
|
||||
TransactionIndex: hexutil.EncodeUint64(uint64(pt.TransactionIndex)),
|
||||
Value: hexEncodeBig(pt.Value),
|
||||
}
|
||||
tx, err := ethTxToTx(&r, int64(pt.BlockTime), 0)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return tx, pt.BlockNumber, nil
|
||||
}
|
||||
|
||||
func (p *EthereumParser) IsUTXOChain() bool {
|
||||
|
|
|
@ -79,7 +79,7 @@ func TestEthereumParser_PackTx(t *testing.T) {
|
|||
args: args{
|
||||
tx: &bchain.Tx{
|
||||
Blocktime: 1521515026,
|
||||
Hex: "7b226e6f6e6365223a2230783239666165222c226761735072696365223a223078313261303566323030222c22676173223a2230786462626130222c22746f223a22307836383262373930336131313039386366373730633761656634616130326138356233663336303161222c2276616c7565223a22307830222c22696e707574223a223078663032356361616630303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030323235222c2268617368223a22307865366231363864366262336438656437386530336462663832386236626664316662363133663665313239636261363234393634393834353533373234633564222c22426c6f636b4e756d626572223a223078326263616630222c22426c6f636b48617368223a22307865636364366230303331303135613139636237643465313066323835393062613635613661353461643162616133323262353066653561643136393033383935222c2246726f6d223a22307864616363396336313735346130633436313666633533323364633934366538396562323732333032222c227472616e73616374696f6e496e646578223a22307831222c2276223a2230783162222c2272223a22307831626434306133313132326330333931386466366431363664373430613661336132326630386132353933346365623136383863363239373736363163383063222c2273223a22307836303766626331356331663739393561343235386635613962636363363362303430333632643139393164356566653133363163353632323265346361383966227d",
|
||||
Hex: "7b226e6f6e6365223a2230783239666165222c226761735072696365223a223078313261303566323030222c22676173223a2230786462626130222c22746f223a22307836383262373930336131313039386366373730633761656634616130326138356233663336303161222c2276616c7565223a22307830222c22696e707574223a223078663032356361616630303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030323235222c2268617368223a22307865366231363864366262336438656437386530336462663832386236626664316662363133663665313239636261363234393634393834353533373234633564222c22626c6f636b4e756d626572223a223078326263616630222c2266726f6d223a22307864616363396336313735346130633436313666633533323364633934366538396562323732333032222c227472616e73616374696f6e496e646578223a22307831222c2276223a2230783162222c2272223a22307831626434306133313132326330333931386466366431363664373430613661336132326630386132353933346365623136383863363239373736363163383063222c2273223a22307836303766626331356331663739393561343235386635613962636363363362303430333632643139393164356566653133363163353632323265346361383966227d",
|
||||
Time: 1521515026,
|
||||
Txid: "e6b168d6bb3d8ed78e03dbf828b6bfd1fb613f6e129cba624964984553724c5d",
|
||||
Vin: []bchain.Vin{
|
||||
|
@ -100,6 +100,32 @@ func TestEthereumParser_PackTx(t *testing.T) {
|
|||
},
|
||||
want: "08aebf0a1205012a05f20018a0f7362a24f025caaf00000000000000000000000000000000000000000000000000000000000002253220e6b168d6bb3d8ed78e03dbf828b6bfd1fb613f6e129cba624964984553724c5d38f095af014092f4c1d5054a14682b7903a11098cf770c7aef4aa02a85b3f3601a5214dacc9c61754a0c4616fc5323dc946e89eb272302580162011b6a201bd40a31122c03918df6d166d740a6a3a22f08a25934ceb1688c62977661c80c7220607fbc15c1f7995a4258f5a9bccc63b040362d1991d5efe1361c56222e4ca89f",
|
||||
},
|
||||
{
|
||||
name: "2",
|
||||
args: args{
|
||||
tx: &bchain.Tx{
|
||||
Blocktime: 1521533434,
|
||||
Hex: "7b226e6f6e6365223a22307862323663222c226761735072696365223a223078343330653233343030222c22676173223a22307835323038222c22746f223a22307835353565653131666264646330653439613962616233353861383934316164393566666462343866222c2276616c7565223a22307831626330313539643533306536303030222c22696e707574223a223078222c2268617368223a22307863643634373135313535326235313332623261656637633962653030646336663733616663353930316464653135376161623133313333356261616138353362222c22626c6f636b4e756d626572223a223078326263663038222c2266726f6d223a22307833653361336436396463363662613130373337663533316564303838393534613965633839643937222c227472616e73616374696f6e496e646578223a22307861222c2276223a2230783239222c2272223a22307866373136316331373064343335373361643963386437303163646166373134666632613534386135363262306463363339323330643137383839666364343035222c2273223a22307833633439373766633930333835613237656661303033326531376234396664353735623238323663623536653364316563663231353234663261393466393135227d",
|
||||
Time: 1521533434,
|
||||
Txid: "cd647151552b5132b2aef7c9be00dc6f73afc5901dde157aab131335baaa853b",
|
||||
Vin: []bchain.Vin{
|
||||
{
|
||||
Addresses: []string{"3e3a3d69dc66ba10737f531ed088954a9ec89d97"},
|
||||
},
|
||||
},
|
||||
Vout: []bchain.Vout{
|
||||
{
|
||||
ScriptPubKey: bchain.ScriptPubKey{
|
||||
Addresses: []string{"555ee11fbddc0e49a9bab358a8941ad95ffdb48f"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
height: 2871048,
|
||||
blockTime: 1521533434,
|
||||
},
|
||||
want: "08ece40212050430e234001888a40122081bc0159d530e60003220cd647151552b5132b2aef7c9be00dc6f73afc5901dde157aab131335baaa853b38889eaf0140fa83c3d5054a14555ee11fbddc0e49a9bab358a8941ad95ffdb48f52143e3a3d69dc66ba10737f531ed088954a9ec89d97580a6201296a20f7161c170d43573ad9c8d701cdaf714ff2a548a562b0dc639230d17889fcd40572203c4977fc90385a27efa0032e17b49fd575b2826cb56e3d1ecf21524f2a94f915",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
@ -116,3 +142,84 @@ func TestEthereumParser_PackTx(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEthereumParser_UnpackTx(t *testing.T) {
|
||||
type args struct {
|
||||
hex string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
p *EthereumParser
|
||||
args args
|
||||
want *bchain.Tx
|
||||
want1 uint32
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "1",
|
||||
args: args{hex: "08aebf0a1205012a05f20018a0f7362a24f025caaf00000000000000000000000000000000000000000000000000000000000002253220e6b168d6bb3d8ed78e03dbf828b6bfd1fb613f6e129cba624964984553724c5d38f095af014092f4c1d5054a14682b7903a11098cf770c7aef4aa02a85b3f3601a5214dacc9c61754a0c4616fc5323dc946e89eb272302580162011b6a201bd40a31122c03918df6d166d740a6a3a22f08a25934ceb1688c62977661c80c7220607fbc15c1f7995a4258f5a9bccc63b040362d1991d5efe1361c56222e4ca89f"},
|
||||
want: &bchain.Tx{
|
||||
Blocktime: 1521515026,
|
||||
Hex: "7b226e6f6e6365223a2230783239666165222c226761735072696365223a223078313261303566323030222c22676173223a2230786462626130222c22746f223a22307836383262373930336131313039386366373730633761656634616130326138356233663336303161222c2276616c7565223a22307830222c22696e707574223a223078663032356361616630303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030323235222c2268617368223a22307865366231363864366262336438656437386530336462663832386236626664316662363133663665313239636261363234393634393834353533373234633564222c22626c6f636b4e756d626572223a223078326263616630222c2266726f6d223a22307864616363396336313735346130633436313666633533323364633934366538396562323732333032222c227472616e73616374696f6e496e646578223a22307831222c2276223a2230783162222c2272223a22307831626434306133313132326330333931386466366431363664373430613661336132326630386132353933346365623136383863363239373736363163383063222c2273223a22307836303766626331356331663739393561343235386635613962636363363362303430333632643139393164356566653133363163353632323265346361383966227d",
|
||||
Time: 1521515026,
|
||||
Txid: "e6b168d6bb3d8ed78e03dbf828b6bfd1fb613f6e129cba624964984553724c5d",
|
||||
Vin: []bchain.Vin{
|
||||
{
|
||||
Addresses: []string{"dacc9c61754a0c4616fc5323dc946e89eb272302"},
|
||||
},
|
||||
},
|
||||
Vout: []bchain.Vout{
|
||||
{
|
||||
ScriptPubKey: bchain.ScriptPubKey{
|
||||
Addresses: []string{"682b7903a11098cf770c7aef4aa02a85b3f3601a"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
want1: 2870000,
|
||||
},
|
||||
{
|
||||
name: "1",
|
||||
args: args{hex: "08ece40212050430e234001888a40122081bc0159d530e60003220cd647151552b5132b2aef7c9be00dc6f73afc5901dde157aab131335baaa853b38889eaf0140fa83c3d5054a14555ee11fbddc0e49a9bab358a8941ad95ffdb48f52143e3a3d69dc66ba10737f531ed088954a9ec89d97580a6201296a20f7161c170d43573ad9c8d701cdaf714ff2a548a562b0dc639230d17889fcd40572203c4977fc90385a27efa0032e17b49fd575b2826cb56e3d1ecf21524f2a94f915"},
|
||||
want: &bchain.Tx{
|
||||
Blocktime: 1521533434,
|
||||
Hex: "7b226e6f6e6365223a22307862323663222c226761735072696365223a223078343330653233343030222c22676173223a22307835323038222c22746f223a22307835353565653131666264646330653439613962616233353861383934316164393566666462343866222c2276616c7565223a22307831626330313539643533306536303030222c22696e707574223a223078222c2268617368223a22307863643634373135313535326235313332623261656637633962653030646336663733616663353930316464653135376161623133313333356261616138353362222c22626c6f636b4e756d626572223a223078326263663038222c2266726f6d223a22307833653361336436396463363662613130373337663533316564303838393534613965633839643937222c227472616e73616374696f6e496e646578223a22307861222c2276223a2230783239222c2272223a22307866373136316331373064343335373361643963386437303163646166373134666632613534386135363262306463363339323330643137383839666364343035222c2273223a22307833633439373766633930333835613237656661303033326531376234396664353735623238323663623536653364316563663231353234663261393466393135227d",
|
||||
Time: 1521533434,
|
||||
Txid: "cd647151552b5132b2aef7c9be00dc6f73afc5901dde157aab131335baaa853b",
|
||||
Vin: []bchain.Vin{
|
||||
{
|
||||
Addresses: []string{"3e3a3d69dc66ba10737f531ed088954a9ec89d97"},
|
||||
},
|
||||
},
|
||||
Vout: []bchain.Vout{
|
||||
{
|
||||
ScriptPubKey: bchain.ScriptPubKey{
|
||||
Addresses: []string{"555ee11fbddc0e49a9bab358a8941ad95ffdb48f"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
want1: 2871048,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
p := &EthereumParser{}
|
||||
b, err := hex.DecodeString(tt.args.hex)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
got, got1, err := p.UnpackTx(b)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("EthereumParser.UnpackTx() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("EthereumParser.UnpackTx() got = %v, want %v", got, tt.want)
|
||||
}
|
||||
if got1 != tt.want1 {
|
||||
t.Errorf("EthereumParser.UnpackTx() got1 = %v, want %v", got1, tt.want1)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -351,7 +351,7 @@ func (b *EthereumRPC) GetTransaction(txid string) (*bchain.Tx, error) {
|
|||
if err != nil {
|
||||
return nil, errors.Annotatef(err, "txid %v", txid)
|
||||
}
|
||||
h, err := b.client.HeaderByHash(ctx, tx.BlockHash)
|
||||
h, err := b.client.HeaderByHash(ctx, *tx.BlockHash)
|
||||
if err != nil {
|
||||
return nil, errors.Annotatef(err, "txid %v", txid)
|
||||
}
|
||||
|
|
|
@ -368,7 +368,7 @@ func TestEthRPC_GetTransaction(t *testing.T) {
|
|||
want: &bchain.Tx{
|
||||
Blocktime: 1521515026,
|
||||
Confirmations: uint32(bh.Number.Uint64()) - 2870000,
|
||||
Hex: "7b226e6f6e6365223a2230783239666165222c226761735072696365223a223078313261303566323030222c22676173223a2230786462626130222c22746f223a22307836383262373930336131313039386366373730633761656634616130326138356233663336303161222c2276616c7565223a22307830222c22696e707574223a223078663032356361616630303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030323235222c2268617368223a22307865366231363864366262336438656437386530336462663832386236626664316662363133663665313239636261363234393634393834353533373234633564222c22426c6f636b4e756d626572223a223078326263616630222c22426c6f636b48617368223a22307865636364366230303331303135613139636237643465313066323835393062613635613661353461643162616133323262353066653561643136393033383935222c2246726f6d223a22307864616363396336313735346130633436313666633533323364633934366538396562323732333032222c227472616e73616374696f6e496e646578223a22307831222c2276223a2230783162222c2272223a22307831626434306133313132326330333931386466366431363664373430613661336132326630386132353933346365623136383863363239373736363163383063222c2273223a22307836303766626331356331663739393561343235386635613962636363363362303430333632643139393164356566653133363163353632323265346361383966227d",
|
||||
Hex: "7b226e6f6e6365223a2230783239666165222c226761735072696365223a223078313261303566323030222c22676173223a2230786462626130222c22746f223a22307836383262373930336131313039386366373730633761656634616130326138356233663336303161222c2276616c7565223a22307830222c22696e707574223a223078663032356361616630303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030323235222c2268617368223a22307865366231363864366262336438656437386530336462663832386236626664316662363133663665313239636261363234393634393834353533373234633564222c22626c6f636b4e756d626572223a223078326263616630222c2266726f6d223a22307864616363396336313735346130633436313666633533323364633934366538396562323732333032222c227472616e73616374696f6e496e646578223a22307831222c2276223a2230783162222c2272223a22307831626434306133313132326330333931386466366431363664373430613661336132326630386132353933346365623136383863363239373736363163383063222c2273223a22307836303766626331356331663739393561343235386635613962636363363362303430333632643139393164356566653133363163353632323265346361383966227d",
|
||||
Time: 1521515026,
|
||||
Txid: "e6b168d6bb3d8ed78e03dbf828b6bfd1fb613f6e129cba624964984553724c5d",
|
||||
Vin: []bchain.Vin{
|
||||
|
@ -396,7 +396,7 @@ func TestEthRPC_GetTransaction(t *testing.T) {
|
|||
want: &bchain.Tx{
|
||||
Blocktime: 1521533434,
|
||||
Confirmations: uint32(bh.Number.Uint64()) - 2871048,
|
||||
Hex: "7b226e6f6e6365223a22307862323663222c226761735072696365223a223078343330653233343030222c22676173223a22307835323038222c22746f223a22307835353565653131666264646330653439613962616233353861383934316164393566666462343866222c2276616c7565223a22307831626330313539643533306536303030222c22696e707574223a223078222c2268617368223a22307863643634373135313535326235313332623261656637633962653030646336663733616663353930316464653135376161623133313333356261616138353362222c22426c6f636b4e756d626572223a223078326263663038222c22426c6f636b48617368223a22307863303266396632623736633265393537643464656139643030366263643636356239303462613866383461653466343836373561383662373536326461366239222c2246726f6d223a22307833653361336436396463363662613130373337663533316564303838393534613965633839643937222c227472616e73616374696f6e496e646578223a22307861222c2276223a2230783239222c2272223a22307866373136316331373064343335373361643963386437303163646166373134666632613534386135363262306463363339323330643137383839666364343035222c2273223a22307833633439373766633930333835613237656661303033326531376234396664353735623238323663623536653364316563663231353234663261393466393135227d",
|
||||
Hex: "7b226e6f6e6365223a22307862323663222c226761735072696365223a223078343330653233343030222c22676173223a22307835323038222c22746f223a22307835353565653131666264646330653439613962616233353861383934316164393566666462343866222c2276616c7565223a22307831626330313539643533306536303030222c22696e707574223a223078222c2268617368223a22307863643634373135313535326235313332623261656637633962653030646336663733616663353930316464653135376161623133313333356261616138353362222c22626c6f636b4e756d626572223a223078326263663038222c2266726f6d223a22307833653361336436396463363662613130373337663533316564303838393534613965633839643937222c227472616e73616374696f6e496e646578223a22307861222c2276223a2230783239222c2272223a22307866373136316331373064343335373361643963386437303163646166373134666632613534386135363262306463363339323330643137383839666364343035222c2273223a22307833633439373766633930333835613237656661303033326531376234396664353735623238323663623536653364316563663231353234663261393466393135227d",
|
||||
Time: 1521533434,
|
||||
Txid: "cd647151552b5132b2aef7c9be00dc6f73afc5901dde157aab131335baaa853b",
|
||||
Vin: []bchain.Vin{
|
||||
|
|
Loading…
Reference in New Issue