loomnetwork / unity-sdk

Loom DAppChain SDK for Unity3d
Other
121 stars 34 forks source link

Golang events are not parsed at all. #45

Open coinsandsteeldev opened 6 years ago

coinsandsteeldev commented 6 years ago

This is Contract.cs method which is used to pre-process event before emitting it to listeners.

protected override ChainEventArgs TransformChainEvent(RawChainEventArgs e) {
    string jsonRpcEventString = Encoding.UTF8.GetString(e.Data);
    JsonRpcEvent jsonRpcEvent = JsonConvert.DeserializeObject<JsonRpcEvent>(jsonRpcEventString);
    byte[] eventData = Encoding.UTF8.GetBytes(jsonRpcEvent.Data);

    return new ChainEventArgs(
         e.ContractAddress,
         e.CallerAddress,
         e.BlockHeight,
         eventData,
         jsonRpcEvent.Method
    );
}

When i emit event in Go either using ctx.Emit() or ctx.EmitTopics() i have same result:

jsonRpcEventString has serialized json from Emit for instance

type AccountCreatedEvt struct {
    Test  string
    Test2 int
}

Will result as {"Test": "test", "Test2": 1488}. So the next line fails to deserialize JsonRpcEvent jsonRpcEvent = JsonConvert.DeserializeObject<JsonRpcEvent>(jsonRpcEventString);

Resulting in converting null to byte[]

I wonder whether it is problem of Loom Node emitting event in the wrong format or something else.

coinsandsteeldev commented 6 years ago

I'm constrained by this issue a lot. And i think this is huge problem right now.

mattkanwisher commented 6 years ago

@ZimM-LostPolygon will take a look at this, should have a new release this week

coinsandsteeldev commented 6 years ago

It seems like we can emit anything we want in Go contract. And i think we need better approach to TransformChainEvent, i would make it injection, with some standard one by default. Right now if i want to have pure protobuf events i need to open source code of SDK and change it. So i would propose this simple change which will make life easier for future generations :)

ZimM-LostPolygon commented 5 years ago

@coinsandsteeldev Not exactly sure I understand the issue. The first argument of ctx.Emit() or ctx.EmitTopics() goes into JsonRpcEvent.Data. And sure, you can put pretty much anything there. But you say that it replaces the complete RawChainEventArgs.Data?..

coinsandsteeldev commented 5 years ago

@ZimM-LostPolygon No it doesn't. It fails to parse JsonRpcEvent at all.

coinsandsteeldev commented 5 years ago

Of course if you follow JsonRpcEvent layout it will be ok, but i don't think it is a way to go. It is fixed easily, and it is done in my own version of your SDK.

ZimM-LostPolygon commented 5 years ago

Yep, I can confirm there is an issue. Meanwhile, you probably should subclass Contract and override TransformChainEvent.