mirror of
https://github.com/trezor/blockbook.git
synced 2026-02-20 00:51:39 +01:00
fix(websocket): avoid panic on missing ethereum specific data
This commit is contained in:
@@ -1037,6 +1037,14 @@ func setConfirmedBlockTxMetadata(tx *bchain.Tx, blockTime int64) {
|
||||
}
|
||||
}
|
||||
|
||||
func getEthereumInternalTransfers(tx *bchain.Tx) []bchain.EthereumInternalTransfer {
|
||||
esd, ok := tx.CoinSpecificData.(bchain.EthereumSpecificData)
|
||||
if !ok || esd.InternalData == nil {
|
||||
return nil
|
||||
}
|
||||
return esd.InternalData.Transfers
|
||||
}
|
||||
|
||||
func (s *WebsocketServer) publishNewBlockTxsByAddr(block *bchain.Block) {
|
||||
for _, tx := range block.Txs {
|
||||
setConfirmedBlockTxMetadata(&tx, block.Time)
|
||||
@@ -1044,10 +1052,7 @@ func (s *WebsocketServer) publishNewBlockTxsByAddr(block *bchain.Block) {
|
||||
var internalTransfers []bchain.EthereumInternalTransfer
|
||||
if s.chainParser.GetChainType() == bchain.ChainEthereumType {
|
||||
tokenTransfers, _ = s.chainParser.EthereumTypeGetTokenTransfersFromTx(&tx)
|
||||
esd := tx.CoinSpecificData.(bchain.EthereumSpecificData)
|
||||
if esd.InternalData != nil {
|
||||
internalTransfers = esd.InternalData.Transfers
|
||||
}
|
||||
internalTransfers = getEthereumInternalTransfers(&tx)
|
||||
}
|
||||
vins := make([]bchain.MempoolVin, len(tx.Vin))
|
||||
for i, vin := range tx.Vin {
|
||||
|
||||
@@ -47,3 +47,35 @@ func TestSetConfirmedBlockTxMetadataLeavesConfirmedTxUnchanged(t *testing.T) {
|
||||
t.Fatalf("Time = %d, want 200", tx.Time)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetEthereumInternalTransfersMissingData(t *testing.T) {
|
||||
tx := bchain.Tx{}
|
||||
|
||||
transfers := getEthereumInternalTransfers(&tx)
|
||||
|
||||
if len(transfers) != 0 {
|
||||
t.Fatalf("len(transfers) = %d, want 0", len(transfers))
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetEthereumInternalTransfersReturnsTransfers(t *testing.T) {
|
||||
expected := []bchain.EthereumInternalTransfer{
|
||||
{From: "0x111", To: "0x222"},
|
||||
}
|
||||
tx := bchain.Tx{
|
||||
CoinSpecificData: bchain.EthereumSpecificData{
|
||||
InternalData: &bchain.EthereumInternalData{
|
||||
Transfers: expected,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
transfers := getEthereumInternalTransfers(&tx)
|
||||
|
||||
if len(transfers) != len(expected) {
|
||||
t.Fatalf("len(transfers) = %d, want %d", len(transfers), len(expected))
|
||||
}
|
||||
if transfers[0].From != expected[0].From || transfers[0].To != expected[0].To {
|
||||
t.Fatalf("transfers[0] = %+v, want %+v", transfers[0], expected[0])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user