forked from cerc-io/ipld-eth-server
finishing porting omni watcher to work with light sync; split into full, light,
and shared directories and refactor as much into shared; finish lightSync omni watcher tests
This commit is contained in:
@@ -17,16 +17,22 @@
|
||||
package converter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"strconv"
|
||||
|
||||
geth "github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
gethTypes "github.com/ethereum/go-ethereum/core/types"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/omni/shared/contract"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/omni/shared/types"
|
||||
)
|
||||
|
||||
type Converter interface {
|
||||
Convert(log geth.Log, event types.Event) (*types.Log, error)
|
||||
Convert(logs []gethTypes.Log, event types.Event, headerID int64) ([]types.Log, error)
|
||||
Update(info *contract.Contract)
|
||||
}
|
||||
|
||||
@@ -45,6 +51,66 @@ func (c *converter) Update(info *contract.Contract) {
|
||||
}
|
||||
|
||||
// Convert the given watched event log into a types.Log for the given event
|
||||
func (c *converter) Convert(log geth.Log, event types.Event) (*types.Log, error) {
|
||||
return nil, errors.New("implement me")
|
||||
func (c *converter) Convert(logs []gethTypes.Log, event types.Event, headerID int64) ([]types.Log, error) {
|
||||
contract := bind.NewBoundContract(common.HexToAddress(c.ContractInfo.Address), c.ContractInfo.ParsedAbi, nil, nil, nil)
|
||||
returnLogs := make([]types.Log, 0, len(logs))
|
||||
for _, log := range logs {
|
||||
values := make(map[string]interface{})
|
||||
for _, field := range event.Fields {
|
||||
var i interface{}
|
||||
values[field.Name] = i
|
||||
}
|
||||
|
||||
err := contract.UnpackLogIntoMap(values, event.Name, log)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
strValues := make(map[string]string, len(values))
|
||||
for fieldName, input := range values {
|
||||
// Postgres cannot handle custom types, resolve everything to strings
|
||||
switch input.(type) {
|
||||
case *big.Int:
|
||||
b := input.(*big.Int)
|
||||
strValues[fieldName] = b.String()
|
||||
case common.Address:
|
||||
a := input.(common.Address)
|
||||
strValues[fieldName] = a.String()
|
||||
c.ContractInfo.AddTokenHolderAddress(a.String()) // cache address in a list of contract's token holder addresses
|
||||
case common.Hash:
|
||||
h := input.(common.Hash)
|
||||
strValues[fieldName] = h.String()
|
||||
case string:
|
||||
strValues[fieldName] = input.(string)
|
||||
case bool:
|
||||
strValues[fieldName] = strconv.FormatBool(input.(bool))
|
||||
case []byte:
|
||||
b := input.([]byte)
|
||||
strValues[fieldName] = string(b)
|
||||
case byte:
|
||||
b := input.(byte)
|
||||
strValues[fieldName] = string(b)
|
||||
default:
|
||||
return nil, errors.New(fmt.Sprintf("error: unhandled abi type %T", input))
|
||||
}
|
||||
}
|
||||
|
||||
// Only hold onto logs that pass our address filter, if any
|
||||
if c.ContractInfo.PassesEventFilter(strValues) {
|
||||
raw, err := json.Marshal(log)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
returnLogs = append(returnLogs, types.Log{
|
||||
LogIndex: log.Index,
|
||||
Values: strValues,
|
||||
Raw: raw,
|
||||
TransactionIndex: log.TxIndex,
|
||||
Id: headerID,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return returnLogs, nil
|
||||
}
|
||||
|
||||
@@ -15,3 +15,98 @@
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package converter_test
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/omni/light/converter"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/omni/shared/contract"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/omni/shared/helpers"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/omni/shared/helpers/test_helpers"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/omni/shared/helpers/test_helpers/mocks"
|
||||
)
|
||||
|
||||
var _ = Describe("Converter", func() {
|
||||
var con *contract.Contract
|
||||
var wantedEvents = []string{"Transfer", "Mint"}
|
||||
var err error
|
||||
|
||||
BeforeEach(func() {
|
||||
con = test_helpers.SetupTusdContract(wantedEvents, []string{})
|
||||
})
|
||||
|
||||
Describe("Update", func() {
|
||||
It("Updates contract info held by the converter", func() {
|
||||
c := converter.NewConverter(con)
|
||||
Expect(c.ContractInfo).To(Equal(con))
|
||||
|
||||
info := test_helpers.SetupTusdContract([]string{}, []string{})
|
||||
c.Update(info)
|
||||
Expect(c.ContractInfo).To(Equal(info))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Convert", func() {
|
||||
It("Converts a watched event log to mapping of event input names to values", func() {
|
||||
_, ok := con.Events["Approval"]
|
||||
Expect(ok).To(Equal(false))
|
||||
|
||||
event, ok := con.Events["Transfer"]
|
||||
Expect(ok).To(Equal(true))
|
||||
|
||||
c := converter.NewConverter(con)
|
||||
logs, err := c.Convert([]types.Log{mocks.MockTransferLog1, mocks.MockTransferLog2}, event, 232)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(len(logs)).To(Equal(2))
|
||||
|
||||
sender1 := common.HexToAddress("0x9dd48110dcc444fdc242510c09bbbbe21a5975cac061d82f7b843bce061ba391")
|
||||
sender2 := common.HexToAddress("0x000000000000000000000000000000000000000000000000000000000000af21")
|
||||
value := helpers.BigFromString("1097077688018008265106216665536940668749033598146")
|
||||
|
||||
Expect(logs[0].Values["to"]).To(Equal(sender1.String()))
|
||||
Expect(logs[0].Values["from"]).To(Equal(sender2.String()))
|
||||
Expect(logs[0].Values["value"]).To(Equal(value.String()))
|
||||
Expect(logs[0].Id).To(Equal(int64(232)))
|
||||
Expect(logs[1].Values["to"]).To(Equal(sender2.String()))
|
||||
Expect(logs[1].Values["from"]).To(Equal(sender1.String()))
|
||||
Expect(logs[1].Values["value"]).To(Equal(value.String()))
|
||||
Expect(logs[1].Id).To(Equal(int64(232)))
|
||||
})
|
||||
|
||||
It("Keeps track of addresses it sees to grow a token holder address list for the contract", func() {
|
||||
event, ok := con.Events["Transfer"]
|
||||
Expect(ok).To(Equal(true))
|
||||
|
||||
c := converter.NewConverter(con)
|
||||
_, err := c.Convert([]types.Log{mocks.MockTransferLog1, mocks.MockTransferLog2}, event, 232)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
b, ok := con.TknHolderAddrs["0x000000000000000000000000000000000000Af21"]
|
||||
Expect(ok).To(Equal(true))
|
||||
Expect(b).To(Equal(true))
|
||||
|
||||
b, ok = con.TknHolderAddrs["0x09BbBBE21a5975cAc061D82f7b843bCE061BA391"]
|
||||
Expect(ok).To(Equal(true))
|
||||
Expect(b).To(Equal(true))
|
||||
|
||||
_, ok = con.TknHolderAddrs["0x"]
|
||||
Expect(ok).To(Equal(false))
|
||||
|
||||
_, ok = con.TknHolderAddrs[""]
|
||||
Expect(ok).To(Equal(false))
|
||||
|
||||
_, ok = con.TknHolderAddrs["0x09THISE21a5IS5cFAKE1D82fAND43bCE06MADEUP"]
|
||||
Expect(ok).To(Equal(false))
|
||||
})
|
||||
|
||||
It("Fails with an empty contract", func() {
|
||||
event := con.Events["Transfer"]
|
||||
c := converter.NewConverter(&contract.Contract{})
|
||||
_, err = c.Convert([]types.Log{mocks.MockTransferLog1}, event, 232)
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user